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