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