xref: /netbsd/sys/arch/hpcmips/dev/ucbsnd.c (revision bf9ec67e)
1 /*	$NetBSD: ucbsnd.c,v 1.8 2002/01/29 18:53:13 uch Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
41  *	Audio codec part.
42  *
43  * /dev/ucbsnd0 : sampling rate 22.154kHz monoral 16bit straight PCM device.
44  */
45 
46 #include "opt_use_poll.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/malloc.h>
52 #include <sys/device.h>
53 #include <sys/proc.h>
54 #include <sys/endian.h>
55 
56 #include <mips/cache.h>
57 
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60 
61 #include <hpcmips/tx/tx39var.h>
62 #include <hpcmips/tx/tx39sibvar.h>
63 #include <hpcmips/tx/tx39sibreg.h>
64 #include <hpcmips/tx/tx39icureg.h>
65 #include <hpcmips/tx/txsnd.h>
66 
67 #include <hpcmips/dev/ucb1200var.h>
68 #include <hpcmips/dev/ucb1200reg.h>
69 
70 #define AUDIOUNIT(x)		(minor(x)&0x0f)
71 #define AUDIODEV(x)		(minor(x)&0xf0)
72 #define	splaudio	splbio	/* XXX */
73 
74 #ifdef UCBSNDDEBUG
75 int	ucbsnd_debug = 1;
76 #define	DPRINTF(arg) if (ucbsnd_debug) printf arg;
77 #define	DPRINTFN(n, arg) if (ucbsnd_debug > (n)) printf arg;
78 #else
79 #define	DPRINTF(arg)
80 #define DPRINTFN(n, arg)
81 #endif
82 
83 #define UCBSND_BUFBLOCK		5
84 /*
85  * XXX temporary DMA buffer
86  */
87 static u_int8_t dmabuf_static[TX39_SIBDMA_SIZE * UCBSND_BUFBLOCK] __attribute__((__aligned__(16))); /* XXX */
88 static size_t	dmabufcnt_static[UCBSND_BUFBLOCK]; /* XXX */
89 
90 enum ucbsnd_state {
91 /* 0 */	UCBSND_IDLE,
92 /* 1 */	UCBSND_INIT,
93 /* 2 */ UCBSND_ENABLE_SAMPLERATE,
94 /* 3 */ UCBSND_ENABLE_OUTPUTPATH,
95 /* 4 */ UCBSND_ENABLE_SETVOLUME,
96 /* 5 */ UCBSND_ENABLE_SPEAKER0,
97 /* 6 */ UCBSND_ENABLE_SPEAKER1,
98 /* 7 */ UCBSND_TRANSITION_PIO,
99 /* 8 */ UCBSND_PIO,
100 /* 9 */ UCBSND_TRANSITION_DISABLE,
101 /*10 */ UCBSND_DISABLE_OUTPUTPATH,
102 /*11 */ UCBSND_DISABLE_SPEAKER0,
103 /*12 */ UCBSND_DISABLE_SPEAKER1,
104 /*13 */	UCBSND_DISABLE_SIB,
105 /*14 */ UCBSND_DMASTART,
106 /*15 */ UCBSND_DMAEND,
107 };
108 
109 struct ring_buf {
110 	u_int32_t rb_buf;	/* buffer start address */
111 	size_t	*rb_bufcnt;	/* effective data count (max rb_blksize)*/
112 
113 	size_t	rb_bufsize;	/* total amount of buffer */
114 	int	rb_blksize;	/* DMA block size */
115 	int	rb_maxblks;	/* # of blocks in ring */
116 
117 	int	rb_inp;		/* start of input (to buffer) */
118 	int	rb_outp;	/* output pointer */
119 };
120 
121 struct ucbsnd_softc {
122 	struct device		sc_dev;
123 	struct device		*sc_sib; /* parent (TX39 SIB module) */
124 	struct device		*sc_ucb; /* parent (UCB1200 module) */
125 	tx_chipset_tag_t	sc_tc;
126 
127 	struct	tx_sound_tag	sc_tag;
128 	int			sc_mute;
129 
130 	/*
131 	 *  audio codec state machine
132 	 */
133 	int		sa_transfer_mode;
134 #define UCBSND_TRANSFERMODE_DMA		0
135 #define UCBSND_TRANSFERMODE_PIO		1
136 	enum ucbsnd_state sa_state;
137 	int		sa_snd_attenuation;
138 #define UCBSND_DEFAULT_ATTENUATION	0	/* Full volume */
139 	int		sa_snd_rate; /* passed down from SIB module */
140 	int		sa_tel_rate;
141 	void*		sa_sf0ih;
142 	void*		sa_sndih;
143 	int		sa_retry;
144 	int		sa_cnt; /* misc counter */
145 
146 	/*
147 	 *  input buffer
148 	 */
149 	size_t		sa_dmacnt;
150 	struct ring_buf sc_rb;
151 };
152 
153 cdev_decl(ucbsnd);
154 
155 int	ucbsnd_match(struct device*, struct cfdata*, void*);
156 void	ucbsnd_attach(struct device*, struct device*, void*);
157 
158 int	ucbsnd_exec_output(void*);
159 int	ucbsnd_busy(void*);
160 
161 void	ucbsnd_sound_init(struct ucbsnd_softc*);
162 void	__ucbsnd_sound_click(tx_sound_tag_t);
163 void	__ucbsnd_sound_mute(tx_sound_tag_t, int);
164 
165 int	ucbsndwrite_subr(struct ucbsnd_softc *, u_int32_t *, size_t,
166 	    struct uio *);
167 
168 int	ringbuf_allocate(struct ring_buf*, size_t, int);
169 void	ringbuf_deallocate(struct ring_buf*);
170 void	ringbuf_reset(struct ring_buf*);
171 int	ringbuf_full(struct ring_buf*);
172 void	*ringbuf_producer_get(struct ring_buf*);
173 void	ringbuf_producer_return(struct ring_buf*, size_t);
174 void	*ringbuf_consumer_get(struct ring_buf*, size_t*);
175 void	ringbuf_consumer_return(struct ring_buf*);
176 
177 struct cfattach ucbsnd_ca = {
178 	sizeof(struct ucbsnd_softc), ucbsnd_match, ucbsnd_attach
179 };
180 
181 int
182 ucbsnd_match(struct device *parent, struct cfdata *cf, void *aux)
183 {
184 
185 	return (1);
186 }
187 
188 void
189 ucbsnd_attach(struct device *parent, struct device *self, void *aux)
190 {
191 	struct ucb1200_attach_args *ucba = aux;
192 	struct ucbsnd_softc *sc = (void*)self;
193 	tx_chipset_tag_t tc;
194 
195 	tc = sc->sc_tc = ucba->ucba_tc;
196 	sc->sc_sib = ucba->ucba_sib;
197 	sc->sc_ucb = ucba->ucba_ucb;
198 
199 	/* register sound functions */
200 	ucbsnd_sound_init(sc);
201 
202 	sc->sa_snd_rate = ucba->ucba_snd_rate;
203 	sc->sa_tel_rate = ucba->ucba_tel_rate;
204 
205 	sc->sa_snd_attenuation = UCBSND_DEFAULT_ATTENUATION;
206 #define KHZ(a) ((a) / 1000), (((a) % 1000))
207 	printf(": audio %d.%03d kHz telecom %d.%03d kHz",
208 	    KHZ((tx39sib_clock(sc->sc_sib) * 2) /
209 		(sc->sa_snd_rate * 64)),
210 	    KHZ((tx39sib_clock(sc->sc_sib) * 2) /
211 		(sc->sa_tel_rate * 64)));
212 
213 	ucb1200_state_install(parent, ucbsnd_busy, self,
214 	    UCB1200_SND_MODULE);
215 
216 	ringbuf_allocate(&sc->sc_rb, TX39_SIBDMA_SIZE, UCBSND_BUFBLOCK);
217 
218 	printf("\n");
219 }
220 
221 int
222 ucbsnd_busy(void *arg)
223 {
224 	struct ucbsnd_softc *sc = arg;
225 
226 	return (sc->sa_state != UCBSND_IDLE);
227 }
228 
229 int
230 ucbsnd_exec_output(void *arg)
231 {
232 	struct ucbsnd_softc *sc = arg;
233 	tx_chipset_tag_t tc = sc->sc_tc;
234 	txreg_t reg;
235 	u_int32_t *buf;
236 	size_t bufcnt;
237 
238 	switch (sc->sa_state) {
239 	default:
240 		panic("ucbsnd_exec_output: invalid state %d", sc->sa_state);
241 		/* NOTREACHED */
242 		break;
243 
244 	case UCBSND_IDLE:
245 		/* nothing to do */
246 		return (0);
247 
248 	case UCBSND_INIT:
249 		sc->sa_sf0ih = tx_intr_establish(
250 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
251 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
252 
253 		sc->sa_state = UCBSND_ENABLE_SAMPLERATE;
254 		return (0);
255 
256 	case UCBSND_ENABLE_SAMPLERATE:
257 		/* Enable UCB1200 side sample rate */
258 		reg = TX39_SIBSF0_WRITE;
259 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLA_REG);
260 		reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_rate);
261 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
262 
263 		sc->sa_state = UCBSND_ENABLE_OUTPUTPATH;
264 		return (0);
265 
266 	case UCBSND_ENABLE_OUTPUTPATH:
267 		/* Enable UCB1200 side */
268 		reg = TX39_SIBSF0_WRITE;
269 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
270 		reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_attenuation |
271 		    UCB1200_AUDIOCTRLB_OUTEN);
272 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
273 
274 		/* Enable SIB side */
275 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
276 		tx_conf_write(tc, TX39_SIBCTRL_REG,
277 		    reg | TX39_SIBCTRL_ENSND);
278 
279 		sc->sa_state = UCBSND_ENABLE_SPEAKER0;
280 		sc->sa_retry = 10;
281 		return (0);
282 	case UCBSND_ENABLE_SPEAKER0:
283 		/* Speaker on */
284 
285 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
286 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
287 
288 		sc->sa_state = UCBSND_ENABLE_SPEAKER1;
289 		return (0);
290 
291 	case UCBSND_ENABLE_SPEAKER1:
292 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
293 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
294 		    --sc->sa_retry > 0) {
295 
296 			sc->sa_state = UCBSND_ENABLE_SPEAKER0;
297 			return (0);
298 		}
299 
300 		if (sc->sa_retry <= 0) {
301 			printf("ucbsnd_exec_output: subframe0 busy\n");
302 
303 			sc->sa_state = UCBSND_IDLE;
304 			return (0);
305 		}
306 
307 		reg |= TX39_SIBSF0_WRITE;
308 		reg |= UCB1200_IO_DATA_SPEAKER;
309 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
310 
311 		/*
312 		 * Begin to transfer.
313 		 */
314 		switch (sc->sa_transfer_mode) {
315 		case UCBSND_TRANSFERMODE_DMA:
316 			sc->sa_state = UCBSND_DMASTART;
317 			sc->sa_dmacnt = 0;
318 			break;
319 		case UCBSND_TRANSFERMODE_PIO:
320 			sc->sa_state = UCBSND_TRANSITION_PIO;
321 			break;
322 		}
323 
324 		return (0);
325 	case UCBSND_DMASTART:
326 		/* get data */
327 		if (sc->sa_dmacnt) /* return previous buffer */
328 			ringbuf_consumer_return(&sc->sc_rb);
329 		buf = ringbuf_consumer_get(&sc->sc_rb, &bufcnt);
330 		if (buf == 0) {
331 			sc->sa_state = UCBSND_DMAEND;
332 			return (0);
333 		}
334 
335 		if (sc->sa_dmacnt == 0) {
336 			/* change interrupt source */
337 			if (sc->sa_sf0ih) {
338 				tx_intr_disestablish(tc, sc->sa_sf0ih);
339 				sc->sa_sf0ih = 0;
340 			}
341 			sc->sa_sndih = tx_intr_establish(
342 				tc, MAKEINTR(1, TX39_INTRSTATUS1_SND1_0INT),
343 				IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
344 		} else {
345 			wakeup(&sc->sc_rb);
346 		}
347 
348 		/* set DMA buffer address */
349 		tx_conf_write(tc, TX39_SIBSNDTXSTART_REG,
350 		    MIPS_KSEG0_TO_PHYS(buf));
351 
352 		/* set DMA buffer size */
353 		tx_conf_write(tc, TX39_SIBSIZE_REG,
354 		    TX39_SIBSIZE_SNDSIZE_SET(0, bufcnt));
355 
356 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
357 
358 		/* kick DMA */
359 		reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
360 		reg |= TX39_SIBDMACTRL_ENDMATXSND;
361 		tx_conf_write(tc, TX39_SIBDMACTRL_REG, reg);
362 
363 		/* set next */
364 		sc->sa_dmacnt += bufcnt;
365 
366 		break;
367 
368 	case UCBSND_DMAEND:
369 		sc->sa_state = UCBSND_TRANSITION_DISABLE;
370 		break;
371 	case UCBSND_TRANSITION_PIO:
372 		/* change interrupt source */
373 		if (sc->sa_sf0ih) {
374 			tx_intr_disestablish(tc, sc->sa_sf0ih);
375 			sc->sa_sf0ih = 0;
376 		}
377 		sc->sa_sndih = tx_intr_establish(
378 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SNDININT),
379 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
380 
381 		sc->sa_state = UCBSND_PIO;
382 		sc->sa_cnt = 0;
383 		return (0);
384 
385 	case UCBSND_PIO:
386 	{
387 		/* PIO test routine */
388 		int dummy_data = sc->sa_cnt * 3;
389 		tx_conf_write(tc, TX39_SIBSNDHOLD_REG,
390 		    dummy_data << 16 | dummy_data);
391 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
392 		if (sc->sa_cnt++ > 50) {
393 			sc->sa_state = UCBSND_TRANSITION_DISABLE;
394 		}
395 		return (0);
396 	}
397 	case UCBSND_TRANSITION_DISABLE:
398 		/* change interrupt source */
399 		if (sc->sa_sndih) {
400 			tx_intr_disestablish(tc, sc->sa_sndih);
401 			sc->sa_sndih = 0;
402 		}
403 		sc->sa_sf0ih = tx_intr_establish(
404 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
405 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
406 
407 		sc->sa_state = UCBSND_DISABLE_OUTPUTPATH;
408 		return (0);
409 
410 	case UCBSND_DISABLE_OUTPUTPATH:
411 		/* disable codec output path and mute */
412 		reg = TX39_SIBSF0_WRITE;
413 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
414 		reg = TX39_SIBSF0_REGDATA_SET(reg, UCB1200_AUDIOCTRLB_MUTE);
415 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
416 
417 		sc->sa_state = UCBSND_DISABLE_SPEAKER0;
418 		sc->sa_retry = 10;
419 		return (0);
420 
421 	case UCBSND_DISABLE_SPEAKER0:
422 		/* Speaker off */
423 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
424 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
425 
426 		sc->sa_state = UCBSND_DISABLE_SPEAKER1;
427 		return (0);
428 
429 	case UCBSND_DISABLE_SPEAKER1:
430 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
431 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
432 		    --sc->sa_retry > 0) {
433 
434 			sc->sa_state = UCBSND_DISABLE_SPEAKER0;
435 			return (0);
436 		}
437 
438 		if (sc->sa_retry <= 0) {
439 			printf("ucbsnd_exec_output: subframe0 busy\n");
440 
441 			sc->sa_state = UCBSND_IDLE;
442 			return (0);
443 		}
444 
445 		reg |= TX39_SIBSF0_WRITE;
446 		reg &= ~UCB1200_IO_DATA_SPEAKER;
447 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
448 
449 		sc->sa_state = UCBSND_DISABLE_SIB;
450 		return (0);
451 
452 	case UCBSND_DISABLE_SIB:
453 		/* Disable SIB side */
454 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
455 		reg &= ~TX39_SIBCTRL_ENSND;
456 		tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
457 
458 		/* end audio disable sequence */
459 		if (sc->sa_sf0ih) {
460 			tx_intr_disestablish(tc, sc->sa_sf0ih);
461 			sc->sa_sf0ih = 0;
462 		}
463 		sc->sa_state = UCBSND_IDLE;
464 
465 		return (0);
466 	}
467 
468 	return (0);
469 }
470 
471 /*
472  * global sound interface.
473  */
474 void
475 ucbsnd_sound_init(struct ucbsnd_softc *sc)
476 {
477 	tx_sound_tag_t ts = &sc->sc_tag;
478 	tx_chipset_tag_t tc = sc->sc_tc;
479 
480 	ts->ts_v = sc;
481 	ts->ts_click	= __ucbsnd_sound_click;
482 	ts->ts_mute	= __ucbsnd_sound_mute;
483 
484 	tx_conf_register_sound(tc, ts);
485 }
486 
487 void
488 __ucbsnd_sound_click(tx_sound_tag_t arg)
489 {
490 	struct ucbsnd_softc *sc = (void*)arg;
491 
492 	if (!sc->sc_mute && sc->sa_state == UCBSND_IDLE) {
493 		sc->sa_transfer_mode = UCBSND_TRANSFERMODE_PIO;
494 		sc->sa_state = UCBSND_INIT;
495 		ucbsnd_exec_output((void*)sc);
496 	}
497 }
498 
499 void
500 __ucbsnd_sound_mute(tx_sound_tag_t arg, int onoff)
501 {
502 	struct ucbsnd_softc *sc = (void*)arg;
503 
504 	sc->sc_mute = onoff;
505 }
506 
507 /*
508  * device access
509  */
510 extern struct cfdriver ucbsnd_cd;
511 
512 int
513 ucbsndopen(dev_t dev, int flags, int ifmt, struct proc *p)
514 {
515 	int unit = AUDIOUNIT(dev);
516 	struct ucbsnd_softc *sc;
517 	int s;
518 
519 	if (unit >= ucbsnd_cd.cd_ndevs ||
520 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
521 		return (ENXIO);
522 
523 	s = splaudio();
524 	ringbuf_reset(&sc->sc_rb);
525 	splx(s);
526 
527 	return (0);
528 }
529 
530 int
531 ucbsndclose(dev_t dev, int flags, int ifmt, struct proc *p)
532 {
533 	int unit = AUDIOUNIT(dev);
534 	struct ucbsnd_softc *sc;
535 
536 	if (unit >= ucbsnd_cd.cd_ndevs ||
537 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
538 		return (ENXIO);
539 
540 	return (0);
541 }
542 
543 int
544 ucbsndread(dev_t dev, struct uio *uio, int ioflag)
545 {
546 	int unit = AUDIOUNIT(dev);
547 	struct ucbsnd_softc *sc;
548 	int error = 0;
549 
550 	if (unit >= ucbsnd_cd.cd_ndevs ||
551 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
552 		return (ENXIO);
553 	/* not supported yet */
554 
555 	return (error);
556 }
557 
558 int
559 ucbsndwrite_subr(struct ucbsnd_softc *sc, u_int32_t *buf, size_t bufsize,
560     struct uio *uio)
561 {
562 	int i, s, error;
563 
564 	error = uiomove(buf, bufsize, uio);
565 	/*
566 	 * inverse endian for UCB1200
567 	 */
568 	for (i = 0; i < bufsize / sizeof(int); i++)
569 		buf[i] = htobe32(buf[i]);
570 	mips_dcache_wbinv_range((vaddr_t)buf, bufsize);
571 
572 	ringbuf_producer_return(&sc->sc_rb, bufsize);
573 
574 	s = splaudio();
575 	if (sc->sa_state == UCBSND_IDLE && ringbuf_full(&sc->sc_rb)) {
576 		sc->sa_transfer_mode = UCBSND_TRANSFERMODE_DMA;
577 		sc->sa_state = UCBSND_INIT;
578 		ucbsnd_exec_output((void*)sc);
579 	}
580 	splx(s);
581 
582 	return (error);
583 }
584 
585 int
586 ucbsndwrite(dev_t dev, struct uio *uio, int ioflag)
587 {
588 	int unit = AUDIOUNIT(dev);
589 	struct ucbsnd_softc *sc;
590 	int len, error = 0;
591 	int i, n, s, rest;
592 	void *buf;
593 
594 	if (unit >= ucbsnd_cd.cd_ndevs ||
595 	    (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
596 		return (ENXIO);
597 
598 	len = uio->uio_resid;
599 	n = (len + TX39_SIBDMA_SIZE - 1) / TX39_SIBDMA_SIZE;
600 	rest = len % TX39_SIBDMA_SIZE;
601 
602 	if (rest)
603 		--n;
604 
605 	for (i = 0; i < n; i++) {
606 		while (!(buf = ringbuf_producer_get(&sc->sc_rb))) {
607 			error = tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 1000);
608 			if (error)
609 				goto errout;
610 		}
611 
612 		error = ucbsndwrite_subr(sc, buf, TX39_SIBDMA_SIZE, uio);
613 		if (error)
614 			goto out;
615 	}
616 
617 	if (rest) {
618 		while (!(buf = ringbuf_producer_get(&sc->sc_rb))) {
619 			error = tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 1000);
620 			if (error)
621 				goto errout;
622 		}
623 
624 		error = ucbsndwrite_subr(sc, buf, rest, uio);
625 	}
626 
627  out:
628 	return (error);
629  errout:
630 	printf("%s: timeout. reset ring-buffer.\n", sc->sc_dev.dv_xname);
631 	s = splaudio();
632 	ringbuf_reset(&sc->sc_rb);
633 	splx(s);
634 
635 	return (error);
636 }
637 
638 int
639 ucbsndioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
640 {
641 	int error = 0;
642 
643 	/* not coded yet */
644 
645 	return (error);
646 }
647 
648 int
649 ucbsndpoll(dev_t dev, int events, struct proc *p)
650 {
651 	int error = 0;
652 
653 	/* not coded yet */
654 
655 	return (error);
656 }
657 
658 paddr_t
659 ucbsndmmap(dev_t dev, off_t off, int prot)
660 {
661 	int error = 0;
662 
663 	/* not coded yet */
664 
665 	return (error);
666 }
667 
668 /*
669  * Ring buffer.
670  */
671 int
672 ringbuf_allocate(struct ring_buf *rb, size_t blksize, int maxblk)
673 {
674 	rb->rb_bufsize = blksize * maxblk;
675 	rb->rb_blksize = blksize;
676 	rb->rb_maxblks = maxblk;
677 #if notyet
678 	rb->rb_buf = (u_int32_t)malloc(rb->rb_bufsize, M_DEVBUF, M_WAITOK);
679 #else
680 	rb->rb_buf = (u_int32_t)dmabuf_static;
681 #endif
682 	if (rb->rb_buf == 0) {
683 		printf("ringbuf_allocate: can't allocate buffer\n");
684 		return (1);
685 	}
686 	memset((char*)rb->rb_buf, 0, rb->rb_bufsize);
687 #if notyet
688 	rb->rb_bufcnt = malloc(rb->rb_maxblks * sizeof(size_t), M_DEVBUF,
689 	    M_WAITOK);
690 #else
691 	rb->rb_bufcnt = dmabufcnt_static;
692 #endif
693 	if (rb->rb_bufcnt == 0) {
694 		printf("ringbuf_allocate: can't allocate buffer\n");
695 		return (1);
696 	}
697 	memset((char*)rb->rb_bufcnt, 0, rb->rb_maxblks * sizeof(size_t));
698 
699 	ringbuf_reset(rb);
700 
701 	return (0);
702 }
703 
704 void
705 ringbuf_deallocate(struct ring_buf *rb)
706 {
707 #if notyet
708 	free((void*)rb->rb_buf, M_DEVBUF);
709 	free(rb->rb_bufcnt, M_DEVBUF);
710 #endif
711 }
712 
713 void
714 ringbuf_reset(struct ring_buf *rb)
715 {
716 	rb->rb_outp = 0;
717 	rb->rb_inp = 0;
718 }
719 
720 int
721 ringbuf_full(struct ring_buf *rb)
722 {
723 	int ret;
724 
725 	ret = rb->rb_outp == rb->rb_maxblks;
726 
727 	return (ret);
728 }
729 
730 void*
731 ringbuf_producer_get(struct ring_buf *rb)
732 {
733 	u_int32_t ret;
734 	int s;
735 
736 	s = splaudio();
737 	ret = ringbuf_full(rb) ? 0 :
738 	    rb->rb_buf + rb->rb_inp * rb->rb_blksize;
739 	splx(s);
740 
741 	return (void *)ret;
742 }
743 
744 void
745 ringbuf_producer_return(struct ring_buf *rb, size_t cnt)
746 {
747 	int s;
748 
749 	assert(cnt <= rb->rb_blksize);
750 
751 	s = splaudio();
752 	rb->rb_outp++;
753 
754 	rb->rb_bufcnt[rb->rb_inp] = cnt;
755 	rb->rb_inp = (rb->rb_inp + 1) % rb->rb_maxblks;
756 	splx(s);
757 }
758 
759 void*
760 ringbuf_consumer_get(struct ring_buf *rb, size_t *cntp)
761 {
762 	u_int32_t p;
763 	int idx;
764 
765 	if (rb->rb_outp == 0)
766 		return (0);
767 
768 	idx = (rb->rb_inp - rb->rb_outp + rb->rb_maxblks) % rb->rb_maxblks;
769 
770 	p = rb->rb_buf + idx * rb->rb_blksize;
771 	*cntp = rb->rb_bufcnt[idx];
772 
773 	return (void *)p;
774 }
775 
776 void
777 ringbuf_consumer_return(struct ring_buf *rb)
778 {
779 
780 	if (rb->rb_outp > 0)
781 		rb->rb_outp--;
782 }
783