xref: /freebsd/sys/dev/sound/pci/hdspe-pcm.c (revision 378503af)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012-2016 Ruslan Bukin <br@bsdpad.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * RME HDSPe driver for FreeBSD (pcm-part).
31  * Supported cards: AIO, RayDAT.
32  */
33 
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pci/hdspe.h>
36 #include <dev/sound/chip.h>
37 
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
40 
41 #include <mixer_if.h>
42 
43 SND_DECLARE_FILE("$FreeBSD$");
44 
45 struct hdspe_latency {
46 	uint32_t n;
47 	uint32_t period;
48 	float ms;
49 };
50 
51 static struct hdspe_latency latency_map[] = {
52 	{ 7,   32, 0.7 },
53 	{ 0,   64, 1.5 },
54 	{ 1,  128,   3 },
55 	{ 2,  256,   6 },
56 	{ 3,  512,  12 },
57 	{ 4, 1024,  23 },
58 	{ 5, 2048,  46 },
59 	{ 6, 4096,  93 },
60 
61 	{ 0,    0,   0 },
62 };
63 
64 struct hdspe_rate {
65 	uint32_t speed;
66 	uint32_t reg;
67 };
68 
69 static struct hdspe_rate rate_map[] = {
70 	{  32000, (HDSPE_FREQ_32000) },
71 	{  44100, (HDSPE_FREQ_44100) },
72 	{  48000, (HDSPE_FREQ_48000) },
73 	{  64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) },
74 	{  88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) },
75 	{  96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) },
76 	{ 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD)   },
77 	{ 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD)   },
78 	{ 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD)   },
79 
80 	{ 0, 0 },
81 };
82 
83 static int
84 hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
85     unsigned int src, unsigned short data)
86 {
87 	struct sc_pcminfo *scp;
88 	struct sc_info *sc;
89 	int offs;
90 
91 	scp = ch->parent;
92 	sc = scp->sc;
93 
94 	offs = 0;
95 	if (ch->dir == PCMDIR_PLAY)
96 		offs = 64;
97 
98 	hdspe_write_4(sc, HDSPE_MIXER_BASE +
99 	    ((offs + src + 128 * dst) * sizeof(uint32_t)),
100 	    data & 0xFFFF);
101 
102 	return (0);
103 };
104 
105 static int
106 hdspechan_setgain(struct sc_chinfo *ch)
107 {
108 
109 	hdspe_hw_mixer(ch, ch->lslot, ch->lslot,
110 	    ch->lvol * HDSPE_MAX_GAIN / 100);
111 	hdspe_hw_mixer(ch, ch->rslot, ch->rslot,
112 	    ch->rvol * HDSPE_MAX_GAIN / 100);
113 
114 	return (0);
115 }
116 
117 static int
118 hdspemixer_init(struct snd_mixer *m)
119 {
120 	struct sc_pcminfo *scp;
121 	struct sc_info *sc;
122 	int mask;
123 
124 	scp = mix_getdevinfo(m);
125 	sc = scp->sc;
126 	if (sc == NULL)
127 		return (-1);
128 
129 	mask = SOUND_MASK_PCM;
130 
131 	if (scp->hc->play)
132 		mask |= SOUND_MASK_VOLUME;
133 
134 	if (scp->hc->rec)
135 		mask |= SOUND_MASK_RECLEV;
136 
137 	snd_mtxlock(sc->lock);
138 	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
139 	mix_setdevs(m, mask);
140 	snd_mtxunlock(sc->lock);
141 
142 	return (0);
143 }
144 
145 static int
146 hdspemixer_set(struct snd_mixer *m, unsigned dev,
147     unsigned left, unsigned right)
148 {
149 	struct sc_pcminfo *scp;
150 	struct sc_chinfo *ch;
151 	int i;
152 
153 	scp = mix_getdevinfo(m);
154 
155 #if 0
156 	device_printf(scp->dev, "hdspemixer_set() %d %d\n",
157 	    left, right);
158 #endif
159 
160 	for (i = 0; i < scp->chnum; i++) {
161 		ch = &scp->chan[i];
162 		if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
163 		    (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
164 			ch->lvol = left;
165 			ch->rvol = right;
166 			if (ch->run)
167 				hdspechan_setgain(ch);
168 		}
169 	}
170 
171 	return (0);
172 }
173 
174 static kobj_method_t hdspemixer_methods[] = {
175 	KOBJMETHOD(mixer_init,      hdspemixer_init),
176 	KOBJMETHOD(mixer_set,       hdspemixer_set),
177 	KOBJMETHOD_END
178 };
179 MIXER_DECLARE(hdspemixer);
180 
181 static void
182 hdspechan_enable(struct sc_chinfo *ch, int value)
183 {
184 	struct sc_pcminfo *scp;
185 	struct sc_info *sc;
186 	int reg;
187 
188 	scp = ch->parent;
189 	sc = scp->sc;
190 
191 	if (ch->dir == PCMDIR_PLAY)
192 		reg = HDSPE_OUT_ENABLE_BASE;
193 	else
194 		reg = HDSPE_IN_ENABLE_BASE;
195 
196 	ch->run = value;
197 
198 	hdspe_write_1(sc, reg + (4 * ch->lslot), value);
199 	hdspe_write_1(sc, reg + (4 * ch->rslot), value);
200 }
201 
202 static int
203 hdspe_running(struct sc_info *sc)
204 {
205 	struct sc_pcminfo *scp;
206 	struct sc_chinfo *ch;
207 	device_t *devlist;
208 	int devcount;
209 	int i, j;
210 	int err;
211 
212 	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
213 		goto bad;
214 
215 	for (i = 0; i < devcount; i++) {
216 		scp = device_get_ivars(devlist[i]);
217 		for (j = 0; j < scp->chnum; j++) {
218 			ch = &scp->chan[j];
219 			if (ch->run)
220 				goto bad;
221 		}
222 	}
223 
224 	free(devlist, M_TEMP);
225 
226 	return (0);
227 bad:
228 
229 #if 0
230 	device_printf(sc->dev, "hdspe is running\n");
231 #endif
232 
233 	free(devlist, M_TEMP);
234 
235 	return (1);
236 }
237 
238 static void
239 hdspe_start_audio(struct sc_info *sc)
240 {
241 
242 	sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
243 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
244 }
245 
246 static void
247 hdspe_stop_audio(struct sc_info *sc)
248 {
249 
250 	if (hdspe_running(sc) == 1)
251 		return;
252 
253 	sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
254 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
255 }
256 
257 /* Multiplex / demultiplex: 2.0 <-> 2 x 1.0. */
258 static void
259 buffer_copy(struct sc_chinfo *ch)
260 {
261 	struct sc_pcminfo *scp;
262 	struct sc_info *sc;
263 	int ssize, dsize;
264 	int src, dst;
265 	int length;
266 	int i;
267 
268 	scp = ch->parent;
269 	sc = scp->sc;
270 
271 	length = sndbuf_getready(ch->buffer) /
272 	    (4 /* Bytes per sample. */ * 2 /* channels */);
273 
274 	if (ch->dir == PCMDIR_PLAY) {
275 		src = sndbuf_getreadyptr(ch->buffer);
276 	} else {
277 		src = sndbuf_getfreeptr(ch->buffer);
278 	}
279 
280 	src /= 4; /* Bytes per sample. */
281 	dst = src / 2; /* Destination buffer twice smaller. */
282 
283 	ssize = ch->size / 4;
284 	dsize = ch->size / 8;
285 
286 	/*
287 	 * Use two fragment buffer to avoid sound clipping.
288 	 */
289 
290 	for (i = 0; i < sc->period * 2 /* fragments */; i++) {
291 		if (ch->dir == PCMDIR_PLAY) {
292 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot] =
293 			    ch->data[src];
294 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot] =
295 			    ch->data[src + 1];
296 
297 		} else {
298 			ch->data[src] =
299 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot];
300 			ch->data[src+1] =
301 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot];
302 		}
303 
304 		dst+=1;
305 		dst %= dsize;
306 		src+=2;
307 		src %= ssize;
308 	}
309 }
310 
311 static int
312 clean(struct sc_chinfo *ch)
313 {
314 	struct sc_pcminfo *scp;
315 	struct sc_info *sc;
316 	uint32_t *buf;
317 
318 	scp = ch->parent;
319 	sc = scp->sc;
320 	buf = sc->rbuf;
321 
322 	if (ch->dir == PCMDIR_PLAY) {
323 		buf = sc->pbuf;
324 	}
325 
326 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->lslot, HDSPE_CHANBUF_SIZE);
327 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->rslot, HDSPE_CHANBUF_SIZE);
328 
329 	return (0);
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 static int
662 hdspe_pcm_probe(device_t dev)
663 {
664 
665 #if 0
666 	device_printf(dev,"hdspe_pcm_probe()\n");
667 #endif
668 
669 	return (0);
670 }
671 
672 static uint32_t
673 hdspe_pcm_intr(struct sc_pcminfo *scp)
674 {
675 	struct sc_chinfo *ch;
676 	struct sc_info *sc;
677 	int i;
678 
679 	sc = scp->sc;
680 
681 	for (i = 0; i < scp->chnum; i++) {
682 		ch = &scp->chan[i];
683 		snd_mtxunlock(sc->lock);
684 		chn_intr(ch->channel);
685 		snd_mtxlock(sc->lock);
686 	}
687 
688 	return (0);
689 }
690 
691 static int
692 hdspe_pcm_attach(device_t dev)
693 {
694 	char status[SND_STATUSLEN];
695 	struct sc_pcminfo *scp;
696 	char desc[64];
697 	int i, err;
698 
699 	scp = device_get_ivars(dev);
700 	scp->ih = &hdspe_pcm_intr;
701 
702 	bzero(desc, sizeof(desc));
703 	snprintf(desc, sizeof(desc), "HDSPe AIO [%s]", scp->hc->descr);
704 	device_set_desc_copy(dev, desc);
705 
706 	/*
707 	 * We don't register interrupt handler with snd_setup_intr
708 	 * in pcm device. Mark pcm device as MPSAFE manually.
709 	 */
710 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
711 
712 	err = pcm_register(dev, scp, scp->hc->play, scp->hc->rec);
713 	if (err) {
714 		device_printf(dev, "Can't register pcm.\n");
715 		return (ENXIO);
716 	}
717 
718 	scp->chnum = 0;
719 	for (i = 0; i < scp->hc->play; i++) {
720 		pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp);
721 		scp->chnum++;
722 	}
723 
724 	for (i = 0; i < scp->hc->rec; i++) {
725 		pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp);
726 		scp->chnum++;
727 	}
728 
729 	snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
730 	    rman_get_start(scp->sc->cs),
731 	    rman_get_start(scp->sc->irq),
732 	    PCM_KLDSTRING(snd_hdspe));
733 	pcm_setstatus(dev, status);
734 
735 	mixer_init(dev, &hdspemixer_class, scp);
736 
737 	return (0);
738 }
739 
740 static int
741 hdspe_pcm_detach(device_t dev)
742 {
743 	int err;
744 
745 	err = pcm_unregister(dev);
746 	if (err) {
747 		device_printf(dev, "Can't unregister device.\n");
748 		return (err);
749 	}
750 
751 	return (0);
752 }
753 
754 static device_method_t hdspe_pcm_methods[] = {
755 	DEVMETHOD(device_probe,     hdspe_pcm_probe),
756 	DEVMETHOD(device_attach,    hdspe_pcm_attach),
757 	DEVMETHOD(device_detach,    hdspe_pcm_detach),
758 	{ 0, 0 }
759 };
760 
761 static driver_t hdspe_pcm_driver = {
762 	"pcm",
763 	hdspe_pcm_methods,
764 	PCM_SOFTC_SIZE,
765 };
766 
767 DRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, pcm_devclass, 0, 0);
768 MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
769 MODULE_VERSION(snd_hdspe, 1);
770