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