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