Merge branch 'vendor/GMP' into gcc441
[dragonfly.git] / sys / dev / sound / isa / sb8.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/sb8.c,v 1.79.2.1 2005/12/30 19:55:53 netchild Exp $
32  * $DragonFly: src/sys/dev/sound/isa/sb8.c,v 1.7 2007/01/04 21:47:02 corecode 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 <bus/isa/isavar.h>
41
42 #include "mixer_if.h"
43
44 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/sb8.c,v 1.7 2007/01/04 21:47:02 corecode Exp $");
45
46 #define SB_DEFAULT_BUFSZ        4096
47
48 static u_int32_t sb_fmt[] = {
49         AFMT_U8,
50         0
51 };
52 static struct pcmchan_caps sb200_playcaps = {4000, 23000, sb_fmt, 0};
53 static struct pcmchan_caps sb200_reccaps = {4000, 13000, sb_fmt, 0};
54 static struct pcmchan_caps sb201_playcaps = {4000, 44100, sb_fmt, 0};
55 static struct pcmchan_caps sb201_reccaps = {4000, 15000, sb_fmt, 0};
56
57 static u_int32_t sbpro_fmt[] = {
58         AFMT_U8,
59         AFMT_STEREO | AFMT_U8,
60         0
61 };
62 static struct pcmchan_caps sbpro_playcaps = {4000, 44100, sbpro_fmt, 0};
63 static struct pcmchan_caps sbpro_reccaps = {4000, 44100, sbpro_fmt, 0};
64
65 struct sb_info;
66
67 struct sb_chinfo {
68         struct sb_info *parent;
69         struct pcm_channel *channel;
70         struct snd_dbuf *buffer;
71         int dir;
72         u_int32_t fmt, spd, blksz;
73 };
74
75 struct sb_info {
76         device_t parent_dev;
77         struct resource *io_base;       /* I/O address for the board */
78         struct resource *irq;
79         struct resource *drq;
80         void *ih;
81         bus_dma_tag_t parent_dmat;
82
83         unsigned int bufsize;
84         int bd_id;
85         u_long bd_flags;       /* board-specific flags */
86         struct sb_chinfo pch, rch;
87 };
88
89 static int sb_rd(struct sb_info *sb, int reg);
90 static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
91 static int sb_dspready(struct sb_info *sb);
92 static int sb_cmd(struct sb_info *sb, u_char val);
93 static int sb_cmd1(struct sb_info *sb, u_char cmd, int val);
94 static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
95 static u_int sb_get_byte(struct sb_info *sb);
96 static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
97 static int sb_getmixer(struct sb_info *sb, u_int port);
98 static int sb_reset_dsp(struct sb_info *sb);
99
100 static void sb_intr(void *arg);
101 static int sb_speed(struct sb_chinfo *ch);
102 static int sb_start(struct sb_chinfo *ch);
103 static int sb_stop(struct sb_chinfo *ch);
104
105 /*
106  * Common code for the midi and pcm functions
107  *
108  * sb_cmd write a single byte to the CMD port.
109  * sb_cmd1 write a CMD + 1 byte arg
110  * sb_cmd2 write a CMD + 2 byte arg
111  * sb_get_byte returns a single byte from the DSP data port
112  */
113
114 static void
115 sb_lock(struct sb_info *sb) {
116
117         sbc_lock(device_get_softc(sb->parent_dev));
118 }
119
120 static void
121 sb_unlock(struct sb_info *sb) {
122
123         sbc_unlock(device_get_softc(sb->parent_dev));
124 }
125
126 static int
127 port_rd(struct resource *port, int off)
128 {
129         return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off);
130 }
131
132 static void
133 port_wr(struct resource *port, int off, u_int8_t data)
134 {
135         bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data);
136 }
137
138 static int
139 sb_rd(struct sb_info *sb, int reg)
140 {
141         return port_rd(sb->io_base, reg);
142 }
143
144 static void
145 sb_wr(struct sb_info *sb, int reg, u_int8_t val)
146 {
147         port_wr(sb->io_base, reg, val);
148 }
149
150 static int
151 sb_dspready(struct sb_info *sb)
152 {
153         return ((sb_rd(sb, SBDSP_STATUS) & 0x80) == 0);
154 }
155
156 static int
157 sb_dspwr(struct sb_info *sb, u_char val)
158 {
159         int  i;
160
161         for (i = 0; i < 1000; i++) {
162                 if (sb_dspready(sb)) {
163                         sb_wr(sb, SBDSP_CMD, val);
164                         return 1;
165                 }
166                 if (i > 10) DELAY((i > 100)? 1000 : 10);
167         }
168         kprintf("sb_dspwr(0x%02x) timed out.\n", val);
169         return 0;
170 }
171
172 static int
173 sb_cmd(struct sb_info *sb, u_char val)
174 {
175 #if 0
176         kprintf("sb_cmd: %x\n", val);
177 #endif
178         return sb_dspwr(sb, val);
179 }
180
181 static int
182 sb_cmd1(struct sb_info *sb, u_char cmd, int val)
183 {
184 #if 0
185         kprintf("sb_cmd1: %x, %x\n", cmd, val);
186 #endif
187         if (sb_dspwr(sb, cmd)) {
188                 return sb_dspwr(sb, val & 0xff);
189         } else return 0;
190 }
191
192 static int
193 sb_cmd2(struct sb_info *sb, u_char cmd, int val)
194 {
195 #if 0
196         kprintf("sb_cmd2: %x, %x\n", cmd, val);
197 #endif
198         if (sb_dspwr(sb, cmd)) {
199                 return sb_dspwr(sb, val & 0xff) &&
200                        sb_dspwr(sb, (val >> 8) & 0xff);
201         } else return 0;
202 }
203
204 /*
205  * in the SB, there is a set of indirect "mixer" registers with
206  * address at offset 4, data at offset 5
207  *
208  * we don't need to interlock these, the mixer lock will suffice.
209  */
210 static void
211 sb_setmixer(struct sb_info *sb, u_int port, u_int value)
212 {
213         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
214         DELAY(10);
215         sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
216         DELAY(10);
217 }
218
219 static int
220 sb_getmixer(struct sb_info *sb, u_int port)
221 {
222         int val;
223
224         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
225         DELAY(10);
226         val = sb_rd(sb, SB_MIX_DATA);
227         DELAY(10);
228
229         return val;
230 }
231
232 static u_int
233 sb_get_byte(struct sb_info *sb)
234 {
235         int i;
236
237         for (i = 1000; i > 0; i--) {
238                 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
239                         return sb_rd(sb, DSP_READ);
240                 else
241                         DELAY(20);
242         }
243         return 0xffff;
244 }
245
246 static int
247 sb_reset_dsp(struct sb_info *sb)
248 {
249         sb_wr(sb, SBDSP_RST, 3);
250         DELAY(100);
251         sb_wr(sb, SBDSP_RST, 0);
252         if (sb_get_byte(sb) != 0xAA) {
253                 DEB(kprintf("sb_reset_dsp 0x%lx failed\n",
254                            rman_get_start(sb->io_base)));
255                 return ENXIO;   /* Sorry */
256         }
257         return 0;
258 }
259
260 static void
261 sb_release_resources(struct sb_info *sb, device_t dev)
262 {
263         if (sb->irq) {
264                 if (sb->ih)
265                         bus_teardown_intr(dev, sb->irq, sb->ih);
266                 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
267                 sb->irq = 0;
268         }
269         if (sb->drq) {
270                 isa_dma_release(rman_get_start(sb->drq));
271                 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq);
272                 sb->drq = 0;
273         }
274         if (sb->io_base) {
275                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
276                 sb->io_base = 0;
277         }
278         if (sb->parent_dmat) {
279                 bus_dma_tag_destroy(sb->parent_dmat);
280                 sb->parent_dmat = 0;
281         }
282         kfree(sb, M_DEVBUF);
283 }
284
285 static int
286 sb_alloc_resources(struct sb_info *sb, device_t dev)
287 {
288         int rid;
289
290         rid = 0;
291         if (!sb->io_base)
292                 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
293                         &rid, RF_ACTIVE);
294         rid = 0;
295         if (!sb->irq)
296                 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
297                         &rid, RF_ACTIVE);
298         rid = 0;
299         if (!sb->drq)
300                 sb->drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
301                         &rid, RF_ACTIVE);
302
303         if (sb->io_base && sb->drq && sb->irq) {
304                 isa_dma_acquire(rman_get_start(sb->drq));
305                 isa_dmainit(rman_get_start(sb->drq), sb->bufsize);
306
307                 return 0;
308         } else return ENXIO;
309 }
310
311 /************************************************************/
312
313 static int
314 sbpromix_init(struct snd_mixer *m)
315 {
316         struct sb_info *sb = mix_getdevinfo(m);
317
318         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
319                        SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME);
320
321         mix_setrecdevs(m, SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD);
322
323         sb_setmixer(sb, 0, 1); /* reset mixer */
324
325         return 0;
326 }
327
328 static int
329 sbpromix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
330 {
331         struct sb_info *sb = mix_getdevinfo(m);
332         int reg, max;
333         u_char val;
334
335         max = 7;
336         switch (dev) {
337         case SOUND_MIXER_PCM:
338                 reg = 0x04;
339                 break;
340
341         case SOUND_MIXER_MIC:
342                 reg = 0x0a;
343                 max = 3;
344                 break;
345
346         case SOUND_MIXER_VOLUME:
347                 reg = 0x22;
348                 break;
349
350         case SOUND_MIXER_SYNTH:
351                 reg = 0x26;
352                 break;
353
354         case SOUND_MIXER_CD:
355                 reg = 0x28;
356                 break;
357
358         case SOUND_MIXER_LINE:
359                 reg = 0x2e;
360                 break;
361
362         default:
363                 return -1;
364         }
365
366         left = (left * max) / 100;
367         right = (dev == SOUND_MIXER_MIC)? left : ((right * max) / 100);
368
369         val = (dev == SOUND_MIXER_MIC)? (left << 1) : (left << 5 | right << 1);
370         sb_setmixer(sb, reg, val);
371
372         left = (left * 100) / max;
373         right = (right * 100) / max;
374
375         return left | (right << 8);
376 }
377
378 static int
379 sbpromix_setrecsrc(struct snd_mixer *m, u_int32_t src)
380 {
381         struct sb_info *sb = mix_getdevinfo(m);
382         u_char recdev;
383
384         if      (src == SOUND_MASK_LINE)
385                 recdev = 0x06;
386         else if (src == SOUND_MASK_CD)
387                 recdev = 0x02;
388         else { /* default: mic */
389                 src = SOUND_MASK_MIC;
390                 recdev = 0;
391         }
392         sb_setmixer(sb, RECORD_SRC, recdev | (sb_getmixer(sb, RECORD_SRC) & ~0x07));
393
394         return src;
395 }
396
397 static kobj_method_t sbpromix_mixer_methods[] = {
398         KOBJMETHOD(mixer_init,          sbpromix_init),
399         KOBJMETHOD(mixer_set,           sbpromix_set),
400         KOBJMETHOD(mixer_setrecsrc,     sbpromix_setrecsrc),
401         { 0, 0 }
402 };
403 MIXER_DECLARE(sbpromix_mixer);
404
405 /************************************************************/
406
407 static int
408 sbmix_init(struct snd_mixer *m)
409 {
410         struct sb_info *sb = mix_getdevinfo(m);
411
412         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_CD | SOUND_MASK_VOLUME);
413
414         mix_setrecdevs(m, 0);
415
416         sb_setmixer(sb, 0, 1); /* reset mixer */
417
418         return 0;
419 }
420
421 static int
422 sbmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
423 {
424         struct sb_info *sb = mix_getdevinfo(m);
425         int reg, max;
426
427         max = 7;
428         switch (dev) {
429         case SOUND_MIXER_VOLUME:
430                 reg = 0x2;
431                 break;
432
433         case SOUND_MIXER_SYNTH:
434                 reg = 0x6;
435                 break;
436
437         case SOUND_MIXER_CD:
438                 reg = 0x8;
439                 break;
440
441         case SOUND_MIXER_PCM:
442                 reg = 0x0a;
443                 max = 3;
444                 break;
445
446         default:
447                 return -1;
448         }
449
450         left = (left * max) / 100;
451
452         sb_setmixer(sb, reg, left << 1);
453
454         left = (left * 100) / max;
455
456         return left | (left << 8);
457 }
458
459 static int
460 sbmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
461 {
462         return 0;
463 }
464
465 static kobj_method_t sbmix_mixer_methods[] = {
466         KOBJMETHOD(mixer_init,          sbmix_init),
467         KOBJMETHOD(mixer_set,           sbmix_set),
468         KOBJMETHOD(mixer_setrecsrc,     sbmix_setrecsrc),
469         { 0, 0 }
470 };
471 MIXER_DECLARE(sbmix_mixer);
472
473 /************************************************************/
474
475 static void
476 sb_intr(void *arg)
477 {
478         struct sb_info *sb = (struct sb_info *)arg;
479
480         sb_lock(sb);
481         if (sndbuf_runsz(sb->pch.buffer) > 0) {
482                 sb_unlock(sb);
483                 chn_intr(sb->pch.channel);
484                 sb_lock(sb);
485         }
486
487         if (sndbuf_runsz(sb->rch.buffer) > 0) {
488                 sb_unlock(sb);
489                 chn_intr(sb->rch.channel);
490                 sb_lock(sb);
491         }
492
493         sb_rd(sb, DSP_DATA_AVAIL); /* int ack */
494         sb_unlock(sb);
495 }
496
497 static int
498 sb_speed(struct sb_chinfo *ch)
499 {
500         struct sb_info *sb = ch->parent;
501         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
502         int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
503         int speed, tmp, thresh, max;
504         u_char tconst;
505
506         if (sb->bd_id >= 0x300) {
507                 thresh = stereo? 11025 : 23000;
508                 max = stereo? 22050 : 44100;
509         } else if (sb->bd_id > 0x200) {
510                 thresh = play? 23000 : 13000;
511                 max = play? 44100 : 15000;
512         } else {
513                 thresh = 999999;
514                 max = play? 23000 : 13000;
515         }
516
517         speed = ch->spd;
518         if (speed > max)
519                 speed = max;
520
521         sb_lock(sb);
522         sb->bd_flags &= ~BD_F_HISPEED;
523         if (speed > thresh)
524                 sb->bd_flags |= BD_F_HISPEED;
525
526         tmp = 65536 - (256000000 / (speed << stereo));
527         tconst = tmp >> 8;
528
529         sb_cmd1(sb, 0x40, tconst); /* set time constant */
530
531         speed = (256000000 / (65536 - tmp)) >> stereo;
532
533         ch->spd = speed;
534         sb_unlock(sb);
535         return speed;
536 }
537
538 static int
539 sb_start(struct sb_chinfo *ch)
540 {
541         struct sb_info *sb = ch->parent;
542         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
543         int stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
544         int l = ch->blksz;
545         u_char i;
546
547         l--;
548
549         sb_lock(sb);
550         if (play)
551                 sb_cmd(sb, DSP_CMD_SPKON);
552
553         if (sb->bd_flags & BD_F_HISPEED)
554                 i = play? 0x90 : 0x98;
555         else
556                 i = play? 0x1c : 0x2c;
557
558         sb_setmixer(sb, 0x0e, stereo? 2 : 0);
559         sb_cmd2(sb, 0x48, l);
560         sb_cmd(sb, i);
561
562         sb->bd_flags |= BD_F_DMARUN;
563         sb_unlock(sb);
564         return 0;
565 }
566
567 static int
568 sb_stop(struct sb_chinfo *ch)
569 {
570         struct sb_info *sb = ch->parent;
571         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
572
573         sb_lock(sb);
574         if (sb->bd_flags & BD_F_HISPEED)
575                 sb_reset_dsp(sb);
576         else {
577 #if 0
578                 /*
579                  * NOTE: DSP_CMD_DMAEXIT_8 does not work with old
580                  * soundblaster.
581                  */
582                 sb_cmd(sb, DSP_CMD_DMAEXIT_8);
583 #endif
584                 sb_reset_dsp(sb);
585         }
586
587         if (play)
588                 sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
589         sb_unlock(sb);
590         sb->bd_flags &= ~BD_F_DMARUN;
591         return 0;
592 }
593
594 /* channel interface */
595 static void *
596 sbchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
597 {
598         struct sb_info *sb = devinfo;
599         struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
600
601         ch->parent = sb;
602         ch->channel = c;
603         ch->dir = dir;
604         ch->buffer = b;
605         if (sndbuf_alloc(ch->buffer, sb->parent_dmat, sb->bufsize) != 0)
606                 return NULL;
607         sndbuf_dmasetup(ch->buffer, sb->drq);
608         return ch;
609 }
610
611 static int
612 sbchan_setformat(kobj_t obj, void *data, u_int32_t format)
613 {
614         struct sb_chinfo *ch = data;
615
616         ch->fmt = format;
617         return 0;
618 }
619
620 static int
621 sbchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
622 {
623         struct sb_chinfo *ch = data;
624
625         ch->spd = speed;
626         return sb_speed(ch);
627 }
628
629 static int
630 sbchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
631 {
632         struct sb_chinfo *ch = data;
633
634         ch->blksz = blocksize;
635         return ch->blksz;
636 }
637
638 static int
639 sbchan_trigger(kobj_t obj, void *data, int go)
640 {
641         struct sb_chinfo *ch = data;
642
643         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
644                 return 0;
645
646         sndbuf_dma(ch->buffer, go);
647         if (go == PCMTRIG_START)
648                 sb_start(ch);
649         else
650                 sb_stop(ch);
651         return 0;
652 }
653
654 static int
655 sbchan_getptr(kobj_t obj, void *data)
656 {
657         struct sb_chinfo *ch = data;
658
659         return sndbuf_dmaptr(ch->buffer);
660 }
661
662 static struct pcmchan_caps *
663 sbchan_getcaps(kobj_t obj, void *data)
664 {
665         struct sb_chinfo *ch = data;
666         int p = (ch->dir == PCMDIR_PLAY)? 1 : 0;
667
668         if (ch->parent->bd_id == 0x200)
669                 return p? &sb200_playcaps : &sb200_reccaps;
670         if (ch->parent->bd_id < 0x300)
671                 return p? &sb201_playcaps : &sb201_reccaps;
672         return p? &sbpro_playcaps : &sbpro_reccaps;
673 }
674
675 static kobj_method_t sbchan_methods[] = {
676         KOBJMETHOD(channel_init,                sbchan_init),
677         KOBJMETHOD(channel_setformat,           sbchan_setformat),
678         KOBJMETHOD(channel_setspeed,            sbchan_setspeed),
679         KOBJMETHOD(channel_setblocksize,        sbchan_setblocksize),
680         KOBJMETHOD(channel_trigger,             sbchan_trigger),
681         KOBJMETHOD(channel_getptr,              sbchan_getptr),
682         KOBJMETHOD(channel_getcaps,             sbchan_getcaps),
683         { 0, 0 }
684 };
685 CHANNEL_DECLARE(sbchan);
686
687 /************************************************************/
688
689 static int
690 sb_probe(device_t dev)
691 {
692         char buf[64];
693         uintptr_t func, ver, r, f;
694
695         /* The parent device has already been probed. */
696         r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
697         if (func != SCF_PCM)
698                 return (ENXIO);
699
700         r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
701         f = (ver & 0xffff0000) >> 16;
702         ver &= 0x0000ffff;
703         if ((f & BD_F_ESS) || (ver >= 0x400))
704                 return (ENXIO);
705
706         ksnprintf(buf, sizeof buf, "SB DSP %d.%02d", (int) ver >> 8, (int) ver & 0xff);
707
708         device_set_desc_copy(dev, buf);
709
710         return 0;
711 }
712
713 static int
714 sb_attach(device_t dev)
715 {
716         struct sb_info *sb;
717         char status[SND_STATUSLEN];
718         uintptr_t ver;
719
720         sb = (struct sb_info *)kmalloc(sizeof *sb, M_DEVBUF, M_NOWAIT | M_ZERO);
721         if (!sb)
722                 return ENXIO;
723
724         sb->parent_dev = device_get_parent(dev);
725         BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
726         sb->bd_id = ver & 0x0000ffff;
727         sb->bd_flags = (ver & 0xffff0000) >> 16;
728         sb->bufsize = pcm_getbuffersize(dev, 4096, SB_DEFAULT_BUFSZ, 65536);
729
730         if (sb_alloc_resources(sb, dev))
731                 goto no;
732         if (sb_reset_dsp(sb))
733                 goto no;
734         if (mixer_init(dev, (sb->bd_id < 0x300)? &sbmix_mixer_class : &sbpromix_mixer_class, sb))
735                 goto no;
736         if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih))
737                 goto no;
738
739         pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
740
741         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
742                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
743                         /*highaddr*/BUS_SPACE_MAXADDR,
744                         /*filter*/NULL, /*filterarg*/NULL,
745                         /*maxsize*/sb->bufsize, /*nsegments*/1,
746                         /*maxsegz*/0x3ffff, /*flags*/0,
747                         &sb->parent_dmat) != 0) {
748                 device_printf(dev, "unable to create dma tag\n");
749                 goto no;
750         }
751
752         ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld bufsz %u %s",
753                 rman_get_start(sb->io_base), rman_get_start(sb->irq),
754                 rman_get_start(sb->drq), sb->bufsize, PCM_KLDSTRING(snd_sb8));
755
756         if (pcm_register(dev, sb, 1, 1))
757                 goto no;
758         pcm_addchan(dev, PCMDIR_REC, &sbchan_class, sb);
759         pcm_addchan(dev, PCMDIR_PLAY, &sbchan_class, sb);
760
761         pcm_setstatus(dev, status);
762
763         return 0;
764
765 no:
766         sb_release_resources(sb, dev);
767         return ENXIO;
768 }
769
770 static int
771 sb_detach(device_t dev)
772 {
773         int r;
774         struct sb_info *sb;
775
776         r = pcm_unregister(dev);
777         if (r)
778                 return r;
779
780         sb = pcm_getdevinfo(dev);
781         sb_release_resources(sb, dev);
782         return 0;
783 }
784
785 static device_method_t sb_methods[] = {
786         /* Device interface */
787         DEVMETHOD(device_probe,         sb_probe),
788         DEVMETHOD(device_attach,        sb_attach),
789         DEVMETHOD(device_detach,        sb_detach),
790
791         { 0, 0 }
792 };
793
794 static driver_t sb_driver = {
795         "pcm",
796         sb_methods,
797         PCM_SOFTC_SIZE,
798 };
799
800 DRIVER_MODULE(snd_sb8, sbc, sb_driver, pcm_devclass, 0, 0);
801 MODULE_DEPEND(snd_sb8, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
802 MODULE_DEPEND(snd_sb8, snd_sbc, 1, 1, 1);
803 MODULE_VERSION(snd_sb8, 1);
804
805
806
807