xref: /freebsd/sys/dev/sound/pci/hdspe-pcm.c (revision af6a5351a1fdb1130f18be6c782c4d48916eb971)
1 /*-
2  * Copyright (c) 2012-2016 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * RME HDSPe driver for FreeBSD (pcm-part).
29  * Supported cards: AIO, RayDAT.
30  */
31 
32 #include <dev/sound/pcm/sound.h>
33 #include <dev/sound/pci/hdspe.h>
34 #include <dev/sound/chip.h>
35 
36 #include <dev/pci/pcireg.h>
37 #include <dev/pci/pcivar.h>
38 
39 #include <mixer_if.h>
40 
41 SND_DECLARE_FILE("$FreeBSD$");
42 
43 struct hdspe_latency {
44 	uint32_t n;
45 	uint32_t period;
46 	float ms;
47 };
48 
49 static struct hdspe_latency latency_map[] = {
50 	{ 7,   32, 0.7 },
51 	{ 0,   64, 1.5 },
52 	{ 1,  128,   3 },
53 	{ 2,  256,   6 },
54 	{ 3,  512,  12 },
55 	{ 4, 1024,  23 },
56 	{ 5, 2048,  46 },
57 	{ 6, 4096,  93 },
58 
59 	{ 0,    0,   0 },
60 };
61 
62 struct hdspe_rate {
63 	uint32_t speed;
64 	uint32_t reg;
65 };
66 
67 static struct hdspe_rate rate_map[] = {
68 	{  32000, (HDSPE_FREQ_32000) },
69 	{  44100, (HDSPE_FREQ_44100) },
70 	{  48000, (HDSPE_FREQ_48000) },
71 	{  64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) },
72 	{  88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) },
73 	{  96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) },
74 	{ 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD)   },
75 	{ 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD)   },
76 	{ 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD)   },
77 
78 	{ 0, 0 },
79 };
80 
81 
82 static int
83 hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
84     unsigned int src, unsigned short data)
85 {
86 	struct sc_pcminfo *scp;
87 	struct sc_info *sc;
88 	int offs;
89 
90 	scp = ch->parent;
91 	sc = scp->sc;
92 
93 	offs = 0;
94 	if (ch->dir == PCMDIR_PLAY)
95 		offs = 64;
96 
97 	hdspe_write_4(sc, HDSPE_MIXER_BASE +
98 	    ((offs + src + 128 * dst) * sizeof(uint32_t)),
99 	    data & 0xFFFF);
100 
101 	return (0);
102 };
103 
104 static int
105 hdspechan_setgain(struct sc_chinfo *ch)
106 {
107 
108 	hdspe_hw_mixer(ch, ch->lslot, ch->lslot,
109 	    ch->lvol * HDSPE_MAX_GAIN / 100);
110 	hdspe_hw_mixer(ch, ch->rslot, ch->rslot,
111 	    ch->rvol * HDSPE_MAX_GAIN / 100);
112 
113 	return (0);
114 }
115 
116 static int
117 hdspemixer_init(struct snd_mixer *m)
118 {
119 	struct sc_pcminfo *scp;
120 	struct sc_info *sc;
121 	int mask;
122 
123 	scp = mix_getdevinfo(m);
124 	sc = scp->sc;
125 	if (sc == NULL)
126 		return (-1);
127 
128 	mask = SOUND_MASK_PCM;
129 
130 	if (scp->hc->play)
131 		mask |= SOUND_MASK_VOLUME;
132 
133 	if (scp->hc->rec)
134 		mask |= SOUND_MASK_RECLEV;
135 
136 	snd_mtxlock(sc->lock);
137 	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
138 	mix_setdevs(m, mask);
139 	snd_mtxunlock(sc->lock);
140 
141 	return (0);
142 }
143 
144 static int
145 hdspemixer_set(struct snd_mixer *m, unsigned dev,
146     unsigned left, unsigned right)
147 {
148 	struct sc_pcminfo *scp;
149 	struct sc_chinfo *ch;
150 	int i;
151 
152 	scp = mix_getdevinfo(m);
153 
154 #if 0
155 	device_printf(scp->dev, "hdspemixer_set() %d %d\n",
156 	    left, right);
157 #endif
158 
159 	for (i = 0; i < scp->chnum; i++) {
160 		ch = &scp->chan[i];
161 		if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
162 		    (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
163 			ch->lvol = left;
164 			ch->rvol = right;
165 			if (ch->run)
166 				hdspechan_setgain(ch);
167 		}
168 	}
169 
170 	return (0);
171 }
172 
173 static kobj_method_t hdspemixer_methods[] = {
174 	KOBJMETHOD(mixer_init,      hdspemixer_init),
175 	KOBJMETHOD(mixer_set,       hdspemixer_set),
176 	KOBJMETHOD_END
177 };
178 MIXER_DECLARE(hdspemixer);
179 
180 static void
181 hdspechan_enable(struct sc_chinfo *ch, int value)
182 {
183 	struct sc_pcminfo *scp;
184 	struct sc_info *sc;
185 	int reg;
186 
187 	scp = ch->parent;
188 	sc = scp->sc;
189 
190 	if (ch->dir == PCMDIR_PLAY)
191 		reg = HDSPE_OUT_ENABLE_BASE;
192 	else
193 		reg = HDSPE_IN_ENABLE_BASE;
194 
195 	ch->run = value;
196 
197 	hdspe_write_1(sc, reg + (4 * ch->lslot), value);
198 	hdspe_write_1(sc, reg + (4 * ch->rslot), value);
199 }
200 
201 static int
202 hdspe_running(struct sc_info *sc)
203 {
204 	struct sc_pcminfo *scp;
205 	struct sc_chinfo *ch;
206 	device_t *devlist;
207 	int devcount;
208 	int i, j;
209 	int err;
210 
211 	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
212 		goto bad;
213 
214 	for (i = 0; i < devcount; i++) {
215 		scp = device_get_ivars(devlist[i]);
216 		for (j = 0; j < scp->chnum; j++) {
217 			ch = &scp->chan[j];
218 			if (ch->run)
219 				goto bad;
220 		}
221 	}
222 
223 	free(devlist, M_TEMP);
224 
225 	return (0);
226 bad:
227 
228 #if 0
229 	device_printf(sc->dev, "hdspe is running\n");
230 #endif
231 
232 	free(devlist, M_TEMP);
233 
234 	return (1);
235 }
236 
237 static void
238 hdspe_start_audio(struct sc_info *sc)
239 {
240 
241 	sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
242 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
243 }
244 
245 static void
246 hdspe_stop_audio(struct sc_info *sc)
247 {
248 
249 	if (hdspe_running(sc) == 1)
250 		return;
251 
252 	sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
253 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
254 }
255 
256 /* Multiplex / demultiplex: 2.0 <-> 2 x 1.0. */
257 static void
258 buffer_copy(struct sc_chinfo *ch)
259 {
260 	struct sc_pcminfo *scp;
261 	struct sc_info *sc;
262 	int ssize, dsize;
263 	int src, dst;
264 	int length;
265 	int i;
266 
267 	scp = ch->parent;
268 	sc = scp->sc;
269 
270 	length = sndbuf_getready(ch->buffer) /
271 	    (4 /* Bytes per sample. */ * 2 /* channels */);
272 
273 	if (ch->dir == PCMDIR_PLAY) {
274 		src = sndbuf_getreadyptr(ch->buffer);
275 	} else {
276 		src = sndbuf_getfreeptr(ch->buffer);
277 	}
278 
279 	src /= 4; /* Bytes per sample. */
280 	dst = src / 2; /* Destination buffer twice smaller. */
281 
282 	ssize = ch->size / 4;
283 	dsize = ch->size / 8;
284 
285 	/*
286 	 * Use two fragment buffer to avoid sound clipping.
287 	 */
288 
289 	for (i = 0; i < sc->period * 2 /* fragments */; i++) {
290 		if (ch->dir == PCMDIR_PLAY) {
291 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot] =
292 			    ch->data[src];
293 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot] =
294 			    ch->data[src + 1];
295 
296 		} else {
297 			ch->data[src] =
298 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot];
299 			ch->data[src+1] =
300 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot];
301 		}
302 
303 		dst+=1;
304 		dst %= dsize;
305 		src+=2;
306 		src %= ssize;
307 	}
308 }
309 
310 static int
311 clean(struct sc_chinfo *ch)
312 {
313 	struct sc_pcminfo *scp;
314 	struct sc_info *sc;
315 	uint32_t *buf;
316 
317 	scp = ch->parent;
318 	sc = scp->sc;
319 	buf = sc->rbuf;
320 
321 	if (ch->dir == PCMDIR_PLAY) {
322 		buf = sc->pbuf;
323 	}
324 
325 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->lslot, HDSPE_CHANBUF_SIZE);
326 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->rslot, HDSPE_CHANBUF_SIZE);
327 
328 	return (0);
329 }
330 
331 
332 /* Channel interface. */
333 static void *
334 hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
335     struct pcm_channel *c, int dir)
336 {
337 	struct sc_pcminfo *scp;
338 	struct sc_chinfo *ch;
339 	struct sc_info *sc;
340 	int num;
341 
342 	scp = devinfo;
343 	sc = scp->sc;
344 
345 	snd_mtxlock(sc->lock);
346 	num = scp->chnum;
347 
348 	ch = &scp->chan[num];
349 	ch->lslot = scp->hc->left;
350 	ch->rslot = scp->hc->right;
351 	ch->run = 0;
352 	ch->lvol = 0;
353 	ch->rvol = 0;
354 
355 	ch->size = HDSPE_CHANBUF_SIZE * 2 /* slots */;
356 	ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
357 
358 	ch->buffer = b;
359 	ch->channel = c;
360 	ch->parent = scp;
361 
362 	ch->dir = dir;
363 
364 	snd_mtxunlock(sc->lock);
365 
366 	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
367 		device_printf(scp->dev, "Can't setup sndbuf.\n");
368 		return (NULL);
369 	}
370 
371 	return (ch);
372 }
373 
374 static int
375 hdspechan_trigger(kobj_t obj, void *data, int go)
376 {
377 	struct sc_pcminfo *scp;
378 	struct sc_chinfo *ch;
379 	struct sc_info *sc;
380 
381 	ch = data;
382 	scp = ch->parent;
383 	sc = scp->sc;
384 
385 	snd_mtxlock(sc->lock);
386 	switch (go) {
387 	case PCMTRIG_START:
388 #if 0
389 		device_printf(scp->dev, "hdspechan_trigger(): start\n");
390 #endif
391 		hdspechan_enable(ch, 1);
392 		hdspechan_setgain(ch);
393 		hdspe_start_audio(sc);
394 		break;
395 
396 	case PCMTRIG_STOP:
397 	case PCMTRIG_ABORT:
398 #if 0
399 		device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
400 #endif
401 		clean(ch);
402 		hdspechan_enable(ch, 0);
403 		hdspe_stop_audio(sc);
404 		break;
405 
406 	case PCMTRIG_EMLDMAWR:
407 	case PCMTRIG_EMLDMARD:
408 		if(ch->run)
409 			buffer_copy(ch);
410 		break;
411 	}
412 
413 	snd_mtxunlock(sc->lock);
414 
415 	return (0);
416 }
417 
418 static uint32_t
419 hdspechan_getptr(kobj_t obj, void *data)
420 {
421 	struct sc_pcminfo *scp;
422 	struct sc_chinfo *ch;
423 	struct sc_info *sc;
424 	uint32_t ret, pos;
425 
426 	ch = data;
427 	scp = ch->parent;
428 	sc = scp->sc;
429 
430 	snd_mtxlock(sc->lock);
431 	ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
432 	snd_mtxunlock(sc->lock);
433 
434 	pos = ret & HDSPE_BUF_POSITION_MASK;
435 	pos *= 2; /* Hardbuf twice bigger. */
436 
437 	return (pos);
438 }
439 
440 static int
441 hdspechan_free(kobj_t obj, void *data)
442 {
443 	struct sc_pcminfo *scp;
444 	struct sc_chinfo *ch;
445 	struct sc_info *sc;
446 
447 	ch = data;
448 	scp = ch->parent;
449 	sc = scp->sc;
450 
451 #if 0
452 	device_printf(scp->dev, "hdspechan_free()\n");
453 #endif
454 
455 	snd_mtxlock(sc->lock);
456 	if (ch->data != NULL) {
457 		free(ch->data, M_HDSPE);
458 		ch->data = NULL;
459 	}
460 	snd_mtxunlock(sc->lock);
461 
462 	return (0);
463 }
464 
465 static int
466 hdspechan_setformat(kobj_t obj, void *data, uint32_t format)
467 {
468 	struct sc_chinfo *ch;
469 
470 	ch = data;
471 
472 #if 0
473 	struct sc_pcminfo *scp = ch->parent;
474 	device_printf(scp->dev, "hdspechan_setformat(%d)\n", format);
475 #endif
476 
477 	ch->format = format;
478 
479 	return (0);
480 }
481 
482 static uint32_t
483 hdspechan_setspeed(kobj_t obj, void *data, uint32_t speed)
484 {
485 	struct sc_pcminfo *scp;
486 	struct hdspe_rate *hr;
487 	struct sc_chinfo *ch;
488 	struct sc_info *sc;
489 	long long period;
490 	int threshold;
491 	int i;
492 
493 	ch = data;
494 	scp = ch->parent;
495 	sc = scp->sc;
496 	hr = NULL;
497 
498 #if 0
499 	device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed);
500 #endif
501 
502 	if (hdspe_running(sc) == 1)
503 		goto end;
504 
505 	/* First look for equal frequency. */
506 	for (i = 0; rate_map[i].speed != 0; i++) {
507 		if (rate_map[i].speed == speed)
508 			hr = &rate_map[i];
509 	}
510 
511 	/* If no match, just find nearest. */
512 	if (hr == NULL) {
513 		for (i = 0; rate_map[i].speed != 0; i++) {
514 			hr = &rate_map[i];
515 			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
516 			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
517 			if (speed < threshold)
518 				break;
519 		}
520 	}
521 
522 	switch (sc->type) {
523 	case RAYDAT:
524 	case AIO:
525 		period = HDSPE_FREQ_AIO;
526 		break;
527 	default:
528 		/* Unsupported card. */
529 		goto end;
530 	}
531 
532 	/* Write frequency on the device. */
533 	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
534 	sc->ctrl_register |= hr->reg;
535 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
536 
537 	speed = hr->speed;
538 	if (speed > 96000)
539 		speed /= 4;
540 	else if (speed > 48000)
541 		speed /= 2;
542 
543 	/* Set DDS value. */
544 	period /= speed;
545 	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
546 
547 	sc->speed = hr->speed;
548 end:
549 
550 	return (sc->speed);
551 }
552 
553 static uint32_t
554 hdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
555 {
556 	struct hdspe_latency *hl;
557 	struct sc_pcminfo *scp;
558 	struct sc_chinfo *ch;
559 	struct sc_info *sc;
560 	int threshold;
561 	int i;
562 
563 	ch = data;
564 	scp = ch->parent;
565 	sc = scp->sc;
566 	hl = NULL;
567 
568 #if 0
569 	device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize);
570 #endif
571 
572 	if (hdspe_running(sc) == 1)
573 		goto end;
574 
575 	if (blocksize > HDSPE_LAT_BYTES_MAX)
576 		blocksize = HDSPE_LAT_BYTES_MAX;
577 	else if (blocksize < HDSPE_LAT_BYTES_MIN)
578 		blocksize = HDSPE_LAT_BYTES_MIN;
579 
580 	blocksize /= 4 /* samples */;
581 
582 	/* First look for equal latency. */
583 	for (i = 0; latency_map[i].period != 0; i++) {
584 		if (latency_map[i].period == blocksize) {
585 			hl = &latency_map[i];
586 		}
587 	}
588 
589 	/* If no match, just find nearest. */
590 	if (hl == NULL) {
591 		for (i = 0; latency_map[i].period != 0; i++) {
592 			hl = &latency_map[i];
593 			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
594 			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
595 			if (blocksize < threshold)
596 				break;
597 		}
598 	}
599 
600 	snd_mtxlock(sc->lock);
601 	sc->ctrl_register &= ~HDSPE_LAT_MASK;
602 	sc->ctrl_register |= hdspe_encode_latency(hl->n);
603 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
604 	sc->period = hl->period;
605 	snd_mtxunlock(sc->lock);
606 
607 #if 0
608 	device_printf(scp->dev, "New period=%d\n", sc->period);
609 #endif
610 
611 	sndbuf_resize(ch->buffer, (HDSPE_CHANBUF_SIZE * 2) / (sc->period * 4),
612 	    (sc->period * 4));
613 end:
614 
615 	return (sndbuf_getblksz(ch->buffer));
616 }
617 
618 static uint32_t hdspe_rfmt[] = {
619 	SND_FORMAT(AFMT_S32_LE, 2, 0),
620 	0
621 };
622 
623 static struct pcmchan_caps hdspe_rcaps = {32000, 192000, hdspe_rfmt, 0};
624 
625 static uint32_t hdspe_pfmt[] = {
626 	SND_FORMAT(AFMT_S32_LE, 2, 0),
627 	0
628 };
629 
630 static struct pcmchan_caps hdspe_pcaps = {32000, 192000, hdspe_pfmt, 0};
631 
632 static struct pcmchan_caps *
633 hdspechan_getcaps(kobj_t obj, void *data)
634 {
635 	struct sc_chinfo *ch;
636 
637 	ch = data;
638 
639 #if 0
640 	struct sc_pcminfo *scl = ch->parent;
641 	device_printf(scp->dev, "hdspechan_getcaps()\n");
642 #endif
643 
644 	return ((ch->dir == PCMDIR_PLAY) ?
645 	    &hdspe_pcaps : &hdspe_rcaps);
646 }
647 
648 static kobj_method_t hdspechan_methods[] = {
649 	KOBJMETHOD(channel_init,         hdspechan_init),
650 	KOBJMETHOD(channel_free,         hdspechan_free),
651 	KOBJMETHOD(channel_setformat,    hdspechan_setformat),
652 	KOBJMETHOD(channel_setspeed,     hdspechan_setspeed),
653 	KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize),
654 	KOBJMETHOD(channel_trigger,      hdspechan_trigger),
655 	KOBJMETHOD(channel_getptr,       hdspechan_getptr),
656 	KOBJMETHOD(channel_getcaps,      hdspechan_getcaps),
657 	KOBJMETHOD_END
658 };
659 CHANNEL_DECLARE(hdspechan);
660 
661 
662 static int
663 hdspe_pcm_probe(device_t dev)
664 {
665 
666 #if 0
667 	device_printf(dev,"hdspe_pcm_probe()\n");
668 #endif
669 
670 	return (0);
671 }
672 
673 static uint32_t
674 hdspe_pcm_intr(struct sc_pcminfo *scp)
675 {
676 	struct sc_chinfo *ch;
677 	struct sc_info *sc;
678 	int i;
679 
680 	sc = scp->sc;
681 
682 	for (i = 0; i < scp->chnum; i++) {
683 		ch = &scp->chan[i];
684 		snd_mtxunlock(sc->lock);
685 		chn_intr(ch->channel);
686 		snd_mtxlock(sc->lock);
687 	}
688 
689 	return (0);
690 }
691 
692 static int
693 hdspe_pcm_attach(device_t dev)
694 {
695 	char status[SND_STATUSLEN];
696 	struct sc_pcminfo *scp;
697 	char desc[64];
698 	int i, err;
699 
700 	scp = device_get_ivars(dev);
701 	scp->ih = &hdspe_pcm_intr;
702 
703 	bzero(desc, sizeof(desc));
704 	snprintf(desc, sizeof(desc), "HDSPe AIO [%s]", scp->hc->descr);
705 	device_set_desc_copy(dev, desc);
706 
707 	/*
708 	 * We don't register interrupt handler with snd_setup_intr
709 	 * in pcm device. Mark pcm device as MPSAFE manually.
710 	 */
711 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
712 
713 	err = pcm_register(dev, scp, scp->hc->play, scp->hc->rec);
714 	if (err) {
715 		device_printf(dev, "Can't register pcm.\n");
716 		return (ENXIO);
717 	}
718 
719 	scp->chnum = 0;
720 	for (i = 0; i < scp->hc->play; i++) {
721 		pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp);
722 		scp->chnum++;
723 	}
724 
725 	for (i = 0; i < scp->hc->rec; i++) {
726 		pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp);
727 		scp->chnum++;
728 	}
729 
730 	snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
731 	    rman_get_start(scp->sc->cs),
732 	    rman_get_start(scp->sc->irq),
733 	    PCM_KLDSTRING(snd_hdspe));
734 	pcm_setstatus(dev, status);
735 
736 	mixer_init(dev, &hdspemixer_class, scp);
737 
738 	return (0);
739 }
740 
741 static int
742 hdspe_pcm_detach(device_t dev)
743 {
744 	int err;
745 
746 	err = pcm_unregister(dev);
747 	if (err) {
748 		device_printf(dev, "Can't unregister device.\n");
749 		return (err);
750 	}
751 
752 	return (0);
753 }
754 
755 static device_method_t hdspe_pcm_methods[] = {
756 	DEVMETHOD(device_probe,     hdspe_pcm_probe),
757 	DEVMETHOD(device_attach,    hdspe_pcm_attach),
758 	DEVMETHOD(device_detach,    hdspe_pcm_detach),
759 	{ 0, 0 }
760 };
761 
762 static driver_t hdspe_pcm_driver = {
763 	"pcm",
764 	hdspe_pcm_methods,
765 	PCM_SOFTC_SIZE,
766 };
767 
768 DRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, pcm_devclass, 0, 0);
769 MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
770 MODULE_VERSION(snd_hdspe, 1);
771