Home | History | Annotate | Line # | Download | only in gpib
cs80bus.c revision 1.17.34.1
      1 /*	$NetBSD: cs80bus.c,v 1.17.34.1 2021/03/20 19:33:40 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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: cs80bus.c,v 1.17.34.1 2021/03/20 19:33:40 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/endian.h>
     39 #include <sys/malloc.h>
     40 
     41 #include <dev/gpib/gpibvar.h>
     42 #include <dev/gpib/cs80busvar.h>
     43 
     44 #ifndef DEBUG
     45 #define DEBUG
     46 #endif
     47 
     48 #ifdef DEBUG
     49 int cs80busdebug = 0xff;
     50 #define DBG_FOLLOW	0x01
     51 #define DBG_STATUS	0x02
     52 #define DBG_FAIL	0x04
     53 #define DPRINTF(mask, str)	if (cs80busdebug & (mask)) printf str
     54 #else
     55 #define DPRINTF(mask, str)	/* nothing */
     56 #endif
     57 
     58 #include "locators.h"
     59 #define	cs80buscf_slave		cf_loc[CS80BUSCF_SLAVE]
     60 #define	cs80buscf_punit		cf_loc[CS80BUSCF_PUNIT]
     61 
     62 int	cs80busmatch(device_t, cfdata_t, void *);
     63 void	cs80busattach(device_t, device_t, void *);
     64 
     65 CFATTACH_DECL_NEW(cs80bus, sizeof(struct cs80bus_softc),
     66 	cs80busmatch, cs80busattach, NULL, NULL);
     67 
     68 static int	cs80bus_alloc(struct cs80bus_softc *, int, int);
     69 static int	cs80bussearch(device_t, cfdata_t,
     70 			      const int *, void *);
     71 static int	cs80busprint(void *, const char *);
     72 
     73 /*
     74  * HP's CS80/SS80 command set can be found on `newer' devices, while
     75  * the HP's Amigo command set is used on before-you-were-born
     76  * devices.  Devices that respond to CS80/SS80 (and probably Amigo, too)
     77  * are tagged with a 16-bit ID.
     78  *
     79  * CS80/SS80 has a 2-level addressing scheme; slave, the analog
     80  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
     81  * IDs are on a per-slave basis; punits are often used for disk
     82  * drives that have an accompanying tape drive on the second punit.
     83  *
     84  * We treat CS80/SS80 as an indirect bus.  However, since we are given
     85  * some ID information, it is unreasonable to disallow cloning of
     86  * CS80/SS80 devices.
     87  *
     88  * To deal with all of this, we use the semi-twisted scheme
     89  * in cs80bus_attach_children().  For each GPIB slave, we loop
     90  * through all of the possibly-configured children, allowing
     91  * them to modify the punit parameter (but NOT the slave!).
     92  *
     93  */
     94 
     95 int
     96 cs80busmatch(device_t parent, cfdata_t match, void *aux)
     97 {
     98 
     99 	return (1);
    100 }
    101 
    102 void
    103 cs80busattach(device_t parent, device_t self, void *aux)
    104 {
    105 	struct cs80bus_softc *sc = device_private(self);
    106 	struct gpib_attach_args *ga = aux;
    107 	struct cs80bus_attach_args ca;
    108 	int slave;
    109 	u_int16_t id;
    110 
    111 	printf("\n");
    112 
    113 	sc->sc_dev = self;
    114 	sc->sc_ic = ga->ga_ic;
    115 
    116 	for (slave = 0; slave < 8; slave++) {
    117 
    118 		if (gpib_isalloc(device_private(device_parent(sc->sc_dev)),
    119 		    slave))
    120 			continue;
    121 
    122 		if (gpibrecv(sc->sc_ic, GPIB_BROADCAST_ADDR,
    123 		    slave, &id, 2) != 2)
    124 			continue;
    125 
    126 		BE16TOH(id);
    127 
    128 		DPRINTF(DBG_STATUS, ("cs80busattach: found id 0x%x\n", id));
    129 
    130 		if ((id & 0x200) == 0)
    131 			continue;
    132 
    133 		ca.ca_ic = sc->sc_ic;
    134 		ca.ca_slave = slave;
    135 		ca.ca_id = id;
    136 
    137 		config_search(sc->sc_dev, &ca,
    138 		    CFARG_SUBMATCH, cs80bussearch,
    139 		    CFARG_IATTR, "cs80bus",
    140 		    CFARG_EOL);
    141 	}
    142 }
    143 
    144 int
    145 cs80bussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    146 {
    147 	struct cs80bus_softc *sc = device_private(parent);
    148 	struct cs80bus_attach_args *ca = aux;
    149 
    150 	/*
    151 	 * Set punit if operator specified one in the kernel
    152 	 * configuration file.
    153 	 */
    154 	if (cf->cs80buscf_punit != CS80BUSCF_PUNIT_DEFAULT &&
    155 	    cf->cs80buscf_punit < CS80BUS_NPUNITS)
    156 		ca->ca_punit = cf->cs80buscf_punit;
    157 	else
    158 		/* default punit */
    159 		ca->ca_punit = 0;
    160 
    161 	DPRINTF(DBG_FOLLOW, ("cs80bussearch: id=0x%x slave=%d punit=%d\n",
    162 	    ca->ca_id, ca->ca_slave, ca->ca_punit));
    163 
    164 	if (config_match(parent, cf, ca) > 0) {
    165 
    166 		DPRINTF(DBG_FOLLOW,
    167 		    ("cs80bussearch: got id=0x%x slave=%d punit %d\n",
    168 		    ca->ca_id, ca->ca_slave, ca->ca_punit));
    169 
    170 		/*
    171 		 * The device probe has succeeded, and filled in
    172 		 * the punit information.  Make sure the configuration
    173 		 * allows for this slave/punit combination.
    174 		 */
    175 		if (cf->cs80buscf_slave != CS80BUSCF_SLAVE_DEFAULT &&
    176 		    cf->cs80buscf_slave != ca->ca_slave)
    177 			goto out;
    178 		if (cf->cs80buscf_punit != CS80BUSCF_PUNIT_DEFAULT &&
    179 		    cf->cs80buscf_punit != ca->ca_punit)
    180 			goto out;
    181 
    182 		/*
    183 		 * Allocate the device's address from the bus's
    184 		 * resource map.
    185 		 */
    186 		if (cs80bus_alloc(sc, ca->ca_slave, ca->ca_punit))
    187 			goto out;
    188 
    189 		/*
    190 		 * This device is allowed; attach it.
    191 		 */
    192 		config_attach(parent, cf, ca, cs80busprint);
    193 	}
    194 out:
    195 	return (0);
    196 }
    197 
    198 int
    199 cs80busprint(void *aux, const char *pnp)
    200 {
    201 	struct cs80bus_attach_args *ca = aux;
    202 
    203 	printf(" slave %d punit %d", ca->ca_slave, ca->ca_punit);
    204 	return (UNCONF);
    205 }
    206 
    207 static int
    208 cs80bus_alloc(struct cs80bus_softc *sc, int slave, int punit)
    209 {
    210 
    211 	DPRINTF(DBG_FOLLOW, ("cs80bus_alloc: sc=%p\n", sc));
    212 
    213 	if (slave >= CS80BUS_NSLAVES || punit >= CS80BUS_NPUNITS)
    214 		panic("cs80bus_alloc: device address out of range");
    215 
    216 	gpib_alloc(device_private(device_parent(sc->sc_dev)), slave);
    217 
    218 	if (sc->sc_rmap[slave][punit] == 0) {
    219 		sc->sc_rmap[slave][punit] = 1;
    220 		return (0);
    221 	}
    222 	return (1);
    223 }
    224 
    225 
    226 
    227 /*
    228  *  CS80/SS80 (secondary) command functions
    229  */
    230 
    231 int
    232 cs80describe(void *v, int slave, int punit, struct cs80_description *csd)
    233 {
    234 	struct cs80bus_softc *sc = v;
    235 	struct cs80_describecmd desc;
    236 	u_int8_t stat;
    237 
    238 	DPRINTF(DBG_FOLLOW, ("cs80describe: sc=%p slave=%d\n", sc, slave));
    239 
    240         /*
    241          * Note command is always issued to unit 0.
    242          */
    243 
    244         desc.c_unit = CS80CMD_SUNIT(0);
    245         desc.c_vol = CS80CMD_SVOL(0);
    246 	desc.c_cmd = CS80CMD_DESC;
    247         (void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &desc, sizeof(desc));
    248         (void) gpibrecv(sc->sc_ic, slave, CS80CMD_EXEC, csd, sizeof(*csd));
    249         (void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    250 	if (stat != 0) {
    251 		DPRINTF(DBG_FAIL, ("cs80describe: failed, stat=0x%x\n", stat));
    252 		return (1);
    253 	}
    254 	BE16TOH(csd->d_iuw);
    255 	BE16TOH(csd->d_cmaxxfr);
    256 	BE16TOH(csd->d_sectsize);
    257 	BE16TOH(csd->d_blocktime);
    258 	BE16TOH(csd->d_uavexfr);
    259 	BE16TOH(csd->d_retry);
    260 	BE16TOH(csd->d_access);
    261 	BE32TOH(csd->d_maxcylhead);
    262 	BE16TOH(csd->d_maxsect);
    263 	BE16TOH(csd->d_maxvsecth);
    264 	BE32TOH(csd->d_maxvsectl);
    265 
    266 	return (0);
    267 }
    268 
    269 int
    270 cs80reset(void *v, int slave, int punit)
    271 {
    272 	struct cs80bus_softc *sc = v;
    273 	struct cs80_clearcmd clear;
    274 	struct cs80_srcmd sr;
    275 	struct cs80_ssmcmd ssm;
    276 
    277 	DPRINTF(DBG_FOLLOW, ("cs80reset: sc=%p slave=%d punit=%d\n", sc,
    278 	    slave, punit));
    279 
    280 	clear.c_unit = CS80CMD_SUNIT(punit);
    281 	clear.c_cmd = CS80CMD_CLEAR;
    282 	if (cs80send(sc, slave, punit, CS80CMD_TCMD, &clear, sizeof(clear))) {
    283 		DPRINTF(DBG_FAIL, ("cs80reset: CLEAR failed\n"));
    284 		return (1);
    285 	}
    286 
    287 	sr.c_unit = CS80CMD_SUNIT(15);		/* XXX */
    288 	sr.c_nop = CS80CMD_NOP;
    289 	sr.c_cmd = CS80CMD_SREL;
    290 	sr.c_param = 0xc0;			/* XXX */
    291 	if (cs80send(sc, slave, punit, CS80CMD_SCMD, &sr, sizeof(sr))) {
    292 		DPRINTF(DBG_FAIL, ("cs80reset: SREL failed\n"));
    293 		return (1);
    294 	}
    295 
    296 	ssm.c_unit = CS80CMD_SUNIT(punit);
    297 	ssm.c_cmd = CS80CMD_SSM;
    298 	ssm.c_refm = htobe16(REF_MASK);
    299 	ssm.c_fefm = htobe16(FEF_MASK);
    300 	ssm.c_aefm = htobe16(AEF_MASK);
    301 	ssm.c_iefm = htobe16(IEF_MASK);
    302 	if (cs80send(sc, slave, punit, CS80CMD_SCMD, &ssm, sizeof(ssm))) {
    303 		DPRINTF(DBG_FAIL, ("cs80reset: SSM failed\n"));
    304 		return (1);
    305 	}
    306 
    307 	return (0);
    308 }
    309 
    310 int
    311 cs80status(void *v, int slave, int punit, struct cs80_stat *css)
    312 {
    313 	struct cs80bus_softc *sc = v;
    314 	struct cs80_statuscmd rs;
    315 	u_int8_t stat;
    316 
    317 	rs.c_unit = CS80CMD_SUNIT(punit);
    318 	rs.c_sram = CS80CMD_SRAM;
    319 	rs.c_param = 0;		/* single vector (i.e. sector number) */
    320 	rs.c_cmd = CS80CMD_STATUS;
    321 	memset((void *)css, 0, sizeof(*css));
    322 	(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &rs, sizeof(rs));
    323 	(void) gpibrecv(sc->sc_ic, slave, CS80CMD_EXEC, css, sizeof(*css));
    324 	(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    325 	if (stat != 0) {
    326 		DPRINTF(DBG_FAIL, ("cs80status: failed, stat=0x%x\n", stat));
    327 		return (1);
    328 	}
    329 	BE16TOH(css->c_ref);
    330 	BE16TOH(css->c_fef);
    331 	BE16TOH(css->c_aef);
    332 	BE16TOH(css->c_ief);
    333 	BE32TOH(css->c_blk);
    334 
    335 	return (0);
    336 }
    337 
    338 int
    339 cs80setoptions(void *v, int slave, int punit, u_int8_t options)
    340 {
    341 	struct cs80bus_softc *sc = v;
    342 	struct cs80_soptcmd opt;
    343 
    344 	opt.c_unit = CS80CMD_SUNIT(punit);
    345 	opt.c_nop = CS80CMD_NOP;
    346 	opt.c_opt = CS80CMD_SOPT;
    347 	opt.c_param = options;
    348 	if (cs80send(sc, slave, punit, CS80CMD_SCMD, &opt, sizeof(opt))) {
    349 		DPRINTF(DBG_FAIL, ("cs80setoptions: failed\n"));
    350 		return (1);
    351 	}
    352 
    353 	return (0);
    354 }
    355 
    356 int
    357 cs80send(void *v, int slave, int punit, int cmd, void *ptr, int cnt)
    358 {
    359 	struct cs80bus_softc *sc = v;
    360 	u_int8_t *buf = ptr;
    361 	u_int8_t stat;
    362 
    363 	DPRINTF(DBG_FOLLOW,
    364 	    ("cs80send: sc=%p slave=%d punit=%d cmd=%d ptr=%p cnt=%d\n", sc,
    365 	    slave, punit, cmd, buf, cnt));
    366 
    367 	if (gpibsend(sc->sc_ic, slave, cmd, buf, cnt) != cnt) {
    368 		DPRINTF(DBG_FAIL, ("cs80send: SCMD failed\n"));
    369 		return (1);
    370 	}
    371 	if (gpibswait(sc->sc_ic, slave)) {
    372 		DPRINTF(DBG_FAIL, ("cs80send: wait failed\n"));
    373 		return (1);
    374 	}
    375 	if (gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1) != 1) {
    376 		DPRINTF(DBG_FAIL, ("cs80send: QSTAT failed\n"));
    377 		return (1);
    378 	}
    379 	if (stat != 0) {
    380 		DPRINTF(DBG_FAIL, ("cs80send: failed, stat=0x%x\n", stat));
    381 		return (1);
    382 	}
    383 
    384 	return (0);
    385 }
    386