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