Home | History | Annotate | Line # | Download | only in gpib
gpib.c revision 1.24
      1 /*	$NetBSD: gpib.c,v 1.24 2019/11/10 21:16:34 chs 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.24 2019/11/10 21:16:34 chs 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 		(void) config_search_ia(gpibsubmatch1, sc->sc_dev, "gpib",
    144 		    &ga);
    145 	}
    146 
    147 	/* attach the wild-carded devices - probably protocol busses */
    148 	ga.ga_ic = sc->sc_ic;
    149 	(void) config_search_ia(gpibsubmatch2, sc->sc_dev, "gpib", &ga);
    150 }
    151 
    152 int
    153 gpibsubmatch1(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    154 {
    155 	struct gpib_softc *sc = device_private(parent);
    156 	struct gpib_attach_args *ga = aux;
    157 
    158 	if (cf->cf_loc[GPIBCF_ADDRESS] != ga->ga_address)
    159 		return (0);
    160 
    161 	if (cf->cf_loc[GPIBCF_ADDRESS] == sc->sc_myaddr)
    162 		return (0);
    163 
    164 	if (config_match(parent, cf, ga) > 0) {
    165 		if (gpib_alloc(sc, ga->ga_address))
    166 			return (0);
    167 		config_attach(parent, cf, ga, gpibprint);
    168 		return (0);
    169 	}
    170 	return (0);
    171 }
    172 
    173 int
    174 gpibsubmatch2(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    175 {
    176 	struct gpib_attach_args *ga = aux;
    177 
    178 	if (cf->cf_loc[GPIBCF_ADDRESS] != GPIBCF_ADDRESS_DEFAULT)
    179 		return (0);
    180 
    181 	ga->ga_address = GPIBCF_ADDRESS_DEFAULT;
    182 	if (config_match(parent, cf, ga) > 0) {
    183 		config_attach(parent, cf, ga, gpibdevprint);
    184 		return (0);
    185 	}
    186 	return (0);
    187 }
    188 
    189 int
    190 gpibprint(void *aux, const char *pnp)
    191 {
    192 	struct gpib_attach_args *ga = aux;
    193 
    194 	if (ga->ga_address != GPIBCF_ADDRESS_DEFAULT)
    195 		printf(" address %d", ga->ga_address);
    196 	return (UNCONF);
    197 }
    198 
    199 int
    200 gpibdevprint(void *aux, const char *pnp)
    201 {
    202 
    203 	if (pnp != NULL)
    204 		printf("gpib at %s", pnp);
    205 	return (UNCONF);
    206 }
    207 
    208 /*
    209  * Called by hardware driver, pass to device driver.
    210  */
    211 int
    212 gpibintr(void *v)
    213 {
    214 	struct gpib_softc *sc = v;
    215 	gpib_handle_t hdl;
    216 
    217 	DPRINTF(DBG_INTR, ("gpibintr: sc=%p\n", sc));
    218 
    219 	hdl = TAILQ_FIRST(&sc->sc_queue);
    220 	(hdl->hq_callback)(hdl->hq_softc, GPIBCBF_INTR);
    221 	return (0);
    222 }
    223 
    224 /*
    225  * Create a callback handle.
    226  */
    227 int
    228 _gpibregister(struct gpib_softc *sc, int slave, gpib_callback_t callback,
    229     void *arg, gpib_handle_t *hdl)
    230 {
    231 
    232 	*hdl = malloc(sizeof(struct gpibqueue), M_DEVBUF, M_WAITOK);
    233 	(*hdl)->hq_slave = slave;
    234 	(*hdl)->hq_callback = callback;
    235 	(*hdl)->hq_softc = arg;
    236 
    237 	return (0);
    238 }
    239 
    240 /*
    241  * Request exclusive access to the GPIB bus.
    242  */
    243 int
    244 _gpibrequest(struct gpib_softc *sc, gpib_handle_t hdl)
    245 {
    246 
    247 	DPRINTF(DBG_FOLLOW, ("_gpibrequest: sc=%p hdl=%p\n", sc, hdl));
    248 
    249 	TAILQ_INSERT_TAIL(&sc->sc_queue, hdl, hq_list);
    250 	if (TAILQ_FIRST(&sc->sc_queue) == hdl)
    251 		return (1);
    252 
    253 	return (0);
    254 }
    255 
    256 /*
    257  * Release exclusive access to the GPIB bus.
    258  */
    259 void
    260 _gpibrelease(struct gpib_softc *sc, gpib_handle_t hdl)
    261 {
    262 
    263 	DPRINTF(DBG_FOLLOW, ("_gpibrelease: sc=%p hdl=%p\n", sc, hdl));
    264 
    265 	TAILQ_REMOVE(&sc->sc_queue, hdl, hq_list);
    266 	if ((hdl = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    267 		(*hdl->hq_callback)(hdl->hq_softc, GPIBCBF_START);
    268 }
    269 
    270 
    271 /*
    272  * Asynchronous wait.
    273  */
    274 void
    275 _gpibawait(struct gpib_softc *sc)
    276 {
    277 	int slave;
    278 
    279 	DPRINTF(DBG_FOLLOW, ("_gpibawait: sc=%p\n", sc));
    280 
    281 	slave = TAILQ_FIRST(&sc->sc_queue)->hq_slave;
    282 	(*sc->sc_ic->ppwatch)(sc->sc_ic->cookie, slave);
    283 }
    284 
    285 /*
    286  * Synchronous (spin) wait.
    287  */
    288 int
    289 _gpibswait(struct gpib_softc *sc, int slave)
    290 {
    291 	int timo = gpibtimeout;
    292 	int (*pptest)(void *, int);
    293 
    294 	DPRINTF(DBG_FOLLOW, ("_gpibswait: sc=%p\n", sc));
    295 
    296 	pptest = sc->sc_ic->pptest;
    297 	while ((*pptest)(sc->sc_ic->cookie, slave) == 0) {
    298 		if (--timo == 0) {
    299 			aprint_error_dev(sc->sc_dev, "swait timeout\n");
    300 			return(-1);
    301 		}
    302 	}
    303 	return (0);
    304 }
    305 
    306 /*
    307  * Resource accounting: check if the address has already been
    308  * claimed and allocated.
    309  */
    310 int
    311 gpib_isalloc(struct gpib_softc *sc, u_int8_t address)
    312 {
    313 
    314 	DPRINTF(DBG_FOLLOW, ("gpib_isalloc: sc=%p address=%d\n", sc, address));
    315 
    316 #ifdef DIAGNOSTIC
    317 	if (address >= GPIB_NDEVS)
    318 		panic("gpib_isalloc: device address out of range");
    319 #endif
    320 
    321 	return ((sc->sc_rmap & (1 << address)) != 0);
    322 }
    323 
    324 /*
    325  * Resource accounting: allocate the address.
    326  */
    327 int
    328 gpib_alloc(struct gpib_softc *sc, u_int8_t address)
    329 {
    330 
    331 	DPRINTF(DBG_FOLLOW, ("gpib_alloc: sc=%p address=%d\n", sc, address));
    332 
    333 #ifdef DIAGNOSTIC
    334 	if (address >= GPIB_NDEVS)
    335 		panic("gpib_alloc: device address out of range");
    336 #endif
    337 
    338 	if (!gpib_isalloc(sc, address)) {
    339 		sc->sc_rmap |= (1 << address);
    340 		return (0);
    341 	}
    342 	return (1);
    343 }
    344 
    345 /*
    346  * Resource accounting: deallocate the address.
    347  */
    348 void
    349 gpib_dealloc(struct gpib_softc *sc, u_int8_t address)
    350 {
    351 
    352 	DPRINTF(DBG_FOLLOW, ("gpib_free: sc=%p address=%d\n", sc, address));
    353 
    354 #ifdef DIAGNOSTIC
    355 	if (address >= GPIB_NDEVS)
    356 		panic("gpib_free: device address out of range");
    357 
    358 	if (!gpib_isalloc(sc, address))
    359 		panic("gpib_free: not allocated");
    360 #endif
    361 
    362 	sc->sc_rmap &= ~(1 << address);
    363 }
    364 
    365 int
    366 _gpibsend(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt)
    367 {
    368 	int rv;
    369 	int cnt = 0;
    370 	u_int8_t cmds[4];
    371 	int i = 0;
    372 
    373 	DPRINTF(DBG_FOLLOW,
    374 	    ("_gpibsend: sc=%p slave %d sec=%d ptr=%p cnt=%d\n",
    375 	    sc, slave, sec, ptr, origcnt));
    376 
    377 	/*
    378 	 * For compatibility, call the hardware driver directly.
    379 	 */
    380 	if (sc->sc_ic->send != NULL) {
    381 		rv = (*sc->sc_ic->send)(sc->sc_ic->cookie,
    382 			slave, sec, ptr, origcnt);
    383 		return (rv);
    384 	}
    385 
    386 	if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
    387 		goto senderror;
    388 	cmds[i++] = GPIBCMD_UNL;
    389 	cmds[i++] = GPIBCMD_TAG | sc->sc_myaddr;
    390 	cmds[i++] = GPIBCMD_LAG | slave;
    391 	if (sec >= 0 || sec == -2) {
    392 		if (sec == -2)		/* selected device clear KLUDGE */
    393 			cmds[i++] = GPIBCMD_SDC;
    394 		else
    395 			cmds[i++] = GPIBCMD_SCG | sec;
    396 	}
    397 	if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i)
    398 		goto senderror;
    399 	if ((*sc->sc_ic->gts)(sc->sc_ic->cookie))
    400 		goto senderror;
    401 	if (origcnt) {
    402 		cnt = (*sc->sc_ic->senddata)(sc->sc_ic->cookie, ptr, origcnt);
    403 		if (cnt != origcnt)
    404 			goto senderror;
    405 		if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
    406 			goto senderror;
    407 	}
    408 	return (origcnt);
    409 
    410 senderror:
    411 	(*sc->sc_ic->ifc)(sc->sc_ic->cookie);
    412 	DPRINTF(DBG_FAIL,
    413 	    ("%s: _gpibsend failed: slave %d, sec %x, sent %d of %d bytes\n",
    414 	    device_xname(sc->sc_dev), slave, sec, cnt, origcnt));
    415 	return (cnt);
    416 }
    417 
    418 int
    419 _gpibrecv(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt)
    420 {
    421 	int rv;
    422 	u_int8_t cmds[4];
    423 	int cnt = 0;
    424 	int i = 0;
    425 
    426 	DPRINTF(DBG_FOLLOW,
    427 	    ("_gpibrecv: sc=%p slave=%d sec=%d buf=%p cnt=%d\n",
    428 	    sc, slave, sec, ptr, origcnt));
    429 
    430 	/*
    431 	 * For compatibility, call the hardware driver directly.
    432 	 */
    433 	if (sc->sc_ic->recv != NULL) {
    434 		rv = (*sc->sc_ic->recv)(sc->sc_ic->cookie,
    435 			slave, sec, ptr, origcnt);
    436 		return (rv);
    437 	}
    438 
    439 	/*
    440 	 * slave < 0 implies continuation of a previous receive
    441 	 * that probably timed out.
    442 	 */
    443 	if (slave >= 0) {
    444 		if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
    445 			goto recverror;
    446 		cmds[i++] = GPIBCMD_UNL;
    447 		cmds[i++] = GPIBCMD_LAG | sc->sc_myaddr;
    448 		cmds[i++] = GPIBCMD_TAG | slave;
    449 		if (sec >= 0)
    450 			cmds[i++] = GPIBCMD_SCG | sec;
    451 		if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i)
    452 			goto recverror;
    453 		if ((*sc->sc_ic->gts)(sc->sc_ic->cookie))
    454 			goto recverror;
    455 	}
    456 	if (origcnt) {
    457 		cnt = (*sc->sc_ic->recvdata)(sc->sc_ic->cookie, ptr, origcnt);
    458 		if (cnt != origcnt)
    459 			goto recverror;
    460 		if ((sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
    461 			goto recverror;
    462 		cmds[0] = (slave == GPIB_BROADCAST_ADDR) ?
    463 		    GPIBCMD_UNA : GPIBCMD_UNT;
    464 		if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, 1) != 1)
    465 			goto recverror;
    466 	}
    467 	return (origcnt);
    468 
    469 recverror:
    470 	(*sc->sc_ic->ifc)(sc->sc_ic->cookie);
    471 	DPRINTF(DBG_FAIL,
    472 	    ("_gpibrecv: failed, sc=%p slave %d, sec %x, got %d of %d bytes\n",
    473 	    sc, slave, sec, cnt, origcnt));
    474 	return (cnt);
    475 }
    476 
    477 /*
    478  * /dev/gpib? interface
    479  */
    480 
    481 int
    482 gpibopen(dev_t dev, int flags, int mode, struct lwp *l)
    483 {
    484 	struct gpib_softc *sc;
    485 
    486 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    487 	if (sc == NULL)
    488 		return (ENXIO);
    489 
    490 	DPRINTF(DBG_FOLLOW, ("gpibopen: sc=%p\n", sc));
    491 
    492 	if (sc->sc_flags & GPIBF_ACTIVE)
    493 		return (EBUSY);
    494 	sc->sc_flags |= GPIBF_ACTIVE;
    495 
    496 	return (0);
    497 }
    498 
    499 int
    500 gpibclose(dev_t dev, int flag, int mode, struct lwp *l)
    501 {
    502 	struct gpib_softc *sc;
    503 
    504 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    505 	if (sc == NULL)
    506 		return (ENXIO);
    507 
    508 	DPRINTF(DBG_FOLLOW, ("gpibclose: sc=%p\n", sc));
    509 
    510 	sc->sc_flags &= ~GPIBF_ACTIVE;
    511 
    512 	return (0);
    513 }
    514 
    515 int
    516 gpibread(dev_t dev, struct uio *uio, int flags)
    517 {
    518 	struct gpib_softc *sc;
    519 
    520 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    521 	if (sc == NULL)
    522 		return (ENXIO);
    523 
    524 	DPRINTF(DBG_FOLLOW, ("gpibread: sc=%p\n", sc));
    525 
    526 	return (EOPNOTSUPP);
    527 }
    528 
    529 int
    530 gpibwrite(dev_t dev, struct uio *uio, int flags)
    531 {
    532 	struct gpib_softc *sc;
    533 
    534 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    535 	if (sc == NULL)
    536 		return (ENXIO);
    537 
    538 	DPRINTF(DBG_FOLLOW, ("gpibwrite: sc=%p\n", sc));
    539 
    540 	return (EOPNOTSUPP);
    541 }
    542 
    543 int
    544 gpibioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    545 {
    546 	struct gpib_softc *sc;
    547 
    548 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    549 	if (sc == NULL)
    550 		return (ENXIO);
    551 
    552 	DPRINTF(DBG_FOLLOW, ("gpibioctl(%lu, '%c',%lu): sc=%p\n",
    553 	    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, sc));
    554 
    555 	switch (cmd) {
    556 	case GPIB_INFO:
    557 		(*(int *)data) = 0xa5a5a5a5;
    558 		break;
    559 	}
    560 
    561 	return (EINVAL);
    562 }
    563 
    564 int
    565 gpibpoll(dev_t dev, int events, struct lwp *l)
    566 {
    567 	struct gpib_softc *sc;
    568 
    569 	sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
    570 	if (sc == NULL)
    571 		return (ENXIO);
    572 
    573 	DPRINTF(DBG_FOLLOW, ("gpibpoll: sc=%p\n", sc));
    574 
    575 	return (0);
    576 }
    577