xref: /freebsd/sys/dev/sound/pci/hdspe-pcm.c (revision f552d7ad)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012-2021 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 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 static int
82 hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
83     unsigned int src, unsigned short data)
84 {
85 	struct sc_pcminfo *scp;
86 	struct sc_info *sc;
87 	int offs;
88 
89 	scp = ch->parent;
90 	sc = scp->sc;
91 
92 	offs = 0;
93 	if (ch->dir == PCMDIR_PLAY)
94 		offs = 64;
95 
96 	hdspe_write_4(sc, HDSPE_MIXER_BASE +
97 	    ((offs + src + 128 * dst) * sizeof(uint32_t)),
98 	    data & 0xFFFF);
99 
100 	return (0);
101 };
102 
103 static int
104 hdspechan_setgain(struct sc_chinfo *ch)
105 {
106 
107 	hdspe_hw_mixer(ch, ch->lslot, ch->lslot,
108 	    ch->lvol * HDSPE_MAX_GAIN / 100);
109 	hdspe_hw_mixer(ch, ch->rslot, ch->rslot,
110 	    ch->rvol * HDSPE_MAX_GAIN / 100);
111 
112 	return (0);
113 }
114 
115 static int
116 hdspemixer_init(struct snd_mixer *m)
117 {
118 	struct sc_pcminfo *scp;
119 	struct sc_info *sc;
120 	int mask;
121 
122 	scp = mix_getdevinfo(m);
123 	sc = scp->sc;
124 	if (sc == NULL)
125 		return (-1);
126 
127 	mask = SOUND_MASK_PCM;
128 
129 	if (scp->hc->play)
130 		mask |= SOUND_MASK_VOLUME;
131 
132 	if (scp->hc->rec)
133 		mask |= SOUND_MASK_RECLEV;
134 
135 	snd_mtxlock(sc->lock);
136 	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
137 	mix_setdevs(m, mask);
138 	snd_mtxunlock(sc->lock);
139 
140 	return (0);
141 }
142 
143 static int
144 hdspemixer_set(struct snd_mixer *m, unsigned dev,
145     unsigned left, unsigned right)
146 {
147 	struct sc_pcminfo *scp;
148 	struct sc_chinfo *ch;
149 	int i;
150 
151 	scp = mix_getdevinfo(m);
152 
153 #if 0
154 	device_printf(scp->dev, "hdspemixer_set() %d %d\n",
155 	    left, right);
156 #endif
157 
158 	for (i = 0; i < scp->chnum; i++) {
159 		ch = &scp->chan[i];
160 		if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
161 		    (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
162 			ch->lvol = left;
163 			ch->rvol = right;
164 			if (ch->run)
165 				hdspechan_setgain(ch);
166 		}
167 	}
168 
169 	return (0);
170 }
171 
172 static kobj_method_t hdspemixer_methods[] = {
173 	KOBJMETHOD(mixer_init,      hdspemixer_init),
174 	KOBJMETHOD(mixer_set,       hdspemixer_set),
175 	KOBJMETHOD_END
176 };
177 MIXER_DECLARE(hdspemixer);
178 
179 static void
180 hdspechan_enable(struct sc_chinfo *ch, int value)
181 {
182 	struct sc_pcminfo *scp;
183 	struct sc_info *sc;
184 	int reg;
185 
186 	scp = ch->parent;
187 	sc = scp->sc;
188 
189 	if (ch->dir == PCMDIR_PLAY)
190 		reg = HDSPE_OUT_ENABLE_BASE;
191 	else
192 		reg = HDSPE_IN_ENABLE_BASE;
193 
194 	ch->run = value;
195 
196 	hdspe_write_1(sc, reg + (4 * ch->lslot), value);
197 	if (AFMT_CHANNEL(ch->format) == 2)
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 n;
265 	int i;
266 
267 	scp = ch->parent;
268 	sc = scp->sc;
269 
270 	n = AFMT_CHANNEL(ch->format); /* n channels */
271 
272 	if (ch->dir == PCMDIR_PLAY) {
273 		src = sndbuf_getreadyptr(ch->buffer);
274 	} else {
275 		src = sndbuf_getfreeptr(ch->buffer);
276 	}
277 
278 	src /= 4; /* Bytes per sample. */
279 	dst = src / n; /* Destination buffer n-times smaller. */
280 
281 	ssize = ch->size / 4;
282 	dsize = ch->size / (4 * n);
283 
284 	/*
285 	 * Use two fragment buffer to avoid sound clipping.
286 	 */
287 
288 	for (i = 0; i < sc->period * 2 /* fragments */; i++) {
289 		if (ch->dir == PCMDIR_PLAY) {
290 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot] =
291 			    ch->data[src];
292 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot] =
293 			    ch->data[src + 1];
294 
295 		} else {
296 			ch->data[src] =
297 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot];
298 			ch->data[src+1] =
299 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot];
300 		}
301 
302 		dst+=1;
303 		dst %= dsize;
304 		src += n;
305 		src %= ssize;
306 	}
307 }
308 
309 static int
310 clean(struct sc_chinfo *ch)
311 {
312 	struct sc_pcminfo *scp;
313 	struct sc_info *sc;
314 	uint32_t *buf;
315 
316 	scp = ch->parent;
317 	sc = scp->sc;
318 	buf = sc->rbuf;
319 
320 	if (ch->dir == PCMDIR_PLAY) {
321 		buf = sc->pbuf;
322 	}
323 
324 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->lslot, HDSPE_CHANBUF_SIZE);
325 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->rslot, HDSPE_CHANBUF_SIZE);
326 
327 	return (0);
328 }
329 
330 /* Channel interface. */
331 static void *
332 hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
333     struct pcm_channel *c, int dir)
334 {
335 	struct sc_pcminfo *scp;
336 	struct sc_chinfo *ch;
337 	struct sc_info *sc;
338 	int num;
339 
340 	scp = devinfo;
341 	sc = scp->sc;
342 
343 	snd_mtxlock(sc->lock);
344 	num = scp->chnum;
345 
346 	ch = &scp->chan[num];
347 	ch->lslot = scp->hc->left;
348 	ch->rslot = scp->hc->right;
349 	ch->run = 0;
350 	ch->lvol = 0;
351 	ch->rvol = 0;
352 
353 	ch->size = HDSPE_CHANBUF_SIZE * 2; /* max size */
354 	ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
355 
356 	ch->buffer = b;
357 	ch->channel = c;
358 	ch->parent = scp;
359 
360 	ch->dir = dir;
361 
362 	snd_mtxunlock(sc->lock);
363 
364 	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
365 		device_printf(scp->dev, "Can't setup sndbuf.\n");
366 		return (NULL);
367 	}
368 
369 	return (ch);
370 }
371 
372 static int
373 hdspechan_trigger(kobj_t obj, void *data, int go)
374 {
375 	struct sc_pcminfo *scp;
376 	struct sc_chinfo *ch;
377 	struct sc_info *sc;
378 
379 	ch = data;
380 	scp = ch->parent;
381 	sc = scp->sc;
382 
383 	snd_mtxlock(sc->lock);
384 	switch (go) {
385 	case PCMTRIG_START:
386 #if 0
387 		device_printf(scp->dev, "hdspechan_trigger(): start\n");
388 #endif
389 		hdspechan_enable(ch, 1);
390 		hdspechan_setgain(ch);
391 		hdspe_start_audio(sc);
392 		break;
393 
394 	case PCMTRIG_STOP:
395 	case PCMTRIG_ABORT:
396 #if 0
397 		device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
398 #endif
399 		clean(ch);
400 		hdspechan_enable(ch, 0);
401 		hdspe_stop_audio(sc);
402 		break;
403 
404 	case PCMTRIG_EMLDMAWR:
405 	case PCMTRIG_EMLDMARD:
406 		if(ch->run)
407 			buffer_copy(ch);
408 		break;
409 	}
410 
411 	snd_mtxunlock(sc->lock);
412 
413 	return (0);
414 }
415 
416 static uint32_t
417 hdspechan_getptr(kobj_t obj, void *data)
418 {
419 	struct sc_pcminfo *scp;
420 	struct sc_chinfo *ch;
421 	struct sc_info *sc;
422 	uint32_t ret, pos;
423 
424 	ch = data;
425 	scp = ch->parent;
426 	sc = scp->sc;
427 
428 	snd_mtxlock(sc->lock);
429 	ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
430 	snd_mtxunlock(sc->lock);
431 
432 	pos = ret & HDSPE_BUF_POSITION_MASK;
433 	if (AFMT_CHANNEL(ch->format) == 2)
434 		pos *= 2; /* Hardbuf twice bigger. */
435 
436 	return (pos);
437 }
438 
439 static int
440 hdspechan_free(kobj_t obj, void *data)
441 {
442 	struct sc_pcminfo *scp;
443 	struct sc_chinfo *ch;
444 	struct sc_info *sc;
445 
446 	ch = data;
447 	scp = ch->parent;
448 	sc = scp->sc;
449 
450 #if 0
451 	device_printf(scp->dev, "hdspechan_free()\n");
452 #endif
453 
454 	snd_mtxlock(sc->lock);
455 	if (ch->data != NULL) {
456 		free(ch->data, M_HDSPE);
457 		ch->data = NULL;
458 	}
459 	snd_mtxunlock(sc->lock);
460 
461 	return (0);
462 }
463 
464 static int
465 hdspechan_setformat(kobj_t obj, void *data, uint32_t format)
466 {
467 	struct sc_chinfo *ch;
468 
469 	ch = data;
470 
471 #if 0
472 	struct sc_pcminfo *scp = ch->parent;
473 	device_printf(scp->dev, "hdspechan_setformat(%d)\n", format);
474 #endif
475 
476 	ch->format = format;
477 
478 	return (0);
479 }
480 
481 static uint32_t
482 hdspechan_setspeed(kobj_t obj, void *data, uint32_t speed)
483 {
484 	struct sc_pcminfo *scp;
485 	struct hdspe_rate *hr;
486 	struct sc_chinfo *ch;
487 	struct sc_info *sc;
488 	long long period;
489 	int threshold;
490 	int i;
491 
492 	ch = data;
493 	scp = ch->parent;
494 	sc = scp->sc;
495 	hr = NULL;
496 
497 #if 0
498 	device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed);
499 #endif
500 
501 	if (hdspe_running(sc) == 1)
502 		goto end;
503 
504 	/* First look for equal frequency. */
505 	for (i = 0; rate_map[i].speed != 0; i++) {
506 		if (rate_map[i].speed == speed)
507 			hr = &rate_map[i];
508 	}
509 
510 	/* If no match, just find nearest. */
511 	if (hr == NULL) {
512 		for (i = 0; rate_map[i].speed != 0; i++) {
513 			hr = &rate_map[i];
514 			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
515 			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
516 			if (speed < threshold)
517 				break;
518 		}
519 	}
520 
521 	switch (sc->type) {
522 	case HDSPE_RAYDAT:
523 	case HDSPE_AIO:
524 		period = HDSPE_FREQ_AIO;
525 		break;
526 	default:
527 		/* Unsupported card. */
528 		goto end;
529 	}
530 
531 	/* Write frequency on the device. */
532 	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
533 	sc->ctrl_register |= hr->reg;
534 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
535 
536 	speed = hr->speed;
537 	if (speed > 96000)
538 		speed /= 4;
539 	else if (speed > 48000)
540 		speed /= 2;
541 
542 	/* Set DDS value. */
543 	period /= speed;
544 	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
545 
546 	sc->speed = hr->speed;
547 end:
548 
549 	return (sc->speed);
550 }
551 
552 static uint32_t
553 hdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
554 {
555 	struct hdspe_latency *hl;
556 	struct sc_pcminfo *scp;
557 	struct sc_chinfo *ch;
558 	struct sc_info *sc;
559 	int threshold;
560 	int i;
561 
562 	ch = data;
563 	scp = ch->parent;
564 	sc = scp->sc;
565 	hl = NULL;
566 
567 #if 0
568 	device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize);
569 #endif
570 
571 	if (hdspe_running(sc) == 1)
572 		goto end;
573 
574 	if (blocksize > HDSPE_LAT_BYTES_MAX)
575 		blocksize = HDSPE_LAT_BYTES_MAX;
576 	else if (blocksize < HDSPE_LAT_BYTES_MIN)
577 		blocksize = HDSPE_LAT_BYTES_MIN;
578 
579 	blocksize /= 4 /* samples */;
580 
581 	/* First look for equal latency. */
582 	for (i = 0; latency_map[i].period != 0; i++) {
583 		if (latency_map[i].period == blocksize) {
584 			hl = &latency_map[i];
585 		}
586 	}
587 
588 	/* If no match, just find nearest. */
589 	if (hl == NULL) {
590 		for (i = 0; latency_map[i].period != 0; i++) {
591 			hl = &latency_map[i];
592 			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
593 			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
594 			if (blocksize < threshold)
595 				break;
596 		}
597 	}
598 
599 	snd_mtxlock(sc->lock);
600 	sc->ctrl_register &= ~HDSPE_LAT_MASK;
601 	sc->ctrl_register |= hdspe_encode_latency(hl->n);
602 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
603 	sc->period = hl->period;
604 	snd_mtxunlock(sc->lock);
605 
606 #if 0
607 	device_printf(scp->dev, "New period=%d\n", sc->period);
608 #endif
609 
610 	sndbuf_resize(ch->buffer,
611 	    (HDSPE_CHANBUF_SIZE * AFMT_CHANNEL(ch->format)) / (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, 1, 0),
627 	SND_FORMAT(AFMT_S32_LE, 2, 0),
628 	0
629 };
630 
631 static struct pcmchan_caps hdspe_pcaps = {32000, 192000, hdspe_pfmt, 0};
632 
633 static struct pcmchan_caps *
634 hdspechan_getcaps(kobj_t obj, void *data)
635 {
636 	struct sc_chinfo *ch;
637 
638 	ch = data;
639 
640 #if 0
641 	struct sc_pcminfo *scl = ch->parent;
642 	device_printf(scp->dev, "hdspechan_getcaps()\n");
643 #endif
644 
645 	return ((ch->dir == PCMDIR_PLAY) ?
646 	    &hdspe_pcaps : &hdspe_rcaps);
647 }
648 
649 static kobj_method_t hdspechan_methods[] = {
650 	KOBJMETHOD(channel_init,         hdspechan_init),
651 	KOBJMETHOD(channel_free,         hdspechan_free),
652 	KOBJMETHOD(channel_setformat,    hdspechan_setformat),
653 	KOBJMETHOD(channel_setspeed,     hdspechan_setspeed),
654 	KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize),
655 	KOBJMETHOD(channel_trigger,      hdspechan_trigger),
656 	KOBJMETHOD(channel_getptr,       hdspechan_getptr),
657 	KOBJMETHOD(channel_getcaps,      hdspechan_getcaps),
658 	KOBJMETHOD_END
659 };
660 CHANNEL_DECLARE(hdspechan);
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, 0, 0);
769 MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
770 MODULE_VERSION(snd_hdspe, 1);
771