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