1 /* $NetBSD: ppi.c,v 1.20 2002/03/15 05:55:36 gmcgarry 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 * 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 * Copyright (c) 1982, 1990, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)ppi.c 8.1 (Berkeley) 6/16/93 72 */ 73 74 /* 75 * Printer/Plotter HPIB interface 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.20 2002/03/15 05:55:36 gmcgarry Exp $"); 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/callout.h> 84 #include <sys/conf.h> 85 #include <sys/device.h> 86 #include <sys/errno.h> 87 #include <sys/malloc.h> 88 #include <sys/proc.h> 89 #include <sys/uio.h> 90 91 #include <hp300/dev/hpibvar.h> 92 93 #include <hp300/dev/ppiioctl.h> 94 95 struct ppi_softc { 96 struct device sc_dev; 97 int sc_flags; 98 struct hpibqueue sc_hq; /* HP-IB job queue entry */ 99 struct ppiparam sc_param; 100 #define sc_burst sc_param.burst 101 #define sc_timo sc_param.timo 102 #define sc_delay sc_param.delay 103 int sc_sec; 104 int sc_slave; /* HP-IB slave address */ 105 struct callout sc_timo_ch; 106 struct callout sc_start_ch; 107 }; 108 109 /* sc_flags values */ 110 #define PPIF_ALIVE 0x01 111 #define PPIF_OPEN 0x02 112 #define PPIF_UIO 0x04 113 #define PPIF_TIMO 0x08 114 #define PPIF_DELAY 0x10 115 116 int ppimatch __P((struct device *, struct cfdata *, void *)); 117 void ppiattach __P((struct device *, struct device *, void *)); 118 119 struct cfattach ppi_ca = { 120 sizeof(struct ppi_softc), ppimatch, ppiattach 121 }; 122 123 extern struct cfdriver ppi_cd; 124 125 void ppistart __P((void *)); 126 void ppinoop __P((void *)); 127 128 void ppitimo __P((void *)); 129 int ppirw __P((dev_t, struct uio *)); 130 int ppihztoms __P((int)); 131 int ppimstohz __P((int)); 132 133 bdev_decl(ppi); 134 cdev_decl(ppi); 135 136 #define UNIT(x) minor(x) 137 138 #ifdef DEBUG 139 int ppidebug = 0x80; 140 #define PDB_FOLLOW 0x01 141 #define PDB_IO 0x02 142 #define PDB_NOCHECK 0x80 143 #endif 144 145 int 146 ppimatch(parent, match, aux) 147 struct device *parent; 148 struct cfdata *match; 149 void *aux; 150 { 151 struct hpibbus_attach_args *ha = aux; 152 153 /* 154 * The printer/plotter doesn't return an ID tag. 155 * The check below prevents us from matching a CS80 156 * device by mistake. 157 */ 158 if (ha->ha_id & 0x200) 159 return (0); 160 161 /* 162 * To prevent matching all unused slots on the bus, we 163 * don't allow wildcarded locators. 164 */ 165 if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT || 166 match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT) 167 return (0); 168 169 return (1); 170 } 171 172 void 173 ppiattach(parent, self, aux) 174 struct device *parent, *self; 175 void *aux; 176 { 177 struct ppi_softc *sc = (struct ppi_softc *)self; 178 struct hpibbus_attach_args *ha = aux; 179 180 printf("\n"); 181 182 sc->sc_slave = ha->ha_slave; 183 184 callout_init(&sc->sc_timo_ch); 185 callout_init(&sc->sc_start_ch); 186 187 /* Initialize the hpib queue entry. */ 188 sc->sc_hq.hq_softc = sc; 189 sc->sc_hq.hq_slave = sc->sc_slave; 190 sc->sc_hq.hq_start = ppistart; 191 sc->sc_hq.hq_go = ppinoop; 192 sc->sc_hq.hq_intr = ppinoop; 193 194 sc->sc_flags = PPIF_ALIVE; 195 } 196 197 void 198 ppinoop(arg) 199 void *arg; 200 { 201 /* Noop! */ 202 } 203 204 int 205 ppiopen(dev, flags, fmt, p) 206 dev_t dev; 207 int flags, fmt; 208 struct proc *p; 209 { 210 int unit = UNIT(dev); 211 struct ppi_softc *sc; 212 213 if (unit >= ppi_cd.cd_ndevs || 214 (sc = ppi_cd.cd_devs[unit]) == NULL || 215 (sc->sc_flags & PPIF_ALIVE) == 0) 216 return (ENXIO); 217 218 #ifdef DEBUG 219 if (ppidebug & PDB_FOLLOW) 220 printf("ppiopen(%x, %x): flags %x\n", 221 dev, flags, sc->sc_flags); 222 #endif 223 if (sc->sc_flags & PPIF_OPEN) 224 return (EBUSY); 225 sc->sc_flags |= PPIF_OPEN; 226 sc->sc_burst = PPI_BURST; 227 sc->sc_timo = ppimstohz(PPI_TIMO); 228 sc->sc_delay = ppimstohz(PPI_DELAY); 229 sc->sc_sec = -1; 230 return(0); 231 } 232 233 int 234 ppiclose(dev, flags, fmt, p) 235 dev_t dev; 236 int flags, fmt; 237 struct proc *p; 238 { 239 int unit = UNIT(dev); 240 struct ppi_softc *sc = ppi_cd.cd_devs[unit]; 241 242 #ifdef DEBUG 243 if (ppidebug & PDB_FOLLOW) 244 printf("ppiclose(%x, %x): flags %x\n", 245 dev, flags, sc->sc_flags); 246 #endif 247 sc->sc_flags &= ~PPIF_OPEN; 248 return(0); 249 } 250 251 void 252 ppistart(arg) 253 void *arg; 254 { 255 struct ppi_softc *sc = arg; 256 257 #ifdef DEBUG 258 if (ppidebug & PDB_FOLLOW) 259 printf("ppistart(%x)\n", sc->sc_dev.dv_unit); 260 #endif 261 sc->sc_flags &= ~PPIF_DELAY; 262 wakeup(sc); 263 } 264 265 void 266 ppitimo(arg) 267 void *arg; 268 { 269 struct ppi_softc *sc = arg; 270 271 #ifdef DEBUG 272 if (ppidebug & PDB_FOLLOW) 273 printf("ppitimo(%x)\n", sc->sc_dev.dv_unit); 274 #endif 275 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO); 276 wakeup(sc); 277 } 278 279 int 280 ppiread(dev, uio, flags) 281 dev_t dev; 282 struct uio *uio; 283 int flags; 284 { 285 286 #ifdef DEBUG 287 if (ppidebug & PDB_FOLLOW) 288 printf("ppiread(%x, %p)\n", dev, uio); 289 #endif 290 return (ppirw(dev, uio)); 291 } 292 293 int 294 ppiwrite(dev, uio, flags) 295 dev_t dev; 296 struct uio *uio; 297 int flags; 298 { 299 300 #ifdef DEBUG 301 if (ppidebug & PDB_FOLLOW) 302 printf("ppiwrite(%x, %p)\n", dev, uio); 303 #endif 304 return (ppirw(dev, uio)); 305 } 306 307 int 308 ppirw(dev, uio) 309 dev_t dev; 310 struct uio *uio; 311 { 312 int unit = UNIT(dev); 313 struct ppi_softc *sc = ppi_cd.cd_devs[unit]; 314 int s, len, cnt; 315 char *cp; 316 int error = 0, gotdata = 0; 317 int buflen, ctlr, slave; 318 char *buf; 319 320 if (uio->uio_resid == 0) 321 return(0); 322 323 ctlr = sc->sc_dev.dv_parent->dv_unit; 324 slave = sc->sc_slave; 325 326 #ifdef DEBUG 327 if (ppidebug & (PDB_FOLLOW|PDB_IO)) 328 printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n", 329 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W', 330 sc->sc_burst, sc->sc_timo, uio->uio_resid); 331 #endif 332 buflen = min(sc->sc_burst, uio->uio_resid); 333 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK); 334 sc->sc_flags |= PPIF_UIO; 335 if (sc->sc_timo > 0) { 336 sc->sc_flags |= PPIF_TIMO; 337 callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc); 338 } 339 len = cnt = 0; 340 while (uio->uio_resid > 0) { 341 len = min(buflen, uio->uio_resid); 342 cp = buf; 343 if (uio->uio_rw == UIO_WRITE) { 344 error = uiomove(cp, len, uio); 345 if (error) 346 break; 347 } 348 again: 349 s = splbio(); 350 if ((sc->sc_flags & PPIF_UIO) && 351 hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq) == 0) 352 (void) tsleep(sc, PRIBIO + 1, "ppirw", 0); 353 /* 354 * Check if we timed out during sleep or uiomove 355 */ 356 (void) spllowersoftclock(); 357 if ((sc->sc_flags & PPIF_UIO) == 0) { 358 #ifdef DEBUG 359 if (ppidebug & PDB_IO) 360 printf("ppirw: uiomove/sleep timo, flags %x\n", 361 sc->sc_flags); 362 #endif 363 if (sc->sc_flags & PPIF_TIMO) { 364 callout_stop(&sc->sc_timo_ch); 365 sc->sc_flags &= ~PPIF_TIMO; 366 } 367 splx(s); 368 break; 369 } 370 splx(s); 371 /* 372 * Perform the operation 373 */ 374 if (uio->uio_rw == UIO_WRITE) 375 cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len); 376 else 377 cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len); 378 s = splbio(); 379 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq); 380 #ifdef DEBUG 381 if (ppidebug & PDB_IO) 382 printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n", 383 uio->uio_rw == UIO_READ ? "recv" : "send", 384 ctlr, slave, sc->sc_sec, cp, len, cnt); 385 #endif 386 splx(s); 387 if (uio->uio_rw == UIO_READ) { 388 if (cnt) { 389 error = uiomove(cp, cnt, uio); 390 if (error) 391 break; 392 gotdata++; 393 } 394 /* 395 * Didn't get anything this time, but did in the past. 396 * Consider us done. 397 */ 398 else if (gotdata) 399 break; 400 } 401 s = splsoftclock(); 402 /* 403 * Operation timeout (or non-blocking), quit now. 404 */ 405 if ((sc->sc_flags & PPIF_UIO) == 0) { 406 #ifdef DEBUG 407 if (ppidebug & PDB_IO) 408 printf("ppirw: timeout/done\n"); 409 #endif 410 splx(s); 411 break; 412 } 413 /* 414 * Implement inter-read delay 415 */ 416 if (sc->sc_delay > 0) { 417 sc->sc_flags |= PPIF_DELAY; 418 callout_reset(&sc->sc_start_ch, sc->sc_delay, 419 ppistart, sc); 420 error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0); 421 if (error) { 422 splx(s); 423 break; 424 } 425 } 426 splx(s); 427 /* 428 * Must not call uiomove again til we've used all data 429 * that we already grabbed. 430 */ 431 if (uio->uio_rw == UIO_WRITE && cnt != len) { 432 cp += cnt; 433 len -= cnt; 434 cnt = 0; 435 goto again; 436 } 437 } 438 s = splsoftclock(); 439 if (sc->sc_flags & PPIF_TIMO) { 440 callout_stop(&sc->sc_timo_ch); 441 sc->sc_flags &= ~PPIF_TIMO; 442 } 443 if (sc->sc_flags & PPIF_DELAY) { 444 callout_stop(&sc->sc_start_ch); 445 sc->sc_flags &= ~PPIF_DELAY; 446 } 447 splx(s); 448 /* 449 * Adjust for those chars that we uiomove'ed but never wrote 450 */ 451 if (uio->uio_rw == UIO_WRITE && cnt != len) { 452 uio->uio_resid += (len - cnt); 453 #ifdef DEBUG 454 if (ppidebug & PDB_IO) 455 printf("ppirw: short write, adjust by %d\n", 456 len-cnt); 457 #endif 458 } 459 free(buf, M_DEVBUF); 460 #ifdef DEBUG 461 if (ppidebug & (PDB_FOLLOW|PDB_IO)) 462 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid); 463 #endif 464 return (error); 465 } 466 467 int 468 ppiioctl(dev, cmd, data, flag, p) 469 dev_t dev; 470 u_long cmd; 471 caddr_t data; 472 int flag; 473 struct proc *p; 474 { 475 struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)]; 476 struct ppiparam *pp, *upp; 477 int error = 0; 478 479 switch (cmd) { 480 case PPIIOCGPARAM: 481 pp = &sc->sc_param; 482 upp = (struct ppiparam *)data; 483 upp->burst = pp->burst; 484 upp->timo = ppihztoms(pp->timo); 485 upp->delay = ppihztoms(pp->delay); 486 break; 487 case PPIIOCSPARAM: 488 pp = &sc->sc_param; 489 upp = (struct ppiparam *)data; 490 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX || 491 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX) 492 return(EINVAL); 493 pp->burst = upp->burst; 494 pp->timo = ppimstohz(upp->timo); 495 pp->delay = ppimstohz(upp->delay); 496 break; 497 case PPIIOCSSEC: 498 sc->sc_sec = *(int *)data; 499 break; 500 default: 501 return(EINVAL); 502 } 503 return (error); 504 } 505 506 int 507 ppihztoms(h) 508 int h; 509 { 510 extern int hz; 511 int m = h; 512 513 if (m > 0) 514 m = m * 1000 / hz; 515 return(m); 516 } 517 518 int 519 ppimstohz(m) 520 int m; 521 { 522 extern int hz; 523 int h = m; 524 525 if (h > 0) { 526 h = h * hz / 1000; 527 if (h == 0) 528 h = 1000 / hz; 529 } 530 return(h); 531 } 532