2 * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 2010 Joerg Wunsch <joerg@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * High-level driver for µPD7210 based GPIB cards.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/module.h>
47 #include <sys/mutex.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <isa/isavar.h>
54 #include <dev/ieee488/ugpib.h>
56 #define UPD7210_SW_DRIVER
57 #include <dev/ieee488/upd7210.h>
58 #include <dev/ieee488/tnt4882.h>
60 static MALLOC_DEFINE(M_IBFOO, "IBFOO", "IBFOO");
65 #include <dev/ieee488/ibfoo_int.h>
67 /* XXX: This is really a bitmap */
75 LIST_ENTRY(handle) list;
80 struct timeval timeout;
88 LIST_HEAD(,handle) handles;
89 struct unrhdr *unrhdr;
90 struct callout callout;
106 struct timeval deadline;
108 struct handle *rdh; /* addressed for read */
109 struct handle *wrh; /* addressed for write */
117 typedef int ibhandler_t(struct ibfoo *ib);
119 static struct timeval timeouts[] = {
123 [T100us] = { 0, 100},
124 [T300us] = { 0, 300},
127 [T10ms] = { 0, 10000},
128 [T30ms] = { 0, 30000},
129 [T100ms] = { 0, 100000},
130 [T300ms] = { 0, 300000},
137 [T1000s] = { 1000, 0}
140 static const u_int max_timeouts = sizeof timeouts / sizeof timeouts[0];
145 ib_set_error(struct ibarg *ap, int error)
148 if (ap->__iberr == 0)
151 ap->__retval = ap->__ibsta;
156 ib_had_timeout(struct ibarg *ap)
159 ib_set_error(ap, EABO);
161 ap->__retval = ap->__ibsta;
166 ib_set_errno(struct ibarg *ap, int errno)
169 if (ap->__iberr == 0) {
174 ap->__retval = ap->__ibsta;
179 gpib_ib_irq(struct upd7210 *u, int isr_3)
185 mtx_assert(&u->mutex, MA_OWNED);
188 if (!(u->rreg[ISR2] & IXR2_CO))
192 upd7210_wr(u, CDOR, *ib->buf);
197 if (!(u->rreg[ISR1] & IXR1_DI))
199 *ib->buf = upd7210_rd(u, DIR);
202 if (ib->buflen == 0 || (u->rreg[ISR1] & IXR1_ENDRX))
206 if (!(u->rreg[ISR1] & IXR1_DO))
210 if (ib->buflen == 1 && ib->doeoi)
211 upd7210_wr(u, AUXMR, AUXMR_SEOI);
212 upd7210_wr(u, CDOR, *ib->buf);
217 if (!(u->rreg[ISR1] & IXR1_ENDRX))
223 while (ib->buflen != 0 && (isr_3 & 0x04 /* NEF */) != 0) {
224 *ib->buf = bus_read_1(u->reg_res[0], fifob);
227 isr_3 = bus_read_1(u->reg_res[0], isr3);
229 if ((isr_3 & 0x01) != 0 /* xfr done */ ||
230 (u->rreg[ISR1] & IXR1_ENDRX) != 0 ||
235 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
236 upd7210_wr(u, AUXMR, AUXMR_RFD);
245 while (ib->buflen != 0 && (isr_3 & 0x08 /* NFF */) != 0) {
246 bus_write_1(u->reg_res[0], fifob, *ib->buf);
249 isr_3 = bus_read_1(u->reg_res[0], isr3);
253 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
255 /* no more NFF interrupts wanted */
256 bus_write_1(u->reg_res[0], imr3, 0x11); /* STOP IE, DONE IE */
261 upd7210_wr(u, IMR1, 0);
262 upd7210_wr(u, IMR2, 0);
264 bus_write_1(u->reg_res[0], imr3, 0x00);
265 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft RESET */
273 gpib_ib_timeout(void *arg)
283 if (ib->mode == DMA_IDATA && isa_dmatc(u->dmachan)) {
284 KASSERT(u->dmachan >= 0, ("Bogus dmachan = %d", u->dmachan));
285 upd7210_wr(u, IMR1, 0);
286 upd7210_wr(u, IMR2, 0);
290 if (ib->mode > BUSY) {
294 isr_3 = bus_read_1(u->reg_res[0], isr3);
297 gpib_ib_irq(u, isr_3);
299 if (ib->mode != IDLE && timevalisset(&ib->deadline)) {
301 if (timevalcmp(&ib->deadline, &tv, <)) {
302 ib_had_timeout(ib->ap);
303 upd7210_wr(u, IMR1, 0);
304 upd7210_wr(u, IMR2, 0);
306 bus_write_1(u->reg_res[0], imr3, 0x00);
307 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft RESET */
313 if (ib->mode != IDLE)
314 callout_reset(&ib->callout, hz / 5, gpib_ib_timeout, arg);
315 mtx_unlock(&u->mutex);
319 gpib_ib_wait_xfer(struct upd7210 *u, struct ibfoo *ib)
323 mtx_assert(&u->mutex, MA_OWNED);
324 while (ib->mode > BUSY) {
325 i = msleep(&ib->buflen, &u->mutex,
326 PZERO | PCATCH, "ibwxfr", 0);
328 ib_set_errno(ib->ap, i);
331 if (u->rreg[ISR1] & IXR1_ERR) {
332 ib_set_error(ib->ap, EABO); /* XXX ? */
336 if ((u->rreg[ISR1] & IXR1_ENDRX) != 0) {
337 ib->ap->__retval |= END;
338 ib->ap->__ibsta |= END;
340 if ((u->rreg[ISR2] & IXR2_SRQI) != 0) {
341 ib->ap->__retval |= SRQI;
342 ib->ap->__ibsta |= SRQI;
346 upd7210_wr(u, IMR1, 0);
347 upd7210_wr(u, IMR2, 0);
349 bus_write_1(u->reg_res[0], imr3, 0x00);
353 config_eos(struct upd7210 *u, struct handle *h)
359 upd7210_wr(u, EOSR, h->eos & 0xff);
363 upd7210_wr(u, EOSR, h->eos & 0xff);
368 upd7210_wr(u, AUXRA, C_AUXA | i);
372 * Look up the handle, and set the deadline if the handle has a timeout.
375 gethandle(struct upd7210 *u, struct ibarg *ap, struct handle **hp)
380 KASSERT(ap->__field & __F_HANDLE, ("gethandle without __F_HANDLE"));
382 LIST_FOREACH(h, &ib->handles, list) {
383 if (h->handle == ap->handle) {
388 ib_set_error(ap, EARG);
393 pio_cmd(struct upd7210 *u, u_char *cmd, int len)
399 if (ib->rdh != NULL || ib->wrh != NULL) {
400 upd7210_take_ctrl_async(u);
408 /* TNT5004 or TNT4882 in FIFO mode */
410 upd7210_wr(u, AUXMR, 0x51); /* holdoff immediately */
411 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
412 bus_write_1(u->reg_res[0], cfg, 0x80); /* CMD, xfer OUT, 8-bit FIFO */
413 bus_write_1(u->reg_res[0], imr3, 0x19); /* STOP IE, NFF IE, DONE IE */
414 bus_write_1(u->reg_res[0], cnt0, -len);
415 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
416 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
417 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
418 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
421 upd7210_wr(u, IMR2, IXR2_CO);
425 gpib_ib_wait_xfer(u, ib);
428 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
430 mtx_unlock(&u->mutex);
431 return (len - ib->buflen);
435 pio_odata(struct upd7210 *u, u_char *data, int len)
447 /* TNT5004 or TNT4882 in FIFO mode */
448 ib->mode = FIFO_ODATA;
449 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
451 bus_write_1(u->reg_res[0], cfg, 0x08); /* CCEN */
453 bus_write_1(u->reg_res[0], cfg, 0x00); /* xfer OUT, 8-bit FIFO */
454 bus_write_1(u->reg_res[0], imr3, 0x19); /* STOP IE, NFF IE, DONE IE */
455 bus_write_1(u->reg_res[0], cnt0, -len);
456 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
457 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
458 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
459 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
461 ib->mode = PIO_ODATA;
462 upd7210_wr(u, IMR1, IXR1_DO);
465 gpib_ib_wait_xfer(u, ib);
468 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
470 mtx_unlock(&u->mutex);
471 return (len - ib->buflen);
475 pio_idata(struct upd7210 *u, u_char *data, int len)
485 /* TNT5004 or TNT4882 in FIFO mode */
486 ib->mode = FIFO_IDATA;
487 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
488 bus_write_1(u->reg_res[0], cfg, 0x20); /* xfer IN, 8-bit FIFO */
489 bus_write_1(u->reg_res[0], cnt0, -len);
490 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
491 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
492 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
493 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
494 upd7210_wr(u, AUXMR, AUXMR_RFD);
495 bus_write_1(u->reg_res[0], imr3, 0x15); /* STOP IE, NEF IE, DONE IE */
497 ib->mode = PIO_IDATA;
498 upd7210_wr(u, IMR1, IXR1_DI);
501 gpib_ib_wait_xfer(u, ib);
504 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
506 mtx_unlock(&u->mutex);
507 return (len - ib->buflen);
511 dma_idata(struct upd7210 *u, u_char *data, int len)
516 KASSERT(u->dmachan >= 0, ("Bogus dmachan %d", u->dmachan));
518 ib->mode = DMA_IDATA;
519 isa_dmastart(ISADMA_READ, data, len, u->dmachan);
521 upd7210_wr(u, IMR1, IXR1_ENDRX);
522 upd7210_wr(u, IMR2, IMR2_DMAI);
523 gpib_ib_wait_xfer(u, ib);
524 mtx_unlock(&u->mutex);
525 j = isa_dmastatus(u->dmachan);
526 isa_dmadone(ISADMA_READ, data, len, u->dmachan);
531 ib_send_msg(struct ibfoo *ib, int msg)
539 buf[i++] = LAD | ib->h->pad;
541 buf[i++] = LAD | TAD | ib->h->sad;
544 j = pio_cmd(ib->u, buf, i);
546 ib_set_error(ib->ap, EABO); /* XXX ? */
551 ibask(struct ibfoo *ib)
554 ibdebug = ib->ap->option;
562 ibclr(struct ibfoo *ib)
565 return (ib_send_msg(ib, SDC));
570 #define ibconfig NULL
573 ibdev(struct ibfoo *ib)
577 h = malloc(sizeof *h, M_IBFOO, M_ZERO | M_WAITOK);
578 h->handle = alloc_unr(ib->unrhdr);
580 h->pad = ib->ap->pad;
581 h->sad = ib->ap->sad;
582 h->timeout = timeouts[ib->ap->tmo];
583 h->eot = ib->ap->eot;
584 h->eos = ib->ap->eos;
585 mtx_lock(&ib->u->mutex);
586 LIST_INSERT_HEAD(&ib->handles, h, list);
587 mtx_unlock(&ib->u->mutex);
588 ib->ap->__retval = h->handle;
595 ibdma(struct ibfoo *ib)
598 if (ib->u->dmachan < 0 && ib->ap->v)
599 return (ib_set_error(ib->ap, EARG));
600 ib->h->dma = ib->ap->v;
605 ibeos(struct ibfoo *ib)
608 ib->ap->__iberr = ib->h->eos;
609 ib->h->eos = ib->ap->eos;
610 if (ib->rdh == ib->h)
611 config_eos(ib->u, ib->h);
616 ibeot(struct ibfoo *ib)
619 ib->h->eot = ib->ap->eot;
632 ibloc(struct ibfoo *ib)
635 if (ib->h->kind == H_BOARD)
636 return (EOPNOTSUPP); /* XXX */
637 return (ib_send_msg(ib, GTL));
641 ibonl(struct ibfoo *ib)
645 return (EOPNOTSUPP); /* XXX */
646 mtx_lock(&ib->u->mutex);
647 LIST_REMOVE(ib->h, list);
648 mtx_unlock(&ib->u->mutex);
649 free(ib->h, M_IBFOO);
655 ibpad(struct ibfoo *ib)
658 ib->h->pad = ib->ap->pad;
667 ibrd(struct ibfoo *ib)
670 int i, j, error, bl, bc;
673 if (ib->h->kind == H_BOARD)
674 return (EOPNOTSUPP); /* XXX */
678 bp = malloc(bl, M_IBFOO, M_WAITOK);
680 if (ib->rdh != ib->h) {
685 buf[i++] = TAD | ib->h->pad;
687 buf[i++] = ib->h->sad;
688 i = pio_cmd(ib->u, buf, i);
689 config_eos(ib->u, ib->h);
693 upd7210_goto_standby(ib->u);
697 while (bc > 0 && ib->ap->__iberr == 0) {
698 j = imin(bc, PAGE_SIZE);
700 i = dma_idata(ib->u, bp, j);
702 i = pio_idata(ib->u, bp, j);
703 error = copyout(bp, dp , i);
706 ib->ap->__ibcnt += i;
712 upd7210_take_ctrl_async(ib->u);
726 ibsad(struct ibfoo *ib)
729 ib->h->sad = ib->ap->sad;
736 ibsic(struct ibfoo *ib)
739 upd7210_wr(ib->u, AUXMR, AUXMR_SIFC);
741 upd7210_wr(ib->u, AUXMR, AUXMR_CIFC);
750 ibtmo(struct ibfoo *ib)
753 ib->h->timeout = timeouts[ib->ap->tmo];
760 ibtrg(struct ibfoo *ib)
763 return (ib_send_msg(ib, GET));
769 ibwrt(struct ibfoo *ib)
774 if (ib->h->kind == H_BOARD)
776 bp = malloc(ib->ap->cnt, M_IBFOO, M_WAITOK);
777 /* XXX: bigger than PAGE_SIZE handling */
778 i = copyin(ib->ap->buffer, bp, ib->ap->cnt);
783 if (ib->wrh != ib->h) {
787 buf[i++] = LAD | ib->h->pad;
789 buf[i++] = LAD | TAD | ib->h->sad;
791 i = pio_cmd(ib->u, buf, i);
794 config_eos(ib->u, ib->h);
796 upd7210_goto_standby(ib->u);
797 ib->doeoi = ib->h->eot;
798 i = pio_odata(ib->u, bp, ib->ap->cnt);
799 upd7210_take_ctrl_async(ib->u);
807 #define ibwrtkey NULL
810 static struct ibhandler {
816 [__ID_IBASK] = { "ibask", H_EITHER, ibask, __F_HANDLE | __F_OPTION | __F_RETVAL },
817 [__ID_IBBNA] = { "ibbna", H_DEV, ibbna, __F_HANDLE | __F_BDNAME },
818 [__ID_IBCAC] = { "ibcac", H_BOARD, ibcac, __F_HANDLE | __F_V },
819 [__ID_IBCLR] = { "ibclr", H_DEV, ibclr, __F_HANDLE },
820 [__ID_IBCMD] = { "ibcmd", H_BOARD, ibcmd, __F_HANDLE | __F_BUFFER | __F_CNT },
821 [__ID_IBCMDA] = { "ibcmda", H_BOARD, ibcmda, __F_HANDLE | __F_BUFFER | __F_CNT },
822 [__ID_IBCONFIG] = { "ibconfig", H_EITHER, ibconfig, __F_HANDLE | __F_OPTION | __F_VALUE },
823 [__ID_IBDEV] = { "ibdev", 0, ibdev, __F_BOARDID | __F_PAD | __F_SAD | __F_TMO | __F_EOT | __F_EOS },
824 [__ID_IBDIAG] = { "ibdiag", H_EITHER, ibdiag, __F_HANDLE | __F_BUFFER | __F_CNT },
825 [__ID_IBDMA] = { "ibdma", H_EITHER, ibdma, __F_HANDLE | __F_V },
826 [__ID_IBEOS] = { "ibeos", H_EITHER, ibeos, __F_HANDLE | __F_EOS },
827 [__ID_IBEOT] = { "ibeot", H_EITHER, ibeot, __F_HANDLE | __F_EOT },
828 [__ID_IBEVENT] = { "ibevent", H_BOARD, ibevent, __F_HANDLE | __F_EVENT },
829 [__ID_IBFIND] = { "ibfind", 0, ibfind, __F_BDNAME },
830 [__ID_IBGTS] = { "ibgts", H_BOARD, ibgts, __F_HANDLE | __F_V },
831 [__ID_IBIST] = { "ibist", H_BOARD, ibist, __F_HANDLE | __F_V },
832 [__ID_IBLINES] = { "iblines", H_BOARD, iblines, __F_HANDLE | __F_LINES },
833 [__ID_IBLLO] = { "ibllo", H_EITHER, ibllo, __F_HANDLE },
834 [__ID_IBLN] = { "ibln", H_BOARD, ibln, __F_HANDLE | __F_PADVAL | __F_SADVAL | __F_LISTENFLAG },
835 [__ID_IBLOC] = { "ibloc", H_EITHER, ibloc, __F_HANDLE },
836 [__ID_IBONL] = { "ibonl", H_EITHER, ibonl, __F_HANDLE | __F_V },
837 [__ID_IBPAD] = { "ibpad", H_EITHER, ibpad, __F_HANDLE | __F_PAD },
838 [__ID_IBPCT] = { "ibpct", H_DEV, ibpct, __F_HANDLE },
839 [__ID_IBPOKE] = { "ibpoke", H_EITHER, ibpoke, __F_HANDLE | __F_OPTION | __F_VALUE },
840 [__ID_IBPPC] = { "ibppc", H_EITHER, ibppc, __F_HANDLE | __F_V },
841 [__ID_IBRD] = { "ibrd", H_EITHER, ibrd, __F_HANDLE | __F_BUFFER | __F_CNT },
842 [__ID_IBRDA] = { "ibrda", H_EITHER, ibrda, __F_HANDLE | __F_BUFFER | __F_CNT },
843 [__ID_IBRDF] = { "ibrdf", H_EITHER, ibrdf, __F_HANDLE | __F_FLNAME },
844 [__ID_IBRDKEY] = { "ibrdkey", H_EITHER, ibrdkey, __F_HANDLE | __F_BUFFER | __F_CNT },
845 [__ID_IBRPP] = { "ibrpp", H_EITHER, ibrpp, __F_HANDLE | __F_PPR },
846 [__ID_IBRSC] = { "ibrsc", H_BOARD, ibrsc, __F_HANDLE | __F_V },
847 [__ID_IBRSP] = { "ibrsp", H_DEV, ibrsp, __F_HANDLE | __F_SPR },
848 [__ID_IBRSV] = { "ibrsv", H_EITHER, ibrsv, __F_HANDLE | __F_V },
849 [__ID_IBSAD] = { "ibsad", H_EITHER, ibsad, __F_HANDLE | __F_SAD },
850 [__ID_IBSGNL] = { "ibsgnl", H_EITHER, ibsgnl, __F_HANDLE | __F_V },
851 [__ID_IBSIC] = { "ibsic", H_BOARD, ibsic, __F_HANDLE },
852 [__ID_IBSRE] = { "ibsre", H_BOARD, ibsre, __F_HANDLE | __F_V },
853 [__ID_IBSRQ] = { "ibsrq", H_EITHER, ibsrq, __F_FUNC },
854 [__ID_IBSTOP] = { "ibstop", H_EITHER, ibstop, __F_HANDLE },
855 [__ID_IBTMO] = { "ibtmo", H_EITHER, ibtmo, __F_HANDLE | __F_TMO },
856 [__ID_IBTRAP] = { "ibtrap", H_EITHER, ibtrap, __F_MASK | __F_MODE },
857 [__ID_IBTRG] = { "ibtrg", H_DEV, ibtrg, __F_HANDLE },
858 [__ID_IBWAIT] = { "ibwait", H_EITHER, ibwait, __F_HANDLE | __F_MASK },
859 [__ID_IBWRT] = { "ibwrt", H_EITHER, ibwrt, __F_HANDLE | __F_BUFFER | __F_CNT },
860 [__ID_IBWRTA] = { "ibwrta", H_EITHER, ibwrta, __F_HANDLE | __F_BUFFER | __F_CNT },
861 [__ID_IBWRTF] = { "ibwrtf", H_EITHER, ibwrtf, __F_HANDLE | __F_FLNAME },
862 [__ID_IBWRTKEY] = { "ibwrtkey", H_EITHER, ibwrtkey, __F_HANDLE | __F_BUFFER | __F_CNT },
863 [__ID_IBXTRC] = { "ibxtrc", H_EITHER, ibxtrc, __F_HANDLE | __F_BUFFER | __F_CNT },
866 static const u_int max_ibhandler = sizeof ibhandlers / sizeof ibhandlers[0];
869 ib_dump_args(struct ibhandler *ih, struct ibarg *ap)
872 if (ih->name != NULL)
873 printf("%s(", ih->name);
875 printf("ibinvalid(");
876 printf("[0x%x]", ap->__field);
877 if (ap->__field & __F_HANDLE) printf(" handle=%d", ap->handle);
878 if (ap->__field & __F_EOS) printf(" eos=0x%x", ap->eos);
879 if (ap->__field & __F_EOT) printf(" eot=%d", ap->eot);
880 if (ap->__field & __F_TMO) printf(" tmo=%d", ap->tmo);
881 if (ap->__field & __F_PAD) printf(" pad=0x%x", ap->pad);
882 if (ap->__field & __F_SAD) printf(" sad=0x%x", ap->sad);
883 if (ap->__field & __F_BUFFER) printf(" buffer=%p", ap->buffer);
884 if (ap->__field & __F_CNT) printf(" cnt=%ld", ap->cnt);
885 if (ap->__field & __F_V) printf(" v=%d/0x%x", ap->v, ap->v);
891 gpib_ib_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
901 mtx_unlock(&u->mutex);
905 mtx_unlock(&u->mutex);
907 if (u->dmachan >= 0) {
908 error = isa_dma_acquire(u->dmachan);
910 error = isa_dma_init(u->dmachan, PAGE_SIZE, M_WAITOK);
912 isa_dma_release(u->dmachan);
919 mtx_unlock(&u->mutex);
923 ib = malloc(sizeof *ib, M_IBFOO, M_WAITOK | M_ZERO);
924 LIST_INIT(&ib->handles);
925 callout_init(&ib->callout, CALLOUT_MPSAFE);
926 ib->unrhdr = new_unrhdr(0, INT_MAX, NULL);
930 u->irq = gpib_ib_irq;
932 upd7210_wr(u, AUXMR, AUXMR_CRST);
935 upd7210_wr(u, IMR1, 0x00);
936 upd7210_wr(u, IMR2, 0x00);
937 upd7210_wr(u, SPMR, 0x00);
938 upd7210_wr(u, ADR, 0x00);
939 upd7210_wr(u, ADR, ADR_ARS | ADR_DL | ADR_DT);
940 upd7210_wr(u, ADMR, ADMR_ADM0 | ADMR_TRM0 | ADMR_TRM1);
941 upd7210_wr(u, EOSR, 0x00);
942 upd7210_wr(u, AUXMR, C_ICR | 8);
943 upd7210_wr(u, AUXMR, C_PPR | PPR_U);
944 upd7210_wr(u, AUXMR, C_AUXA);
945 upd7210_wr(u, AUXMR, C_AUXB + 3);
946 upd7210_wr(u, AUXMR, C_AUXE + 0);
947 upd7210_wr(u, AUXMR, AUXMR_PON);
949 bus_write_1(u->reg_res[0], imr3, 0x00);
950 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft reset */
951 bus_write_1(u->reg_res[0], cmdr, 0x03); /* set system
954 upd7210_wr(u, AUXMR, AUXMR_CIFC);
956 upd7210_wr(u, AUXMR, AUXMR_SIFC);
957 upd7210_wr(u, AUXMR, AUXMR_SREN);
962 gpib_ib_close(struct cdev *dev, int oflags, int devtype, struct thread *td)
969 /* XXX: assert pointer consistency */
972 /* XXX: free handles */
976 if (u->dmachan >= 0) {
977 isa_dma_release(u->dmachan);
982 upd7210_wr(u, IMR1, 0x00);
983 upd7210_wr(u, IMR2, 0x00);
985 bus_write_1(u->reg_res[0], imr3, 0x00);
986 bus_write_1(u->reg_res[0], cmdr, 0x02); /* clear system
989 upd7210_wr(u, AUXMR, AUXMR_CRST);
991 mtx_unlock(&u->mutex);
996 gpib_ib_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
999 struct ibhandler *ih;
1004 struct timeval deadline, tv;
1009 /* We only support a single ioctl, everything else is a mistake */
1010 if (cmd != GPIB_IBFOO)
1013 /* Check the identifier and field-bitmap in the arguments. */
1015 if (ap->__ident < 0 || ap->__ident >= max_ibhandler)
1017 ih = &ibhandlers[ap->__ident];
1018 if (ap->__field != ih->args)
1022 ib_dump_args(ih, ap);
1024 if (ih->func == NULL)
1025 return (EOPNOTSUPP);
1032 if (ap->__field & __F_TMO) {
1033 if (ap->tmo < 0 || ap->tmo >= max_timeouts)
1034 return (ib_set_error(ap, EARG));
1037 if (ap->__field & __F_EOS) {
1038 if ((ap->eos & ~(REOS | XEOS | BIN | 0xff)) ||
1039 ((ap->eos & (BIN | 0x80)) == 0x80))
1040 return (ib_set_error(ap, EARG));
1042 if (ap->__field & __F_PAD) {
1043 if (ap->pad < 0 || ap->pad > 30)
1044 return (ib_set_error(ap, EARG));
1046 if (ap->__field & __F_SAD) {
1047 if (ap->sad != 0 && (ap->sad < 0x60 || ap->sad > 126))
1048 return (ib_set_error(ap, EARG));
1052 mtx_lock(&u->mutex);
1055 /* Find the handle, if any */
1057 if ((ap->__field & __F_HANDLE) && gethandle(u, ap, &h)) {
1058 mtx_unlock(&u->mutex);
1062 /* Check that the handle is the right kind */
1063 if (h != NULL && !(h->kind & ih->kind)) {
1064 mtx_unlock(&u->mutex);
1065 return (ib_set_error(ap, EARG));
1068 /* Set up handle and deadline */
1069 if (h != NULL && timevalisset(&h->timeout)) {
1070 getmicrouptime(&deadline);
1071 timevaladd(&deadline, &h->timeout);
1073 timevalclear(&deadline);
1076 /* Wait for the card to be(come) available, respect deadline */
1077 while(u->busy != 1) {
1078 error = msleep(ib, &u->mutex,
1079 PZERO | PCATCH, "gpib_ibioctl", hz / 10);
1082 mtx_unlock(&u->mutex);
1084 return(ib_set_error(ap, EABO));
1085 if (error == EWOULDBLOCK && timevalisset(&deadline)) {
1086 getmicrouptime(&tv);
1087 if (timevalcmp(&deadline, &tv, <))
1088 return(ib_had_timeout(ap));
1090 mtx_lock(&u->mutex);
1093 mtx_unlock(&u->mutex);
1095 /* Hand over deadline handling to the callout routine */
1099 ib->deadline = deadline;
1100 callout_reset(&ib->callout, hz / 5, gpib_ib_timeout, u);
1102 error = ih->func(ib);
1108 timevalclear(&deadline);
1109 callout_stop(&ib->callout);
1111 mtx_lock(&u->mutex);
1114 mtx_unlock(&u->mutex);
1117 return(ib_set_errno(ap, error));
1121 struct cdevsw gpib_ib_cdevsw = {
1122 .d_version = D_VERSION,
1123 .d_name = "gpib_ib",
1124 .d_open = gpib_ib_open,
1125 .d_ioctl = gpib_ib_ioctl,
1126 .d_close = gpib_ib_close,