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