1d27cfc0687148d33edb0feac3801b65a26531af
[dragonfly.git] / sys / dev / sound / isa / ess.c
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * Copyright 1997,1998 Luigi Rizzo.
4  *
5  * Derived from files in the Voxware 3.5 distribution,
6  * Copyright by Hannu Savolainen 1994, under the same copyright
7  * conditions.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/dev/sound/isa/ess.c,v 1.34.2.2 2006/01/19 01:17:00 ariff Exp $
32  */
33
34 #include <dev/sound/pcm/sound.h>
35
36 #include  <dev/sound/isa/sb.h>
37 #include  <dev/sound/chip.h>
38
39 #include <bus/isa/isavar.h>
40
41 #include "mixer_if.h"
42
43 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/ess.c,v 1.7 2007/01/04 21:47:02 corecode Exp $");
44
45 #define ESS_BUFFSIZE (4096)
46 #define ABS(x) (((x) < 0)? -(x) : (x))
47
48 /* audio2 never generates irqs and sounds very noisy */
49 #undef ESS18XX_DUPLEX
50
51 /* more accurate clocks and split audio1/audio2 rates */
52 #define ESS18XX_NEWSPEED
53
54 static u_int32_t ess_pfmt[] = {
55         AFMT_U8,
56         AFMT_STEREO | AFMT_U8,
57         AFMT_S8,
58         AFMT_STEREO | AFMT_S8,
59         AFMT_S16_LE,
60         AFMT_STEREO | AFMT_S16_LE,
61         AFMT_U16_LE,
62         AFMT_STEREO | AFMT_U16_LE,
63         0
64 };
65
66 static struct pcmchan_caps ess_playcaps = {6000, 48000, ess_pfmt, 0};
67
68 static u_int32_t ess_rfmt[] = {
69         AFMT_U8,
70         AFMT_STEREO | AFMT_U8,
71         AFMT_S8,
72         AFMT_STEREO | AFMT_S8,
73         AFMT_S16_LE,
74         AFMT_STEREO | AFMT_S16_LE,
75         AFMT_U16_LE,
76         AFMT_STEREO | AFMT_U16_LE,
77         0
78 };
79
80 static struct pcmchan_caps ess_reccaps = {6000, 48000, ess_rfmt, 0};
81
82 struct ess_info;
83
84 struct ess_chinfo {
85         struct ess_info *parent;
86         struct pcm_channel *channel;
87         struct snd_dbuf *buffer;
88         int dir, hwch, stopping, run;
89         u_int32_t fmt, spd, blksz;
90 };
91
92 struct ess_info {
93         device_t parent_dev;
94         struct resource *io_base;       /* I/O address for the board */
95         struct resource *irq;
96         struct resource *drq1;
97         struct resource *drq2;
98         void *ih;
99         bus_dma_tag_t parent_dmat;
100
101         unsigned int bufsize;
102         int type, duplex:1, newspeed:1;
103         u_long bd_flags;       /* board-specific flags */
104         struct ess_chinfo pch, rch;
105 };
106
107 #if 0
108 static int ess_rd(struct ess_info *sc, int reg);
109 static void ess_wr(struct ess_info *sc, int reg, u_int8_t val);
110 static int ess_dspready(struct ess_info *sc);
111 static int ess_cmd(struct ess_info *sc, u_char val);
112 static int ess_cmd1(struct ess_info *sc, u_char cmd, int val);
113 static int ess_get_byte(struct ess_info *sc);
114 static void ess_setmixer(struct ess_info *sc, u_int port, u_int value);
115 static int ess_getmixer(struct ess_info *sc, u_int port);
116 static int ess_reset_dsp(struct ess_info *sc);
117
118 static int ess_write(struct ess_info *sc, u_char reg, int val);
119 static int ess_read(struct ess_info *sc, u_char reg);
120
121 static void ess_intr(void *arg);
122 static int ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len);
123 static int ess_start(struct ess_chinfo *ch);
124 static int ess_stop(struct ess_chinfo *ch);
125 #endif
126
127 /*
128  * Common code for the midi and pcm functions
129  *
130  * ess_cmd write a single byte to the CMD port.
131  * ess_cmd1 write a CMD + 1 byte arg
132  * ess_cmd2 write a CMD + 2 byte arg
133  * ess_get_byte returns a single byte from the DSP data port
134  *
135  * ess_write is actually ess_cmd1
136  * ess_read access ext. regs via ess_cmd(0xc0, reg) followed by ess_get_byte
137  */
138
139 static void
140 ess_lock(struct ess_info *sc) {
141
142         sbc_lock(device_get_softc(sc->parent_dev));
143 }
144
145 static void
146 ess_unlock(struct ess_info *sc) {
147
148         sbc_unlock(device_get_softc(sc->parent_dev));
149 }
150
151 static int
152 port_rd(struct resource *port, int off)
153 {
154         return bus_space_read_1(rman_get_bustag(port),
155                                 rman_get_bushandle(port),
156                                 off);
157 }
158
159 static void
160 port_wr(struct resource *port, int off, u_int8_t data)
161 {
162         bus_space_write_1(rman_get_bustag(port),
163                           rman_get_bushandle(port),
164                           off, data);
165 }
166
167 static int
168 ess_rd(struct ess_info *sc, int reg)
169 {
170         return port_rd(sc->io_base, reg);
171 }
172
173 static void
174 ess_wr(struct ess_info *sc, int reg, u_int8_t val)
175 {
176         port_wr(sc->io_base, reg, val);
177 }
178
179 static int
180 ess_dspready(struct ess_info *sc)
181 {
182         return ((ess_rd(sc, SBDSP_STATUS) & 0x80) == 0);
183 }
184
185 static int
186 ess_dspwr(struct ess_info *sc, u_char val)
187 {
188         int  i;
189
190         for (i = 0; i < 1000; i++) {
191                 if (ess_dspready(sc)) {
192                         ess_wr(sc, SBDSP_CMD, val);
193                         return 1;
194                 }
195                 if (i > 10) DELAY((i > 100)? 1000 : 10);
196         }
197         kprintf("ess_dspwr(0x%02x) timed out.\n", val);
198         return 0;
199 }
200
201 static int
202 ess_cmd(struct ess_info *sc, u_char val)
203 {
204 #if 0
205         kprintf("ess_cmd: %x\n", val);
206 #endif
207         return ess_dspwr(sc, val);
208 }
209
210 static int
211 ess_cmd1(struct ess_info *sc, u_char cmd, int val)
212 {
213 #if 0
214         kprintf("ess_cmd1: %x, %x\n", cmd, val);
215 #endif
216         if (ess_dspwr(sc, cmd)) {
217                 return ess_dspwr(sc, val & 0xff);
218         } else return 0;
219 }
220
221 static void
222 ess_setmixer(struct ess_info *sc, u_int port, u_int value)
223 {
224         DEB(kprintf("ess_setmixer: reg=%x, val=%x\n", port, value);)
225         ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
226         DELAY(10);
227         ess_wr(sc, SB_MIX_DATA, (u_char) (value & 0xff));
228         DELAY(10);
229 }
230
231 static int
232 ess_getmixer(struct ess_info *sc, u_int port)
233 {
234         int val;
235         ess_wr(sc, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
236         DELAY(10);
237         val = ess_rd(sc, SB_MIX_DATA);
238         DELAY(10);
239
240         return val;
241 }
242
243 static int
244 ess_get_byte(struct ess_info *sc)
245 {
246         int i;
247
248         for (i = 1000; i > 0; i--) {
249                 if (ess_rd(sc, DSP_DATA_AVAIL) & 0x80)
250                         return ess_rd(sc, DSP_READ);
251                 else
252                         DELAY(20);
253         }
254         return -1;
255 }
256
257 static int
258 ess_write(struct ess_info *sc, u_char reg, int val)
259 {
260         return ess_cmd1(sc, reg, val);
261 }
262
263 static int
264 ess_read(struct ess_info *sc, u_char reg)
265 {
266         return (ess_cmd(sc, 0xc0) && ess_cmd(sc, reg))? ess_get_byte(sc) : -1;
267 }
268
269 static int
270 ess_reset_dsp(struct ess_info *sc)
271 {
272         ess_wr(sc, SBDSP_RST, 3);
273         DELAY(100);
274         ess_wr(sc, SBDSP_RST, 0);
275         if (ess_get_byte(sc) != 0xAA) {
276                 DEB(kprintf("ess_reset_dsp 0x%lx failed\n",
277                            rman_get_start(sc->io_base)));
278                 return ENXIO;   /* Sorry */
279         }
280         ess_cmd(sc, 0xc6);
281         return 0;
282 }
283
284 static void
285 ess_release_resources(struct ess_info *sc, device_t dev)
286 {
287         if (sc->irq) {
288                 if (sc->ih)
289                         bus_teardown_intr(dev, sc->irq, sc->ih);
290                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
291                 sc->irq = 0;
292         }
293         if (sc->drq1) {
294                 isa_dma_release(rman_get_start(sc->drq1));
295                 bus_release_resource(dev, SYS_RES_DRQ, 0, sc->drq1);
296                 sc->drq1 = 0;
297         }
298         if (sc->drq2) {
299                 isa_dma_release(rman_get_start(sc->drq2));
300                 bus_release_resource(dev, SYS_RES_DRQ, 1, sc->drq2);
301                 sc->drq2 = 0;
302         }
303         if (sc->io_base) {
304                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->io_base);
305                 sc->io_base = 0;
306         }
307         if (sc->parent_dmat) {
308                 bus_dma_tag_destroy(sc->parent_dmat);
309                 sc->parent_dmat = 0;
310         }
311         kfree(sc, M_DEVBUF);
312 }
313
314 static int
315 ess_alloc_resources(struct ess_info *sc, device_t dev)
316 {
317         int rid;
318
319         rid = 0;
320         if (!sc->io_base)
321                 sc->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
322                                                      &rid, RF_ACTIVE);
323         rid = 0;
324         if (!sc->irq)
325                 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
326                                                  &rid, RF_ACTIVE);
327         rid = 0;
328         if (!sc->drq1)
329                 sc->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
330                                                   &rid, RF_ACTIVE);
331         rid = 1;
332         if (!sc->drq2)
333                 sc->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ,
334                                                   &rid, RF_ACTIVE);
335
336         if (sc->io_base && sc->drq1 && sc->irq) {
337                 isa_dma_acquire(rman_get_start(sc->drq1));
338                 isa_dmainit(rman_get_start(sc->drq1), sc->bufsize);
339
340                 if (sc->drq2) {
341                         isa_dma_acquire(rman_get_start(sc->drq2));
342                         isa_dmainit(rman_get_start(sc->drq2), sc->bufsize);
343                 }
344
345                 return 0;
346         } else return ENXIO;
347 }
348
349 static void
350 ess_intr(void *arg)
351 {
352         struct ess_info *sc = (struct ess_info *)arg;
353         int src, pirq, rirq;
354
355         ess_lock(sc);
356         src = 0;
357         if (ess_getmixer(sc, 0x7a) & 0x80)
358                 src |= 2;
359         if (ess_rd(sc, 0x0c) & 0x01)
360                 src |= 1;
361
362         pirq = (src & sc->pch.hwch)? 1 : 0;
363         rirq = (src & sc->rch.hwch)? 1 : 0;
364
365         if (pirq) {
366                 if (sc->pch.run) {
367                         ess_unlock(sc);
368                         chn_intr(sc->pch.channel);
369                         ess_lock(sc);
370                 }
371                 if (sc->pch.stopping) {
372                         sc->pch.run = 0;
373                         sndbuf_dma(sc->pch.buffer, PCMTRIG_STOP);
374                         sc->pch.stopping = 0;
375                         if (sc->pch.hwch == 1)
376                                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
377                         else
378                                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x03);
379                 }
380         }
381
382         if (rirq) {
383                 if (sc->rch.run) {
384                         ess_unlock(sc);
385                         chn_intr(sc->rch.channel);
386                         ess_lock(sc);
387                 }
388                 if (sc->rch.stopping) {
389                         sc->rch.run = 0;
390                         sndbuf_dma(sc->rch.buffer, PCMTRIG_STOP);
391                         sc->rch.stopping = 0;
392                         /* XXX: will this stop audio2? */
393                         ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x01);
394                 }
395         }
396
397         if (src & 2)
398                 ess_setmixer(sc, 0x7a, ess_getmixer(sc, 0x7a) & ~0x80);
399         if (src & 1)
400                 ess_rd(sc, DSP_DATA_AVAIL);
401         ess_unlock(sc);
402 }
403
404 /* utility functions for ESS */
405 static u_int8_t
406 ess_calcspeed8(int *spd)
407 {
408         int speed = *spd;
409         u_int32_t t;
410
411         if (speed > 22000) {
412                 t = (795500 + speed / 2) / speed;
413                 speed = (795500 + t / 2) / t;
414                 t = (256 - t) | 0x80;
415         } else {
416                 t = (397700 + speed / 2) / speed;
417                 speed = (397700 + t / 2) / t;
418                 t = 128 - t;
419         }
420         *spd = speed;
421         return t & 0x000000ff;
422 }
423
424 static u_int8_t
425 ess_calcspeed9(int *spd)
426 {
427         int speed, s0, s1, use0;
428         u_int8_t t0, t1;
429
430         /* rate = source / (256 - divisor) */
431         /* divisor = 256 - (source / rate) */
432         speed = *spd;
433         t0 = 128 - (793800 / speed);
434         s0 = 793800 / (128 - t0);
435
436         t1 = 128 - (768000 / speed);
437         s1 = 768000 / (128 - t1);
438         t1 |= 0x80;
439
440         use0 = (ABS(speed - s0) < ABS(speed - s1))? 1 : 0;
441
442         *spd = use0? s0 : s1;
443         return use0? t0 : t1;
444 }
445
446 static u_int8_t
447 ess_calcfilter(int spd)
448 {
449         int cutoff;
450
451         /* cutoff = 7160000 / (256 - divisor) */
452         /* divisor = 256 - (7160000 / cutoff) */
453         cutoff = (spd * 9 * 82) / 20;
454         return (256 - (7160000 / cutoff));
455 }
456
457 static int
458 ess_setupch(struct ess_info *sc, int ch, int dir, int spd, u_int32_t fmt, int len)
459 {
460         int play = (dir == PCMDIR_PLAY)? 1 : 0;
461         int b16 = (fmt & AFMT_16BIT)? 1 : 0;
462         int stereo = (fmt & AFMT_STEREO)? 1 : 0;
463         int unsign = (fmt == AFMT_U8 || fmt == AFMT_U16_LE)? 1 : 0;
464         u_int8_t spdval, fmtval;
465
466
467         spdval = (sc->newspeed)? ess_calcspeed9(&spd) : ess_calcspeed8(&spd);
468         len = -len;
469
470         if (ch == 1) {
471                 KASSERT((dir == PCMDIR_PLAY) || (dir == PCMDIR_REC), ("ess_setupch: dir1 bad"));
472                 /* transfer length low */
473                 ess_write(sc, 0xa4, len & 0x00ff);
474                 /* transfer length high */
475                 ess_write(sc, 0xa5, (len & 0xff00) >> 8);
476                 /* autoinit, dma dir */
477                 ess_write(sc, 0xb8, 0x04 | (play? 0x00 : 0x0a));
478                 /* mono/stereo */
479                 ess_write(sc, 0xa8, (ess_read(sc, 0xa8) & ~0x03) | (stereo? 0x01 : 0x02));
480                 /* demand mode, 4 bytes/xfer */
481                 ess_write(sc, 0xb9, 0x02);
482                 /* sample rate */
483                 ess_write(sc, 0xa1, spdval);
484                 /* filter cutoff */
485                 ess_write(sc, 0xa2, ess_calcfilter(spd));
486                 /* setup dac/adc */
487                 if (play)
488                         ess_write(sc, 0xb6, unsign? 0x80 : 0x00);
489                 /* mono, b16: signed, load signal */
490                 ess_write(sc, 0xb7, 0x51 | (unsign? 0x00 : 0x20));
491                 /* setup fifo */
492                 ess_write(sc, 0xb7, 0x90 | (unsign? 0x00 : 0x20) |
493                                            (b16? 0x04 : 0x00) |
494                                            (stereo? 0x08 : 0x40));
495                 /* irq control */
496                 ess_write(sc, 0xb1, (ess_read(sc, 0xb1) & 0x0f) | 0x50);
497                 /* drq control */
498                 ess_write(sc, 0xb2, (ess_read(sc, 0xb2) & 0x0f) | 0x50);
499         } else if (ch == 2) {
500                 KASSERT(dir == PCMDIR_PLAY, ("ess_setupch: dir2 bad"));
501                 /* transfer length low */
502                 ess_setmixer(sc, 0x74, len & 0x00ff);
503                 /* transfer length high */
504                 ess_setmixer(sc, 0x76, (len & 0xff00) >> 8);
505                 /* autoinit, 4 bytes/req */
506                 ess_setmixer(sc, 0x78, 0x90);
507                 fmtval = b16 | (stereo << 1) | (unsign << 2);
508                 /* enable irq, set format */
509                 ess_setmixer(sc, 0x7a, 0x40 | fmtval);
510                 if (sc->newspeed) {
511                         /* sample rate */
512                         ess_setmixer(sc, 0x70, spdval);
513                         /* filter cutoff */
514                         ess_setmixer(sc, 0x72, ess_calcfilter(spd));
515                 }
516         }
517
518         return 0;
519 }
520 static int
521 ess_start(struct ess_chinfo *ch)
522 {
523         struct ess_info *sc = ch->parent;
524         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
525
526         ess_lock(sc);
527         ess_setupch(sc, ch->hwch, ch->dir, ch->spd, ch->fmt, ch->blksz);
528         ch->stopping = 0;
529         if (ch->hwch == 1)
530                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) | 0x01);
531         else
532                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) | 0x03);
533         if (play)
534                 ess_cmd(sc, DSP_CMD_SPKON);
535         ess_unlock(sc);
536         return 0;
537 }
538
539 static int
540 ess_stop(struct ess_chinfo *ch)
541 {
542         struct ess_info *sc = ch->parent;
543         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
544
545         ess_lock(sc);
546         ch->stopping = 1;
547         if (ch->hwch == 1)
548                 ess_write(sc, 0xb8, ess_read(sc, 0xb8) & ~0x04);
549         else
550                 ess_setmixer(sc, 0x78, ess_getmixer(sc, 0x78) & ~0x10);
551         if (play)
552                 ess_cmd(sc, DSP_CMD_SPKOFF);
553         ess_unlock(sc);
554         return 0;
555 }
556
557 /* -------------------------------------------------------------------- */
558 /* channel interface for ESS18xx */
559 static void *
560 esschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
561 {
562         struct ess_info *sc = devinfo;
563         struct ess_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
564
565         ch->parent = sc;
566         ch->channel = c;
567         ch->buffer = b;
568         if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsize) != 0)
569                 return NULL;
570         ch->dir = dir;
571         ch->hwch = 1;
572         if ((dir == PCMDIR_PLAY) && (sc->duplex))
573                 ch->hwch = 2;
574         sndbuf_dmasetup(ch->buffer, (ch->hwch == 1)? sc->drq1 : sc->drq2);
575         return ch;
576 }
577
578 static int
579 esschan_setformat(kobj_t obj, void *data, u_int32_t format)
580 {
581         struct ess_chinfo *ch = data;
582
583         ch->fmt = format;
584         return 0;
585 }
586
587 static int
588 esschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
589 {
590         struct ess_chinfo *ch = data;
591         struct ess_info *sc = ch->parent;
592
593         ch->spd = speed;
594         if (sc->newspeed)
595                 ess_calcspeed9(&ch->spd);
596         else
597                 ess_calcspeed8(&ch->spd);
598         return ch->spd;
599 }
600
601 static int
602 esschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
603 {
604         struct ess_chinfo *ch = data;
605
606         ch->blksz = blocksize;
607         return ch->blksz;
608 }
609
610 static int
611 esschan_trigger(kobj_t obj, void *data, int go)
612 {
613         struct ess_chinfo *ch = data;
614
615         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
616                 return 0;
617
618         switch (go) {
619         case PCMTRIG_START:
620                 ch->run = 1;
621                 sndbuf_dma(ch->buffer, go);
622                 ess_start(ch);
623                 break;
624
625         case PCMTRIG_STOP:
626         case PCMTRIG_ABORT:
627         default:
628                 ess_stop(ch);
629                 break;
630         }
631         return 0;
632 }
633
634 static int
635 esschan_getptr(kobj_t obj, void *data)
636 {
637         struct ess_chinfo *ch = data;
638
639         return sndbuf_dmaptr(ch->buffer);
640 }
641
642 static struct pcmchan_caps *
643 esschan_getcaps(kobj_t obj, void *data)
644 {
645         struct ess_chinfo *ch = data;
646
647         return (ch->dir == PCMDIR_PLAY)? &ess_playcaps : &ess_reccaps;
648 }
649
650 static kobj_method_t esschan_methods[] = {
651         KOBJMETHOD(channel_init,                esschan_init),
652         KOBJMETHOD(channel_setformat,           esschan_setformat),
653         KOBJMETHOD(channel_setspeed,            esschan_setspeed),
654         KOBJMETHOD(channel_setblocksize,        esschan_setblocksize),
655         KOBJMETHOD(channel_trigger,             esschan_trigger),
656         KOBJMETHOD(channel_getptr,              esschan_getptr),
657         KOBJMETHOD(channel_getcaps,             esschan_getcaps),
658         { 0, 0 }
659 };
660 CHANNEL_DECLARE(esschan);
661
662 /************************************************************/
663
664 static int
665 essmix_init(struct snd_mixer *m)
666 {
667         struct ess_info *sc = mix_getdevinfo(m);
668
669         mix_setrecdevs(m, SOUND_MASK_CD | SOUND_MASK_MIC | SOUND_MASK_LINE |
670                           SOUND_MASK_IMIX);
671
672         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
673                        SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME |
674                        SOUND_MASK_LINE1 | SOUND_MASK_SPEAKER);
675
676         ess_setmixer(sc, 0, 0); /* reset */
677
678         return 0;
679 }
680
681 static int
682 essmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
683 {
684         struct ess_info *sc = mix_getdevinfo(m);
685         int preg = 0, rreg = 0, l, r;
686
687         l = (left * 15) / 100;
688         r = (right * 15) / 100;
689         switch (dev) {
690         case SOUND_MIXER_SYNTH:
691                 preg = 0x36;
692                 rreg = 0x6b;
693                 break;
694
695         case SOUND_MIXER_PCM:
696                 preg = 0x14;
697                 rreg = 0x7c;
698                 break;
699
700         case SOUND_MIXER_LINE:
701                 preg = 0x3e;
702                 rreg = 0x6e;
703                 break;
704
705         case SOUND_MIXER_MIC:
706                 preg = 0x1a;
707                 rreg = 0x68;
708                 break;
709
710         case SOUND_MIXER_LINE1:
711                 preg = 0x3a;
712                 rreg = 0x6c;
713                 break;
714
715         case SOUND_MIXER_CD:
716                 preg = 0x38;
717                 rreg = 0x6a;
718                 break;
719
720         case SOUND_MIXER_SPEAKER:
721                 preg = 0x3c;
722                 break;
723
724         case SOUND_MIXER_VOLUME:
725                 l = left? (left * 63) / 100 : 64;
726                 r = right? (right * 63) / 100 : 64;
727                 ess_setmixer(sc, 0x60, l);
728                 ess_setmixer(sc, 0x62, r);
729                 left = (l == 64)? 0 : (l * 100) / 63;
730                 right = (r == 64)? 0 : (r * 100) / 63;
731                 return left | (right << 8);
732         }
733
734         if (preg)
735                 ess_setmixer(sc, preg, (l << 4) | r);
736         if (rreg)
737                 ess_setmixer(sc, rreg, (l << 4) | r);
738
739         left = (l * 100) / 15;
740         right = (r * 100) / 15;
741
742         return left | (right << 8);
743 }
744
745 static int
746 essmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
747 {
748         struct ess_info *sc = mix_getdevinfo(m);
749         u_char recdev;
750
751         switch (src) {
752         case SOUND_MASK_CD:
753                 recdev = 0x02;
754                 break;
755
756         case SOUND_MASK_LINE:
757                 recdev = 0x06;
758                 break;
759
760         case SOUND_MASK_IMIX:
761                 recdev = 0x05;
762                 break;
763
764         case SOUND_MASK_MIC:
765         default:
766                 recdev = 0x00;
767                 src = SOUND_MASK_MIC;
768                 break;
769         }
770
771         ess_setmixer(sc, 0x1c, recdev);
772
773         return src;
774 }
775
776 static kobj_method_t essmixer_methods[] = {
777         KOBJMETHOD(mixer_init,          essmix_init),
778         KOBJMETHOD(mixer_set,           essmix_set),
779         KOBJMETHOD(mixer_setrecsrc,     essmix_setrecsrc),
780         { 0, 0 }
781 };
782 MIXER_DECLARE(essmixer);
783
784 /************************************************************/
785
786 static int
787 ess_probe(device_t dev)
788 {
789         uintptr_t func, ver, r, f;
790
791         /* The parent device has already been probed. */
792         r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
793         if (func != SCF_PCM)
794                 return (ENXIO);
795
796         r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
797         f = (ver & 0xffff0000) >> 16;
798         if (!(f & BD_F_ESS))
799                 return (ENXIO);
800
801         device_set_desc(dev, "ESS 18xx DSP");
802
803         return 0;
804 }
805
806 static int
807 ess_attach(device_t dev)
808 {
809         struct ess_info *sc;
810         char status[SND_STATUSLEN], buf[64];
811         int ver;
812
813         sc = kmalloc(sizeof *sc, M_DEVBUF, M_WAITOK | M_ZERO);
814         sc->parent_dev = device_get_parent(dev);
815         sc->bufsize = pcm_getbuffersize(dev, 4096, ESS_BUFFSIZE, 65536);
816         if (ess_alloc_resources(sc, dev))
817                 goto no;
818         if (ess_reset_dsp(sc))
819                 goto no;
820         if (mixer_init(dev, &essmixer_class, sc))
821                 goto no;
822
823         sc->duplex = 0;
824         sc->newspeed = 0;
825         ver = (ess_getmixer(sc, 0x40) << 8) | ess_rd(sc, SB_MIX_DATA);
826         ksnprintf(buf, sizeof buf, "ESS %x DSP", ver);
827         device_set_desc_copy(dev, buf);
828         if (bootverbose)
829                 device_printf(dev, "ESS%x detected", ver);
830
831         switch (ver) {
832         case 0x1869:
833         case 0x1879:
834 #ifdef ESS18XX_DUPLEX
835                 sc->duplex = sc->drq2? 1 : 0;
836 #endif
837 #ifdef ESS18XX_NEWSPEED
838                 sc->newspeed = 1;
839 #endif
840                 break;
841         }
842         if (bootverbose)
843                 kprintf("%s%s\n", sc->duplex? ", duplex" : "",
844                                  sc->newspeed? ", newspeed" : "");
845
846         if (sc->newspeed)
847                 ess_setmixer(sc, 0x71, 0x22);
848
849         snd_setup_intr(dev, sc->irq, 0, ess_intr, sc, &sc->ih);
850         if (!sc->duplex)
851                 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
852
853         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
854                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
855                         /*highaddr*/BUS_SPACE_MAXADDR,
856                         /*filter*/NULL, /*filterarg*/NULL,
857                         /*maxsize*/sc->bufsize, /*nsegments*/1,
858                         /*maxsegz*/0x3ffff,
859                         /*flags*/0,
860                         &sc->parent_dmat) != 0) {
861                 device_printf(dev, "unable to create dma tag\n");
862                 goto no;
863         }
864
865         if (sc->drq2)
866                 ksnprintf(buf, SND_STATUSLEN, ":%ld", rman_get_start(sc->drq2));
867         else
868                 buf[0] = '\0';
869
870         ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s",
871                 rman_get_start(sc->io_base), rman_get_start(sc->irq),
872                 rman_get_start(sc->drq1), buf, sc->bufsize,
873                 PCM_KLDSTRING(snd_ess));
874
875         if (pcm_register(dev, sc, 1, 1))
876                 goto no;
877         pcm_addchan(dev, PCMDIR_REC, &esschan_class, sc);
878         pcm_addchan(dev, PCMDIR_PLAY, &esschan_class, sc);
879         pcm_setstatus(dev, status);
880
881         return 0;
882
883 no:
884         ess_release_resources(sc, dev);
885         return ENXIO;
886 }
887
888 static int
889 ess_detach(device_t dev)
890 {
891         int r;
892         struct ess_info *sc;
893
894         r = pcm_unregister(dev);
895         if (r)
896                 return r;
897
898         sc = pcm_getdevinfo(dev);
899         ess_release_resources(sc, dev);
900         return 0;
901 }
902
903 static int
904 ess_resume(device_t dev)
905 {
906         struct ess_info *sc;
907
908         sc = pcm_getdevinfo(dev);
909
910         if (ess_reset_dsp(sc)) {
911                 device_printf(dev, "unable to reset DSP at resume\n");
912                 return ENXIO;
913         }
914
915         if (mixer_reinit(dev)) {
916                 device_printf(dev, "unable to reinitialize mixer at resume\n");
917                 return ENXIO;
918         }
919
920         return 0;
921 }
922
923 static device_method_t ess_methods[] = {
924         /* Device interface */
925         DEVMETHOD(device_probe,         ess_probe),
926         DEVMETHOD(device_attach,        ess_attach),
927         DEVMETHOD(device_detach,        ess_detach),
928         DEVMETHOD(device_resume,        ess_resume),
929
930         { 0, 0 }
931 };
932
933 static driver_t ess_driver = {
934         "pcm",
935         ess_methods,
936         PCM_SOFTC_SIZE,
937 };
938
939 DRIVER_MODULE(snd_ess, sbc, ess_driver, pcm_devclass, NULL, NULL);
940 MODULE_DEPEND(snd_ess, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
941 MODULE_DEPEND(snd_ess, snd_sbc, 1, 1, 1);
942 MODULE_VERSION(snd_ess, 1);
943
944 /************************************************************/
945
946 static devclass_t esscontrol_devclass;
947
948 static struct isa_pnp_id essc_ids[] = {
949         {0x06007316, "ESS Control"},
950         {0}
951 };
952
953 static int
954 esscontrol_probe(device_t dev)
955 {
956         int i;
957
958         i = ISA_PNP_PROBE(device_get_parent(dev), dev, essc_ids);
959         if (i == 0)
960                 device_quiet(dev);
961         return i;
962 }
963
964 static int
965 esscontrol_attach(device_t dev)
966 {
967 #ifdef notyet
968         struct resource *io;
969         int rid, i, x;
970
971         rid = 0;
972         io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
973         x = 0;
974         for (i = 0; i < 0x100; i++) {
975                 port_wr(io, 0, i);
976                 x = port_rd(io, 1);
977                 if ((i & 0x0f) == 0)
978                         kprintf("%3.3x: ", i);
979                 kprintf("%2.2x ", x);
980                 if ((i & 0x0f) == 0x0f)
981                         kprintf("\n");
982         }
983         bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
984         io = NULL;
985 #endif
986
987         return 0;
988 }
989
990 static int
991 esscontrol_detach(device_t dev)
992 {
993         return 0;
994 }
995
996 static device_method_t esscontrol_methods[] = {
997         /* Device interface */
998         DEVMETHOD(device_probe,         esscontrol_probe),
999         DEVMETHOD(device_attach,        esscontrol_attach),
1000         DEVMETHOD(device_detach,        esscontrol_detach),
1001
1002         { 0, 0 }
1003 };
1004
1005 static driver_t esscontrol_driver = {
1006         "esscontrol",
1007         esscontrol_methods,
1008         1,
1009 };
1010
1011 DRIVER_MODULE(esscontrol, isa, esscontrol_driver, esscontrol_devclass, NULL, NULL);
1012 DRIVER_MODULE(esscontrol, acpi, esscontrol_driver, esscontrol_devclass, NULL, NULL);