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