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