1 /* $NetBSD: ppi.c,v 1.25 2019/11/12 13:17:44 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 1996-2003 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) 1982, 1990, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)ppi.c 8.1 (Berkeley) 6/16/93 61 */ 62 63 /* 64 * Printer/Plotter GPIB interface 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.25 2019/11/12 13:17:44 msaitoh Exp $"); 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/callout.h> 73 #include <sys/conf.h> 74 #include <sys/device.h> 75 #include <sys/malloc.h> 76 #include <sys/proc.h> 77 #include <sys/uio.h> 78 79 #include <dev/gpib/gpibvar.h> 80 81 #include <dev/gpib/ppiio.h> 82 83 #include "ioconf.h" 84 85 struct ppi_softc { 86 device_t sc_dev; 87 gpib_chipset_tag_t sc_ic; 88 gpib_handle_t sc_hdl; 89 90 int sc_address; /* GPIB address */ 91 int sc_flags; 92 int sc_sec; 93 struct ppiparam sc_param; 94 #define sc_burst sc_param.burst 95 #define sc_timo sc_param.timo 96 #define sc_delay sc_param.delay 97 struct callout sc_timo_ch; 98 struct callout sc_start_ch; 99 }; 100 101 /* sc_flags values */ 102 #define PPIF_ALIVE 0x01 103 #define PPIF_OPEN 0x02 104 #define PPIF_UIO 0x04 105 #define PPIF_TIMO 0x08 106 #define PPIF_DELAY 0x10 107 108 int ppimatch(device_t, cfdata_t, void *); 109 void ppiattach(device_t, device_t, void *); 110 111 CFATTACH_DECL_NEW(ppi, sizeof(struct ppi_softc), 112 ppimatch, ppiattach, NULL, NULL); 113 114 void ppicallback(void *, int); 115 void ppistart(void *); 116 117 void ppitimo(void *); 118 int ppirw(dev_t, struct uio *); 119 int ppihztoms(int); 120 int ppimstohz(int); 121 122 dev_type_open(ppiopen); 123 dev_type_close(ppiclose); 124 dev_type_read(ppiread); 125 dev_type_write(ppiwrite); 126 dev_type_ioctl(ppiioctl); 127 128 const struct cdevsw ppi_cdevsw = { 129 .d_open = ppiopen, 130 .d_close = ppiclose, 131 .d_read = ppiread, 132 .d_write = ppiwrite, 133 .d_ioctl = ppiioctl, 134 .d_stop = nostop, 135 .d_tty = notty, 136 .d_poll = nopoll, 137 .d_mmap = nommap, 138 .d_kqfilter = nokqfilter, 139 .d_discard = nodiscard, 140 .d_flag = D_OTHER 141 }; 142 143 #define UNIT(x) minor(x) 144 145 #ifdef DEBUG 146 int ppidebug = 0x80; 147 #define PDB_FOLLOW 0x01 148 #define PDB_IO 0x02 149 #define PDB_NOCHECK 0x80 150 #define DPRINTF(mask, str) if (ppidebug & (mask)) printf str 151 #else 152 #define DPRINTF(mask, str) /* nothing */ 153 #endif 154 155 int 156 ppimatch(device_t parent, cfdata_t match, void *aux) 157 { 158 159 return (1); 160 } 161 162 void 163 ppiattach(device_t parent, device_t self, void *aux) 164 { 165 struct ppi_softc *sc = device_private(self); 166 struct gpib_attach_args *ga = aux; 167 168 printf("\n"); 169 170 sc->sc_dev = self; 171 sc->sc_ic = ga->ga_ic; 172 sc->sc_address = ga->ga_address; 173 174 callout_init(&sc->sc_timo_ch, 0); 175 callout_init(&sc->sc_start_ch, 0); 176 177 if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc, 178 &sc->sc_hdl)) { 179 aprint_error_dev(sc->sc_dev, "can't register callback\n"); 180 return; 181 } 182 183 sc->sc_flags = PPIF_ALIVE; 184 } 185 186 int 187 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l) 188 { 189 struct ppi_softc *sc; 190 191 sc = device_lookup_private(&ppi_cd, UNIT(dev)); 192 if (sc == NULL) 193 return (ENXIO); 194 195 if ((sc->sc_flags & PPIF_ALIVE) == 0) 196 return (ENXIO); 197 198 DPRINTF(PDB_FOLLOW, ("ppiopen(%" PRIx64 ", %x): flags %x\n", 199 dev, flags, sc->sc_flags)); 200 201 if (sc->sc_flags & PPIF_OPEN) 202 return (EBUSY); 203 sc->sc_flags |= PPIF_OPEN; 204 sc->sc_burst = PPI_BURST; 205 sc->sc_timo = ppimstohz(PPI_TIMO); 206 sc->sc_delay = ppimstohz(PPI_DELAY); 207 sc->sc_sec = -1; 208 return (0); 209 } 210 211 int 212 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l) 213 { 214 struct ppi_softc *sc; 215 216 sc = device_lookup_private(&ppi_cd, UNIT(dev)); 217 218 DPRINTF(PDB_FOLLOW, ("ppiclose(%" PRIx64 ", %x): flags %x\n", 219 dev, flags, sc->sc_flags)); 220 221 sc->sc_flags &= ~PPIF_OPEN; 222 return (0); 223 } 224 225 void 226 ppicallback(void *v, int action) 227 { 228 struct ppi_softc *sc = v; 229 230 DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action)); 231 232 switch (action) { 233 case GPIBCBF_START: 234 ppistart(sc); 235 case GPIBCBF_INTR: 236 /* no-op */ 237 break; 238 #ifdef DEBUG 239 default: 240 DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n", 241 action)); 242 break; 243 #endif 244 } 245 } 246 247 void 248 ppistart(void *v) 249 { 250 struct ppi_softc *sc = v; 251 252 DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", device_unit(sc->sc_dev))); 253 254 sc->sc_flags &= ~PPIF_DELAY; 255 wakeup(sc); 256 } 257 258 void 259 ppitimo(void *arg) 260 { 261 struct ppi_softc *sc = arg; 262 263 DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", device_unit(sc->sc_dev))); 264 265 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO); 266 wakeup(sc); 267 } 268 269 int 270 ppiread(dev_t dev, struct uio *uio, int flags) 271 { 272 273 DPRINTF(PDB_FOLLOW, ("ppiread(%" PRIx64 ", %p)\n", dev, uio)); 274 275 return (ppirw(dev, uio)); 276 } 277 278 int 279 ppiwrite(dev_t dev, struct uio *uio, int flags) 280 { 281 282 DPRINTF(PDB_FOLLOW, ("ppiwrite(%" PRIx64 ", %p)\n", dev, uio)); 283 284 return (ppirw(dev, uio)); 285 } 286 287 int 288 ppirw(dev_t dev, struct uio *uio) 289 { 290 struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev)); 291 int s1, s2, len, cnt; 292 char *cp; 293 int error = 0, gotdata = 0; 294 int buflen, address; 295 char *buf; 296 297 if (uio->uio_resid == 0) 298 return (0); 299 300 address = sc->sc_address; 301 302 DPRINTF(PDB_FOLLOW|PDB_IO, 303 ("ppirw(%" PRIx64 ", %p, %c): burst %d, timo %d, resid %x\n", 304 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W', 305 sc->sc_burst, sc->sc_timo, uio->uio_resid)); 306 307 buflen = uimin(sc->sc_burst, uio->uio_resid); 308 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK); 309 sc->sc_flags |= PPIF_UIO; 310 if (sc->sc_timo > 0) { 311 sc->sc_flags |= PPIF_TIMO; 312 callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc); 313 } 314 len = cnt = 0; 315 while (uio->uio_resid > 0) { 316 len = uimin(buflen, uio->uio_resid); 317 cp = buf; 318 if (uio->uio_rw == UIO_WRITE) { 319 error = uiomove(cp, len, uio); 320 if (error) 321 break; 322 } 323 again: 324 s1 = splsoftclock(); 325 s2 = splbio(); 326 if (sc->sc_flags & PPIF_UIO) { 327 if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0) 328 (void) tsleep(sc, PRIBIO + 1, "ppirw", 0); 329 } 330 /* 331 * Check if we timed out during sleep or uiomove 332 */ 333 splx(s2); 334 if ((sc->sc_flags & PPIF_UIO) == 0) { 335 DPRINTF(PDB_IO, 336 ("ppirw: uiomove/sleep timo, flags %x\n", 337 sc->sc_flags)); 338 if (sc->sc_flags & PPIF_TIMO) { 339 callout_stop(&sc->sc_timo_ch); 340 sc->sc_flags &= ~PPIF_TIMO; 341 } 342 splx(s1); 343 break; 344 } 345 splx(s1); 346 /* 347 * Perform the operation 348 */ 349 if (uio->uio_rw == UIO_WRITE) 350 cnt = gpibsend(sc->sc_ic, address, sc->sc_sec, 351 cp, len); 352 else 353 cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec, 354 cp, len); 355 s1 = splbio(); 356 gpibrelease(sc->sc_ic, sc->sc_hdl); 357 DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n", 358 uio->uio_rw == UIO_READ ? "recv" : "send", 359 address, sc->sc_sec, cp, len, cnt)); 360 splx(s1); 361 if (uio->uio_rw == UIO_READ) { 362 if (cnt) { 363 error = uiomove(cp, cnt, uio); 364 if (error) 365 break; 366 gotdata++; 367 } 368 /* 369 * Didn't get anything this time, but did in the past. 370 * Consider us done. 371 */ 372 else if (gotdata) 373 break; 374 } 375 s1 = splsoftclock(); 376 /* 377 * Operation timeout (or non-blocking), quit now. 378 */ 379 if ((sc->sc_flags & PPIF_UIO) == 0) { 380 DPRINTF(PDB_IO, ("ppirw: timeout/done\n")); 381 splx(s1); 382 break; 383 } 384 /* 385 * Implement inter-read delay 386 */ 387 if (sc->sc_delay > 0) { 388 sc->sc_flags |= PPIF_DELAY; 389 callout_reset(&sc->sc_start_ch, sc->sc_delay, 390 ppistart, sc); 391 error = tsleep(sc, (PCATCH|PZERO) + 1, "gpib", 0); 392 if (error) { 393 splx(s1); 394 break; 395 } 396 } 397 splx(s1); 398 /* 399 * Must not call uiomove again til we've used all data 400 * that we already grabbed. 401 */ 402 if (uio->uio_rw == UIO_WRITE && cnt != len) { 403 cp += cnt; 404 len -= cnt; 405 cnt = 0; 406 goto again; 407 } 408 } 409 s1 = splsoftclock(); 410 if (sc->sc_flags & PPIF_TIMO) { 411 callout_stop(&sc->sc_timo_ch); 412 sc->sc_flags &= ~PPIF_TIMO; 413 } 414 if (sc->sc_flags & PPIF_DELAY) { 415 callout_stop(&sc->sc_start_ch); 416 sc->sc_flags &= ~PPIF_DELAY; 417 } 418 splx(s1); 419 /* 420 * Adjust for those chars that we uiomove'ed but never wrote 421 */ 422 if (uio->uio_rw == UIO_WRITE && cnt != len) { 423 uio->uio_resid += (len - cnt); 424 DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n", 425 len - cnt)); 426 } 427 free(buf, M_DEVBUF); 428 DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n", 429 error, uio->uio_resid)); 430 return (error); 431 } 432 433 int 434 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 435 { 436 struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev)); 437 struct ppiparam *pp, *upp; 438 int error = 0; 439 440 switch (cmd) { 441 case PPIIOCGPARAM: 442 pp = &sc->sc_param; 443 upp = (struct ppiparam *)data; 444 upp->burst = pp->burst; 445 upp->timo = ppihztoms(pp->timo); 446 upp->delay = ppihztoms(pp->delay); 447 break; 448 case PPIIOCSPARAM: 449 pp = &sc->sc_param; 450 upp = (struct ppiparam *)data; 451 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX || 452 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX) 453 return (EINVAL); 454 pp->burst = upp->burst; 455 pp->timo = ppimstohz(upp->timo); 456 pp->delay = ppimstohz(upp->delay); 457 break; 458 case PPIIOCSSEC: 459 sc->sc_sec = *(int *)data; 460 break; 461 default: 462 return (EINVAL); 463 } 464 return (error); 465 } 466 467 int 468 ppihztoms(int h) 469 { 470 extern int hz; 471 int m = h; 472 473 if (m > 0) 474 m = m * 1000 / hz; 475 return (m); 476 } 477 478 int 479 ppimstohz(int m) 480 { 481 extern int hz; 482 int h = m; 483 484 if (h > 0) { 485 h = h * hz / 1000; 486 if (h == 0) 487 h = 1000 / hz; 488 } 489 return (h); 490 } 491