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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
/* $NetBSD: gpib.c,v 1.26 2021/08/07 16:19:10 thorpej Exp $ */
/*-
* Copyright (c) 2003 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: gpib.c,v 1.26 2021/08/07 16:19:10 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <dev/gpib/gpibvar.h>
#include <dev/gpib/gpibio.h> /* XXX */
#include "locators.h"
#ifndef DEBUG
#define DEBUG
#endif
#ifdef DEBUG
int gpibdebug = 0xff;
#define DBG_FOLLOW 0x01
#define DBG_INTR 0x02
#define DBG_FAIL 0x04
#define DPRINTF(mask, str) if (gpibdebug & (mask)) printf str
#else
#define DPRINTF(mask, str) /* nothing */
#endif
int gpibmatch(device_t, cfdata_t, void *);
void gpibattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(gpib, sizeof(struct gpib_softc),
gpibmatch, gpibattach, NULL, NULL);
static int gpibsubmatch1(device_t, cfdata_t, const int *, void *);
static int gpibsubmatch2(device_t, cfdata_t, const int *, void *);
static int gpibprint(void *, const char *);
dev_type_open(gpibopen);
dev_type_close(gpibclose);
dev_type_read(gpibread);
dev_type_write(gpibwrite);
dev_type_ioctl(gpibioctl);
dev_type_poll(gpibpoll);
const struct cdevsw gpib_cdevsw = {
.d_open = gpibopen,
.d_close = gpibclose,
.d_read = gpibread,
.d_write = gpibwrite,
.d_ioctl = gpibioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = gpibpoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER
};
extern struct cfdriver gpib_cd;
#define GPIBUNIT(dev) (minor(dev) & 0x0f)
int gpibtimeout = 100000; /* # of status tests before we give up */
int
gpibmatch(device_t parent, cfdata_t match, void *aux)
{
return (1);
}
void
gpibattach(device_t parent, device_t self, void *aux)
{
struct gpib_softc *sc = device_private(self);
cfdata_t cf = device_cfdata(self);
struct gpibdev_attach_args *gda = aux;
struct gpib_attach_args ga;
int address;
sc->sc_dev = self;
sc->sc_ic = gda->ga_ic;
/*
* If the configuration file specified a host address, then
* use it in favour of registers/switches or the default (30).
*/
if (cf->cf_loc[GPIBDEVCF_ADDRESS] != GPIBDEVCF_ADDRESS_DEFAULT)
sc->sc_myaddr = cf->cf_loc[GPIBDEVCF_ADDRESS];
else if (gda->ga_address != GPIBDEVCF_ADDRESS_DEFAULT)
sc->sc_myaddr = gda->ga_address;
else
sc->sc_myaddr = 30;
printf(": host address %d\n", sc->sc_myaddr);
/* record our softc pointer */
sc->sc_ic->bus = sc;
/* Initialize the slave request queue */
TAILQ_INIT(&sc->sc_queue);
/* attach addressed devices */
for (address=0; address<GPIB_NDEVS; address++) {
ga.ga_ic = sc->sc_ic;
ga.ga_address = address;
config_search(sc->sc_dev, &ga,
CFARGS(.search = gpibsubmatch1));
}
/* attach the wild-carded devices - probably protocol busses */
ga.ga_ic = sc->sc_ic;
config_search(sc->sc_dev, &ga,
CFARGS(.search = gpibsubmatch2));
}
int
gpibsubmatch1(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct gpib_softc *sc = device_private(parent);
struct gpib_attach_args *ga = aux;
if (cf->cf_loc[GPIBCF_ADDRESS] != ga->ga_address)
return (0);
if (cf->cf_loc[GPIBCF_ADDRESS] == sc->sc_myaddr)
return (0);
if (config_probe(parent, cf, ga)) {
if (gpib_alloc(sc, ga->ga_address))
return (0);
config_attach(parent, cf, ga, gpibprint, CFARGS_NONE);
return (0);
}
return (0);
}
int
gpibsubmatch2(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct gpib_attach_args *ga = aux;
if (cf->cf_loc[GPIBCF_ADDRESS] != GPIBCF_ADDRESS_DEFAULT)
return (0);
ga->ga_address = GPIBCF_ADDRESS_DEFAULT;
if (config_probe(parent, cf, ga)) {
config_attach(parent, cf, ga, gpibdevprint, CFARGS_NONE);
return (0);
}
return (0);
}
int
gpibprint(void *aux, const char *pnp)
{
struct gpib_attach_args *ga = aux;
if (ga->ga_address != GPIBCF_ADDRESS_DEFAULT)
printf(" address %d", ga->ga_address);
return (UNCONF);
}
int
gpibdevprint(void *aux, const char *pnp)
{
if (pnp != NULL)
printf("gpib at %s", pnp);
return (UNCONF);
}
/*
* Called by hardware driver, pass to device driver.
*/
int
gpibintr(void *v)
{
struct gpib_softc *sc = v;
gpib_handle_t hdl;
DPRINTF(DBG_INTR, ("gpibintr: sc=%p\n", sc));
hdl = TAILQ_FIRST(&sc->sc_queue);
(hdl->hq_callback)(hdl->hq_softc, GPIBCBF_INTR);
return (0);
}
/*
* Create a callback handle.
*/
int
_gpibregister(struct gpib_softc *sc, int slave, gpib_callback_t callback,
void *arg, gpib_handle_t *hdl)
{
*hdl = malloc(sizeof(struct gpibqueue), M_DEVBUF, M_WAITOK);
(*hdl)->hq_slave = slave;
(*hdl)->hq_callback = callback;
(*hdl)->hq_softc = arg;
return (0);
}
/*
* Request exclusive access to the GPIB bus.
*/
int
_gpibrequest(struct gpib_softc *sc, gpib_handle_t hdl)
{
DPRINTF(DBG_FOLLOW, ("_gpibrequest: sc=%p hdl=%p\n", sc, hdl));
TAILQ_INSERT_TAIL(&sc->sc_queue, hdl, hq_list);
if (TAILQ_FIRST(&sc->sc_queue) == hdl)
return (1);
return (0);
}
/*
* Release exclusive access to the GPIB bus.
*/
void
_gpibrelease(struct gpib_softc *sc, gpib_handle_t hdl)
{
DPRINTF(DBG_FOLLOW, ("_gpibrelease: sc=%p hdl=%p\n", sc, hdl));
TAILQ_REMOVE(&sc->sc_queue, hdl, hq_list);
if ((hdl = TAILQ_FIRST(&sc->sc_queue)) != NULL)
(*hdl->hq_callback)(hdl->hq_softc, GPIBCBF_START);
}
/*
* Asynchronous wait.
*/
void
_gpibawait(struct gpib_softc *sc)
{
int slave;
DPRINTF(DBG_FOLLOW, ("_gpibawait: sc=%p\n", sc));
slave = TAILQ_FIRST(&sc->sc_queue)->hq_slave;
(*sc->sc_ic->ppwatch)(sc->sc_ic->cookie, slave);
}
/*
* Synchronous (spin) wait.
*/
int
_gpibswait(struct gpib_softc *sc, int slave)
{
int timo = gpibtimeout;
int (*pptest)(void *, int);
DPRINTF(DBG_FOLLOW, ("_gpibswait: sc=%p\n", sc));
pptest = sc->sc_ic->pptest;
while ((*pptest)(sc->sc_ic->cookie, slave) == 0) {
if (--timo == 0) {
aprint_error_dev(sc->sc_dev, "swait timeout\n");
return(-1);
}
}
return (0);
}
/*
* Resource accounting: check if the address has already been
* claimed and allocated.
*/
int
gpib_isalloc(struct gpib_softc *sc, u_int8_t address)
{
DPRINTF(DBG_FOLLOW, ("gpib_isalloc: sc=%p address=%d\n", sc, address));
#ifdef DIAGNOSTIC
if (address >= GPIB_NDEVS)
panic("gpib_isalloc: device address out of range");
#endif
return ((sc->sc_rmap & (1 << address)) != 0);
}
/*
* Resource accounting: allocate the address.
*/
int
gpib_alloc(struct gpib_softc *sc, u_int8_t address)
{
DPRINTF(DBG_FOLLOW, ("gpib_alloc: sc=%p address=%d\n", sc, address));
#ifdef DIAGNOSTIC
if (address >= GPIB_NDEVS)
panic("gpib_alloc: device address out of range");
#endif
if (!gpib_isalloc(sc, address)) {
sc->sc_rmap |= (1 << address);
return (0);
}
return (1);
}
/*
* Resource accounting: deallocate the address.
*/
void
gpib_dealloc(struct gpib_softc *sc, u_int8_t address)
{
DPRINTF(DBG_FOLLOW, ("gpib_free: sc=%p address=%d\n", sc, address));
#ifdef DIAGNOSTIC
if (address >= GPIB_NDEVS)
panic("gpib_free: device address out of range");
if (!gpib_isalloc(sc, address))
panic("gpib_free: not allocated");
#endif
sc->sc_rmap &= ~(1 << address);
}
int
_gpibsend(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt)
{
int rv;
int cnt = 0;
u_int8_t cmds[4];
int i = 0;
DPRINTF(DBG_FOLLOW,
("_gpibsend: sc=%p slave %d sec=%d ptr=%p cnt=%d\n",
sc, slave, sec, ptr, origcnt));
/*
* For compatibility, call the hardware driver directly.
*/
if (sc->sc_ic->send != NULL) {
rv = (*sc->sc_ic->send)(sc->sc_ic->cookie,
slave, sec, ptr, origcnt);
return (rv);
}
if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
goto senderror;
cmds[i++] = GPIBCMD_UNL;
cmds[i++] = GPIBCMD_TAG | sc->sc_myaddr;
cmds[i++] = GPIBCMD_LAG | slave;
if (sec >= 0 || sec == -2) {
if (sec == -2) /* selected device clear KLUDGE */
cmds[i++] = GPIBCMD_SDC;
else
cmds[i++] = GPIBCMD_SCG | sec;
}
if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i)
goto senderror;
if ((*sc->sc_ic->gts)(sc->sc_ic->cookie))
goto senderror;
if (origcnt) {
cnt = (*sc->sc_ic->senddata)(sc->sc_ic->cookie, ptr, origcnt);
if (cnt != origcnt)
goto senderror;
if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
goto senderror;
}
return (origcnt);
senderror:
(*sc->sc_ic->ifc)(sc->sc_ic->cookie);
DPRINTF(DBG_FAIL,
("%s: _gpibsend failed: slave %d, sec %x, sent %d of %d bytes\n",
device_xname(sc->sc_dev), slave, sec, cnt, origcnt));
return (cnt);
}
int
_gpibrecv(struct gpib_softc *sc, int slave, int sec, void *ptr, int origcnt)
{
int rv;
u_int8_t cmds[4];
int cnt = 0;
int i = 0;
DPRINTF(DBG_FOLLOW,
("_gpibrecv: sc=%p slave=%d sec=%d buf=%p cnt=%d\n",
sc, slave, sec, ptr, origcnt));
/*
* For compatibility, call the hardware driver directly.
*/
if (sc->sc_ic->recv != NULL) {
rv = (*sc->sc_ic->recv)(sc->sc_ic->cookie,
slave, sec, ptr, origcnt);
return (rv);
}
/*
* slave < 0 implies continuation of a previous receive
* that probably timed out.
*/
if (slave >= 0) {
if ((*sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
goto recverror;
cmds[i++] = GPIBCMD_UNL;
cmds[i++] = GPIBCMD_LAG | sc->sc_myaddr;
cmds[i++] = GPIBCMD_TAG | slave;
if (sec >= 0)
cmds[i++] = GPIBCMD_SCG | sec;
if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, i) != i)
goto recverror;
if ((*sc->sc_ic->gts)(sc->sc_ic->cookie))
goto recverror;
}
if (origcnt) {
cnt = (*sc->sc_ic->recvdata)(sc->sc_ic->cookie, ptr, origcnt);
if (cnt != origcnt)
goto recverror;
if ((sc->sc_ic->tc)(sc->sc_ic->cookie, 0))
goto recverror;
cmds[0] = (slave == GPIB_BROADCAST_ADDR) ?
GPIBCMD_UNA : GPIBCMD_UNT;
if ((*sc->sc_ic->sendcmds)(sc->sc_ic->cookie, cmds, 1) != 1)
goto recverror;
}
return (origcnt);
recverror:
(*sc->sc_ic->ifc)(sc->sc_ic->cookie);
DPRINTF(DBG_FAIL,
("_gpibrecv: failed, sc=%p slave %d, sec %x, got %d of %d bytes\n",
sc, slave, sec, cnt, origcnt));
return (cnt);
}
/*
* /dev/gpib? interface
*/
int
gpibopen(dev_t dev, int flags, int mode, struct lwp *l)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibopen: sc=%p\n", sc));
if (sc->sc_flags & GPIBF_ACTIVE)
return (EBUSY);
sc->sc_flags |= GPIBF_ACTIVE;
return (0);
}
int
gpibclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibclose: sc=%p\n", sc));
sc->sc_flags &= ~GPIBF_ACTIVE;
return (0);
}
int
gpibread(dev_t dev, struct uio *uio, int flags)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibread: sc=%p\n", sc));
return (EOPNOTSUPP);
}
int
gpibwrite(dev_t dev, struct uio *uio, int flags)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibwrite: sc=%p\n", sc));
return (EOPNOTSUPP);
}
int
gpibioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibioctl(%lu, '%c',%lu): sc=%p\n",
IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, sc));
switch (cmd) {
case GPIB_INFO:
(*(int *)data) = 0xa5a5a5a5;
break;
}
return (EINVAL);
}
int
gpibpoll(dev_t dev, int events, struct lwp *l)
{
struct gpib_softc *sc;
sc = device_lookup_private(&gpib_cd, GPIBUNIT(dev));
if (sc == NULL)
return (ENXIO);
DPRINTF(DBG_FOLLOW, ("gpibpoll: sc=%p\n", sc));
return (0);
}
|