1 /* $NetBSD: dl.c,v 1.48 2014/03/16 05:20:29 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * Copyright (c) 1997 Ben Harris. All rights reserved. 34 * Copyright (c) 1982, 1986, 1990, 1992, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * Ralph Campbell and Rick Macklem. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65 /* 66 * Copyright (c) 1996 Ken C. Wellsch. All rights reserved. 67 * 68 * This code is derived from software contributed to Berkeley by 69 * Ralph Campbell and Rick Macklem. 70 * 71 * Redistribution and use in source and binary forms, with or without 72 * modification, are permitted provided that the following conditions 73 * are met: 74 * 1. Redistributions of source code must retain the above copyright 75 * notice, this list of conditions and the following disclaimer. 76 * 2. Redistributions in binary form must reproduce the above copyright 77 * notice, this list of conditions and the following disclaimer in the 78 * documentation and/or other materials provided with the distribution. 79 * 3. All advertising materials mentioning features or use of this software 80 * must display the following acknowledgement: 81 * This product includes software developed by the University of 82 * California, Berkeley and its contributors. 83 * 4. Neither the name of the University nor the names of its contributors 84 * may be used to endorse or promote products derived from this software 85 * without specific prior written permission. 86 * 87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 97 * SUCH DAMAGE. 98 */ 99 100 /* 101 * dl.c -- Device driver for the DL11 and DLV11 serial cards. 102 * 103 * OS-interface code derived from the dz and dca (hp300) drivers. 104 */ 105 106 #include <sys/cdefs.h> 107 __KERNEL_RCSID(0, "$NetBSD: dl.c,v 1.48 2014/03/16 05:20:29 dholland Exp $"); 108 109 #include <sys/param.h> 110 #include <sys/systm.h> 111 #include <sys/ioctl.h> 112 #include <sys/tty.h> 113 #include <sys/proc.h> 114 #include <sys/buf.h> 115 #include <sys/conf.h> 116 #include <sys/file.h> 117 #include <sys/uio.h> 118 #include <sys/kernel.h> 119 #include <sys/syslog.h> 120 #include <sys/device.h> 121 #include <sys/kauth.h> 122 123 #include <sys/bus.h> 124 125 #include <dev/qbus/ubavar.h> 126 127 #include <dev/qbus/dlreg.h> 128 129 #include "ioconf.h" 130 131 struct dl_softc { 132 device_t sc_dev; 133 struct evcnt sc_rintrcnt; 134 struct evcnt sc_tintrcnt; 135 bus_space_tag_t sc_iot; 136 bus_space_handle_t sc_ioh; 137 struct tty *sc_tty; 138 }; 139 140 static int dl_match (device_t, cfdata_t, void *); 141 static void dl_attach (device_t, device_t, void *); 142 static void dlrint (void *); 143 static void dlxint (void *); 144 static void dlstart (struct tty *); 145 static int dlparam (struct tty *, struct termios *); 146 static void dlbrk (struct dl_softc *, int); 147 148 CFATTACH_DECL_NEW(dl, sizeof(struct dl_softc), 149 dl_match, dl_attach, NULL, NULL); 150 151 dev_type_open(dlopen); 152 dev_type_close(dlclose); 153 dev_type_read(dlread); 154 dev_type_write(dlwrite); 155 dev_type_ioctl(dlioctl); 156 dev_type_stop(dlstop); 157 dev_type_tty(dltty); 158 dev_type_poll(dlpoll); 159 160 const struct cdevsw dl_cdevsw = { 161 .d_open = dlopen, 162 .d_close = dlclose, 163 .d_read = dlread, 164 .d_write = dlwrite, 165 .d_ioctl = dlioctl, 166 .d_stop = dlstop, 167 .d_tty = dltty, 168 .d_poll = dlpoll, 169 .d_mmap = nommap, 170 .d_kqfilter = ttykqfilter, 171 .d_flag = D_TTY 172 }; 173 174 #define DL_READ_WORD(reg) \ 175 bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg) 176 #define DL_WRITE_WORD(reg, val) \ 177 bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val) 178 #define DL_WRITE_BYTE(reg, val) \ 179 bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val) 180 181 /* Autoconfig handles: setup the controller to interrupt, */ 182 /* then complete the housecleaning for full operation */ 183 184 static int 185 dl_match (device_t parent, cfdata_t cf, void *aux) 186 { 187 struct uba_attach_args *ua = aux; 188 189 #ifdef DL_DEBUG 190 printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr); 191 #endif 192 193 bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE); 194 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) != 195 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) { 196 #ifdef DL_DEBUG 197 printf("failed (step 1; XCSR = %.4b)\n", 198 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR), 199 DL_XCSR_BITS); 200 #endif 201 return 0; 202 } 203 204 /* 205 * We have to force an interrupt so the uba driver can work 206 * out where we are. Unfortunately, the only way to make a 207 * DL11 interrupt is to get it to send or receive a 208 * character. We'll send a NUL and hope it doesn't hurt 209 * anything. 210 */ 211 212 bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0'); 213 #if 0 /* This test seems to fail 2/3 of the time :-( */ 214 if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) { 215 #ifdef DL_DEBUG 216 printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr, 217 DL_XCSR_BITS); 218 #endif 219 return 0; 220 } 221 #endif 222 DELAY(100000); /* delay 1/10 s for character to transmit */ 223 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) != 224 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) { 225 #ifdef DL_DEBUG 226 printf("failed (step 3; XCSR = %.4b)\n", 227 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR), 228 DL_XCSR_BITS); 229 #endif 230 return 0; 231 } 232 233 234 /* What else do I need to do? */ 235 236 return 1; 237 238 } 239 240 static void 241 dl_attach (device_t parent, device_t self, void *aux) 242 { 243 struct dl_softc *sc = device_private(self); 244 struct uba_attach_args *ua = aux; 245 246 sc->sc_dev = self; 247 sc->sc_iot = ua->ua_iot; 248 sc->sc_ioh = ua->ua_ioh; 249 250 /* Tidy up the device */ 251 252 DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE); 253 DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE); 254 255 /* Initialize our softc structure. Should be done in open? */ 256 257 sc->sc_tty = tty_alloc(); 258 tty_attach(sc->sc_tty); 259 260 /* Now register the TX & RX interrupt handlers */ 261 uba_intr_establish(ua->ua_icookie, ua->ua_cvec, 262 dlxint, sc, &sc->sc_tintrcnt); 263 uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4, 264 dlrint, sc, &sc->sc_rintrcnt); 265 evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt, 266 device_xname(sc->sc_dev), "rintr"); 267 evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt, 268 device_xname(sc->sc_dev), "tintr"); 269 270 printf("\n"); 271 } 272 273 /* Receiver Interrupt Handler */ 274 275 static void 276 dlrint(void *arg) 277 { 278 struct dl_softc *sc = arg; 279 280 if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) { 281 struct tty *tp = sc->sc_tty; 282 unsigned c; 283 int cc; 284 285 c = DL_READ_WORD(DL_UBA_RBUF); 286 cc = c & 0xFF; 287 288 if (!(tp->t_state & TS_ISOPEN)) { 289 cv_broadcast(&tp->t_rawcv); 290 return; 291 } 292 293 if (c & DL_RBUF_OVERRUN_ERR) { 294 /* 295 * XXX: This should really be logged somwhere 296 * else where we can afford the time. 297 */ 298 log(LOG_WARNING, "%s: rx overrun\n", 299 device_xname(sc->sc_dev)); 300 } 301 if (c & DL_RBUF_FRAMING_ERR) 302 cc |= TTY_FE; 303 if (c & DL_RBUF_PARITY_ERR) 304 cc |= TTY_PE; 305 306 (*tp->t_linesw->l_rint)(cc, tp); 307 #if defined(DIAGNOSTIC) 308 } else { 309 log(LOG_WARNING, "%s: stray rx interrupt\n", 310 device_xname(sc->sc_dev)); 311 #endif 312 } 313 } 314 315 /* Transmitter Interrupt Handler */ 316 317 static void 318 dlxint(void *arg) 319 { 320 struct dl_softc *sc = arg; 321 struct tty *tp = sc->sc_tty; 322 323 tp->t_state &= ~(TS_BUSY | TS_FLUSH); 324 (*tp->t_linesw->l_start)(tp); 325 326 return; 327 } 328 329 int 330 dlopen(dev_t dev, int flag, int mode, struct lwp *l) 331 { 332 struct tty *tp; 333 struct dl_softc *sc; 334 int unit; 335 336 unit = minor(dev); 337 338 sc = device_lookup_private(&dl_cd, unit); 339 if (!sc) 340 return ENXIO; 341 342 tp = sc->sc_tty; 343 if (tp == NULL) 344 return ENODEV; 345 tp->t_oproc = dlstart; 346 tp->t_param = dlparam; 347 tp->t_dev = dev; 348 349 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 350 return (EBUSY); 351 352 if (!(tp->t_state & TS_ISOPEN)) { 353 ttychars(tp); 354 tp->t_iflag = TTYDEF_IFLAG; 355 tp->t_oflag = TTYDEF_OFLAG; 356 /* No modem control, so set CLOCAL. */ 357 tp->t_cflag = TTYDEF_CFLAG | CLOCAL; 358 tp->t_lflag = TTYDEF_LFLAG; 359 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 360 361 dlparam(tp, &tp->t_termios); 362 ttsetwater(tp); 363 364 } 365 366 return ((*tp->t_linesw->l_open)(dev, tp)); 367 } 368 369 /*ARGSUSED*/ 370 int 371 dlclose(dev_t dev, int flag, int mode, struct lwp *l) 372 { 373 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 374 struct tty *tp = sc->sc_tty; 375 376 (*tp->t_linesw->l_close)(tp, flag); 377 378 /* Make sure a BREAK state is not left enabled. */ 379 dlbrk(sc, 0); 380 381 return (ttyclose(tp)); 382 } 383 384 int 385 dlread(dev_t dev, struct uio *uio, int flag) 386 { 387 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 388 struct tty *tp = sc->sc_tty; 389 390 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 391 } 392 393 int 394 dlwrite(dev_t dev, struct uio *uio, int flag) 395 { 396 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 397 struct tty *tp = sc->sc_tty; 398 399 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 400 } 401 402 int 403 dlpoll(dev_t dev, int events, struct lwp *l) 404 { 405 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 406 struct tty *tp = sc->sc_tty; 407 408 return ((*tp->t_linesw->l_poll)(tp, events, l)); 409 } 410 411 int 412 dlioctl(dev_t dev, unsigned long cmd, void *data, int flag, struct lwp *l) 413 { 414 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 415 struct tty *tp = sc->sc_tty; 416 int error; 417 418 419 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 420 if (error != EPASSTHROUGH) 421 return (error); 422 423 error = ttioctl(tp, cmd, data, flag, l); 424 if (error != EPASSTHROUGH) 425 return (error); 426 427 switch (cmd) { 428 429 case TIOCSBRK: 430 dlbrk(sc, 1); 431 break; 432 433 case TIOCCBRK: 434 dlbrk(sc, 0); 435 break; 436 437 case TIOCMGET: 438 /* No modem control, assume they're all low. */ 439 *(int *)data = 0; 440 break; 441 442 default: 443 return (EPASSTHROUGH); 444 } 445 return (0); 446 } 447 448 struct tty * 449 dltty(dev_t dev) 450 { 451 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev)); 452 453 return sc->sc_tty; 454 } 455 456 void 457 dlstop(struct tty *tp, int flag) 458 { 459 int s = spltty(); 460 461 if ((tp->t_state & (TS_BUSY|TS_TTSTOP)) == TS_BUSY) 462 tp->t_state |= TS_FLUSH; 463 splx(s); 464 } 465 466 static void 467 dlstart(struct tty *tp) 468 { 469 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(tp->t_dev)); 470 int s = spltty(); 471 472 if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) 473 goto out; 474 if (!ttypull(tp)) 475 goto out; 476 if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) { 477 tp->t_state |= TS_BUSY; 478 DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq)); 479 } 480 out: 481 splx(s); 482 return; 483 } 484 485 /*ARGSUSED*/ 486 static int 487 dlparam(struct tty *tp, struct termios *t) 488 { 489 /* 490 * All this kind of stuff (speed, character format, whatever) 491 * is set by jumpers on the card. Changing it is thus rather 492 * tricky for a mere device driver. 493 */ 494 return 0; 495 } 496 497 static void 498 dlbrk(struct dl_softc *sc, int state) 499 { 500 int s = spltty(); 501 502 if (state) { 503 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) | 504 DL_XCSR_TX_BREAK); 505 } else { 506 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) & 507 ~DL_XCSR_TX_BREAK); 508 } 509 splx(s); 510 } 511