Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / maestro.c
blob6bdb2ade8a37d5529731243385d6e02b878effb5
1 /*****************************************************************************
3 * ESS Maestro/Maestro-2/Maestro-2E driver for Linux 2.[23].x
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * (c) Copyright 1999 Alan Cox <alan.cox@linux.org>
21 * Based heavily on SonicVibes.c:
22 * Copyright (C) 1998-1999 Thomas Sailer (sailer@ife.ee.ethz.ch)
24 * Heavily modified by Zach Brown <zab@zabbo.net> based on lunch
25 * with ESS engineers. Many thanks to Howard Kim for providing
26 * contacts and hardware. Honorable mention goes to Eric
27 * Brombaugh for all sorts of things. Best regards to the
28 * proprietors of Hack Central for fine lodging.
30 * Supported devices:
31 * /dev/dsp0-3 standard /dev/dsp device, (mostly) OSS compatible
32 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
34 * Hardware Description
36 * A working Maestro setup contains the Maestro chip wired to a
37 * codec or 2. In the Maestro we have the APUs, the ASSP, and the
38 * Wavecache. The APUs can be though of as virtual audio routing
39 * channels. They can take data from a number of sources and perform
40 * basic encodings of the data. The wavecache is a storehouse for
41 * PCM data. Typically it deals with PCI and interracts with the
42 * APUs. The ASSP is a wacky DSP like device that ESS is loth
43 * to release docs on. Thankfully it isn't required on the Maestro
44 * until you start doing insane things like FM emulation and surround
45 * encoding. The codecs are almost always AC-97 compliant codecs,
46 * but it appears that early Maestros may have had PT101 (an ESS
47 * part?) wired to them. The only real difference in the Maestro
48 * families is external goop like docking capability, memory for
49 * the ASSP, and initialization differences.
51 * Driver Operation
53 * We only drive the APU/Wavecache as typical DACs and drive the
54 * mixers in the codecs. There are 64 APUs. We assign 6 to each
55 * /dev/dsp? device. 2 channels for output, and 4 channels for
56 * input.
58 * Each APU can do a number of things, but we only really use
59 * 3 basic functions. For playback we use them to convert PCM
60 * data fetched over PCI by the wavecahche into analog data that
61 * is handed to the codec. One APU for mono, and a pair for stereo.
62 * When in stereo, the combination of smarts in the APU and Wavecache
63 * decide which wavecache gets the left or right channel.
65 * For record we still use the old overly mono system. For each in
66 * coming channel the data comes in from the codec, through a 'input'
67 * APU, through another rate converter APU, and then into memory via
68 * the wavecache and PCI. If its stereo, we mash it back into LRLR in
69 * software. The pass between the 2 APUs is supposedly what requires us
70 * to have a 512 byte buffer sitting around in wavecache/memory.
72 * The wavecache makes our life even more fun. First off, it can
73 * only address the first 28 bits of PCI address space, making it
74 * useless on quite a few architectures. Secondly, its insane.
75 * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
76 * But that doesn't really work. You can only use 1 region. So all our
77 * allocations have to be in 4meg of each other. Booo. Hiss.
78 * So we have a module parameter, dsps_order, that is the order of
79 * the number of dsps to provide. All their buffer space is allocated
80 * on open time. The sonicvibes OSS routines we inherited really want
81 * power of 2 buffers, so we have all those next to each other, then
82 * 512 byte regions for the recording wavecaches. This ends up
83 * wasting quite a bit of memory. The only fixes I can see would be
84 * getting a kernel allocator that could work in zones, or figuring out
85 * just how to coerce the WP into doing what we want.
87 * The indirection of the various registers means we have to spinlock
88 * nearly all register accesses. We have the main register indirection
89 * like the wave cache, maestro registers, etc. Then we have beasts
90 * like the APU interface that is indirect registers gotten at through
91 * the main maestro indirection. Ouch. We spinlock around the actual
92 * ports on a per card basis. This means spinlock activity at each IO
93 * operation, but the only IO operation clusters are in non critical
94 * paths and it makes the code far easier to follow. Interrupts are
95 * blocked while holding the locks because the int handler has to
96 * get at some of them :(. The mixer interface doesn't, however.
97 * We also have an OSS state lock that is thrown around in a few
98 * places.
100 * This driver has brute force APM suspend support. We catch suspend
101 * notifications and stop all work being done on the chip. Any people
102 * that try between this shutdown and the real suspend operation will
103 * be put to sleep. When we resume we restore our software state on
104 * the chip and wake up the people that were using it. The code thats
105 * being used now is quite dirty and assumes we're on a uni-processor
106 * machine. Much of it will need to be cleaned up for SMP ACPI or
107 * similar.
109 * We also pay attention to PCI power management now. The driver
110 * will power down units of the chip that it knows aren't needed.
111 * The WaveProcessor and company are only powered on when people
112 * have /dev/dsp*s open. On removal the driver will
113 * power down the maestro entirely. There could still be
114 * trouble with BIOSen that magically change power states
115 * themselves, but we'll see.
117 * History
118 * (still kind of v0.14) Nov 23 - Alan Cox <alan@redhat.com>
119 * Add clocking= for people with seriously warped hardware
120 * (still v0.14) Nov 10 2000 - Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
121 * add __init to maestro_ac97_init() and maestro_install()
122 * (still based on v0.14) Mar 29 2000 - Zach Brown <zab@redhat.com>
123 * move to 2.3 power management interface, which
124 * required hacking some suspend/resume/check paths
125 * make static compilation work
126 * v0.14 - Jan 28 2000 - Zach Brown <zab@redhat.com>
127 * add PCI power management through ACPI regs.
128 * we now shut down on machine reboot/halt
129 * leave scary PCI config items alone (isa stuff, mostly)
130 * enable 1921s, it seems only mine was broke.
131 * fix swapped left/right pcm dac. har har.
132 * up bob freq, increase buffers, fix pointers at underflow
133 * silly compilation problems
134 * v0.13 - Nov 18 1999 - Zach Brown <zab@redhat.com>
135 * fix nec Versas? man would that be cool.
136 * v0.12 - Nov 12 1999 - Zach Brown <zab@redhat.com>
137 * brown bag volume max fix..
138 * v0.11 - Nov 11 1999 - Zach Brown <zab@redhat.com>
139 * use proper stereo apu decoding, mmap/write should work.
140 * make volume sliders more useful, tweak rate calculation.
141 * fix lame 8bit format reporting bug. duh. apm apu saving buglet also
142 * fix maestro 1 clock freq "bug", remove pt101 support
143 * v0.10 - Oct 28 1999 - Zach Brown <zab@redhat.com>
144 * aha, so, sometimes the WP writes a status word to offset 0
145 * from one of the PCMBARs. rearrange allocation accordingly..
146 * cheers again to Eric for being a good hacker in investigating this.
147 * Jeroen Hoogervorst submits 7500 fix out of nowhere. yay. :)
148 * v0.09 - Oct 23 1999 - Zach Brown <zab@redhat.com>
149 * added APM support.
150 * re-order something such that some 2Es now work. Magic!
151 * new codec reset routine. made some codecs come to life.
152 * fix clear_advance, sync some control with ESS.
153 * now write to all base regs to be paranoid.
154 * v0.08 - Oct 20 1999 - Zach Brown <zab@redhat.com>
155 * Fix initial buflen bug. I am so smart. also smp compiling..
156 * I owe Eric yet another beer: fixed recmask, igain,
157 * muting, and adc sync consistency. Go Team.
158 * v0.07 - Oct 4 1999 - Zach Brown <zab@redhat.com>
159 * tweak adc/dac, formating, and stuff to allow full duplex
160 * allocate dsps memory at open() so we can fit in the wavecache window
161 * fix wavecache braindamage. again. no more scribbling?
162 * fix ess 1921 codec bug on some laptops.
163 * fix dumb pci scanning bug
164 * started 2.3 cleanup, redid spinlocks, little cleanups
165 * v0.06 - Sep 20 1999 - Zach Brown <zab@redhat.com>
166 * fix wavecache thinkos. limit to 1 /dev/dsp.
167 * eric is wearing his thinking toque this week.
168 * spotted apu mode bugs and gain ramping problem
169 * don't touch weird mixer regs, make recmask optional
170 * fixed igain inversion, defaults for mixers, clean up rec_start
171 * make mono recording work.
172 * report subsystem stuff, please send reports.
173 * littles: parallel out, amp now
174 * v0.05 - Sep 17 1999 - Zach Brown <zab@redhat.com>
175 * merged and fixed up Eric's initial recording code
176 * munged format handling to catch misuse, needs rewrite.
177 * revert ring bus init, fixup shared int, add pci busmaster setting
178 * fix mixer oss interface, fix mic mute and recmask
179 * mask off unsupported mixers, reset with all 1s, modularize defaults
180 * make sure bob is running while we need it
181 * got rid of device limit, initial minimal apm hooks
182 * pull out dead code/includes, only allow multimedia/audio maestros
183 * v0.04 - Sep 01 1999 - Zach Brown <zab@redhat.com>
184 * copied memory leak fix from sonicvibes driver
185 * different ac97 reset, play with 2.0 ac97, simplify ring bus setup
186 * bob freq code, region sanity, jitter sync fix; all from Eric
188 * TODO
189 * fix bob frequency
190 * endianness
191 * do smart things with ac97 2.0 bits.
192 * docking and dual codecs and 978?
193 * leave 54->61 open
195 * it also would be fun to have a mode that would not use pci dma at all
196 * but would copy into the wavecache on board memory and use that
197 * on architectures that don't like the maestro's pci dma ickiness.
200 /*****************************************************************************/
202 #include <linux/version.h>
203 #include <linux/module.h>
204 #include <linux/sched.h>
205 #include <linux/smp_lock.h>
206 #include <linux/wrapper.h>
208 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
210 #define DECLARE_WAITQUEUE(QUEUE,INIT) struct wait_queue QUEUE = {INIT, NULL}
211 #define wait_queue_head_t struct wait_queue *
212 #define SILLY_PCI_BASE_ADDRESS(PCIDEV) (PCIDEV->base_address[0] & PCI_BASE_ADDRESS_IO_MASK)
213 #define SILLY_INIT_SEM(SEM) SEM=MUTEX;
214 #define init_waitqueue_head init_waitqueue
215 #define SILLY_MAKE_INIT(FUNC) __initfunc(FUNC)
216 #define SILLY_OFFSET(VMA) ((VMA)->vm_offset)
219 #else
221 #define SILLY_PCI_BASE_ADDRESS(PCIDEV) (PCIDEV->resource[0].start)
222 #define SILLY_INIT_SEM(SEM) init_MUTEX(&SEM)
223 #define SILLY_MAKE_INIT(FUNC) __init FUNC
224 #define SILLY_OFFSET(VMA) ((VMA)->vm_pgoff)
227 #endif
229 #include <linux/string.h>
230 #include <linux/ctype.h>
231 #include <linux/ioport.h>
232 #include <linux/delay.h>
233 #include <linux/sound.h>
234 #include <linux/malloc.h>
235 #include <linux/soundcard.h>
236 #include <linux/pci.h>
237 #include <linux/spinlock.h>
238 #include <asm/io.h>
239 #include <asm/dma.h>
240 #include <linux/init.h>
241 #include <linux/poll.h>
242 #include <linux/reboot.h>
243 #include <asm/uaccess.h>
244 #include <asm/hardirq.h>
245 #include <linux/bitops.h>
247 #include <linux/pm.h>
248 static intmaestro_pm_callback(struct pm_dev *dev, pm_request_t rqst,void*d);
250 #include"maestro.h"
252 /* --------------------------------------------------------------------- */
254 #define M_DEBUG 1
256 #ifdef M_DEBUG
257 static int debug=0;
258 #define M_printk(args...) {if (debug) printk(args);}
259 #else
260 #define M_printk(x)
261 #endif
263 /* we try to setup 2^(dsps_order) /dev/dsp devices */
264 static int dsps_order=0;
265 /* wether or not we mess around with power management */
266 static int use_pm=2;/* set to 1 for force */
267 /* clocking for broken hardware - a few laptops seem to use a 50Khz clock
268 ie insmod with clocking=50000 or so */
270 static int clocking=48000;
272 /* --------------------------------------------------------------------- */
273 #define DRIVER_VERSION"0.14"
275 #ifndef PCI_VENDOR_ESS
276 #define PCI_VENDOR_ESS 0x125D
277 #define PCI_DEVICE_ID_ESS_ESS1968 0x1968/* Maestro 2 */
278 #define PCI_DEVICE_ID_ESS_ESS1978 0x1978/* Maestro 2E */
280 #define PCI_VENDOR_ESS_OLD 0x1285/* Platform Tech,
281 the people the maestro
282 was bought from */
283 #define PCI_DEVICE_ID_ESS_ESS0100 0x0100/* maestro 1 */
284 #endif/* PCI_VENDOR_ESS */
286 #define ESS_CHAN_HARD 0x100
288 /* NEC Versas ? */
289 #define NEC_VERSA_SUBID1 0x80581033
290 #define NEC_VERSA_SUBID2 0x803c1033
293 /* changed so that I could actually find all the
294 references and fix them up. its a little more readable now. */
295 #define ESS_FMT_STEREO 0x01
296 #define ESS_FMT_16BIT 0x02
297 #define ESS_FMT_MASK 0x03
298 #define ESS_DAC_SHIFT 0
299 #define ESS_ADC_SHIFT 4
301 #define ESS_STATE_MAGIC 0x125D1968
302 #define ESS_CARD_MAGIC 0x19283746
304 #define DAC_RUNNING 1
305 #define ADC_RUNNING 2
307 #define MAX_DSP_ORDER 2
308 #define MAX_DSPS (1<<MAX_DSP_ORDER)
309 #define NR_DSPS (1<<dsps_order)
310 #define NR_IDRS 32
312 #define NR_APUS 64
313 #define NR_APU_REGS 16
315 /* acpi states */
316 enum{
317 ACPI_D0=0,
318 ACPI_D1,
319 ACPI_D2,
320 ACPI_D3
323 /* bits in the acpi masks */
324 #define ACPI_12MHZ ( 1 << 15)
325 #define ACPI_24MHZ ( 1 << 14)
326 #define ACPI_978 ( 1 << 13)
327 #define ACPI_SPDIF ( 1 << 12)
328 #define ACPI_GLUE ( 1 << 11)
329 #define ACPI__10 ( 1 << 10)/* reserved */
330 #define ACPI_PCIINT ( 1 << 9)
331 #define ACPI_HV ( 1 << 8)/* hardware volume */
332 #define ACPI_GPIO ( 1 << 7)
333 #define ACPI_ASSP ( 1 << 6)
334 #define ACPI_SB ( 1 << 5)/* sb emul */
335 #define ACPI_FM ( 1 << 4)/* fm emul */
336 #define ACPI_RB ( 1 << 3)/* ringbus / aclink */
337 #define ACPI_MIDI ( 1 << 2)
338 #define ACPI_GP ( 1 << 1)/* game port */
339 #define ACPI_WP ( 1 << 0)/* wave processor */
341 #define ACPI_ALL (0xffff)
342 #define ACPI_SLEEP (~(ACPI_SPDIF|ACPI_ASSP|ACPI_SB|ACPI_FM| \
343 ACPI_MIDI|ACPI_GP|ACPI_WP))
344 #define ACPI_NONE (ACPI__10)
346 /* these masks indicate which units we care about at
347 which states */
348 u16 acpi_state_mask[] = {
349 [ACPI_D0] = ACPI_ALL,
350 [ACPI_D1] = ACPI_SLEEP,
351 [ACPI_D2] = ACPI_SLEEP,
352 [ACPI_D3] = ACPI_NONE
355 static const unsigned sample_size[] = {1,2,2,4};
356 static const unsigned sample_shift[] = {0,1,1,2};
358 enum card_types_t {
359 TYPE_MAESTRO,
360 TYPE_MAESTRO2,
361 TYPE_MAESTRO2E
364 static const char*card_names[]={
365 [TYPE_MAESTRO] ="ESS Maestro",
366 [TYPE_MAESTRO2] ="ESS Maestro 2",
367 [TYPE_MAESTRO2E] ="ESS Maestro 2E"
370 static int clock_freq[]={
371 [TYPE_MAESTRO] = (49152000L/1024L),
372 [TYPE_MAESTRO2] = (50000000L/1024L),
373 [TYPE_MAESTRO2E] = (50000000L/1024L)
376 static intmaestro_notifier(struct notifier_block *nb,unsigned long event,void*buf);
378 static struct notifier_block maestro_nb = {maestro_notifier, NULL,0};
380 /* --------------------------------------------------------------------- */
382 struct ess_state {
383 unsigned int magic;
384 /* FIXME: we probably want submixers in here, but only one record pair */
385 u8 apu[6];/* l/r output, l/r intput converters, l/r input apus */
386 u8 apu_mode[6];/* Running mode for this APU */
387 u8 apu_pan[6];/* Panning setup for this APU */
388 u32 apu_base[6];/* base address for this apu */
389 struct ess_card *card;/* Card info */
390 /* wave stuff */
391 unsigned int rateadc, ratedac;
392 unsigned char fmt, enable;
394 int index;
396 /* this locks around the oss state in the driver */
397 spinlock_t lock;
398 /* only let 1 be opening at a time */
399 struct semaphore open_sem;
400 wait_queue_head_t open_wait;
401 mode_t open_mode;
403 /* soundcore stuff */
404 int dev_audio;
406 struct dmabuf {
407 void*rawbuf;
408 unsigned buforder;
409 unsigned numfrag;
410 unsigned fragshift;
411 /* XXX zab - swptr only in here so that it can be referenced by
412 clear_advance, as far as I can tell :( */
413 unsigned hwptr, swptr;
414 unsigned total_bytes;
415 int count;
416 unsigned error;/* over/underrun */
417 wait_queue_head_t wait;
418 /* redundant, but makes calculations easier */
419 unsigned fragsize;
420 unsigned dmasize;
421 unsigned fragsamples;
422 /* OSS stuff */
423 unsigned mapped:1;
424 unsigned ready:1;/* our oss buffers are ready to go */
425 unsigned endcleared:1;
426 unsigned ossfragshift;
427 int ossmaxfrags;
428 unsigned subdivision;
429 u16 base;/* Offset for ptr */
430 } dma_dac, dma_adc;
432 /* pointer to each dsp?s piece of the apu->src buffer page */
433 void*mixbuf;
437 struct ess_card {
438 unsigned int magic;
440 /* We keep maestro cards in a linked list */
441 struct ess_card *next;
443 int dev_mixer;
445 int card_type;
447 /* as most of this is static,
448 perhaps it should be a pointer to a global struct */
449 struct mixer_goo {
450 int modcnt;
451 int supported_mixers;
452 int stereo_mixers;
453 int record_sources;
454 /* the caller must guarantee arg sanity before calling these */
455 /* int (*read_mixer)(struct ess_card *card, int index);*/
456 void(*write_mixer)(struct ess_card *card,int mixer,unsigned int left,unsigned int right);
457 int(*recmask_io)(struct ess_card *card,int rw,int mask);
458 unsigned int mixer_state[SOUND_MIXER_NRDEVICES];
459 } mix;
461 int power_regs;
463 int in_suspend;
464 wait_queue_head_t suspend_queue;
466 struct ess_state channels[MAX_DSPS];
467 u16 maestro_map[NR_IDRS];/* Register map */
468 /* we have to store this junk so that we can come back from a
469 suspend */
470 u16 apu_map[NR_APUS][NR_APU_REGS];/* contents of apu regs */
472 /* this locks around the physical registers on the card */
473 spinlock_t lock;
475 /* memory for this card.. wavecache limited :(*/
476 void*dmapages;
477 int dmaorder;
479 /* hardware resources */
480 struct pci_dev *pcidev;
481 u32 iobase;
482 u32 irq;
484 int bob_freq;
485 char dsps_open;
488 static unsigned
489 ld2(unsigned int x)
491 unsigned r =0;
493 if(x >=0x10000) {
494 x >>=16;
495 r +=16;
497 if(x >=0x100) {
498 x >>=8;
499 r +=8;
501 if(x >=0x10) {
502 x >>=4;
503 r +=4;
505 if(x >=4) {
506 x >>=2;
507 r +=2;
509 if(x >=2)
510 r++;
511 return r;
515 /* --------------------------------------------------------------------- */
517 static voidcheck_suspend(struct ess_card *card);
519 static struct ess_card *devs = NULL;
521 /* --------------------------------------------------------------------- */
525 * ESS Maestro AC97 codec programming interface.
528 static voidmaestro_ac97_set(struct ess_card *card, u8 cmd, u16 val)
530 int io = card->iobase;
531 int i;
533 * Wait for the codec bus to be free
536 check_suspend(card);
538 for(i=0;i<10000;i++)
540 if(!(inb(io+ESS_AC97_INDEX)&1))
541 break;
544 * Write the bus
546 outw(val, io+ESS_AC97_DATA);
547 mdelay(1);
548 outb(cmd, io+ESS_AC97_INDEX);
549 mdelay(1);
552 static u16 maestro_ac97_get(struct ess_card *card, u8 cmd)
554 int io = card->iobase;
555 int sanity=10000;
556 u16 data;
557 int i;
559 check_suspend(card);
561 * Wait for the codec bus to be free
564 for(i=0;i<10000;i++)
566 if(!(inb(io+ESS_AC97_INDEX)&1))
567 break;
570 outb(cmd|0x80, io+ESS_AC97_INDEX);
571 mdelay(1);
573 while(inb(io+ESS_AC97_INDEX)&1)
575 sanity--;
576 if(!sanity)
578 printk(KERN_ERR "maestro: ac97 codec timeout reading 0x%x.\n",cmd);
579 return0;
582 data=inw(io+ESS_AC97_DATA);
583 mdelay(1);
584 return data;
587 /* OSS interface to the ac97s.. */
589 #define AC97_STEREO_MASK (SOUND_MASK_VOLUME|\
590 SOUND_MASK_PCM|SOUND_MASK_LINE|SOUND_MASK_CD|\
591 SOUND_MASK_VIDEO|SOUND_MASK_LINE1|SOUND_MASK_IGAIN)
593 #define AC97_SUPPORTED_MASK (AC97_STEREO_MASK | \
594 SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_MIC|\
595 SOUND_MASK_SPEAKER)
597 #define AC97_RECORD_MASK (SOUND_MASK_MIC|\
598 SOUND_MASK_CD| SOUND_MASK_VIDEO| SOUND_MASK_LINE1| SOUND_MASK_LINE|\
599 SOUND_MASK_PHONEIN)
601 #define supported_mixer(CARD,FOO) ( CARD->mix.supported_mixers & (1<<FOO) )
603 /* this table has default mixer values for all OSS mixers.
604 be sure to fill it in if you add oss mixers
605 to anyone's supported mixer defines */
607 unsigned int mixer_defaults[SOUND_MIXER_NRDEVICES] = {
608 [SOUND_MIXER_VOLUME] =0x3232,
609 [SOUND_MIXER_BASS] =0x3232,
610 [SOUND_MIXER_TREBLE] =0x3232,
611 [SOUND_MIXER_SPEAKER] =0x3232,
612 [SOUND_MIXER_MIC] =0x8000,/* annoying */
613 [SOUND_MIXER_LINE] =0x3232,
614 [SOUND_MIXER_CD] =0x3232,
615 [SOUND_MIXER_VIDEO] =0x3232,
616 [SOUND_MIXER_LINE1] =0x3232,
617 [SOUND_MIXER_PCM] =0x3232,
618 [SOUND_MIXER_IGAIN] =0x3232
621 static struct ac97_mixer_hw {
622 unsigned char offset;
623 int scale;
624 } ac97_hw[SOUND_MIXER_NRDEVICES]= {
625 [SOUND_MIXER_VOLUME] = {0x02,63},
626 [SOUND_MIXER_BASS] = {0x08,15},
627 [SOUND_MIXER_TREBLE] = {0x08,15},
628 [SOUND_MIXER_SPEAKER] = {0x0a,15},
629 [SOUND_MIXER_MIC] = {0x0e,31},
630 [SOUND_MIXER_LINE] = {0x10,31},
631 [SOUND_MIXER_CD] = {0x12,31},
632 [SOUND_MIXER_VIDEO] = {0x14,31},
633 [SOUND_MIXER_LINE1] = {0x16,31},
634 [SOUND_MIXER_PCM] = {0x18,31},
635 [SOUND_MIXER_IGAIN] = {0x1c,15}
638 #if 0/* *shrug* removed simply because we never used it.
639 feel free to implement again if needed */
641 /* reads the given OSS mixer from the ac97
642 the caller must have insured that the ac97 knows
643 about that given mixer, and should be holding a
644 spinlock for the card */
645 static intac97_read_mixer(struct ess_card *card,int mixer)
647 u16 val;
648 int ret=0;
649 struct ac97_mixer_hw *mh = &ac97_hw[mixer];
651 val =maestro_ac97_get(card, mh->offset);
653 if(AC97_STEREO_MASK & (1<<mixer)) {
654 /* nice stereo mixers .. */
655 int left,right;
657 left = (val >>8) &0x7f;
658 right = val &0x7f;
660 if(mixer == SOUND_MIXER_IGAIN) {
661 right = (right *100) / mh->scale;
662 left = (left *100) / mh->scale;
663 else{
664 right =100- ((right *100) / mh->scale);
665 left =100- ((left *100) / mh->scale);
668 ret = left | (right <<8);
669 }else if(mixer == SOUND_MIXER_SPEAKER) {
670 ret =100- ((((val &0x1e)>>1) *100) / mh->scale);
671 }else if(mixer == SOUND_MIXER_MIC) {
672 ret =100- (((val &0x1f) *100) / mh->scale);
673 /* the low bit is optional in the tone sliders and masking
674 it lets is avoid the 0xf 'bypass'.. */
675 }else if(mixer == SOUND_MIXER_BASS) {
676 ret =100- ((((val >>8) &0xe) *100) / mh->scale);
677 }else if(mixer == SOUND_MIXER_TREBLE) {
678 ret =100- (((val &0xe) *100) / mh->scale);
681 M_printk("read mixer %d (0x%x) %x -> %x\n",mixer,mh->offset,val,ret);
683 return ret;
685 #endif
687 /* write the OSS encoded volume to the given OSS encoded mixer,
688 again caller's job to make sure all is well in arg land,
689 call with spinlock held */
691 /* linear scale -> log */
692 static unsigned char lin2log[101] =
694 0,0,15,23,30,34,38,42,45,47,
695 50,52,53,55,57,58,60,61,62,
696 63,65,66,67,68,69,69,70,71,
697 72,73,73,74,75,75,76,77,77,
698 78,78,79,80,80,81,81,82,82,
699 83,83,84,84,84,85,85,86,86,
700 87,87,87,88,88,88,89,89,89,
701 90,90,90,91,91,91,92,92,92,
702 93,93,93,94,94,94,94,95,95,
703 95,95,96,96,96,96,97,97,97,
704 97,98,98,98,98,99,99,99,99,99
707 static voidac97_write_mixer(struct ess_card *card,int mixer,unsigned int left,unsigned int right)
709 u16 val=0;
710 struct ac97_mixer_hw *mh = &ac97_hw[mixer];
712 M_printk("wrote mixer %d (0x%x) %d,%d",mixer,mh->offset,left,right);
714 if(AC97_STEREO_MASK & (1<<mixer)) {
715 /* stereo mixers, mute them if we can */
717 if(mixer == SOUND_MIXER_IGAIN) {
718 /* igain's slider is reversed.. */
719 right = (right * mh->scale) /100;
720 left = (left * mh->scale) /100;
721 if((left ==0) && (right ==0))
722 val |=0x8000;
723 }else{
724 /* log conversion for the stereo controls */
725 if((left ==0) && (right ==0))
726 val =0x8000;
727 right = ((100- lin2log[right]) * mh->scale) /100;
728 left = ((100- lin2log[left]) * mh->scale) /100;
731 val |= (left <<8) | right;
733 }else if(mixer == SOUND_MIXER_SPEAKER) {
734 val = (((100- left) * mh->scale) /100) <<1;
735 }else if(mixer == SOUND_MIXER_MIC) {
736 val =maestro_ac97_get(card, mh->offset) & ~0x801f;
737 val |= (((100- left) * mh->scale) /100);
738 /* the low bit is optional in the tone sliders and masking
739 it lets is avoid the 0xf 'bypass'.. */
740 }else if(mixer == SOUND_MIXER_BASS) {
741 val =maestro_ac97_get(card , mh->offset) & ~0x0f00;
742 val |= ((((100- left) * mh->scale) /100) <<8) &0x0e00;
743 }else if(mixer == SOUND_MIXER_TREBLE) {
744 val =maestro_ac97_get(card , mh->offset) & ~0x000f;
745 val |= (((100- left) * mh->scale) /100) &0x000e;
748 maestro_ac97_set(card , mh->offset, val);
750 M_printk(" -> %x\n",val);
753 /* the following tables allow us to go from
754 OSS <-> ac97 quickly. */
756 enum ac97_recsettings {
757 AC97_REC_MIC=0,
758 AC97_REC_CD,
759 AC97_REC_VIDEO,
760 AC97_REC_AUX,
761 AC97_REC_LINE,
762 AC97_REC_STEREO,/* combination of all enabled outputs.. */
763 AC97_REC_MONO,/*.. or the mono equivalent */
764 AC97_REC_PHONE
767 static unsigned int ac97_oss_mask[] = {
768 [AC97_REC_MIC] = SOUND_MASK_MIC,
769 [AC97_REC_CD] = SOUND_MASK_CD,
770 [AC97_REC_VIDEO] = SOUND_MASK_VIDEO,
771 [AC97_REC_AUX] = SOUND_MASK_LINE1,
772 [AC97_REC_LINE] = SOUND_MASK_LINE,
773 [AC97_REC_PHONE] = SOUND_MASK_PHONEIN
776 /* indexed by bit position */
777 static unsigned int ac97_oss_rm[] = {
778 [SOUND_MIXER_MIC] = AC97_REC_MIC,
779 [SOUND_MIXER_CD] = AC97_REC_CD,
780 [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
781 [SOUND_MIXER_LINE1] = AC97_REC_AUX,
782 [SOUND_MIXER_LINE] = AC97_REC_LINE,
783 [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
786 /* read or write the recmask
787 the ac97 can really have left and right recording
788 inputs independantly set, but OSS doesn't seem to
789 want us to express that to the user.
790 the caller guarantees that we have a supported bit set,
791 and they must be holding the card's spinlock */
792 static int
793 ac97_recmask_io(struct ess_card *card,int read,int mask)
795 unsigned int val = ac97_oss_mask[maestro_ac97_get(card,0x1a) &0x7];
797 if(read)return val;
799 /* oss can have many inputs, maestro cant. try
800 to pick the 'new' one */
802 if(mask != val) mask &= ~val;
804 val =ffs(mask) -1;
805 val = ac97_oss_rm[val];
806 val |= val <<8;/* set both channels */
808 M_printk("maestro: setting ac97 recmask to 0x%x\n",val);
810 maestro_ac97_set(card,0x1a,val);
812 return0;
816 * The Maestro can be wired to a standard AC97 compliant codec
817 * (see www.intel.com for the pdf's on this), or to a PT101 codec
818 * which appears to be the ES1918 (data sheet on the esstech.com.tw site)
820 * The PT101 setup is untested.
823 static u16 __init maestro_ac97_init(struct ess_card *card)
825 u16 vend1, vend2, caps;
827 card->mix.supported_mixers = AC97_SUPPORTED_MASK;
828 card->mix.stereo_mixers = AC97_STEREO_MASK;
829 card->mix.record_sources = AC97_RECORD_MASK;
830 /* card->mix.read_mixer = ac97_read_mixer;*/
831 card->mix.write_mixer = ac97_write_mixer;
832 card->mix.recmask_io = ac97_recmask_io;
834 vend1 =maestro_ac97_get(card,0x7c);
835 vend2 =maestro_ac97_get(card,0x7e);
837 caps =maestro_ac97_get(card,0x00);
839 printk(KERN_INFO "maestro: AC97 Codec detected: v: 0x%2x%2x caps: 0x%x pwr: 0x%x\n",
840 vend1,vend2,caps,maestro_ac97_get(card,0x26) &0xf);
842 if(! (caps &0x4) ) {
843 /* no bass/treble nobs */
844 card->mix.supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
847 /* XXX endianness, dork head. */
848 /* vendor specifc bits.. */
849 switch((long)(vend1 <<16) | vend2) {
850 case0x545200ff:/* TriTech */
851 /* no idea what this does */
852 maestro_ac97_set(card,0x2a,0x0001);
853 maestro_ac97_set(card,0x2c,0x0000);
854 maestro_ac97_set(card,0x2c,0xffff);
855 break;
856 #if 0/* i thought the problems I was seeing were with
857 the 1921, but apparently they were with the pci board
858 it was on, so this code is commented out.
859 lets see if this holds true. */
860 case0x83847609:/* ESS 1921 */
861 /* writing to 0xe (mic) or 0x1a (recmask) seems
862 to hang this codec */
863 card->mix.supported_mixers &= ~(SOUND_MASK_MIC);
864 card->mix.record_sources =0;
865 card->mix.recmask_io = NULL;
866 #if 0/* don't ask. I have yet to see what these actually do. */
867 maestro_ac97_set(card,0x76,0xABBA);/* o/~ Take a chance on me o/~ */
868 udelay(20);
869 maestro_ac97_set(card,0x78,0x3002);
870 udelay(20);
871 maestro_ac97_set(card,0x78,0x3802);
872 udelay(20);
873 #endif
874 break;
875 #endif
876 default:break;
879 maestro_ac97_set(card,0x1E,0x0404);
880 /* null misc stuff */
881 maestro_ac97_set(card,0x20,0x0000);
883 return0;
886 #if 0/* there has been 1 person on the planet with a pt101 that we
887 know of. If they care, they can put this back in :) */
888 static u16 maestro_pt101_init(struct ess_card *card,int iobase)
890 printk(KERN_INFO "maestro: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
891 /* who knows.. */
892 maestro_ac97_set(iobase,0x2A,0x0001);
893 maestro_ac97_set(iobase,0x2C,0x0000);
894 maestro_ac97_set(iobase,0x2C,0xFFFF);
895 maestro_ac97_set(iobase,0x10,0x9F1F);
896 maestro_ac97_set(iobase,0x12,0x0808);
897 maestro_ac97_set(iobase,0x14,0x9F1F);
898 maestro_ac97_set(iobase,0x16,0x9F1F);
899 maestro_ac97_set(iobase,0x18,0x0404);
900 maestro_ac97_set(iobase,0x1A,0x0000);
901 maestro_ac97_set(iobase,0x1C,0x0000);
902 maestro_ac97_set(iobase,0x02,0x0404);
903 maestro_ac97_set(iobase,0x04,0x0808);
904 maestro_ac97_set(iobase,0x0C,0x801F);
905 maestro_ac97_set(iobase,0x0E,0x801F);
906 return0;
908 #endif
910 /* this is very magic, and very slow.. */
911 static void
912 maestro_ac97_reset(int ioaddr,struct pci_dev *pcidev)
914 u16 save_68;
915 u16 w;
916 u32 vend;
918 outw(inw(ioaddr +0x38) &0xfffc, ioaddr +0x38);
919 outw(inw(ioaddr +0x3a) &0xfffc, ioaddr +0x3a);
920 outw(inw(ioaddr +0x3c) &0xfffc, ioaddr +0x3c);
922 /* reset the first codec */
923 outw(0x0000, ioaddr+0x36);
924 save_68 =inw(ioaddr+0x68);
925 pci_read_config_word(pcidev,0x58, &w);/* something magical with gpio and bus arb. */
926 pci_read_config_dword(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &vend);
927 if( w &0x1)
928 save_68 |=0x10;
929 outw(0xfffe, ioaddr +0x64);/* tickly gpio 0.. */
930 outw(0x0001, ioaddr +0x68);
931 outw(0x0000, ioaddr +0x60);
932 udelay(20);
933 outw(0x0001, ioaddr +0x60);
934 mdelay(20);
936 outw(save_68 |0x1, ioaddr +0x68);/* now restore .. */
937 outw( (inw(ioaddr +0x38) &0xfffc)|0x1, ioaddr +0x38);
938 outw( (inw(ioaddr +0x3a) &0xfffc)|0x1, ioaddr +0x3a);
939 outw( (inw(ioaddr +0x3c) &0xfffc)|0x1, ioaddr +0x3c);
941 /* now the second codec */
942 outw(0x0000, ioaddr+0x36);
943 outw(0xfff7, ioaddr +0x64);
944 save_68 =inw(ioaddr+0x68);
945 outw(0x0009, ioaddr +0x68);
946 outw(0x0001, ioaddr +0x60);
947 udelay(20);
948 outw(0x0009, ioaddr +0x60);
949 mdelay(500);/* .. ouch.. */
950 outw(inw(ioaddr +0x38) &0xfffc, ioaddr +0x38);
951 outw(inw(ioaddr +0x3a) &0xfffc, ioaddr +0x3a);
952 outw(inw(ioaddr +0x3c) &0xfffc, ioaddr +0x3c);
954 #if 0/* the loop here needs to be much better if we want it.. */
955 M_printk("trying software reset\n");
956 /* try and do a software reset */
957 outb(0x80|0x7c, ioaddr +0x30);
958 for(w=0; ; w++) {
959 if((inw(ioaddr+0x30) &1) ==0) {
960 if(inb(ioaddr +0x32) !=0)break;
962 outb(0x80|0x7d, ioaddr +0x30);
963 if(((inw(ioaddr+0x30) &1) ==0) && (inb(ioaddr +0x32) !=0))break;
964 outb(0x80|0x7f, ioaddr +0x30);
965 if(((inw(ioaddr+0x30) &1) ==0) && (inb(ioaddr +0x32) !=0))break;
968 if( w >10000) {
969 outb(inb(ioaddr +0x37) |0x08, ioaddr +0x37);/* do a software reset */
970 mdelay(500);/* oh my.. */
971 outb(inb(ioaddr +0x37) & ~0x08, ioaddr +0x37);
972 udelay(1);
973 outw(0x80, ioaddr+0x30);
974 for(w =0; w <10000; w++) {
975 if((inw(ioaddr +0x30) &1) ==0)break;
979 #endif
980 if( vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
981 /* turn on external amp? */
982 outw(0xf9ff, ioaddr +0x64);
983 outw(inw(ioaddr+0x68) |0x600, ioaddr +0x68);
984 outw(0x0209, ioaddr +0x60);
988 * Indirect register access. Not all registers are readable so we
989 * need to keep register state ourselves
992 #define WRITEABLE_MAP 0xEFFFFF
993 #define READABLE_MAP 0x64003F
996 * The Maestro engineers were a little indirection happy. These indirected
997 * registers themselves include indirect registers at another layer
1000 static void__maestro_write(struct ess_card *card, u16 reg, u16 data)
1002 long ioaddr = card->iobase;
1004 outw(reg, ioaddr+0x02);
1005 outw(data, ioaddr+0x00);
1006 if( reg >= NR_IDRS)printk("maestro: IDR %d out of bounds!\n",reg);
1007 else card->maestro_map[reg]=data;
1011 static voidmaestro_write(struct ess_state *s, u16 reg, u16 data)
1013 unsigned long flags;
1015 check_suspend(s->card);
1016 spin_lock_irqsave(&s->card->lock,flags);
1018 __maestro_write(s->card,reg,data);
1020 spin_unlock_irqrestore(&s->card->lock,flags);
1023 static u16 __maestro_read(struct ess_card *card, u16 reg)
1025 long ioaddr = card->iobase;
1027 outw(reg, ioaddr+0x02);
1028 return card->maestro_map[reg]=inw(ioaddr+0x00);
1031 static u16 maestro_read(struct ess_state *s, u16 reg)
1033 if(READABLE_MAP & (1<<reg))
1035 unsigned long flags;
1036 check_suspend(s->card);
1037 spin_lock_irqsave(&s->card->lock,flags);
1039 __maestro_read(s->card,reg);
1041 spin_unlock_irqrestore(&s->card->lock,flags);
1043 return s->card->maestro_map[reg];
1047 * These routines handle accessing the second level indirections to the
1048 * wave ram.
1052 * The register names are the ones ESS uses (see 104T31.ZIP)
1055 #define IDR0_DATA_PORT 0x00
1056 #define IDR1_CRAM_POINTER 0x01
1057 #define IDR2_CRAM_DATA 0x02
1058 #define IDR3_WAVE_DATA 0x03
1059 #define IDR4_WAVE_PTR_LOW 0x04
1060 #define IDR5_WAVE_PTR_HI 0x05
1061 #define IDR6_TIMER_CTRL 0x06
1062 #define IDR7_WAVE_ROMRAM 0x07
1064 static voidapu_index_set(struct ess_card *card, u16 index)
1066 int i;
1067 __maestro_write(card, IDR1_CRAM_POINTER, index);
1068 for(i=0;i<1000;i++)
1069 if(__maestro_read(card, IDR1_CRAM_POINTER)==index)
1070 return;
1071 printk(KERN_WARNING "maestro: APU register select failed.\n");
1074 static voidapu_data_set(struct ess_card *card, u16 data)
1076 int i;
1077 for(i=0;i<1000;i++)
1079 if(__maestro_read(card, IDR0_DATA_PORT)==data)
1080 return;
1081 __maestro_write(card, IDR0_DATA_PORT, data);
1086 * This is the public interface for APU manipulation. It handles the
1087 * interlock to avoid two APU writes in parallel etc. Don't diddle
1088 * directly with the stuff above.
1091 static voidapu_set_register(struct ess_state *s, u16 channel, u8 reg, u16 data)
1093 unsigned long flags;
1095 check_suspend(s->card);
1097 if(channel&ESS_CHAN_HARD)
1098 channel&=~ESS_CHAN_HARD;
1099 else
1101 if(channel>5)
1102 printk("BAD CHANNEL %d.\n",channel);
1103 else
1104 channel = s->apu[channel];
1105 /* store based on real hardware apu/reg */
1106 s->card->apu_map[channel][reg]=data;
1108 reg|=(channel<<4);
1110 /* hooray for double indirection!! */
1111 spin_lock_irqsave(&s->card->lock,flags);
1113 apu_index_set(s->card, reg);
1114 apu_data_set(s->card, data);
1116 spin_unlock_irqrestore(&s->card->lock,flags);
1119 static u16 apu_get_register(struct ess_state *s, u16 channel, u8 reg)
1121 unsigned long flags;
1122 u16 v;
1124 check_suspend(s->card);
1126 if(channel&ESS_CHAN_HARD)
1127 channel&=~ESS_CHAN_HARD;
1128 else
1129 channel = s->apu[channel];
1131 reg|=(channel<<4);
1133 spin_lock_irqsave(&s->card->lock,flags);
1135 apu_index_set(s->card, reg);
1136 v=__maestro_read(s->card, IDR0_DATA_PORT);
1138 spin_unlock_irqrestore(&s->card->lock,flags);
1139 return v;
1144 * The wavecache buffers between the APUs and
1145 * pci bus mastering
1148 static voidwave_set_register(struct ess_state *s, u16 reg, u16 value)
1150 long ioaddr = s->card->iobase;
1151 unsigned long flags;
1152 check_suspend(s->card);
1154 spin_lock_irqsave(&s->card->lock,flags);
1156 outw(reg, ioaddr+0x10);
1157 outw(value, ioaddr+0x12);
1159 spin_unlock_irqrestore(&s->card->lock,flags);
1162 static u16 wave_get_register(struct ess_state *s, u16 reg)
1164 long ioaddr = s->card->iobase;
1165 unsigned long flags;
1166 u16 value;
1167 check_suspend(s->card);
1169 spin_lock_irqsave(&s->card->lock,flags);
1170 outw(reg, ioaddr+0x10);
1171 value=inw(ioaddr+0x12);
1172 spin_unlock_irqrestore(&s->card->lock,flags);
1174 return value;
1177 static voidsound_reset(int ioaddr)
1179 outw(0x2000,0x18+ioaddr);
1180 udelay(1);
1181 outw(0x0000,0x18+ioaddr);
1182 udelay(1);
1185 /* sets the play formats of these apus, should be passed the already shifted format */
1186 static voidset_apu_fmt(struct ess_state *s,int apu,int mode)
1188 int apu_fmt =0x10;
1190 if(!(mode&ESS_FMT_16BIT)) apu_fmt+=0x20;
1191 if((mode&ESS_FMT_STEREO)) apu_fmt+=0x10;
1192 s->apu_mode[apu] = apu_fmt;
1193 s->apu_mode[apu+1] = apu_fmt;
1196 /* this only fixes the output apu mode to be later set by start_dac and
1197 company. output apu modes are set in ess_rec_setup */
1198 static voidset_fmt(struct ess_state *s,unsigned char mask,unsigned char data)
1200 s->fmt = (s->fmt & mask) | data;
1201 set_apu_fmt(s,0, (s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK);
1204 /* this is off by a little bit.. */
1205 static u32 compute_rate(struct ess_state *s, u32 freq)
1207 u32 clock = clock_freq[s->card->card_type];
1209 freq = (freq * clocking)/48000;
1211 if(freq ==48000)
1212 return0x10000;
1214 return((freq / clock) <<16)+
1215 (((freq % clock) <<16) / clock);
1218 static voidset_dac_rate(struct ess_state *s,unsigned int rate)
1220 u32 freq;
1221 int fmt = (s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK;
1223 if(rate >48000)
1224 rate =48000;
1225 if(rate <4000)
1226 rate =4000;
1228 s->ratedac = rate;
1230 if(! (fmt & ESS_FMT_16BIT) && !(fmt & ESS_FMT_STEREO))
1231 rate >>=1;
1233 /* M_printk("computing dac rate %d with mode %d\n",rate,s->fmt);*/
1235 freq =compute_rate(s, rate);
1237 /* Load the frequency, turn on 6dB */
1238 apu_set_register(s,0,2,(apu_get_register(s,0,2)&0x00FF)|
1239 ( ((freq&0xFF)<<8)|0x10));
1240 apu_set_register(s,0,3, freq>>8);
1241 apu_set_register(s,1,2,(apu_get_register(s,1,2)&0x00FF)|
1242 ( ((freq&0xFF)<<8)|0x10));
1243 apu_set_register(s,1,3, freq>>8);
1246 static voidset_adc_rate(struct ess_state *s,unsigned rate)
1248 u32 freq;
1250 /* Sample Rate conversion APUs don't like 0x10000 for their rate */
1251 if(rate >47999)
1252 rate =47999;
1253 if(rate <4000)
1254 rate =4000;
1256 s->rateadc = rate;
1258 freq =compute_rate(s, rate);
1260 /* Load the frequency, turn on 6dB */
1261 apu_set_register(s,2,2,(apu_get_register(s,2,2)&0x00FF)|
1262 ( ((freq&0xFF)<<8)|0x10));
1263 apu_set_register(s,2,3, freq>>8);
1264 apu_set_register(s,3,2,(apu_get_register(s,3,2)&0x00FF)|
1265 ( ((freq&0xFF)<<8)|0x10));
1266 apu_set_register(s,3,3, freq>>8);
1268 /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
1269 freq =0x10000;
1271 apu_set_register(s,4,2,(apu_get_register(s,4,2)&0x00FF)|
1272 ( ((freq&0xFF)<<8)|0x10));
1273 apu_set_register(s,4,3, freq>>8);
1274 apu_set_register(s,5,2,(apu_get_register(s,5,2)&0x00FF)|
1275 ( ((freq&0xFF)<<8)|0x10));
1276 apu_set_register(s,5,3, freq>>8);
1279 /* Stop our host of recording apus */
1280 extern inlinevoidstop_adc(struct ess_state *s)
1282 /* XXX lets hope we don't have to lock around this */
1283 if(! (s->enable & ADC_RUNNING))return;
1285 s->enable &= ~ADC_RUNNING;
1286 apu_set_register(s,2,0,apu_get_register(s,2,0)&0xFF0F);
1287 apu_set_register(s,3,0,apu_get_register(s,3,0)&0xFF0F);
1288 apu_set_register(s,4,0,apu_get_register(s,2,0)&0xFF0F);
1289 apu_set_register(s,5,0,apu_get_register(s,3,0)&0xFF0F);
1292 /* stop output apus */
1293 static voidstop_dac(struct ess_state *s)
1295 /* XXX have to lock around this? */
1296 if(! (s->enable & DAC_RUNNING))return;
1298 s->enable &= ~DAC_RUNNING;
1299 apu_set_register(s,0,0,apu_get_register(s,0,0)&0xFF0F);
1300 apu_set_register(s,1,0,apu_get_register(s,1,0)&0xFF0F);
1303 static voidstart_dac(struct ess_state *s)
1305 /* XXX locks? */
1306 if( (s->dma_dac.mapped || s->dma_dac.count >0) &&
1307 s->dma_dac.ready &&
1308 (! (s->enable & DAC_RUNNING)) ) {
1310 s->enable |= DAC_RUNNING;
1312 apu_set_register(s,0,0,
1313 (apu_get_register(s,0,0)&0xFF0F)|s->apu_mode[0]);
1315 if((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_STEREO)
1316 apu_set_register(s,1,0,
1317 (apu_get_register(s,1,0)&0xFF0F)|s->apu_mode[1]);
1321 static voidstart_adc(struct ess_state *s)
1323 /* XXX locks? */
1324 if((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize -2*s->dma_adc.fragsize))
1325 && s->dma_adc.ready && (! (s->enable & ADC_RUNNING)) ) {
1327 s->enable |= ADC_RUNNING;
1328 apu_set_register(s,2,0,
1329 (apu_get_register(s,2,0)&0xFF0F)|s->apu_mode[2]);
1330 apu_set_register(s,4,0,
1331 (apu_get_register(s,4,0)&0xFF0F)|s->apu_mode[4]);
1333 if( s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
1334 apu_set_register(s,3,0,
1335 (apu_get_register(s,3,0)&0xFF0F)|s->apu_mode[3]);
1336 apu_set_register(s,5,0,
1337 (apu_get_register(s,5,0)&0xFF0F)|s->apu_mode[5]);
1345 * Native play back driver
1348 /* the mode passed should be already shifted and masked */
1349 static void
1350 ess_play_setup(struct ess_state *ess,int mode, u32 rate,void*buffer,int size)
1352 u32 pa;
1353 u32 tmpval;
1354 int high_apu =0;
1355 int channel;
1357 M_printk("mode=%d rate=%d buf=%p len=%d.\n",
1358 mode, rate, buffer, size);
1360 /* all maestro sizes are in 16bit words */
1361 size >>=1;
1363 if(mode&ESS_FMT_STEREO) {
1364 high_apu++;
1365 /* only 16/stereo gets size divided */
1366 if(mode&ESS_FMT_16BIT)
1367 size>>=1;
1370 for(channel=0; channel <= high_apu; channel++)
1372 pa =virt_to_bus(buffer);
1374 /* set the wavecache control reg */
1375 tmpval = (pa -0x10) &0xFFF8;
1376 if(!(mode & ESS_FMT_16BIT)) tmpval |=4;
1377 if(mode & ESS_FMT_STEREO) tmpval |=2;
1378 ess->apu_base[channel]=tmpval;
1379 wave_set_register(ess, ess->apu[channel]<<3, tmpval);
1381 pa -=virt_to_bus(ess->card->dmapages);
1382 pa>>=1;/* words */
1384 /* base offset of dma calcs when reading the pointer
1385 on the left one */
1386 if(!channel) ess->dma_dac.base = pa&0xFFFF;
1388 pa|=0x00400000;/* System RAM */
1390 /* XXX the 16bit here might not be needed.. */
1391 if((mode & ESS_FMT_STEREO) && (mode & ESS_FMT_16BIT)) {
1392 if(channel)
1393 pa|=0x00800000;/* Stereo */
1394 pa>>=1;
1397 /* XXX think about endianess when writing these registers */
1398 M_printk("maestro: ess_play_setup: APU[%d] pa = 0x%x\n", ess->apu[channel], pa);
1399 /* start of sample */
1400 apu_set_register(ess, channel,4, ((pa>>16)&0xFF)<<8);
1401 apu_set_register(ess, channel,5, pa&0xFFFF);
1402 /* sample end */
1403 apu_set_register(ess, channel,6, (pa+size)&0xFFFF);
1404 /* setting loop len == sample len */
1405 apu_set_register(ess, channel,7, size);
1407 /* clear effects/env.. */
1408 apu_set_register(ess, channel,8,0x0000);
1409 /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
1410 apu_set_register(ess, channel,9,0xD000);
1412 /* clear routing stuff */
1413 apu_set_register(ess, channel,11,0x0000);
1414 /* dma on, no envelopes, filter to all 1s) */
1415 apu_set_register(ess, channel,0,0x400F);
1417 if(mode&ESS_FMT_16BIT)
1418 ess->apu_mode[channel]=0x10;
1419 else
1420 ess->apu_mode[channel]=0x30;
1422 if(mode&ESS_FMT_STEREO) {
1423 /* set panning: left or right */
1424 apu_set_register(ess, channel,10,0x8F00| (channel ?0:0x10));
1425 ess->apu_mode[channel] +=0x10;
1426 }else
1427 apu_set_register(ess, channel,10,0x8F08);
1430 /* clear WP interupts */
1431 outw(1, ess->card->iobase+0x04);
1432 /* enable WP ints */
1433 outw(inw(ess->card->iobase+0x18)|4, ess->card->iobase+0x18);
1435 /* go team! */
1436 set_dac_rate(ess,rate);
1437 start_dac(ess);
1441 * Native record driver
1444 /* again, passed mode is alrady shifted/masked */
1445 static void
1446 ess_rec_setup(struct ess_state *ess,int mode, u32 rate,void*buffer,int size)
1448 int apu_step =2;
1449 int channel;
1451 M_printk("maestro: ess_rec_setup: mode=%d rate=%d buf=0x%p len=%d.\n",
1452 mode, rate, buffer, size);
1454 /* all maestro sizes are in 16bit words */
1455 size >>=1;
1457 /* we're given the full size of the buffer, but
1458 in stereo each channel will only use its half */
1459 if(mode&ESS_FMT_STEREO) {
1460 size >>=1;
1461 apu_step =1;
1464 /* APU assignments: 2 = mono/left SRC
1465 3 = right SRC
1466 4 = mono/left Input Mixer
1467 5 = right Input Mixer */
1468 for(channel=2;channel<6;channel+=apu_step)
1470 int i;
1471 int bsize, route;
1472 u32 pa;
1473 u32 tmpval;
1475 /* data seems to flow from the codec, through an apu into
1476 the 'mixbuf' bit of page, then through the SRC apu
1477 and out to the real 'buffer'. ok. sure. */
1479 if(channel &0x04) {
1480 /* ok, we're an input mixer going from adc
1481 through the mixbuf to the other apus */
1483 if(!(channel &0x01)) {
1484 pa =virt_to_bus(ess->mixbuf);
1485 }else{
1486 pa =virt_to_bus(ess->mixbuf + (PAGE_SIZE >>4));
1489 /* we source from a 'magic' apu */
1490 bsize = PAGE_SIZE >>5;/* half of this channels alloc, in words */
1491 route =0x14+ (channel -4);/* parallel in crap, see maestro reg 0xC [8-11] */
1492 ess->apu_mode[channel] =0x90;/* Input Mixer */
1494 }else{
1495 /* we're a rate converter taking
1496 input from the input apus and outputing it to
1497 system memory */
1498 if(!(channel &0x01)) {
1499 pa =virt_to_bus(buffer);
1500 }else{
1501 /* right channel records its split half.
1502 *2 accomodates for rampant shifting earlier */
1503 pa =virt_to_bus(buffer + size*2);
1506 ess->apu_mode[channel] =0xB0;/* Sample Rate Converter */
1508 bsize = size;
1509 /* get input from inputing apu */
1510 route = channel +2;
1513 M_printk("maestro: ess_rec_setup: getting pa 0x%x from %d\n",pa,channel);
1515 /* set the wavecache control reg */
1516 tmpval = (pa -0x10) &0xFFF8;
1517 ess->apu_base[channel]=tmpval;
1518 wave_set_register(ess, ess->apu[channel]<<3, tmpval);
1520 pa -=virt_to_bus(ess->card->dmapages);
1521 pa>>=1;/* words */
1523 /* base offset of dma calcs when reading the pointer
1524 on this left one */
1525 if(channel==2) ess->dma_adc.base = pa&0xFFFF;
1527 pa|=0x00400000;/* bit 22 -> System RAM */
1529 M_printk("maestro: ess_rec_setup: APU[%d] pa = 0x%x size = 0x%x route = 0x%x\n",
1530 ess->apu[channel], pa, bsize, route);
1532 /* Begin loading the APU */
1533 for(i=0;i<15;i++)/* clear all PBRs */
1534 apu_set_register(ess, channel, i,0x0000);
1536 apu_set_register(ess, channel,0,0x400F);
1538 /* need to enable subgroups.. and we should probably
1539 have different groups for different /dev/dsps.. */
1540 apu_set_register(ess, channel,2,0x8);
1542 /* Load the buffer into the wave engine */
1543 apu_set_register(ess, channel,4, ((pa>>16)&0xFF)<<8);
1544 /* XXX reg is little endian.. */
1545 apu_set_register(ess, channel,5, pa&0xFFFF);
1546 apu_set_register(ess, channel,6, (pa+bsize)&0xFFFF);
1547 apu_set_register(ess, channel,7, bsize);
1549 /* clear effects/env.. */
1550 apu_set_register(ess, channel,8,0x00F0);
1552 /* amplitude now? sure. why not. */
1553 apu_set_register(ess, channel,9,0x0000);
1555 /* set filter tune, radius, polar pan */
1556 apu_set_register(ess, channel,10,0x8F08);
1558 /* route input */
1559 apu_set_register(ess, channel,11, route);
1562 /* clear WP interupts */
1563 outw(1, ess->card->iobase+0x04);
1564 /* enable WP ints */
1565 outw(inw(ess->card->iobase+0x18)|4, ess->card->iobase+0x18);
1567 /* let 'er rip */
1568 set_adc_rate(ess,rate);
1569 start_adc(ess);
1571 /* --------------------------------------------------------------------- */
1573 static voidset_dmaa(struct ess_state *s,unsigned int addr,unsigned int count)
1575 M_printk("set_dmaa??\n");
1578 static voidset_dmac(struct ess_state *s,unsigned int addr,unsigned int count)
1580 M_printk("set_dmac??\n");
1583 /* Playback pointer */
1584 extern __inline__ unsignedget_dmaa(struct ess_state *s)
1586 int offset;
1588 offset =apu_get_register(s,0,5);
1590 /* M_printk("dmaa: offset: %d, base: %d\n",offset,s->dma_dac.base); */
1592 offset-=s->dma_dac.base;
1594 return(offset&0xFFFE)<<1;/* hardware is in words */
1597 /* Record pointer */
1598 extern __inline__ unsignedget_dmac(struct ess_state *s)
1600 int offset;
1602 offset =apu_get_register(s,2,5);
1604 /* M_printk("dmac: offset: %d, base: %d\n",offset,s->dma_adc.base); */
1606 /* The offset is an address not a position relative to base */
1607 offset-=s->dma_adc.base;
1609 return(offset&0xFFFE)<<1;/* hardware is in words */
1613 * Meet Bob, the timer...
1616 static voidess_interrupt(int irq,void*dev_id,struct pt_regs *regs);
1618 static voidstop_bob(struct ess_state *s)
1620 /* Mask IDR 11,17 */
1621 maestro_write(s,0x11,maestro_read(s,0x11)&~1);
1622 maestro_write(s,0x17,maestro_read(s,0x17)&~1);
1625 /* eventually we could be clever and limit bob ints
1626 to the frequency at which our smallest duration
1627 chunks may expire */
1628 #define ESS_SYSCLK 50000000
1629 static voidstart_bob(struct ess_state *s)
1631 int prescale;
1632 int divide;
1634 /* XXX make freq selector much smarter, see calc_bob_rate */
1635 int freq =200;
1637 /* compute ideal interrupt frequency for buffer size & play rate */
1638 /* first, find best prescaler value to match freq */
1639 for(prescale=5;prescale<12;prescale++)
1640 if(freq > (ESS_SYSCLK>>(prescale+9)))
1641 break;
1643 /* next, back off prescaler whilst getting divider into optimum range */
1644 divide=1;
1645 while((prescale >5) && (divide<32))
1647 prescale--;
1648 divide <<=1;
1650 divide>>=1;
1652 /* now fine-tune the divider for best match */
1653 for(;divide<31;divide++)
1654 if(freq >= ((ESS_SYSCLK>>(prescale+9))/(divide+1)))
1655 break;
1657 /* divide = 0 is illegal, but don't let prescale = 4! */
1658 if(divide ==0)
1660 divide++;
1661 if(prescale>5)
1662 prescale--;
1665 maestro_write(s,6,0x9000| (prescale<<5) | divide);/* set reg */
1667 /* Now set IDR 11/17 */
1668 maestro_write(s,0x11,maestro_read(s,0x11)|1);
1669 maestro_write(s,0x17,maestro_read(s,0x17)|1);
1671 /* --------------------------------------------------------------------- */
1673 /* this quickly calculates the frequency needed for bob
1674 and sets it if its different than what bob is
1675 currently running at. its called often so
1676 needs to be fairly quick. */
1677 #define BOB_MIN 50
1678 #define BOB_MAX 400
1679 static voidcalc_bob_rate(struct ess_state *s) {
1680 #if 0/* this thing tries to set the frequency of bob such that
1681 there are 2 interrupts / buffer walked by the dac/adc. That
1682 is probably very wrong for people who actually care about
1683 mid buffer positioning. it should be calculated as bytes/interrupt
1684 and that needs to be decided :) so for now just use the static 150
1685 in start_bob.*/
1687 unsigned int dac_rate=2,adc_rate=1,newrate;
1688 static int israte=-1;
1690 if(s->dma_dac.fragsize ==0) dac_rate = BOB_MIN;
1691 else{
1692 dac_rate = (2* s->ratedac * sample_size[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK]) /
1693 (s->dma_dac.fragsize) ;
1696 if(s->dma_adc.fragsize ==0) adc_rate = BOB_MIN;
1697 else{
1698 adc_rate = (2* s->rateadc * sample_size[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK]) /
1699 (s->dma_adc.fragsize) ;
1702 if(dac_rate > adc_rate) newrate = adc_rate;
1703 else newrate=dac_rate;
1705 if(newrate > BOB_MAX) newrate = BOB_MAX;
1706 else{
1707 if(newrate < BOB_MIN)
1708 newrate = BOB_MIN;
1711 if( israte != newrate) {
1712 printk("dac: %d adc: %d rate: %d\n",dac_rate,adc_rate,israte);
1713 israte=newrate;
1715 #endif
1719 static int
1720 prog_dmabuf(struct ess_state *s,unsigned rec)
1722 struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
1723 unsigned rate = rec ? s->rateadc : s->ratedac;
1724 unsigned bytepersec;
1725 unsigned bufs;
1726 unsigned char fmt;
1727 unsigned long flags;
1729 spin_lock_irqsave(&s->lock, flags);
1730 fmt = s->fmt;
1731 if(rec) {
1732 stop_adc(s);
1733 fmt >>= ESS_ADC_SHIFT;
1734 }else{
1735 stop_dac(s);
1736 fmt >>= ESS_DAC_SHIFT;
1738 spin_unlock_irqrestore(&s->lock, flags);
1739 fmt &= ESS_FMT_MASK;
1741 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared =0;
1743 /* this algorithm is a little nuts.. where did /1000 come from? */
1744 bytepersec = rate << sample_shift[fmt];
1745 bufs = PAGE_SIZE << db->buforder;
1746 if(db->ossfragshift) {
1747 if((1000<< db->ossfragshift) < bytepersec)
1748 db->fragshift =ld2(bytepersec/1000);
1749 else
1750 db->fragshift = db->ossfragshift;
1751 }else{
1752 db->fragshift =ld2(bytepersec/100/(db->subdivision ? db->subdivision :1));
1753 if(db->fragshift <3)
1754 db->fragshift =3;
1756 db->numfrag = bufs >> db->fragshift;
1757 while(db->numfrag <4&& db->fragshift >3) {
1758 db->fragshift--;
1759 db->numfrag = bufs >> db->fragshift;
1761 db->fragsize =1<< db->fragshift;
1762 if(db->ossmaxfrags >=4&& db->ossmaxfrags < db->numfrag)
1763 db->numfrag = db->ossmaxfrags;
1764 db->fragsamples = db->fragsize >> sample_shift[fmt];
1765 db->dmasize = db->numfrag << db->fragshift;
1767 M_printk("maestro: setup oss: numfrag: %d fragsize: %d dmasize: %d\n",db->numfrag,db->fragsize,db->dmasize);
1769 memset(db->rawbuf, (fmt & ESS_FMT_16BIT) ?0:0x80, db->dmasize);
1771 spin_lock_irqsave(&s->lock, flags);
1772 if(rec)
1773 ess_rec_setup(s, fmt, s->rateadc, db->rawbuf, db->dmasize);
1774 else
1775 ess_play_setup(s, fmt, s->ratedac, db->rawbuf, db->dmasize);
1777 spin_unlock_irqrestore(&s->lock, flags);
1778 db->ready =1;
1780 return0;
1783 static __inline__ void
1784 clear_advance(struct ess_state *s)
1786 unsigned char c = ((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_16BIT) ?0:0x80;
1788 unsigned char*buf = s->dma_dac.rawbuf;
1789 unsigned bsize = s->dma_dac.dmasize;
1790 unsigned bptr = s->dma_dac.swptr;
1791 unsigned len = s->dma_dac.fragsize;
1793 if(bptr + len > bsize) {
1794 unsigned x = bsize - bptr;
1795 memset(buf + bptr, c, x);
1796 /* account for wrapping? */
1797 bptr =0;
1798 len -= x;
1800 memset(buf + bptr, c, len);
1803 /* call with spinlock held! */
1804 static void
1805 ess_update_ptr(struct ess_state *s)
1807 unsigned hwptr;
1808 int diff;
1810 /* update ADC pointer */
1811 if(s->dma_adc.ready) {
1812 /* oh boy should this all be re-written. everything in the current code paths think
1813 that the various counters/pointers are expressed in bytes to the user but we have
1814 two apus doing stereo stuff so we fix it up here.. it propogates to all the various
1815 counters from here. */
1816 if( s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
1817 hwptr = (get_dmac(s)*2) % s->dma_adc.dmasize;
1818 }else{
1819 hwptr =get_dmac(s) % s->dma_adc.dmasize;
1821 diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
1822 s->dma_adc.hwptr = hwptr;
1823 s->dma_adc.total_bytes += diff;
1824 s->dma_adc.count += diff;
1825 if(s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1826 wake_up(&s->dma_adc.wait);
1827 if(!s->dma_adc.mapped) {
1828 if(s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3* s->dma_adc.fragsize) >>1))) {
1829 /* FILL ME
1830 wrindir(s, SV_CIENABLE, s->enable); */
1831 stop_adc(s);
1832 /* brute force everyone back in sync, sigh */
1833 s->dma_adc.count =0;
1834 s->dma_adc.swptr =0;
1835 s->dma_adc.hwptr =0;
1836 s->dma_adc.error++;
1840 /* update DAC pointer */
1841 if(s->dma_dac.ready) {
1842 hwptr =get_dmaa(s) % s->dma_dac.dmasize;
1843 /* the apu only reports the length it has seen, not the
1844 length of the memory that has been used (the WP
1845 knows that) */
1846 if( ((s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK) == (ESS_FMT_STEREO|ESS_FMT_16BIT))
1847 hwptr<<=1;
1849 diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
1850 /* M_printk("updating dac: hwptr: %d diff: %d\n",hwptr,diff);*/
1851 s->dma_dac.hwptr = hwptr;
1852 s->dma_dac.total_bytes += diff;
1853 if(s->dma_dac.mapped) {
1854 s->dma_dac.count += diff;
1855 if(s->dma_dac.count >= (signed)s->dma_dac.fragsize) {
1856 wake_up(&s->dma_dac.wait);
1858 }else{
1859 s->dma_dac.count -= diff;
1860 /* M_printk("maestro: ess_update_ptr: diff: %d, count: %d\n", diff, s->dma_dac.count); */
1861 if(s->dma_dac.count <=0) {
1862 M_printk("underflow! diff: %d count: %d hw: %d sw: %d\n", diff, s->dma_dac.count,
1863 hwptr, s->dma_dac.swptr);
1864 /* FILL ME
1865 wrindir(s, SV_CIENABLE, s->enable); */
1866 /* XXX how on earth can calling this with the lock held work.. */
1867 stop_dac(s);
1868 /* brute force everyone back in sync, sigh */
1869 s->dma_dac.count =0;
1870 s->dma_dac.swptr = hwptr;
1871 s->dma_dac.error++;
1872 }else if(s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
1873 clear_advance(s);
1874 s->dma_dac.endcleared =1;
1876 if(s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize) {
1877 wake_up(&s->dma_dac.wait);
1878 /* printk("waking up DAC count: %d sw: %d hw: %d\n",s->dma_dac.count, s->dma_dac.swptr,
1879 hwptr);*/
1885 static void
1886 ess_interrupt(int irq,void*dev_id,struct pt_regs *regs)
1888 struct ess_state *s;
1889 struct ess_card *c = (struct ess_card *)dev_id;
1890 int i;
1891 u32 event;
1893 if( ! (event =inb(c->iobase+0x1A)) )return;
1895 outw(inw(c->iobase+4)&1, c->iobase+4);
1897 /* M_printk("maestro int: %x\n",event);*/
1899 if(event&(1<<6))
1901 /* XXX if we have a hw volume control int enable
1902 all the ints? doesn't make sense.. */
1903 event =inw(c->iobase+0x18);
1904 outb(0xFF, c->iobase+0x1A);
1906 else
1908 /* else ack 'em all, i imagine */
1909 outb(0xFF, c->iobase+0x1A);
1913 * Update the pointers for all APU's we are running.
1915 for(i=0;i<NR_DSPS;i++)
1917 s=&c->channels[i];
1918 if(s->dev_audio == -1)
1919 break;
1920 spin_lock(&s->lock);
1921 ess_update_ptr(s);
1922 spin_unlock(&s->lock);
1927 /* --------------------------------------------------------------------- */
1929 static const char invalid_magic[] = KERN_CRIT "maestro: invalid magic value in %s\n";
1931 #define VALIDATE_MAGIC(FOO,MAG) \
1932 ({ \
1933 if (!(FOO) || (FOO)->magic != MAG) { \
1934 printk(invalid_magic,__FUNCTION__); \
1935 return -ENXIO; \
1939 #define VALIDATE_STATE(a) VALIDATE_MAGIC(a,ESS_STATE_MAGIC)
1940 #define VALIDATE_CARD(a) VALIDATE_MAGIC(a,ESS_CARD_MAGIC)
1942 static voidset_mixer(struct ess_card *card,unsigned int mixer,unsigned int val )
1944 unsigned int left,right;
1945 /* cleanse input a little */
1946 right = ((val >>8) &0xff) ;
1947 left = (val &0xff) ;
1949 if(right >100) right =100;
1950 if(left >100) left =100;
1952 card->mix.mixer_state[mixer]=(right <<8) | left;
1953 card->mix.write_mixer(card,mixer,left,right);
1956 static void
1957 mixer_push_state(struct ess_card *card)
1959 int i;
1960 for(i =0; i < SOUND_MIXER_NRDEVICES ; i++) {
1961 if( !supported_mixer(card,i))continue;
1963 set_mixer(card,i,card->mix.mixer_state[i]);
1967 static intmixer_ioctl(struct ess_card *card,unsigned int cmd,unsigned long arg)
1969 int i, val=0;
1970 unsigned long flags;
1972 VALIDATE_CARD(card);
1973 if(cmd == SOUND_MIXER_INFO) {
1974 mixer_info info;
1975 strncpy(info.id, card_names[card->card_type],sizeof(info.id));
1976 strncpy(info.name,card_names[card->card_type],sizeof(info.name));
1977 info.modify_counter = card->mix.modcnt;
1978 if(copy_to_user((void*)arg, &info,sizeof(info)))
1979 return-EFAULT;
1980 return0;
1982 if(cmd == SOUND_OLD_MIXER_INFO) {
1983 _old_mixer_info info;
1984 strncpy(info.id, card_names[card->card_type],sizeof(info.id));
1985 strncpy(info.name,card_names[card->card_type],sizeof(info.name));
1986 if(copy_to_user((void*)arg, &info,sizeof(info)))
1987 return-EFAULT;
1988 return0;
1990 if(cmd == OSS_GETVERSION)
1991 returnput_user(SOUND_VERSION, (int*)arg);
1993 if(_IOC_TYPE(cmd) !='M'||_IOC_SIZE(cmd) !=sizeof(int))
1994 return-EINVAL;
1996 if(_IOC_DIR(cmd) == _IOC_READ) {
1997 switch(_IOC_NR(cmd)) {
1998 case SOUND_MIXER_RECSRC:/* give them the current record source */
2000 if(!card->mix.recmask_io) {
2001 val =0;
2002 }else{
2003 spin_lock_irqsave(&card->lock, flags);
2004 val = card->mix.recmask_io(card,1,0);
2005 spin_unlock_irqrestore(&card->lock, flags);
2007 break;
2009 case SOUND_MIXER_DEVMASK:/* give them the supported mixers */
2010 val = card->mix.supported_mixers;
2011 break;
2013 case SOUND_MIXER_RECMASK:/* Arg contains a bit for each supported recording source */
2014 val = card->mix.record_sources;
2015 break;
2017 case SOUND_MIXER_STEREODEVS:/* Mixer channels supporting stereo */
2018 val = card->mix.stereo_mixers;
2019 break;
2021 case SOUND_MIXER_CAPS:
2022 val = SOUND_CAP_EXCL_INPUT;
2023 break;
2025 default:/* read a specific mixer */
2026 i =_IOC_NR(cmd);
2028 if( !supported_mixer(card,i))
2029 return-EINVAL;
2031 /* do we ever want to touch the hardware? */
2032 /* spin_lock_irqsave(&card->lock, flags);
2033 val = card->mix.read_mixer(card,i);
2034 spin_unlock_irqrestore(&card->lock, flags);*/
2036 val = card->mix.mixer_state[i];
2037 /* M_printk("returned 0x%x for mixer %d\n",val,i);*/
2039 break;
2041 returnput_user(val,(int*)arg);
2044 if(_IOC_DIR(cmd) != (_IOC_WRITE|_IOC_READ))
2045 return-EINVAL;
2047 card->mix.modcnt++;
2049 if(get_user(val, (int*)arg))
2050 return-EFAULT;
2052 switch(_IOC_NR(cmd)) {
2053 case SOUND_MIXER_RECSRC:/* Arg contains a bit for each recording source */
2055 if(!card->mix.recmask_io)return-EINVAL;
2056 if(!val)return0;
2057 if(! (val &= card->mix.record_sources))return-EINVAL;
2059 spin_lock_irqsave(&card->lock, flags);
2060 card->mix.recmask_io(card,0,val);
2061 spin_unlock_irqrestore(&card->lock, flags);
2062 return0;
2064 default:
2065 i =_IOC_NR(cmd);
2067 if( !supported_mixer(card,i))
2068 return-EINVAL;
2070 spin_lock_irqsave(&card->lock, flags);
2071 set_mixer(card,i,val);
2072 spin_unlock_irqrestore(&card->lock, flags);
2074 return0;
2078 /* --------------------------------------------------------------------- */
2080 static loff_t ess_llseek(struct file *file, loff_t offset,int origin)
2082 return-ESPIPE;
2085 /* --------------------------------------------------------------------- */
2087 static intess_open_mixdev(struct inode *inode,struct file *file)
2089 int minor =MINOR(inode->i_rdev);
2090 struct ess_card *card = devs;
2092 while(card && card->dev_mixer != minor)
2093 card = card->next;
2094 if(!card)
2095 return-ENODEV;
2097 file->private_data = card;
2098 return0;
2101 static intess_release_mixdev(struct inode *inode,struct file *file)
2103 struct ess_card *card = (struct ess_card *)file->private_data;
2105 VALIDATE_CARD(card);
2107 return0;
2110 static intess_ioctl_mixdev(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
2112 struct ess_card *card = (struct ess_card *)file->private_data;
2114 VALIDATE_CARD(card);
2116 returnmixer_ioctl(card, cmd, arg);
2119 static/*const*/struct file_operations ess_mixer_fops = {
2120 owner: THIS_MODULE,
2121 llseek: ess_llseek,
2122 ioctl: ess_ioctl_mixdev,
2123 open: ess_open_mixdev,
2124 release: ess_release_mixdev,
2127 /* --------------------------------------------------------------------- */
2129 static intdrain_dac(struct ess_state *s,int nonblock)
2131 DECLARE_WAITQUEUE(wait,current);
2132 unsigned long flags;
2133 int count;
2134 signed long tmo;
2136 if(s->dma_dac.mapped || !s->dma_dac.ready)
2137 return0;
2138 current->state = TASK_INTERRUPTIBLE;
2139 add_wait_queue(&s->dma_dac.wait, &wait);
2140 for(;;) {
2141 /* XXX uhm.. questionable locking*/
2142 spin_lock_irqsave(&s->lock, flags);
2143 count = s->dma_dac.count;
2144 spin_unlock_irqrestore(&s->lock, flags);
2145 if(count <=0)
2146 break;
2147 if(signal_pending(current))
2148 break;
2149 if(nonblock) {
2150 remove_wait_queue(&s->dma_dac.wait, &wait);
2151 current->state = TASK_RUNNING;
2152 return-EBUSY;
2154 tmo = (count * HZ) / s->ratedac;
2155 tmo >>= sample_shift[(s->fmt >> ESS_DAC_SHIFT) & ESS_FMT_MASK];
2156 /* XXX this is just broken. someone is waking us up alot, or schedule_timeout is broken.
2157 or something. who cares. - zach */
2158 if(!schedule_timeout(tmo ? tmo :1) && tmo)
2159 M_printk(KERN_DEBUG "maestro: dma timed out?? %ld\n",jiffies);
2161 remove_wait_queue(&s->dma_dac.wait, &wait);
2162 current->state = TASK_RUNNING;
2163 if(signal_pending(current))
2164 return-ERESTARTSYS;
2165 return0;
2168 /* --------------------------------------------------------------------- */
2169 /* Zach sez: "god this is gross.." */
2170 static int
2171 comb_stereo(unsigned char*real_buffer,unsigned char*tmp_buffer,int offset,
2172 int count,int bufsize)
2174 /* No such thing as stereo recording, so we
2175 use dual input mixers. which means we have to
2176 combine mono to stereo buffer. yuck.
2178 but we don't have to be able to work a byte at a time..*/
2180 unsigned char*so,*left,*right;
2181 int i;
2183 so = tmp_buffer;
2184 left = real_buffer + offset;
2185 right = real_buffer + bufsize/2+ offset;
2187 /* M_printk("comb_stereo writing %d to %p from %p and %p, offset: %d size: %d\n",count/2, tmp_buffer,left,right,offset,bufsize);*/
2189 for(i=count/4; i ; i--) {
2190 (*(so+2)) = *(right++);
2191 (*(so+3)) = *(right++);
2192 (*so) = *(left++);
2193 (*(so+1)) = *(left++);
2194 so+=4;
2197 return0;
2200 /* in this loop, dma_adc.count signifies the amount of data thats waiting
2201 to be copied to the user's buffer. it is filled by the interrupt
2202 handler and drained by this loop. */
2203 static ssize_t
2204 ess_read(struct file *file,char*buffer,size_t count, loff_t *ppos)
2206 struct ess_state *s = (struct ess_state *)file->private_data;
2207 ssize_t ret;
2208 unsigned long flags;
2209 unsigned swptr;
2210 int cnt;
2211 unsigned char*combbuf = NULL;
2213 VALIDATE_STATE(s);
2214 if(ppos != &file->f_pos)
2215 return-ESPIPE;
2216 if(s->dma_adc.mapped)
2217 return-ENXIO;
2218 if(!s->dma_adc.ready && (ret =prog_dmabuf(s,1)))
2219 return ret;
2220 if(!access_ok(VERIFY_WRITE, buffer, count))
2221 return-EFAULT;
2222 if(!(combbuf =kmalloc(count,GFP_KERNEL)))
2223 return-ENOMEM;
2224 ret =0;
2226 calc_bob_rate(s);
2228 while(count >0) {
2229 spin_lock_irqsave(&s->lock, flags);
2230 /* remember, all these things are expressed in bytes to be
2231 sent to the user.. hence the evil / 2 down below */
2232 swptr = s->dma_adc.swptr;
2233 cnt = s->dma_adc.dmasize-swptr;
2234 if(s->dma_adc.count < cnt)
2235 cnt = s->dma_adc.count;
2236 spin_unlock_irqrestore(&s->lock, flags);
2238 if(cnt > count)
2239 cnt = count;
2241 if( cnt >0) cnt &= ~3;
2243 if(cnt <=0) {
2244 start_adc(s);
2245 if(file->f_flags & O_NONBLOCK)
2247 ret = ret ? ret : -EAGAIN;
2248 goto rec_return_free;
2250 if(!interruptible_sleep_on_timeout(&s->dma_adc.wait, HZ)) {
2251 if(! s->card->in_suspend)printk(KERN_DEBUG "maestro: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
2252 s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
2253 s->dma_adc.hwptr, s->dma_adc.swptr);
2254 stop_adc(s);
2255 spin_lock_irqsave(&s->lock, flags);
2256 set_dmac(s,virt_to_bus(s->dma_adc.rawbuf), s->dma_adc.numfrag << s->dma_adc.fragshift);
2257 /* program enhanced mode registers */
2258 /* FILL ME */
2259 /* wrindir(s, SV_CIDMACBASECOUNT1, (s->dma_adc.fragsamples-1) >> 8);
2260 wrindir(s, SV_CIDMACBASECOUNT0, s->dma_adc.fragsamples-1); */
2261 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr =0;
2262 spin_unlock_irqrestore(&s->lock, flags);
2264 if(signal_pending(current))
2266 ret = ret ? ret : -ERESTARTSYS;
2267 goto rec_return_free;
2269 continue;
2272 if(s->fmt & (ESS_FMT_STEREO << ESS_ADC_SHIFT)) {
2273 /* swptr/2 so that we know the real offset in each apu's buffer */
2274 comb_stereo(s->dma_adc.rawbuf,combbuf,swptr/2,cnt,s->dma_adc.dmasize);
2275 if(copy_to_user(buffer, combbuf, cnt)) {
2276 ret = ret ? ret : -EFAULT;
2277 goto rec_return_free;
2279 }else{
2280 if(copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
2281 ret = ret ? ret : -EFAULT;
2282 goto rec_return_free;
2286 swptr = (swptr + cnt) % s->dma_adc.dmasize;
2287 spin_lock_irqsave(&s->lock, flags);
2288 s->dma_adc.swptr = swptr;
2289 s->dma_adc.count -= cnt;
2290 spin_unlock_irqrestore(&s->lock, flags);
2291 count -= cnt;
2292 buffer += cnt;
2293 ret += cnt;
2294 start_adc(s);
2297 rec_return_free:
2298 if(combbuf)kfree(combbuf);
2299 return ret;
2302 static ssize_t
2303 ess_write(struct file *file,const char*buffer,size_t count, loff_t *ppos)
2305 struct ess_state *s = (struct ess_state *)file->private_data;
2306 ssize_t ret;
2307 unsigned long flags;
2308 unsigned swptr;
2309 int cnt;
2311 VALIDATE_STATE(s);
2312 if(ppos != &file->f_pos)
2313 return-ESPIPE;
2314 if(s->dma_dac.mapped)
2315 return-ENXIO;
2316 if(!s->dma_dac.ready && (ret =prog_dmabuf(s,0)))
2317 return ret;
2318 if(!access_ok(VERIFY_READ, buffer, count))
2319 return-EFAULT;
2320 ret =0;
2322 calc_bob_rate(s);
2324 while(count >0) {
2325 spin_lock_irqsave(&s->lock, flags);
2327 if(s->dma_dac.count <0) {
2328 s->dma_dac.count =0;
2329 s->dma_dac.swptr = s->dma_dac.hwptr;
2331 swptr = s->dma_dac.swptr;
2333 cnt = s->dma_dac.dmasize-swptr;
2335 if(s->dma_dac.count + cnt > s->dma_dac.dmasize)
2336 cnt = s->dma_dac.dmasize - s->dma_dac.count;
2338 spin_unlock_irqrestore(&s->lock, flags);
2340 if(cnt > count)
2341 cnt = count;
2343 if(cnt <=0) {
2344 start_dac(s);
2345 if(file->f_flags & O_NONBLOCK) {
2346 if(!ret) ret = -EAGAIN;
2347 goto return_free;
2349 if(!interruptible_sleep_on_timeout(&s->dma_dac.wait, HZ)) {
2350 if(! s->card->in_suspend)printk(KERN_DEBUG "maestro: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
2351 s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
2352 s->dma_dac.hwptr, s->dma_dac.swptr);
2353 stop_dac(s);
2354 spin_lock_irqsave(&s->lock, flags);
2355 set_dmaa(s,virt_to_bus(s->dma_dac.rawbuf), s->dma_dac.numfrag << s->dma_dac.fragshift);
2356 /* program enhanced mode registers */
2357 /* wrindir(s, SV_CIDMAABASECOUNT1, (s->dma_dac.fragsamples-1) >> 8);
2358 wrindir(s, SV_CIDMAABASECOUNT0, s->dma_dac.fragsamples-1); */
2359 /* FILL ME */
2360 s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr =0;
2361 spin_unlock_irqrestore(&s->lock, flags);
2363 if(signal_pending(current)) {
2364 if(!ret) ret = -ERESTARTSYS;
2365 goto return_free;
2367 continue;
2369 if(copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
2370 if(!ret) ret = -EFAULT;
2371 goto return_free;
2373 /* printk("wrote %d bytes at sw: %d cnt: %d while hw: %d\n",cnt, swptr, s->dma_dac.count, s->dma_dac.hwptr);*/
2375 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2377 spin_lock_irqsave(&s->lock, flags);
2378 s->dma_dac.swptr = swptr;
2379 s->dma_dac.count += cnt;
2380 s->dma_dac.endcleared =0;
2381 spin_unlock_irqrestore(&s->lock, flags);
2382 count -= cnt;
2383 buffer += cnt;
2384 ret += cnt;
2385 start_dac(s);
2387 return_free:
2388 return ret;
2391 /* No kernel lock - we have our own spinlock */
2392 static unsigned intess_poll(struct file *file,struct poll_table_struct *wait)
2394 struct ess_state *s = (struct ess_state *)file->private_data;
2395 unsigned long flags;
2396 unsigned int mask =0;
2398 VALIDATE_STATE(s);
2400 /* In 0.14 prog_dmabuf always returns success anyway ... */
2401 if(file->f_mode & FMODE_WRITE) {
2402 if(!s->dma_dac.ready &&prog_dmabuf(s,0))
2403 return0;
2405 if(file->f_mode & FMODE_READ) {
2406 if(!s->dma_adc.ready &&prog_dmabuf(s,1))
2407 return0;
2410 if(file->f_mode & FMODE_WRITE)
2411 poll_wait(file, &s->dma_dac.wait, wait);
2412 if(file->f_mode & FMODE_READ)
2413 poll_wait(file, &s->dma_adc.wait, wait);
2414 spin_lock_irqsave(&s->lock, flags);
2415 ess_update_ptr(s);
2416 if(file->f_mode & FMODE_READ) {
2417 if(s->dma_adc.count >= (signed)s->dma_adc.fragsize)
2418 mask |= POLLIN | POLLRDNORM;
2420 if(file->f_mode & FMODE_WRITE) {
2421 if(s->dma_dac.mapped) {
2422 if(s->dma_dac.count >= (signed)s->dma_dac.fragsize)
2423 mask |= POLLOUT | POLLWRNORM;
2424 }else{
2425 if((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
2426 mask |= POLLOUT | POLLWRNORM;
2429 spin_unlock_irqrestore(&s->lock, flags);
2430 return mask;
2433 static intess_mmap(struct file *file,struct vm_area_struct *vma)
2435 struct ess_state *s = (struct ess_state *)file->private_data;
2436 struct dmabuf *db;
2437 int ret = -EINVAL;
2438 unsigned long size;
2440 VALIDATE_STATE(s);
2441 lock_kernel();
2442 if(vma->vm_flags & VM_WRITE) {
2443 if((ret =prog_dmabuf(s,1)) !=0)
2444 goto out;
2445 db = &s->dma_dac;
2446 }else
2447 #if 0
2448 /* if we can have the wp/wc do the combining
2449 we can turn this back on. */
2450 if(vma->vm_flags & VM_READ) {
2451 if((ret =prog_dmabuf(s,0)) !=0)
2452 goto out;
2453 db = &s->dma_adc;
2454 }else
2455 #endif
2456 goto out;
2457 ret = -EINVAL;
2458 if(SILLY_OFFSET(vma) !=0)
2459 goto out;
2460 size = vma->vm_end - vma->vm_start;
2461 if(size > (PAGE_SIZE << db->buforder))
2462 goto out;
2463 ret = -EAGAIN;
2464 if(remap_page_range(vma->vm_start,virt_to_phys(db->rawbuf), size, vma->vm_page_prot))
2465 goto out;
2466 db->mapped =1;
2467 ret =0;
2468 out:
2469 unlock_kernel();
2470 return ret;
2473 static intess_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
2475 struct ess_state *s = (struct ess_state *)file->private_data;
2476 unsigned long flags;
2477 audio_buf_info abinfo;
2478 count_info cinfo;
2479 int val, mapped, ret;
2480 unsigned char fmtm, fmtd;
2482 /* printk("maestro: ess_ioctl: cmd %d\n", cmd);*/
2484 VALIDATE_STATE(s);
2485 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
2486 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
2487 switch(cmd) {
2488 case OSS_GETVERSION:
2489 returnput_user(SOUND_VERSION, (int*)arg);
2491 case SNDCTL_DSP_SYNC:
2492 if(file->f_mode & FMODE_WRITE)
2493 returndrain_dac(s, file->f_flags & O_NONBLOCK);
2494 return0;
2496 case SNDCTL_DSP_SETDUPLEX:
2497 /* XXX fix */
2498 return0;
2500 case SNDCTL_DSP_GETCAPS:
2501 returnput_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int*)arg);
2503 case SNDCTL_DSP_RESET:
2504 if(file->f_mode & FMODE_WRITE) {
2505 stop_dac(s);
2506 synchronize_irq();
2507 s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes =0;
2509 if(file->f_mode & FMODE_READ) {
2510 stop_adc(s);
2511 synchronize_irq();
2512 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes =0;
2514 return0;
2516 case SNDCTL_DSP_SPEED:
2517 if(get_user(val, (int*)arg))
2518 return-EFAULT;
2519 if(val >=0) {
2520 if(file->f_mode & FMODE_READ) {
2521 stop_adc(s);
2522 s->dma_adc.ready =0;
2523 set_adc_rate(s, val);
2525 if(file->f_mode & FMODE_WRITE) {
2526 stop_dac(s);
2527 s->dma_dac.ready =0;
2528 set_dac_rate(s, val);
2531 returnput_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int*)arg);
2533 case SNDCTL_DSP_STEREO:
2534 if(get_user(val, (int*)arg))
2535 return-EFAULT;
2536 fmtd =0;
2537 fmtm = ~0;
2538 if(file->f_mode & FMODE_READ) {
2539 stop_adc(s);
2540 s->dma_adc.ready =0;
2541 if(val)
2542 fmtd |= ESS_FMT_STEREO << ESS_ADC_SHIFT;
2543 else
2544 fmtm &= ~(ESS_FMT_STEREO << ESS_ADC_SHIFT);
2546 if(file->f_mode & FMODE_WRITE) {
2547 stop_dac(s);
2548 s->dma_dac.ready =0;
2549 if(val)
2550 fmtd |= ESS_FMT_STEREO << ESS_DAC_SHIFT;
2551 else
2552 fmtm &= ~(ESS_FMT_STEREO << ESS_DAC_SHIFT);
2554 set_fmt(s, fmtm, fmtd);
2555 return0;
2557 case SNDCTL_DSP_CHANNELS:
2558 if(get_user(val, (int*)arg))
2559 return-EFAULT;
2560 if(val !=0) {
2561 fmtd =0;
2562 fmtm = ~0;
2563 if(file->f_mode & FMODE_READ) {
2564 stop_adc(s);
2565 s->dma_adc.ready =0;
2566 if(val >=2)
2567 fmtd |= ESS_FMT_STEREO << ESS_ADC_SHIFT;
2568 else
2569 fmtm &= ~(ESS_FMT_STEREO << ESS_ADC_SHIFT);
2571 if(file->f_mode & FMODE_WRITE) {
2572 stop_dac(s);
2573 s->dma_dac.ready =0;
2574 if(val >=2)
2575 fmtd |= ESS_FMT_STEREO << ESS_DAC_SHIFT;
2576 else
2577 fmtm &= ~(ESS_FMT_STEREO << ESS_DAC_SHIFT);
2579 set_fmt(s, fmtm, fmtd);
2581 returnput_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_STEREO << ESS_ADC_SHIFT)
2582 : (ESS_FMT_STEREO << ESS_DAC_SHIFT))) ?2:1, (int*)arg);
2584 case SNDCTL_DSP_GETFMTS:/* Returns a mask */
2585 returnput_user(AFMT_U8|AFMT_S16_LE, (int*)arg);
2587 case SNDCTL_DSP_SETFMT:/* Selects ONE fmt*/
2588 if(get_user(val, (int*)arg))
2589 return-EFAULT;
2590 if(val != AFMT_QUERY) {
2591 fmtd =0;
2592 fmtm = ~0;
2593 if(file->f_mode & FMODE_READ) {
2594 stop_adc(s);
2595 s->dma_adc.ready =0;
2596 /* fixed at 16bit for now */
2597 fmtd |= ESS_FMT_16BIT << ESS_ADC_SHIFT;
2598 #if 0
2599 if(val == AFMT_S16_LE)
2600 fmtd |= ESS_FMT_16BIT << ESS_ADC_SHIFT;
2601 else
2602 fmtm &= ~(ESS_FMT_16BIT << ESS_ADC_SHIFT);
2603 #endif
2605 if(file->f_mode & FMODE_WRITE) {
2606 stop_dac(s);
2607 s->dma_dac.ready =0;
2608 if(val == AFMT_S16_LE)
2609 fmtd |= ESS_FMT_16BIT << ESS_DAC_SHIFT;
2610 else
2611 fmtm &= ~(ESS_FMT_16BIT << ESS_DAC_SHIFT);
2613 set_fmt(s, fmtm, fmtd);
2615 returnput_user((s->fmt & ((file->f_mode & FMODE_READ) ?
2616 (ESS_FMT_16BIT << ESS_ADC_SHIFT)
2617 : (ESS_FMT_16BIT << ESS_DAC_SHIFT))) ?
2618 AFMT_S16_LE :
2619 AFMT_U8,
2620 (int*)arg);
2622 case SNDCTL_DSP_POST:
2623 return0;
2625 case SNDCTL_DSP_GETTRIGGER:
2626 val =0;
2627 if((file->f_mode & FMODE_READ) && (s->enable & ADC_RUNNING))
2628 val |= PCM_ENABLE_INPUT;
2629 if((file->f_mode & FMODE_WRITE) && (s->enable & DAC_RUNNING))
2630 val |= PCM_ENABLE_OUTPUT;
2631 returnput_user(val, (int*)arg);
2633 case SNDCTL_DSP_SETTRIGGER:
2634 if(get_user(val, (int*)arg))
2635 return-EFAULT;
2636 if(file->f_mode & FMODE_READ) {
2637 if(val & PCM_ENABLE_INPUT) {
2638 if(!s->dma_adc.ready && (ret =prog_dmabuf(s,1)))
2639 return ret;
2640 start_adc(s);
2641 }else
2642 stop_adc(s);
2644 if(file->f_mode & FMODE_WRITE) {
2645 if(val & PCM_ENABLE_OUTPUT) {
2646 if(!s->dma_dac.ready && (ret =prog_dmabuf(s,0)))
2647 return ret;
2648 start_dac(s);
2649 }else
2650 stop_dac(s);
2652 return0;
2654 case SNDCTL_DSP_GETOSPACE:
2655 if(!(file->f_mode & FMODE_WRITE))
2656 return-EINVAL;
2657 if(!s->dma_dac.ready && (ret =prog_dmabuf(s,0)))
2658 return ret;
2659 spin_lock_irqsave(&s->lock, flags);
2660 ess_update_ptr(s);
2661 abinfo.fragsize = s->dma_dac.fragsize;
2662 abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
2663 abinfo.fragstotal = s->dma_dac.numfrag;
2664 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
2665 spin_unlock_irqrestore(&s->lock, flags);
2666 returncopy_to_user((void*)arg, &abinfo,sizeof(abinfo)) ? -EFAULT :0;
2668 case SNDCTL_DSP_GETISPACE:
2669 if(!(file->f_mode & FMODE_READ))
2670 return-EINVAL;
2671 if(!s->dma_adc.ready && (ret =prog_dmabuf(s,1)))
2672 return ret;
2673 spin_lock_irqsave(&s->lock, flags);
2674 ess_update_ptr(s);
2675 abinfo.fragsize = s->dma_adc.fragsize;
2676 abinfo.bytes = s->dma_adc.count;
2677 abinfo.fragstotal = s->dma_adc.numfrag;
2678 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
2679 spin_unlock_irqrestore(&s->lock, flags);
2680 returncopy_to_user((void*)arg, &abinfo,sizeof(abinfo)) ? -EFAULT :0;
2682 case SNDCTL_DSP_NONBLOCK:
2683 file->f_flags |= O_NONBLOCK;
2684 return0;
2686 case SNDCTL_DSP_GETODELAY:
2687 if(!(file->f_mode & FMODE_WRITE))
2688 return-EINVAL;
2689 if(!s->dma_dac.ready && (ret =prog_dmabuf(s,0)))
2690 return ret;
2691 spin_lock_irqsave(&s->lock, flags);
2692 ess_update_ptr(s);
2693 val = s->dma_dac.count;
2694 spin_unlock_irqrestore(&s->lock, flags);
2695 returnput_user(val, (int*)arg);
2697 case SNDCTL_DSP_GETIPTR:
2698 if(!(file->f_mode & FMODE_READ))
2699 return-EINVAL;
2700 if(!s->dma_adc.ready && (ret =prog_dmabuf(s,1)))
2701 return ret;
2702 spin_lock_irqsave(&s->lock, flags);
2703 ess_update_ptr(s);
2704 cinfo.bytes = s->dma_adc.total_bytes;
2705 cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
2706 cinfo.ptr = s->dma_adc.hwptr;
2707 if(s->dma_adc.mapped)
2708 s->dma_adc.count &= s->dma_adc.fragsize-1;
2709 spin_unlock_irqrestore(&s->lock, flags);
2710 returncopy_to_user((void*)arg, &cinfo,sizeof(cinfo));
2712 case SNDCTL_DSP_GETOPTR:
2713 if(!(file->f_mode & FMODE_WRITE))
2714 return-EINVAL;
2715 if(!s->dma_dac.ready && (ret =prog_dmabuf(s,0)))
2716 return ret;
2717 spin_lock_irqsave(&s->lock, flags);
2718 ess_update_ptr(s);
2719 cinfo.bytes = s->dma_dac.total_bytes;
2720 cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
2721 cinfo.ptr = s->dma_dac.hwptr;
2722 if(s->dma_dac.mapped)
2723 s->dma_dac.count &= s->dma_dac.fragsize-1;
2724 spin_unlock_irqrestore(&s->lock, flags);
2725 returncopy_to_user((void*)arg, &cinfo,sizeof(cinfo));
2727 case SNDCTL_DSP_GETBLKSIZE:
2728 if(file->f_mode & FMODE_WRITE) {
2729 if((val =prog_dmabuf(s,0)))
2730 return val;
2731 returnput_user(s->dma_dac.fragsize, (int*)arg);
2733 if((val =prog_dmabuf(s,1)))
2734 return val;
2735 returnput_user(s->dma_adc.fragsize, (int*)arg);
2737 case SNDCTL_DSP_SETFRAGMENT:
2738 if(get_user(val, (int*)arg))
2739 return-EFAULT;
2740 M_printk("maestro: SETFRAGMENT: %0x\n",val);
2741 if(file->f_mode & FMODE_READ) {
2742 s->dma_adc.ossfragshift = val &0xffff;
2743 s->dma_adc.ossmaxfrags = (val >>16) &0xffff;
2744 if(s->dma_adc.ossfragshift <4)
2745 s->dma_adc.ossfragshift =4;
2746 if(s->dma_adc.ossfragshift >15)
2747 s->dma_adc.ossfragshift =15;
2748 if(s->dma_adc.ossmaxfrags <4)
2749 s->dma_adc.ossmaxfrags =4;
2751 if(file->f_mode & FMODE_WRITE) {
2752 s->dma_dac.ossfragshift = val &0xffff;
2753 s->dma_dac.ossmaxfrags = (val >>16) &0xffff;
2754 if(s->dma_dac.ossfragshift <4)
2755 s->dma_dac.ossfragshift =4;
2756 if(s->dma_dac.ossfragshift >15)
2757 s->dma_dac.ossfragshift =15;
2758 if(s->dma_dac.ossmaxfrags <4)
2759 s->dma_dac.ossmaxfrags =4;
2761 return0;
2763 case SNDCTL_DSP_SUBDIVIDE:
2764 if((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
2765 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
2766 return-EINVAL;
2767 if(get_user(val, (int*)arg))
2768 return-EFAULT;
2769 if(val !=1&& val !=2&& val !=4)
2770 return-EINVAL;
2771 if(file->f_mode & FMODE_READ)
2772 s->dma_adc.subdivision = val;
2773 if(file->f_mode & FMODE_WRITE)
2774 s->dma_dac.subdivision = val;
2775 return0;
2777 case SOUND_PCM_READ_RATE:
2778 returnput_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int*)arg);
2780 case SOUND_PCM_READ_CHANNELS:
2781 returnput_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_STEREO << ESS_ADC_SHIFT)
2782 : (ESS_FMT_STEREO << ESS_DAC_SHIFT))) ?2:1, (int*)arg);
2784 case SOUND_PCM_READ_BITS:
2785 returnput_user((s->fmt & ((file->f_mode & FMODE_READ) ? (ESS_FMT_16BIT << ESS_ADC_SHIFT)
2786 : (ESS_FMT_16BIT << ESS_DAC_SHIFT))) ?16:8, (int*)arg);
2788 case SOUND_PCM_WRITE_FILTER:
2789 case SNDCTL_DSP_SETSYNCRO:
2790 case SOUND_PCM_READ_FILTER:
2791 return-EINVAL;
2794 return-EINVAL;
2797 static void
2798 set_base_registers(struct ess_state *s,void*vaddr)
2800 unsigned long packed_phys =virt_to_bus(vaddr)>>12;
2801 wave_set_register(s,0x01FC, packed_phys);
2802 wave_set_register(s,0x01FD, packed_phys);
2803 wave_set_register(s,0x01FE, packed_phys);
2804 wave_set_register(s,0x01FF, packed_phys);
2808 * this guy makes sure we're in the right power
2809 * state for what we want to be doing
2811 static voidmaestro_power(struct ess_card *card,int tostate)
2813 u16 active_mask = acpi_state_mask[tostate];
2814 u8 state;
2816 if(!use_pm)return;
2818 pci_read_config_byte(card->pcidev, card->power_regs+0x4, &state);
2819 state&=3;
2821 /* make sure we're in the right state */
2822 if(state != tostate) {
2823 M_printk(KERN_WARNING "maestro: dev %02x:%02x.%x switching from D%d to D%d\n",
2824 card->pcidev->bus->number,
2825 PCI_SLOT(card->pcidev->devfn),
2826 PCI_FUNC(card->pcidev->devfn),
2827 state,tostate);
2828 pci_write_config_byte(card->pcidev, card->power_regs+0x4, tostate);
2831 /* and make sure the units we care about are on
2832 XXX we might want to do this before state flipping? */
2833 pci_write_config_word(card->pcidev,0x54, ~ active_mask);
2834 pci_write_config_word(card->pcidev,0x56, ~ active_mask);
2837 /* we allocate a large power of two for all our memory.
2838 this is cut up into (not to scale :):
2839 |silly fifo word | 512byte mixbuf per adc | dac/adc * channels |
2841 static int
2842 allocate_buffers(struct ess_state *s)
2844 void*rawbuf=NULL;
2845 int order,i;
2846 struct page *page, *pend;
2848 /* alloc as big a chunk as we can */
2849 for(order = (dsps_order + (16-PAGE_SHIFT) +1); order >= (dsps_order +2+1); order--)
2850 if((rawbuf = (void*)__get_free_pages(GFP_KERNEL|GFP_DMA, order)))
2851 break;
2853 if(!rawbuf)
2854 return1;
2856 M_printk("maestro: allocated %ld (%d) bytes at %p\n",PAGE_SIZE<<order,order, rawbuf);
2858 if((virt_to_bus(rawbuf) + (PAGE_SIZE << order) -1) & ~((1<<28)-1)) {
2859 printk(KERN_ERR "maestro: DMA buffer beyond 256MB! busaddr 0x%lx size %ld\n",
2860 virt_to_bus(rawbuf), PAGE_SIZE << order);
2861 kfree(rawbuf);
2862 return1;
2865 s->card->dmapages = rawbuf;
2866 s->card->dmaorder = order;
2868 for(i=0;i<NR_DSPS;i++) {
2869 struct ess_state *ess = &s->card->channels[i];
2871 if(ess->dev_audio == -1)
2872 continue;
2874 ess->dma_dac.ready = s->dma_dac.mapped =0;
2875 ess->dma_adc.ready = s->dma_adc.mapped =0;
2876 ess->dma_adc.buforder = ess->dma_dac.buforder = order -1- dsps_order -1;
2878 /* offset dac and adc buffers starting half way through and then at each [da][ad]c's
2879 order's intervals.. */
2880 ess->dma_dac.rawbuf = rawbuf + (PAGE_SIZE<<(order-1)) + (i * ( PAGE_SIZE << (ess->dma_dac.buforder +1)));
2881 ess->dma_adc.rawbuf = ess->dma_dac.rawbuf + ( PAGE_SIZE << ess->dma_dac.buforder);
2882 /* offset mixbuf by a mixbuf so that the lame status fifo can
2883 happily scribble away.. */
2884 ess->mixbuf = rawbuf + (512* (i+1));
2886 M_printk("maestro: setup apu %d: dac: %p adc: %p mix: %p\n",i,ess->dma_dac.rawbuf,
2887 ess->dma_adc.rawbuf, ess->mixbuf);
2891 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
2892 pend =virt_to_page(rawbuf + (PAGE_SIZE << order) -1);
2893 for(page =virt_to_page(rawbuf); page <= pend; page++)
2894 mem_map_reserve(page);
2896 return0;
2898 static void
2899 free_buffers(struct ess_state *s)
2901 struct page *page, *pend;
2903 s->dma_dac.rawbuf = s->dma_adc.rawbuf = NULL;
2904 s->dma_dac.mapped = s->dma_adc.mapped =0;
2905 s->dma_dac.ready = s->dma_adc.ready =0;
2907 M_printk("maestro: freeing %p\n",s->card->dmapages);
2908 /* undo marking the pages as reserved */
2910 pend =virt_to_page(s->card->dmapages + (PAGE_SIZE << s->card->dmaorder) -1);
2911 for(page =virt_to_page(s->card->dmapages); page <= pend; page++)
2912 mem_map_unreserve(page);
2914 free_pages((unsigned long)s->card->dmapages,s->card->dmaorder);
2915 s->card->dmapages = NULL;
2918 static int
2919 ess_open(struct inode *inode,struct file *file)
2921 int minor =MINOR(inode->i_rdev);
2922 struct ess_card *c = devs;
2923 struct ess_state *s = NULL, *sp;
2924 int i;
2925 unsigned char fmtm = ~0, fmts =0;
2928 * Scan the cards and find the channel. We only
2929 * do this at open time so it is ok
2932 while(c!=NULL)
2934 for(i=0;i<NR_DSPS;i++)
2936 sp=&c->channels[i];
2937 if(sp->dev_audio <0)
2938 continue;
2939 if((sp->dev_audio ^ minor) & ~0xf)
2940 continue;
2941 s=sp;
2943 c=c->next;
2946 if(!s)
2947 return-ENODEV;
2949 VALIDATE_STATE(s);
2950 file->private_data = s;
2951 /* wait for device to become free */
2952 down(&s->open_sem);
2953 while(s->open_mode & file->f_mode) {
2954 if(file->f_flags & O_NONBLOCK) {
2955 up(&s->open_sem);
2956 return-EWOULDBLOCK;
2958 up(&s->open_sem);
2959 interruptible_sleep_on(&s->open_wait);
2960 if(signal_pending(current))
2961 return-ERESTARTSYS;
2962 down(&s->open_sem);
2965 /* under semaphore.. */
2966 if((s->card->dmapages==NULL) &&allocate_buffers(s)) {
2967 up(&s->open_sem);
2968 return-ENOMEM;
2971 /* we're covered by the open_sem */
2972 if( ! s->card->dsps_open ) {
2973 maestro_power(s->card,ACPI_D0);
2974 start_bob(s);
2976 s->card->dsps_open++;
2977 M_printk("maestro: open, %d bobs now\n",s->card->dsps_open);
2979 /* ok, lets write WC base regs now that we've
2980 powered up the chip */
2981 M_printk("maestro: writing 0x%lx (bus 0x%lx) to the wp\n",virt_to_bus(s->card->dmapages),
2982 ((virt_to_bus(s->card->dmapages))&0xFFE00000)>>12);
2983 set_base_registers(s,s->card->dmapages);
2985 if(file->f_mode & FMODE_READ) {
2987 fmtm &= ~((ESS_FMT_STEREO | ESS_FMT_16BIT) << ESS_ADC_SHIFT);
2988 if ((minor & 0xf) == SND_DEV_DSP16)
2989 fmts |= ESS_FMT_16BIT << ESS_ADC_SHIFT; */
2991 fmtm &= ~((ESS_FMT_STEREO|ESS_FMT_16BIT) << ESS_ADC_SHIFT);
2992 fmts = (ESS_FMT_STEREO|ESS_FMT_16BIT) << ESS_ADC_SHIFT;
2994 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision =0;
2995 set_adc_rate(s,8000);
2997 if(file->f_mode & FMODE_WRITE) {
2998 fmtm &= ~((ESS_FMT_STEREO | ESS_FMT_16BIT) << ESS_DAC_SHIFT);
2999 if((minor &0xf) == SND_DEV_DSP16)
3000 fmts |= ESS_FMT_16BIT << ESS_DAC_SHIFT;
3002 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision =0;
3003 set_dac_rate(s,8000);
3005 set_fmt(s, fmtm, fmts);
3006 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
3008 up(&s->open_sem);
3009 return0;
3012 static int
3013 ess_release(struct inode *inode,struct file *file)
3015 struct ess_state *s = (struct ess_state *)file->private_data;
3017 VALIDATE_STATE(s);
3018 lock_kernel();
3019 if(file->f_mode & FMODE_WRITE)
3020 drain_dac(s, file->f_flags & O_NONBLOCK);
3021 down(&s->open_sem);
3022 if(file->f_mode & FMODE_WRITE) {
3023 stop_dac(s);
3025 if(file->f_mode & FMODE_READ) {
3026 stop_adc(s);
3029 s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3030 /* we're covered by the open_sem */
3031 M_printk("maestro: %d dsps now alive\n",s->card->dsps_open-1);
3032 if( --s->card->dsps_open <=0) {
3033 s->card->dsps_open =0;
3034 stop_bob(s);
3035 free_buffers(s);
3036 maestro_power(s->card,ACPI_D2);
3038 up(&s->open_sem);
3039 wake_up(&s->open_wait);
3040 unlock_kernel();
3041 return0;
3044 static struct file_operations ess_audio_fops = {
3045 owner: THIS_MODULE,
3046 llseek: ess_llseek,
3047 read: ess_read,
3048 write: ess_write,
3049 poll: ess_poll,
3050 ioctl: ess_ioctl,
3051 mmap: ess_mmap,
3052 open: ess_open,
3053 release: ess_release,
3056 static int
3057 maestro_config(struct ess_card *card)
3059 struct pci_dev *pcidev = card->pcidev;
3060 struct ess_state *ess = &card->channels[0];
3061 int apu,iobase = card->iobase;
3062 u16 w;
3063 u32 n;
3065 /* We used to muck around with pci config space that
3066 * we had no business messing with. We don't know enough
3067 * about the machine to know which DMA mode is appropriate,
3068 * etc. We were guessing wrong on some machines and making
3069 * them unhappy. We now trust in the BIOS to do things right,
3070 * which almost certainly means a new host of problems will
3071 * arise with broken BIOS implementations. screw 'em.
3072 * We're already intolerant of machines that don't assign
3073 * IRQs.
3076 /* do config work at full power */
3077 maestro_power(card,ACPI_D0);
3079 pci_read_config_word(pcidev,0x50, &w);
3081 w&=~(1<<5);/* Don't swap left/right (undoc)*/
3083 pci_write_config_word(pcidev,0x50, w);
3085 pci_read_config_word(pcidev,0x52, &w);
3086 w&=~(1<<15);/* Turn off internal clock multiplier */
3087 /* XXX how do we know which to use? */
3088 w&=~(1<<14);/* External clock */
3090 w&=~(1<<7);/* HWV off */
3091 w&=~(1<<6);/* Debounce off */
3092 w&=~(1<<5);/* GPIO 4:5 */
3093 w|= (1<<4);/* Disconnect from the CHI. Enabling this made a dell 7500 work. */
3094 w&=~(1<<2);/* MIDI fix off (undoc) */
3095 w&=~(1<<1);/* reserved, always write 0 */
3096 pci_write_config_word(pcidev,0x52, w);
3099 * Legacy mode
3102 pci_read_config_word(pcidev,0x40, &w);
3103 w|=(1<<15);/* legacy decode off */
3104 w&=~(1<<14);/* Disable SIRQ */
3105 w&=~(0x1f);/* disable mpu irq/io, game port, fm, SB */
3107 pci_write_config_word(pcidev,0x40, w);
3110 sound_reset(iobase);
3113 * Ring Bus Setup
3116 /* setup usual 0x34 stuff.. 0x36 may be chip specific */
3117 outw(0xC090, iobase+0x34);/* direct sound, stereo */
3118 udelay(20);
3119 outw(0x3000, iobase+0x36);/* direct sound, stereo */
3120 udelay(20);
3124 * Reset the CODEC
3127 maestro_ac97_reset(iobase,pcidev);
3130 * Ring Bus Setup
3133 n=inl(iobase+0x34);
3134 n&=~0xF000;
3135 n|=12<<12;/* Direct Sound, Stereo */
3136 outl(n, iobase+0x34);
3138 n=inl(iobase+0x34);
3139 n&=~0x0F00;/* Modem off */
3140 outl(n, iobase+0x34);
3142 n=inl(iobase+0x34);
3143 n&=~0x00F0;
3144 n|=9<<4;/* DAC, Stereo */
3145 outl(n, iobase+0x34);
3147 n=inl(iobase+0x34);
3148 n&=~0x000F;/* ASSP off */
3149 outl(n, iobase+0x34);
3151 n=inl(iobase+0x34);
3152 n|=(1<<29);/* Enable ring bus */
3153 outl(n, iobase+0x34);
3155 n=inl(iobase+0x34);
3156 n|=(1<<28);/* Enable serial bus */
3157 outl(n, iobase+0x34);
3159 n=inl(iobase+0x34);
3160 n&=~0x00F00000;/* MIC off */
3161 outl(n, iobase+0x34);
3163 n=inl(iobase+0x34);
3164 n&=~0x000F0000;/* I2S off */
3165 outl(n, iobase+0x34);
3168 w=inw(iobase+0x18);
3169 w&=~(1<<7);/* ClkRun off */
3170 outw(w, iobase+0x18);
3172 w=inw(iobase+0x18);
3173 w&=~(1<<6);/* Harpo off */
3174 outw(w, iobase+0x18);
3176 w=inw(iobase+0x18);
3177 w&=~(1<<4);/* ASSP irq off */
3178 outw(w, iobase+0x18);
3180 w=inw(iobase+0x18);
3181 w&=~(1<<3);/* ISDN irq off */
3182 outw(w, iobase+0x18);
3184 w=inw(iobase+0x18);
3185 w|=(1<<2);/* Direct Sound IRQ on */
3186 outw(w, iobase+0x18);
3188 w=inw(iobase+0x18);
3189 w&=~(1<<1);/* MPU401 IRQ off */
3190 outw(w, iobase+0x18);
3192 w=inw(iobase+0x18);
3193 w|=(1<<0);/* SB IRQ on */
3194 outw(w, iobase+0x18);
3196 /* it appears some maestros (dell 7500) only work if these are set,
3197 regardless of wether we use the assp or not. */
3199 outb(0, iobase+0xA4);
3200 outb(3, iobase+0xA2);
3201 outb(0, iobase+0xA6);
3203 for(apu=0;apu<16;apu++)
3205 /* Write 0 into the buffer area 0x1E0->1EF */
3206 outw(0x01E0+apu,0x10+iobase);
3207 outw(0x0000,0x12+iobase);
3210 * The 1.10 test program seem to write 0 into the buffer area
3211 * 0x1D0-0x1DF too.
3213 outw(0x01D0+apu,0x10+iobase);
3214 outw(0x0000,0x12+iobase);
3217 #if 1
3218 wave_set_register(ess, IDR7_WAVE_ROMRAM,
3219 (wave_get_register(ess, IDR7_WAVE_ROMRAM)&0xFF00));
3220 wave_set_register(ess, IDR7_WAVE_ROMRAM,
3221 wave_get_register(ess, IDR7_WAVE_ROMRAM)|0x100);
3222 wave_set_register(ess, IDR7_WAVE_ROMRAM,
3223 wave_get_register(ess, IDR7_WAVE_ROMRAM)&~0x200);
3224 wave_set_register(ess, IDR7_WAVE_ROMRAM,
3225 wave_get_register(ess, IDR7_WAVE_ROMRAM)|~0x400);
3226 #else
3227 maestro_write(ess, IDR7_WAVE_ROMRAM,
3228 (maestro_read(ess, IDR7_WAVE_ROMRAM)&0xFF00));
3229 maestro_write(ess, IDR7_WAVE_ROMRAM,
3230 maestro_read(ess, IDR7_WAVE_ROMRAM)|0x100);
3231 maestro_write(ess, IDR7_WAVE_ROMRAM,
3232 maestro_read(ess, IDR7_WAVE_ROMRAM)&~0x200);
3233 maestro_write(ess, IDR7_WAVE_ROMRAM,
3234 maestro_read(ess, IDR7_WAVE_ROMRAM)|0x400);
3235 #endif
3237 maestro_write(ess, IDR2_CRAM_DATA,0x0000);
3238 maestro_write(ess,0x08,0xB004);
3239 /* Now back to the DirectSound stuff */
3240 maestro_write(ess,0x09,0x001B);
3241 maestro_write(ess,0x0A,0x8000);
3242 maestro_write(ess,0x0B,0x3F37);
3243 maestro_write(ess,0x0C,0x0098);
3245 /* parallel out ?? */
3246 maestro_write(ess,0x0C,
3247 (maestro_read(ess,0x0C)&~0xF000)|0x8000);
3248 /* parallel in, has something to do with recording :) */
3249 maestro_write(ess,0x0C,
3250 (maestro_read(ess,0x0C)&~0x0F00)|0x0500);
3252 maestro_write(ess,0x0D,0x7632);
3254 /* Wave cache control on - test off, sg off,
3255 enable, enable extra chans 1Mb */
3257 outw(inw(0x14+iobase)|(1<<8),0x14+iobase);
3258 outw(inw(0x14+iobase)&0xFE03,0x14+iobase);
3259 outw((inw(0x14+iobase)&0xFFFC),0x14+iobase);
3260 outw(inw(0x14+iobase)|(1<<7),0x14+iobase);
3262 outw(0xA1A0,0x14+iobase);/* 0300 ? */
3264 /* Now clear the APU control ram */
3265 for(apu=0;apu<NR_APUS;apu++)
3267 for(w=0;w<NR_APU_REGS;w++)
3268 apu_set_register(ess, apu|ESS_CHAN_HARD, w,0);
3272 return0;
3276 /* this guy tries to find the pci power management
3277 * register bank. this should really be in core
3278 * code somewhere. 1 on success. */
3280 parse_power(struct ess_card *card,struct pci_dev *pcidev)
3282 u32 n;
3283 u16 w;
3284 u8 next;
3285 int max =64;/* an a 8bit guy pointing to 32bit guys
3286 can only express so much. */
3288 card->power_regs =0;
3290 /* check to see if we have a capabilities list in
3291 the config register */
3292 pci_read_config_word(pcidev, PCI_STATUS, &w);
3293 if(! w & PCI_STATUS_CAP_LIST)return0;
3295 /* walk the list, starting at the head. */
3296 pci_read_config_byte(pcidev,PCI_CAPABILITY_LIST,&next);
3298 while(next && max--) {
3299 pci_read_config_dword(pcidev, next & ~3, &n);
3300 if((n &0xff) == PCI_CAP_ID_PM) {
3301 card->power_regs = next;
3302 break;
3304 next = ((n>>8) &0xff);
3307 return card->power_regs ?1:0;
3310 static int __init
3311 maestro_install(struct pci_dev *pcidev,int card_type)
3313 u32 n;
3314 int iobase;
3315 int i;
3316 struct ess_card *card;
3317 struct ess_state *ess;
3318 struct pm_dev *pmdev;
3319 int num =0;
3321 /* don't pick up weird modem maestros */
3322 if(((pcidev->class>>8) &0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
3323 return0;
3325 iobase =SILLY_PCI_BASE_ADDRESS(pcidev);
3327 /* stake our claim on the iospace */
3328 if(request_region(iobase,256, card_names[card_type]) == NULL )
3330 printk(KERN_WARNING "maestro: can't allocate 256 bytes I/O at 0x%4.4x\n", iobase);
3331 return0;
3334 /* this was tripping up some machines */
3335 if(pcidev->irq ==0) {
3336 printk(KERN_WARNING "maestro: pci subsystem reports irq 0, this might not be correct.\n");
3339 /* just to be sure */
3340 pci_set_master(pcidev);
3342 card =kmalloc(sizeof(struct ess_card), GFP_KERNEL);
3343 if(card == NULL)
3345 printk(KERN_WARNING "maestro: out of memory\n");
3346 return0;
3349 memset(card,0,sizeof(*card));
3350 card->pcidev = pcidev;
3352 pmdev =pm_register(PM_PCI_DEV,PM_PCI_ID(pcidev),
3353 maestro_pm_callback);
3354 if(pmdev)
3355 pmdev->data = card;
3357 if(register_reboot_notifier(&maestro_nb)) {
3358 printk(KERN_WARNING "maestro: reboot notifier registration failed; may not reboot properly.\n");
3361 card->iobase = iobase;
3362 card->card_type = card_type;
3363 card->irq = pcidev->irq;
3364 card->next = devs;
3365 card->magic = ESS_CARD_MAGIC;
3366 spin_lock_init(&card->lock);
3367 init_waitqueue_head(&card->suspend_queue);
3368 devs = card;
3370 /* init our groups of 6 apus */
3371 for(i=0;i<NR_DSPS;i++)
3373 struct ess_state *s=&card->channels[i];
3375 s->index = i;
3377 s->card = card;
3378 init_waitqueue_head(&s->dma_adc.wait);
3379 init_waitqueue_head(&s->dma_dac.wait);
3380 init_waitqueue_head(&s->open_wait);
3381 spin_lock_init(&s->lock);
3382 SILLY_INIT_SEM(s->open_sem);
3383 s->magic = ESS_STATE_MAGIC;
3385 s->apu[0] =6*i;
3386 s->apu[1] = (6*i)+1;
3387 s->apu[2] = (6*i)+2;
3388 s->apu[3] = (6*i)+3;
3389 s->apu[4] = (6*i)+4;
3390 s->apu[5] = (6*i)+5;
3392 if(s->dma_adc.ready || s->dma_dac.ready || s->dma_adc.rawbuf)
3393 printk("maestro: BOTCH!\n");
3394 /* register devices */
3395 if((s->dev_audio =register_sound_dsp(&ess_audio_fops, -1)) <0)
3396 break;
3399 num = i;
3401 /* clear the rest if we ran out of slots to register */
3402 for(;i<NR_DSPS;i++)
3404 struct ess_state *s=&card->channels[i];
3405 s->dev_audio = -1;
3408 ess = &card->channels[0];
3410 if(pci_enable_device(pcidev)) {
3411 printk(KERN_ERR "maestro: pci_enable_device() failed\n");
3412 for(i =0; i < NR_DSPS; i++) {
3413 struct ess_state *s = &card->channels[i];
3414 if(s->dev_audio != -1)
3415 unregister_sound_dsp(s->dev_audio);
3417 release_region(card->iobase,256);
3418 unregister_reboot_notifier(&maestro_nb);
3419 kfree(card);
3420 return0;
3424 * Ok card ready. Begin setup proper
3427 printk(KERN_INFO "maestro: Configuring %s found at IO 0x%04X IRQ %d\n",
3428 card_names[card_type],iobase,card->irq);
3429 pci_read_config_dword(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &n);
3430 printk(KERN_INFO "maestro: subvendor id: 0x%08x\n",n);
3432 /* turn off power management unless:
3433 * - the user explicitly asks for it
3434 * or
3435 * - we're not a 2e, lesser chipps seem to have problems.
3436 * - we're not on our _very_ small whitelist. some implemenetations
3437 * really dont' like the pm code, others require it.
3438 * feel free to expand this as required.
3440 #define SUBSYSTEM_VENDOR(x) (x&0xffff)
3441 if( (use_pm !=1) &&
3442 ((card_type != TYPE_MAESTRO2E) || (SUBSYSTEM_VENDOR(n) !=0x1028)))
3443 use_pm =0;
3445 if(!use_pm)
3446 printk(KERN_INFO "maestro: not attempting power management.\n");
3447 else{
3448 if(!parse_power(card,pcidev))
3449 printk(KERN_INFO "maestro: no PCI power managment interface found.\n");
3450 else{
3451 pci_read_config_dword(pcidev, card->power_regs, &n);
3452 printk(KERN_INFO "maestro: PCI power managment capability: 0x%x\n",n>>16);
3456 maestro_config(card);
3458 if(maestro_ac97_get(card,0x00)==0x0080) {
3459 printk(KERN_ERR "maestro: my goodness! you seem to have a pt101 codec, which is quite rare.\n"
3460 "\tyou should tell someone about this.\n");
3461 }else{
3462 maestro_ac97_init(card);
3465 if((card->dev_mixer =register_sound_mixer(&ess_mixer_fops, -1)) <0) {
3466 printk("maestro: couldn't register mixer!\n");
3467 }else{
3468 memcpy(card->mix.mixer_state,mixer_defaults,sizeof(card->mix.mixer_state));
3469 mixer_push_state(card);
3472 if(request_irq(card->irq, ess_interrupt, SA_SHIRQ, card_names[card_type], card))
3474 printk(KERN_ERR "maestro: unable to allocate irq %d,\n", card->irq);
3475 unregister_sound_mixer(card->dev_mixer);
3476 for(i=0;i<NR_DSPS;i++)
3478 struct ess_state *s = &card->channels[i];
3479 if(s->dev_audio != -1)
3480 unregister_sound_dsp(s->dev_audio);
3482 release_region(card->iobase,256);
3483 unregister_reboot_notifier(&maestro_nb);
3484 kfree(card);
3485 return0;
3487 /* now go to sleep 'till something interesting happens */
3488 maestro_power(card,ACPI_D2);
3490 printk(KERN_INFO "maestro: %d channels configured.\n", num);
3491 return1;
3494 int __init init_maestro(void)
3496 struct pci_dev *pcidev = NULL;
3497 int foundone =0;
3499 if(!pci_present())/* No PCI bus in this machine! */
3500 return-ENODEV;
3501 printk(KERN_INFO "maestro: version " DRIVER_VERSION " time " __TIME__ " " __DATE__ "\n");
3503 pcidev = NULL;
3505 if(dsps_order <0) {
3506 dsps_order =1;
3507 printk(KERN_WARNING "maestro: clipping dsps_order to %d\n",dsps_order);
3509 else if(dsps_order > MAX_DSP_ORDER) {
3510 dsps_order = MAX_DSP_ORDER;
3511 printk(KERN_WARNING "maestro: clipping dsps_order to %d\n",dsps_order);
3515 * Find the ESS Maestro 2.
3518 while( (pcidev =pci_find_device(PCI_VENDOR_ESS, PCI_DEVICE_ID_ESS_ESS1968, pcidev))!=NULL ) {
3519 if(maestro_install(pcidev, TYPE_MAESTRO2))
3520 foundone=1;
3524 * Find the ESS Maestro 2E
3527 while( (pcidev =pci_find_device(PCI_VENDOR_ESS, PCI_DEVICE_ID_ESS_ESS1978, pcidev))!=NULL) {
3528 if(maestro_install(pcidev, TYPE_MAESTRO2E))
3529 foundone=1;
3533 * ESS Maestro 1
3536 while((pcidev =pci_find_device(PCI_VENDOR_ESS_OLD, PCI_DEVICE_ID_ESS_ESS0100, pcidev))!=NULL) {
3537 if(maestro_install(pcidev, TYPE_MAESTRO))
3538 foundone=1;
3540 if( ! foundone ) {
3541 printk("maestro: no devices found.\n");
3542 return-ENODEV;
3544 return0;
3547 static voidnuke_maestros(void)
3549 struct ess_card *card;
3551 /* we do these unconditionally, which is probably wrong */
3552 pm_unregister_all(maestro_pm_callback);
3553 unregister_reboot_notifier(&maestro_nb);
3555 while((card = devs)) {
3556 int i;
3557 devs = devs->next;
3559 /* XXX maybe should force stop bob, but should be all
3560 stopped by _release by now */
3561 free_irq(card->irq, card);
3562 unregister_sound_mixer(card->dev_mixer);
3563 for(i=0;i<NR_DSPS;i++)
3565 struct ess_state *ess = &card->channels[i];
3566 if(ess->dev_audio != -1)
3567 unregister_sound_dsp(ess->dev_audio);
3569 /* Goodbye, Mr. Bond. */
3570 maestro_power(card,ACPI_D3);
3571 release_region(card->iobase,256);
3572 kfree(card);
3574 devs = NULL;
3577 static intmaestro_notifier(struct notifier_block *nb,unsigned long event,void*buf)
3579 /* this notifier is called when the kernel is really shut down. */
3580 M_printk("maestro: shutting down\n");
3581 nuke_maestros();
3582 return NOTIFY_OK;
3585 /* --------------------------------------------------------------------- */
3587 #ifdef MODULE
3588 MODULE_AUTHOR("Zach Brown <zab@zabbo.net>, Alan Cox <alan@redhat.com>");
3589 MODULE_DESCRIPTION("ESS Maestro Driver");
3590 #ifdef M_DEBUG
3591 MODULE_PARM(debug,"i");
3592 #endif
3593 MODULE_PARM(dsps_order,"i");
3594 MODULE_PARM(use_pm,"i");
3595 MODULE_PARM(clocking,"i");
3597 voidcleanup_module(void) {
3598 M_printk("maestro: unloading\n");
3599 nuke_maestros();
3602 #endif
3604 /* --------------------------------------------------------------------- */
3606 void
3607 check_suspend(struct ess_card *card)
3609 DECLARE_WAITQUEUE(wait, current);
3611 if(!card->in_suspend)return;
3613 card->in_suspend++;
3614 add_wait_queue(&(card->suspend_queue), &wait);
3615 current->state = TASK_UNINTERRUPTIBLE;
3616 schedule();
3617 remove_wait_queue(&(card->suspend_queue), &wait);
3618 current->state = TASK_RUNNING;
3621 static int
3622 maestro_suspend(struct ess_card *card)
3624 unsigned long flags;
3625 int i,j;
3627 save_flags(flags);
3628 cli();/* over-kill */
3630 M_printk("maestro: apm in dev %p\n",card);
3632 /* we have to read from the apu regs, need
3633 to power it up */
3634 maestro_power(card,ACPI_D0);
3636 for(i=0;i<NR_DSPS;i++) {
3637 struct ess_state *s = &card->channels[i];
3639 if(s->dev_audio == -1)
3640 continue;
3642 M_printk("maestro: stopping apus for device %d\n",i);
3643 stop_dac(s);
3644 stop_adc(s);
3645 for(j=0;j<6;j++)
3646 card->apu_map[s->apu[j]][5]=apu_get_register(s,j,5);
3650 /* get rid of interrupts? */
3651 if( card->dsps_open >0)
3652 stop_bob(&card->channels[0]);
3654 card->in_suspend++;
3656 restore_flags(flags);
3658 /* we trust in the bios to power down the chip on suspend.
3659 * XXX I'm also not sure that in_suspend will protect
3660 * against all reg accesses from here on out.
3662 return0;
3664 static int
3665 maestro_resume(struct ess_card *card)
3667 unsigned long flags;
3668 int i;
3670 save_flags(flags);
3671 cli();/* over-kill */
3673 card->in_suspend =0;
3675 M_printk("maestro: resuming card at %p\n",card);
3677 /* restore all our config */
3678 maestro_config(card);
3679 /* need to restore the base pointers.. */
3680 if(card->dmapages)
3681 set_base_registers(&card->channels[0],card->dmapages);
3683 mixer_push_state(card);
3685 /* set each channels' apu control registers before
3686 * restoring audio
3688 for(i=0;i<NR_DSPS;i++) {
3689 struct ess_state *s = &card->channels[i];
3690 int chan,reg;
3692 if(s->dev_audio == -1)
3693 continue;
3695 for(chan =0; chan <6; chan++) {
3696 wave_set_register(s,s->apu[chan]<<3,s->apu_base[chan]);
3697 for(reg =1; reg < NR_APU_REGS ; reg++)
3698 apu_set_register(s,chan,reg,s->card->apu_map[s->apu[chan]][reg]);
3700 for(chan =0; chan <6; chan++)
3701 apu_set_register(s,chan,0,s->card->apu_map[s->apu[chan]][0] &0xFF0F);
3704 /* now we flip on the music */
3706 if( card->dsps_open <=0) {
3707 /* this card's idle */
3708 maestro_power(card,ACPI_D2);
3709 }else{
3710 /* ok, we're actually playing things on
3711 this card */
3712 maestro_power(card,ACPI_D0);
3713 start_bob(&card->channels[0]);
3714 for(i=0;i<NR_DSPS;i++) {
3715 struct ess_state *s = &card->channels[i];
3717 /* these use the apu_mode, and can handle
3718 spurious calls */
3719 start_dac(s);
3720 start_adc(s);
3724 restore_flags(flags);
3726 /* all right, we think things are ready,
3727 wake up people who were using the device
3728 when we suspended */
3729 wake_up(&(card->suspend_queue));
3731 return0;
3735 maestro_pm_callback(struct pm_dev *dev, pm_request_t rqst,void*data)
3737 struct ess_card *card = (struct ess_card*) dev->data;
3739 if( ! card )goto out;
3741 M_printk("maestro: pm event 0x%x received for card %p\n", rqst, card);
3743 switch(rqst) {
3744 case PM_SUSPEND:
3745 maestro_suspend(card);
3746 break;
3747 case PM_RESUME:
3748 maestro_resume(card);
3749 break;
3751 * we'd also like to find out about
3752 * power level changes because some biosen
3753 * do mean things to the maestro when they
3754 * change their power state.
3757 out:
3758 return0;
3761 module_init(init_maestro);
close