xref: /freebsd/sys/dev/sound/pci/hdspe-pcm.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 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 	if (AFMT_CHANNEL(ch->format) == 2)
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 n;
268 	int i;
269 
270 	scp = ch->parent;
271 	sc = scp->sc;
272 
273 	n = AFMT_CHANNEL(ch->format); /* n channels */
274 
275 	length = sndbuf_getready(ch->buffer) /
276 	    (4 /* Bytes per sample. */ * n);
277 
278 	if (ch->dir == PCMDIR_PLAY) {
279 		src = sndbuf_getreadyptr(ch->buffer);
280 	} else {
281 		src = sndbuf_getfreeptr(ch->buffer);
282 	}
283 
284 	src /= 4; /* Bytes per sample. */
285 	dst = src / n; /* Destination buffer n-times smaller. */
286 
287 	ssize = ch->size / 4;
288 	dsize = ch->size / (4 * n);
289 
290 	/*
291 	 * Use two fragment buffer to avoid sound clipping.
292 	 */
293 
294 	for (i = 0; i < sc->period * 2 /* fragments */; i++) {
295 		if (ch->dir == PCMDIR_PLAY) {
296 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot] =
297 			    ch->data[src];
298 			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot] =
299 			    ch->data[src + 1];
300 
301 		} else {
302 			ch->data[src] =
303 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot];
304 			ch->data[src+1] =
305 			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot];
306 		}
307 
308 		dst+=1;
309 		dst %= dsize;
310 		src += n;
311 		src %= ssize;
312 	}
313 }
314 
315 static int
316 clean(struct sc_chinfo *ch)
317 {
318 	struct sc_pcminfo *scp;
319 	struct sc_info *sc;
320 	uint32_t *buf;
321 
322 	scp = ch->parent;
323 	sc = scp->sc;
324 	buf = sc->rbuf;
325 
326 	if (ch->dir == PCMDIR_PLAY) {
327 		buf = sc->pbuf;
328 	}
329 
330 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->lslot, HDSPE_CHANBUF_SIZE);
331 	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->rslot, HDSPE_CHANBUF_SIZE);
332 
333 	return (0);
334 }
335 
336 /* Channel interface. */
337 static void *
338 hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
339     struct pcm_channel *c, int dir)
340 {
341 	struct sc_pcminfo *scp;
342 	struct sc_chinfo *ch;
343 	struct sc_info *sc;
344 	int num;
345 
346 	scp = devinfo;
347 	sc = scp->sc;
348 
349 	snd_mtxlock(sc->lock);
350 	num = scp->chnum;
351 
352 	ch = &scp->chan[num];
353 	ch->lslot = scp->hc->left;
354 	ch->rslot = scp->hc->right;
355 	ch->run = 0;
356 	ch->lvol = 0;
357 	ch->rvol = 0;
358 
359 	ch->size = HDSPE_CHANBUF_SIZE * 2; /* max size */
360 	ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
361 
362 	ch->buffer = b;
363 	ch->channel = c;
364 	ch->parent = scp;
365 
366 	ch->dir = dir;
367 
368 	snd_mtxunlock(sc->lock);
369 
370 	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
371 		device_printf(scp->dev, "Can't setup sndbuf.\n");
372 		return (NULL);
373 	}
374 
375 	return (ch);
376 }
377 
378 static int
379 hdspechan_trigger(kobj_t obj, void *data, int go)
380 {
381 	struct sc_pcminfo *scp;
382 	struct sc_chinfo *ch;
383 	struct sc_info *sc;
384 
385 	ch = data;
386 	scp = ch->parent;
387 	sc = scp->sc;
388 
389 	snd_mtxlock(sc->lock);
390 	switch (go) {
391 	case PCMTRIG_START:
392 #if 0
393 		device_printf(scp->dev, "hdspechan_trigger(): start\n");
394 #endif
395 		hdspechan_enable(ch, 1);
396 		hdspechan_setgain(ch);
397 		hdspe_start_audio(sc);
398 		break;
399 
400 	case PCMTRIG_STOP:
401 	case PCMTRIG_ABORT:
402 #if 0
403 		device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
404 #endif
405 		clean(ch);
406 		hdspechan_enable(ch, 0);
407 		hdspe_stop_audio(sc);
408 		break;
409 
410 	case PCMTRIG_EMLDMAWR:
411 	case PCMTRIG_EMLDMARD:
412 		if(ch->run)
413 			buffer_copy(ch);
414 		break;
415 	}
416 
417 	snd_mtxunlock(sc->lock);
418 
419 	return (0);
420 }
421 
422 static uint32_t
423 hdspechan_getptr(kobj_t obj, void *data)
424 {
425 	struct sc_pcminfo *scp;
426 	struct sc_chinfo *ch;
427 	struct sc_info *sc;
428 	uint32_t ret, pos;
429 
430 	ch = data;
431 	scp = ch->parent;
432 	sc = scp->sc;
433 
434 	snd_mtxlock(sc->lock);
435 	ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
436 	snd_mtxunlock(sc->lock);
437 
438 	pos = ret & HDSPE_BUF_POSITION_MASK;
439 	if (AFMT_CHANNEL(ch->format) == 2)
440 		pos *= 2; /* Hardbuf twice bigger. */
441 
442 	return (pos);
443 }
444 
445 static int
446 hdspechan_free(kobj_t obj, void *data)
447 {
448 	struct sc_pcminfo *scp;
449 	struct sc_chinfo *ch;
450 	struct sc_info *sc;
451 
452 	ch = data;
453 	scp = ch->parent;
454 	sc = scp->sc;
455 
456 #if 0
457 	device_printf(scp->dev, "hdspechan_free()\n");
458 #endif
459 
460 	snd_mtxlock(sc->lock);
461 	if (ch->data != NULL) {
462 		free(ch->data, M_HDSPE);
463 		ch->data = NULL;
464 	}
465 	snd_mtxunlock(sc->lock);
466 
467 	return (0);
468 }
469 
470 static int
471 hdspechan_setformat(kobj_t obj, void *data, uint32_t format)
472 {
473 	struct sc_chinfo *ch;
474 
475 	ch = data;
476 
477 #if 0
478 	struct sc_pcminfo *scp = ch->parent;
479 	device_printf(scp->dev, "hdspechan_setformat(%d)\n", format);
480 #endif
481 
482 	ch->format = format;
483 
484 	return (0);
485 }
486 
487 static uint32_t
488 hdspechan_setspeed(kobj_t obj, void *data, uint32_t speed)
489 {
490 	struct sc_pcminfo *scp;
491 	struct hdspe_rate *hr;
492 	struct sc_chinfo *ch;
493 	struct sc_info *sc;
494 	long long period;
495 	int threshold;
496 	int i;
497 
498 	ch = data;
499 	scp = ch->parent;
500 	sc = scp->sc;
501 	hr = NULL;
502 
503 #if 0
504 	device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed);
505 #endif
506 
507 	if (hdspe_running(sc) == 1)
508 		goto end;
509 
510 	/* First look for equal frequency. */
511 	for (i = 0; rate_map[i].speed != 0; i++) {
512 		if (rate_map[i].speed == speed)
513 			hr = &rate_map[i];
514 	}
515 
516 	/* If no match, just find nearest. */
517 	if (hr == NULL) {
518 		for (i = 0; rate_map[i].speed != 0; i++) {
519 			hr = &rate_map[i];
520 			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
521 			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
522 			if (speed < threshold)
523 				break;
524 		}
525 	}
526 
527 	switch (sc->type) {
528 	case RAYDAT:
529 	case AIO:
530 		period = HDSPE_FREQ_AIO;
531 		break;
532 	default:
533 		/* Unsupported card. */
534 		goto end;
535 	}
536 
537 	/* Write frequency on the device. */
538 	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
539 	sc->ctrl_register |= hr->reg;
540 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
541 
542 	speed = hr->speed;
543 	if (speed > 96000)
544 		speed /= 4;
545 	else if (speed > 48000)
546 		speed /= 2;
547 
548 	/* Set DDS value. */
549 	period /= speed;
550 	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
551 
552 	sc->speed = hr->speed;
553 end:
554 
555 	return (sc->speed);
556 }
557 
558 static uint32_t
559 hdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
560 {
561 	struct hdspe_latency *hl;
562 	struct sc_pcminfo *scp;
563 	struct sc_chinfo *ch;
564 	struct sc_info *sc;
565 	int threshold;
566 	int i;
567 
568 	ch = data;
569 	scp = ch->parent;
570 	sc = scp->sc;
571 	hl = NULL;
572 
573 #if 0
574 	device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize);
575 #endif
576 
577 	if (hdspe_running(sc) == 1)
578 		goto end;
579 
580 	if (blocksize > HDSPE_LAT_BYTES_MAX)
581 		blocksize = HDSPE_LAT_BYTES_MAX;
582 	else if (blocksize < HDSPE_LAT_BYTES_MIN)
583 		blocksize = HDSPE_LAT_BYTES_MIN;
584 
585 	blocksize /= 4 /* samples */;
586 
587 	/* First look for equal latency. */
588 	for (i = 0; latency_map[i].period != 0; i++) {
589 		if (latency_map[i].period == blocksize) {
590 			hl = &latency_map[i];
591 		}
592 	}
593 
594 	/* If no match, just find nearest. */
595 	if (hl == NULL) {
596 		for (i = 0; latency_map[i].period != 0; i++) {
597 			hl = &latency_map[i];
598 			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
599 			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
600 			if (blocksize < threshold)
601 				break;
602 		}
603 	}
604 
605 	snd_mtxlock(sc->lock);
606 	sc->ctrl_register &= ~HDSPE_LAT_MASK;
607 	sc->ctrl_register |= hdspe_encode_latency(hl->n);
608 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
609 	sc->period = hl->period;
610 	snd_mtxunlock(sc->lock);
611 
612 #if 0
613 	device_printf(scp->dev, "New period=%d\n", sc->period);
614 #endif
615 
616 	sndbuf_resize(ch->buffer,
617 	    (HDSPE_CHANBUF_SIZE * AFMT_CHANNEL(ch->format)) / (sc->period * 4),
618 	    (sc->period * 4));
619 end:
620 
621 	return (sndbuf_getblksz(ch->buffer));
622 }
623 
624 static uint32_t hdspe_rfmt[] = {
625 	SND_FORMAT(AFMT_S32_LE, 2, 0),
626 	0
627 };
628 
629 static struct pcmchan_caps hdspe_rcaps = {32000, 192000, hdspe_rfmt, 0};
630 
631 static uint32_t hdspe_pfmt[] = {
632 	SND_FORMAT(AFMT_S32_LE, 1, 0),
633 	SND_FORMAT(AFMT_S32_LE, 2, 0),
634 	0
635 };
636 
637 static struct pcmchan_caps hdspe_pcaps = {32000, 192000, hdspe_pfmt, 0};
638 
639 static struct pcmchan_caps *
640 hdspechan_getcaps(kobj_t obj, void *data)
641 {
642 	struct sc_chinfo *ch;
643 
644 	ch = data;
645 
646 #if 0
647 	struct sc_pcminfo *scl = ch->parent;
648 	device_printf(scp->dev, "hdspechan_getcaps()\n");
649 #endif
650 
651 	return ((ch->dir == PCMDIR_PLAY) ?
652 	    &hdspe_pcaps : &hdspe_rcaps);
653 }
654 
655 static kobj_method_t hdspechan_methods[] = {
656 	KOBJMETHOD(channel_init,         hdspechan_init),
657 	KOBJMETHOD(channel_free,         hdspechan_free),
658 	KOBJMETHOD(channel_setformat,    hdspechan_setformat),
659 	KOBJMETHOD(channel_setspeed,     hdspechan_setspeed),
660 	KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize),
661 	KOBJMETHOD(channel_trigger,      hdspechan_trigger),
662 	KOBJMETHOD(channel_getptr,       hdspechan_getptr),
663 	KOBJMETHOD(channel_getcaps,      hdspechan_getcaps),
664 	KOBJMETHOD_END
665 };
666 CHANNEL_DECLARE(hdspechan);
667 
668 static int
669 hdspe_pcm_probe(device_t dev)
670 {
671 
672 #if 0
673 	device_printf(dev,"hdspe_pcm_probe()\n");
674 #endif
675 
676 	return (0);
677 }
678 
679 static uint32_t
680 hdspe_pcm_intr(struct sc_pcminfo *scp)
681 {
682 	struct sc_chinfo *ch;
683 	struct sc_info *sc;
684 	int i;
685 
686 	sc = scp->sc;
687 
688 	for (i = 0; i < scp->chnum; i++) {
689 		ch = &scp->chan[i];
690 		snd_mtxunlock(sc->lock);
691 		chn_intr(ch->channel);
692 		snd_mtxlock(sc->lock);
693 	}
694 
695 	return (0);
696 }
697 
698 static int
699 hdspe_pcm_attach(device_t dev)
700 {
701 	char status[SND_STATUSLEN];
702 	struct sc_pcminfo *scp;
703 	char desc[64];
704 	int i, err;
705 
706 	scp = device_get_ivars(dev);
707 	scp->ih = &hdspe_pcm_intr;
708 
709 	bzero(desc, sizeof(desc));
710 	snprintf(desc, sizeof(desc), "HDSPe AIO [%s]", scp->hc->descr);
711 	device_set_desc_copy(dev, desc);
712 
713 	/*
714 	 * We don't register interrupt handler with snd_setup_intr
715 	 * in pcm device. Mark pcm device as MPSAFE manually.
716 	 */
717 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
718 
719 	err = pcm_register(dev, scp, scp->hc->play, scp->hc->rec);
720 	if (err) {
721 		device_printf(dev, "Can't register pcm.\n");
722 		return (ENXIO);
723 	}
724 
725 	scp->chnum = 0;
726 	for (i = 0; i < scp->hc->play; i++) {
727 		pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp);
728 		scp->chnum++;
729 	}
730 
731 	for (i = 0; i < scp->hc->rec; i++) {
732 		pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp);
733 		scp->chnum++;
734 	}
735 
736 	snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
737 	    rman_get_start(scp->sc->cs),
738 	    rman_get_start(scp->sc->irq),
739 	    PCM_KLDSTRING(snd_hdspe));
740 	pcm_setstatus(dev, status);
741 
742 	mixer_init(dev, &hdspemixer_class, scp);
743 
744 	return (0);
745 }
746 
747 static int
748 hdspe_pcm_detach(device_t dev)
749 {
750 	int err;
751 
752 	err = pcm_unregister(dev);
753 	if (err) {
754 		device_printf(dev, "Can't unregister device.\n");
755 		return (err);
756 	}
757 
758 	return (0);
759 }
760 
761 static device_method_t hdspe_pcm_methods[] = {
762 	DEVMETHOD(device_probe,     hdspe_pcm_probe),
763 	DEVMETHOD(device_attach,    hdspe_pcm_attach),
764 	DEVMETHOD(device_detach,    hdspe_pcm_detach),
765 	{ 0, 0 }
766 };
767 
768 static driver_t hdspe_pcm_driver = {
769 	"pcm",
770 	hdspe_pcm_methods,
771 	PCM_SOFTC_SIZE,
772 };
773 
774 DRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, pcm_devclass, 0, 0);
775 MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
776 MODULE_VERSION(snd_hdspe, 1);
777