1 /* $NetBSD: gpib.c,v 1.26 2021/08/07 16:19:10 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gregory McGarry. 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 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: gpib.c,v 1.26 2021/08/07 16:19:10 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/device.h> 39 #include <sys/ioctl.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 43 #include <dev/gpib/gpibvar.h> 44 45 #include <dev/gpib/gpibio.h> /* XXX */ 46 47 #include "locators.h" 48 49 #ifndef DEBUG 50 #define DEBUG 51 #endif 52 53 #ifdef DEBUG 54 int gpibdebug = 0xff; 55 #define DBG_FOLLOW 0x01 56 #define DBG_INTR 0x02 57 #define DBG_FAIL 0x04 58 #define DPRINTF(mask, str) if (gpibdebug & (mask)) printf str 59 #else 60 #define DPRINTF(mask, str) /* nothing */ 61 #endif 62 63 int gpibmatch(device_t, cfdata_t, void *); 64 void gpibattach(device_t, device_t, void *); 65 66 CFATTACH_DECL_NEW(gpib, sizeof(struct gpib_softc), 67 gpibmatch, gpibattach, NULL, NULL); 68 69 static int gpibsubmatch1(device_t, cfdata_t, const int *, void *); 70 static int gpibsubmatch2(device_t, cfdata_t, const int *, void *); 71 static int gpibprint(void *, const char *); 72 73 dev_type_open(gpibopen); 74 dev_type_close(gpibclose); 75 dev_type_read(gpibread); 76 dev_type_write(gpibwrite); 77 dev_type_ioctl(gpibioctl); 78 dev_type_poll(gpibpoll); 79 80 const struct cdevsw gpib_cdevsw = { 81 .d_open = gpibopen, 82 .d_close = gpibclose, 83 .d_read = gpibread, 84 .d_write = gpibwrite, 85 .d_ioctl = gpibioctl, 86 .d_stop = nostop, 87 .d_tty = notty, 88 .d_poll = gpibpoll, 89 .d_mmap = nommap, 90 .d_kqfilter = nokqfilter, 91 .d_discard = nodiscard, 92 .d_flag = D_OTHER 93 }; 94 95 extern struct cfdriver gpib_cd; 96 97 #define GPIBUNIT(dev) (minor(dev) & 0x0f) 98 99 int gpibtimeout = 100000; /* # of status tests before we give up */ 100 101 int 102 gpibmatch(device_t parent, cfdata_t match, void *aux) 103 { 104 105 return (1); 106 } 107 108 void 109 gpibattach(device_t parent, device_t self, void *aux) 110 { 111 struct gpib_softc *sc = device_private(self); 112 cfdata_t cf = device_cfdata(self); 113 struct gpibdev_attach_args *gda = aux; 114 struct gpib_attach_args ga; 115 int address; 116 117 sc->sc_dev = self; 118 sc->sc_ic = gda->ga_ic; 119 120 /* 121 * If the configuration file specified a host address, then 122 * use it in favour of registers/switches or the default (30). 123 */ 124 if (cf->cf_loc[GPIBDEVCF_ADDRESS] != GPIBDEVCF_ADDRESS_DEFAULT) 125 sc->sc_myaddr = cf->cf_loc[GPIBDEVCF_ADDRESS]; 126 else if (gda->ga_address != GPIBDEVCF_ADDRESS_DEFAULT) 127 sc->sc_myaddr = gda->ga_address; 128 else 129 sc->sc_myaddr = 30; 130 131 printf(": host address %d\n", sc->sc_myaddr); 132 133 /* record our softc pointer */ 134 sc->sc_ic->bus = sc; 135 136 /* Initialize the slave request queue */ 137 TAILQ_INIT(&sc->sc_queue); 138 139 /* attach addressed devices */ 140 for (address=0; address<GPIB_NDEVS; address++) { 141 ga.ga_ic = sc->sc_ic; 142 ga.ga_address = address; 143 config_search(sc->sc_dev, &ga, 144 CFARGS(.search = gpibsubmatch1)); 145 } 146 147 /* attach the wild-carded devices - probably protocol busses */ 148 ga.ga_ic = sc->sc_ic; 149 config_search(sc->sc_dev, &ga, 150 CFARGS(.search = gpibsubmatch2)); 151 } 152 153 int 154 gpibsubmatch1(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 155 { 156 struct gpib_softc *sc = device_private(parent); 157 struct gpib_attach_args *ga = aux; 158 159 if (cf->cf_loc[GPIBCF_ADDRESS] != ga->ga_address) 160 return (0); 161 162 if (cf->cf_loc[GPIBCF_ADDRESS] == sc->sc_myaddr) 163 return (0); 164 165 if (config_probe(parent, cf, ga)) { 166 if (gpib_alloc(sc, ga->ga_address)) 167 return (0); 168 config_attach(parent, cf, ga, gpibprint, CFARGS_NONE); 169 return (0); 170 } 171 return (0); 172 } 173 174 int 175 gpibsubmatch2(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 176 { 177 struct gpib_attach_args *ga = aux; 178 179 if (cf->cf_loc[GPIBCF_ADDRESS] != GPIBCF_ADDRESS_DEFAULT) 180 return (0); 181 182 ga->ga_address = GPIBCF_ADDRESS_DEFAULT; 183 if (config_probe(parent, cf, ga)) { 184 config_attach(parent, cf, ga, gpibdevprint, CFARGS_NONE); 185 return (0); 186 } 187 return (0); 188 } 189 190 int 191 gpibprint(void *aux, const char *pnp) 192 { 193 struct gpib_attach_args *ga = aux; 194 195 if (ga->ga_address != GPIBCF_ADDRESS_DEFAULT) 196 printf(" address %d", ga->ga_address); 197 return (UNCONF); 198 } 199 200 int 201 gpibdevprint(void *aux, const char *pnp) 202 { 203 204 if (pnp != NULL) 205 printf("gpib at %s", pnp); 206 return (UNCONF); 207 } 208 209 /* 210 * Called by hardware driver, pass to device driver. 211 */ 212 int 213 gpibintr(void *v) 214 { 215 struct gpib_softc *sc = v; 216 gpib_handle_t hdl; 217 218 DPRINTF(DBG_INTR, ("gpibintr: sc=%p\n", sc)); 219 220 hdl = TAILQ_FIRST(&sc->sc_queue); 221 (hdl->hq_callback)(hdl->hq_softc, GPIBCBF_INTR); 222 return (0); 223 } 224 225 /* 226 * Create a callback handle. 227 */ 228 int 229 _gpibregister(struct gpib_softc *sc, int slave, gpib_callback_t callback, 230 void *arg, gpib_handle_t *hdl) 231 { 232 233 *hdl = malloc(sizeof(struct gpibqueue), M_DEVBUF, M_WAITOK); 234 (*hdl)->hq_slave = slave; 235 (*hdl)->hq_callback = callback; 236 (*hdl)->hq_softc = arg; 237 238 return (0); 239 } 240 241 /* 242 * Request exclusive access to the GPIB bus. 243 */ 244 int 245 _gpibrequest(struct gpib_softc *sc, gpib_handle_t hdl) 246 { 247 248 DPRINTF(DBG_FOLLOW, ("_gpibrequest: sc=%p hdl=%p\n", sc, hdl)); 249 250 TAILQ_INSERT_TAIL(&sc->sc_queue, hdl, hq_list); 251 if (TAILQ_FIRST(&sc->sc_queue) == hdl) 252 return (1); 253 254 return (0); 255 } 256 257 /* 258 * Release exclusive access to the GPIB bus. 259 */ 260 void 261 _gpibrelease(struct gpib_softc *sc, gpib_handle_t hdl) 262 { 263 264 DPRINTF(DBG_FOLLOW, ("_gpibrelease: sc=%p hdl=%p\n", sc, hdl)); 265 266 TAILQ_REMOVE(&sc->sc_queue, hdl, hq_list); 267 if ((hdl = TAILQ_FIRST(&sc->sc_queue)) != NULL) 268 (*hdl->hq_callback)(hdl->hq_softc, GPIBCBF_START); 269 } 270 271 272 /* 273 * Asynchronous wait. 274 */ 275 void 276 _gpibawait(struct gpib_softc *sc) 277 { 278 int slave; 279 280 DPRINTF(DBG_FOLLOW, ("_gpibawait: sc=%p\n", sc)); 281 282 slave = TAILQ_FIRST(&sc->sc_queue)->hq_slave; 283 (*sc->sc_ic->ppwatch)(sc->sc_ic->cookie, slave); 284 } 285 286 /* 287 * Synchronous (spin) wait. 288 */ 289 int 290 _gpibswait(struct gpib_softc *sc, int slave) 291 { 292 int timo = gpibtimeout; 293 int (*pptest)(void *, int); 294 295 DPRINTF(DBG_FOLLOW, ("_gpibswait: sc=%p\n", sc)); 296 297 pptest = sc->sc_ic->pptest; 298 while ((*pptest)(sc->sc_ic->cookie, slave) == 0) { 299 if (--timo == 0) { 300 aprint_error_dev(sc->sc_dev, "swait timeout\n"); 301 return(-1); 302 } 303 } 304 return (0); 305 } 306 307 /* 308 * Resource accounting: check if the address has already been 309 * claimed and allocated. 310 */ 311 int 312 gpib_isalloc(struct gpib_softc *sc, u_int8_t address) 313 { 314 315 DPRINTF(DBG_FOLLOW, ("gpib_isalloc: sc=%p address=%d\n", sc, address)); 316 317 #ifdef DIAGNOSTIC 318 if (address >= GPIB_NDEVS) 319 panic("gpib_isalloc: device address out of range"); 320 #endif 321 322 return ((sc->sc_rmap & (1 << address)) != 0); 323 } 324 325 /* 326 * Resource accounting: allocate the address. 327 */ 328 int 329 gpib_alloc(struct gpib_softc *sc, u_int8_t address) 330 { 331 332 DPRINTF(DBG_FOLLOW, ("gpib_alloc: sc=%p address=%d\n", sc, address)); 333 334 #ifdef DIAGNOSTIC 335 if (address >= GPIB_NDEVS) 336 panic("gpib_alloc: device address out of range"); 337 #endif 338 339 if (!gpib_isalloc(sc, address)) { 340 sc->sc_rmap |= (1 << address); 341 return (0); 342 } 343 return (1); 344 } 345 346 /* 347 * Resource accounting: deallocate the address. 348 */ 349 void 350 gpib_dealloc(struct gpib_softc *sc, u_int8_t address) 351 { 352 353 DPRINTF(DBG_FOLLOW, ("gpib_free: sc=%p address=%d\n", sc, address)); 354 355 #ifdef DIAGNOSTIC 356 if (address >= GPIB_NDEVS) 357 panic("gpib_free: device address out of range"); 358 359 if (!gpib_isalloc(sc, address)) 360 panic("gpib_free: not allocated"); 361 #endif 362 363 sc->sc_rmap &= ~(1 << address); 364 } 365 366 int 367 _gpibsend(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt) 368 { 369 int rv; 370 int cnt = 0; 371 u_int8_t cmds[4]; 372 int i = 0; 373 374 DPRINTF(DBG_FOLLOW, 375 ("_gpibsend: sc=%p slave %d sec=%d ptr=%p cnt=%d\n", 376 sc, slave, sec, ptr, origcnt)); 377 378 /* 379 * For compatibility, call the hardware driver directly. 380 */ 381 if (sc->sc_ic->send != NULL) { 382 rv = (*sc->sc_ic->send)(sc->sc_ic->cookie, 383 slave, sec, ptr, origcnt); 384 return (rv); 385 } 386 387 if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0)) 388 goto senderror; 389 cmds[i++] = GPIBCMD_UNL; 390 cmds[i++] = GPIBCMD_TAG | sc->sc_myaddr; 391 cmds[i++] = GPIBCMD_LAG | slave; 392 if (sec >= 0 || sec == -2) { 393 if (sec == -2) /* selected device clear KLUDGE */ 394 cmds[i++] = GPIBCMD_SDC; 395 else 396 cmds[i++] = GPIBCMD_SCG | sec; 397 } 398 if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i) 399 goto senderror; 400 if ((*sc->sc_ic->gts)(sc->sc_ic->cookie)) 401 goto senderror; 402 if (origcnt) { 403 cnt = (*sc->sc_ic->senddata)(sc->sc_ic->cookie, ptr, origcnt); 404 if (cnt != origcnt) 405 goto senderror; 406 if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0)) 407 goto senderror; 408 } 409 return (origcnt); 410 411 senderror: 412 (*sc->sc_ic->ifc)(sc->sc_ic->cookie); 413 DPRINTF(DBG_FAIL, 414 ("%s: _gpibsend failed: slave %d, sec %x, sent %d of %d bytes\n", 415 device_xname(sc->sc_dev), slave, sec, cnt, origcnt)); 416 return (cnt); 417 } 418 419 int 420 _gpibrecv(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt) 421 { 422 int rv; 423 u_int8_t cmds[4]; 424 int cnt = 0; 425 int i = 0; 426 427 DPRINTF(DBG_FOLLOW, 428 ("_gpibrecv: sc=%p slave=%d sec=%d buf=%p cnt=%d\n", 429 sc, slave, sec, ptr, origcnt)); 430 431 /* 432 * For compatibility, call the hardware driver directly. 433 */ 434 if (sc->sc_ic->recv != NULL) { 435 rv = (*sc->sc_ic->recv)(sc->sc_ic->cookie, 436 slave, sec, ptr, origcnt); 437 return (rv); 438 } 439 440 /* 441 * slave < 0 implies continuation of a previous receive 442 * that probably timed out. 443 */ 444 if (slave >= 0) { 445 if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0)) 446 goto recverror; 447 cmds[i++] = GPIBCMD_UNL; 448 cmds[i++] = GPIBCMD_LAG | sc->sc_myaddr; 449 cmds[i++] = GPIBCMD_TAG | slave; 450 if (sec >= 0) 451 cmds[i++] = GPIBCMD_SCG | sec; 452 if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i) 453 goto recverror; 454 if ((*sc->sc_ic->gts)(sc->sc_ic->cookie)) 455 goto recverror; 456 } 457 if (origcnt) { 458 cnt = (*sc->sc_ic->recvdata)(sc->sc_ic->cookie, ptr, origcnt); 459 if (cnt != origcnt) 460 goto recverror; 461 if ((sc->sc_ic->tc)(sc->sc_ic->cookie, 0)) 462 goto recverror; 463 cmds[0] = (slave == GPIB_BROADCAST_ADDR) ? 464 GPIBCMD_UNA : GPIBCMD_UNT; 465 if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, 1) != 1) 466 goto recverror; 467 } 468 return (origcnt); 469 470 recverror: 471 (*sc->sc_ic->ifc)(sc->sc_ic->cookie); 472 DPRINTF(DBG_FAIL, 473 ("_gpibrecv: failed, sc=%p slave %d, sec %x, got %d of %d bytes\n", 474 sc, slave, sec, cnt, origcnt)); 475 return (cnt); 476 } 477 478 /* 479 * /dev/gpib? interface 480 */ 481 482 int 483 gpibopen(dev_t dev, int flags, int mode, struct lwp *l) 484 { 485 struct gpib_softc *sc; 486 487 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 488 if (sc == NULL) 489 return (ENXIO); 490 491 DPRINTF(DBG_FOLLOW, ("gpibopen: sc=%p\n", sc)); 492 493 if (sc->sc_flags & GPIBF_ACTIVE) 494 return (EBUSY); 495 sc->sc_flags |= GPIBF_ACTIVE; 496 497 return (0); 498 } 499 500 int 501 gpibclose(dev_t dev, int flag, int mode, struct lwp *l) 502 { 503 struct gpib_softc *sc; 504 505 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 506 if (sc == NULL) 507 return (ENXIO); 508 509 DPRINTF(DBG_FOLLOW, ("gpibclose: sc=%p\n", sc)); 510 511 sc->sc_flags &= ~GPIBF_ACTIVE; 512 513 return (0); 514 } 515 516 int 517 gpibread(dev_t dev, struct uio *uio, int flags) 518 { 519 struct gpib_softc *sc; 520 521 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 522 if (sc == NULL) 523 return (ENXIO); 524 525 DPRINTF(DBG_FOLLOW, ("gpibread: sc=%p\n", sc)); 526 527 return (EOPNOTSUPP); 528 } 529 530 int 531 gpibwrite(dev_t dev, struct uio *uio, int flags) 532 { 533 struct gpib_softc *sc; 534 535 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 536 if (sc == NULL) 537 return (ENXIO); 538 539 DPRINTF(DBG_FOLLOW, ("gpibwrite: sc=%p\n", sc)); 540 541 return (EOPNOTSUPP); 542 } 543 544 int 545 gpibioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 546 { 547 struct gpib_softc *sc; 548 549 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 550 if (sc == NULL) 551 return (ENXIO); 552 553 DPRINTF(DBG_FOLLOW, ("gpibioctl(%lu, '%c',%lu): sc=%p\n", 554 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, sc)); 555 556 switch (cmd) { 557 case GPIB_INFO: 558 (*(int *)data) = 0xa5a5a5a5; 559 break; 560 } 561 562 return (EINVAL); 563 } 564 565 int 566 gpibpoll(dev_t dev, int events, struct lwp *l) 567 { 568 struct gpib_softc *sc; 569 570 sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev)); 571 if (sc == NULL) 572 return (ENXIO); 573 574 DPRINTF(DBG_FOLLOW, ("gpibpoll: sc=%p\n", sc)); 575 576 return (0); 577 } 578