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