]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/ieee488/ibfoo.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / ieee488 / ibfoo.c
1 /*-
2  * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3  * Copyright (c) 2010 Joerg Wunsch <joerg@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * High-level driver for µPD7210 based GPIB cards.
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #  define       IBDEBUG
35 #  undef        IBDEBUG
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/module.h>
44 #include <sys/rman.h>
45 #include <sys/bus.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/uio.h>
49 #include <sys/time.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <isa/isavar.h>
53
54 #include <dev/ieee488/ugpib.h>
55
56 #define UPD7210_SW_DRIVER
57 #include <dev/ieee488/upd7210.h>
58 #include <dev/ieee488/tnt4882.h>
59
60 static MALLOC_DEFINE(M_IBFOO, "IBFOO", "IBFOO");
61
62
63 /* ibfoo API */
64
65 #include <dev/ieee488/ibfoo_int.h>
66
67 /* XXX: This is really a bitmap */
68 enum h_kind {
69         H_DEV = 1,
70         H_BOARD = 2,
71         H_EITHER = 3
72 };
73
74 struct handle {
75         LIST_ENTRY(handle)      list;
76         int                     handle;
77         enum h_kind             kind;
78         int                     pad;
79         int                     sad;
80         struct timeval          timeout;
81         int                     eot;
82         int                     eos;
83         int                     dma;
84 };
85
86 struct ibfoo {
87         struct upd7210          *u;
88         LIST_HEAD(,handle)      handles;
89         struct unrhdr           *unrhdr;
90         struct callout          callout;
91         struct handle           *h;
92         struct ibarg            *ap;
93
94         enum {
95                 IDLE,
96                 BUSY,
97                 PIO_IDATA,
98                 PIO_ODATA,
99                 PIO_CMD,
100                 DMA_IDATA,
101                 FIFO_IDATA,
102                 FIFO_ODATA,
103                 FIFO_CMD
104         }                       mode;
105
106         struct timeval          deadline;
107
108         struct handle           *rdh;           /* addressed for read */
109         struct handle           *wrh;           /* addressed for write */
110
111         int                     doeoi;
112
113         u_char                  *buf;
114         u_int                   buflen;
115 };
116
117 typedef int ibhandler_t(struct ibfoo *ib);
118
119 static struct timeval timeouts[] = {
120         [TNONE] =       {    0,      0},
121         [T10us] =       {    0,     10},
122         [T30us] =       {    0,     30},
123         [T100us] =      {    0,    100},
124         [T300us] =      {    0,    300},
125         [T1ms] =        {    0,   1000},
126         [T3ms] =        {    0,   3000},
127         [T10ms] =       {    0,  10000},
128         [T30ms] =       {    0,  30000},
129         [T100ms] =      {    0, 100000},
130         [T300ms] =      {    0, 300000},
131         [T1s] =         {    1,      0},
132         [T3s] =         {    3,      0},
133         [T10s] =        {   10,      0},
134         [T30s] =        {   30,      0},
135         [T100s] =       {  100,      0},
136         [T300s] =       {  300,      0},
137         [T1000s] =      { 1000,      0}
138 };
139
140 static const u_int max_timeouts = sizeof timeouts / sizeof timeouts[0];
141
142 static int ibdebug;
143
144 static int
145 ib_set_error(struct ibarg *ap, int error)
146 {
147
148         if (ap->__iberr == 0)
149                 ap->__iberr = error;
150         ap->__ibsta |= ERR;
151         ap->__retval = ap->__ibsta;
152         return (0);
153 }
154
155 static int
156 ib_had_timeout(struct ibarg *ap)
157 {
158
159         ib_set_error(ap, EABO);
160         ap->__ibsta |= TIMO;
161         ap->__retval = ap->__ibsta;
162         return (0);
163 }
164
165 static int
166 ib_set_errno(struct ibarg *ap, int errno)
167 {
168
169         if (ap->__iberr == 0) {
170                 ap->__iberr = EDVR;
171                 ap->__ibcnt = errno;
172         }
173         ap->__ibsta |= ERR;
174         ap->__retval = ap->__ibsta;
175         return (0);
176 }
177
178 static int
179 gpib_ib_irq(struct upd7210 *u, int isr_3)
180 {
181         struct ibfoo *ib;
182
183         ib = u->ibfoo;
184
185         mtx_assert(&u->mutex, MA_OWNED);
186         switch (ib->mode) {
187         case PIO_CMD:
188                 if (!(u->rreg[ISR2] & IXR2_CO))
189                         return (0);
190                 if (ib->buflen == 0)
191                         break;
192                 upd7210_wr(u, CDOR, *ib->buf);
193                 ib->buf++;
194                 ib->buflen--;
195                 return (1);
196         case PIO_IDATA:
197                 if (!(u->rreg[ISR1] & IXR1_DI))
198                         return (0);
199                 *ib->buf = upd7210_rd(u, DIR);
200                 ib->buf++;
201                 ib->buflen--;
202                 if (ib->buflen == 0 || (u->rreg[ISR1] & IXR1_ENDRX))
203                         break;
204                 return (1);
205         case PIO_ODATA:
206                 if (!(u->rreg[ISR1] & IXR1_DO))
207                         return (0);
208                 if (ib->buflen == 0)
209                         break;
210                 if (ib->buflen == 1 && ib->doeoi)
211                         upd7210_wr(u, AUXMR, AUXMR_SEOI);
212                 upd7210_wr(u, CDOR, *ib->buf);
213                 ib->buf++;
214                 ib->buflen--;
215                 return (1);
216         case DMA_IDATA:
217                 if (!(u->rreg[ISR1] & IXR1_ENDRX))
218                         return (0);
219                 break;
220         case FIFO_IDATA:
221                 if (!(isr_3 & 0x15))
222                         return (0);
223                 while (ib->buflen != 0 && (isr_3 & 0x04 /* NEF */) != 0) {
224                         *ib->buf = bus_read_1(u->reg_res[0], fifob);
225                         ib->buf++;
226                         ib->buflen--;
227                         isr_3 = bus_read_1(u->reg_res[0], isr3);
228                 }
229                 if ((isr_3 & 0x01) != 0 /* xfr done */ ||
230                     (u->rreg[ISR1] & IXR1_ENDRX) != 0 ||
231                     ib->buflen == 0)
232                         break;
233                 if (isr_3 & 0x10)
234                         /* xfr stopped */
235                         bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
236                 upd7210_wr(u, AUXMR, AUXMR_RFD);
237                 return (1);
238         case FIFO_CMD:
239         case FIFO_ODATA:
240                 if (!(isr_3 & 0x19))
241                         return (0);
242                 if (ib->buflen == 0)
243                         /* xfr DONE */
244                         break;
245                 while (ib->buflen != 0 && (isr_3 & 0x08 /* NFF */) != 0) {
246                         bus_write_1(u->reg_res[0], fifob, *ib->buf);
247                         ib->buf++;
248                         ib->buflen--;
249                         isr_3 = bus_read_1(u->reg_res[0], isr3);
250                 }
251                 if (isr_3 & 0x10)
252                         /* xfr stopped */
253                         bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
254                 if (ib->buflen == 0)
255                         /* no more NFF interrupts wanted */
256                         bus_write_1(u->reg_res[0], imr3, 0x11); /* STOP IE, DONE IE */
257                 return (1);
258         default:
259                 return (0);
260         }
261         upd7210_wr(u, IMR1, 0);
262         upd7210_wr(u, IMR2, 0);
263         if (u->use_fifo) {
264                 bus_write_1(u->reg_res[0], imr3, 0x00);
265                 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft RESET */
266         }
267         ib->mode = BUSY;
268         wakeup(&ib->buflen);
269         return (1);
270 }
271
272 static void
273 gpib_ib_timeout(void *arg)
274 {
275         struct upd7210 *u;
276         struct ibfoo *ib;
277         struct timeval tv;
278         u_int isr_3;
279
280         u = arg;
281         ib = u->ibfoo;
282         mtx_lock(&u->mutex);
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);
287                 ib->mode = BUSY;
288                 wakeup(&ib->buflen);
289         }
290         if (ib->mode > BUSY) {
291                 upd7210_rd(u, ISR1);
292                 upd7210_rd(u, ISR2);
293                 if (u->use_fifo)
294                         isr_3 = bus_read_1(u->reg_res[0], isr3);
295                 else
296                         isr_3 = 0;
297                 gpib_ib_irq(u, isr_3);
298         }
299         if (ib->mode != IDLE && timevalisset(&ib->deadline)) {
300                 getmicrouptime(&tv);
301                 if (timevalcmp(&ib->deadline, &tv, <)) {
302                         ib_had_timeout(ib->ap);
303                         upd7210_wr(u, IMR1, 0);
304                         upd7210_wr(u, IMR2, 0);
305                         if (u->use_fifo) {
306                                 bus_write_1(u->reg_res[0], imr3, 0x00);
307                                 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft RESET */
308                         }
309                         ib->mode = BUSY;
310                         wakeup(&ib->buflen);
311                 }
312         }
313         if (ib->mode != IDLE)
314                 callout_reset(&ib->callout, hz / 5, gpib_ib_timeout, arg);
315         mtx_unlock(&u->mutex);
316 }
317
318 static void
319 gpib_ib_wait_xfer(struct upd7210 *u, struct ibfoo *ib)
320 {
321         int i;
322
323         mtx_assert(&u->mutex, MA_OWNED);
324         while (ib->mode > BUSY) {
325                 i = msleep(&ib->buflen, &u->mutex,
326                     PZERO | PCATCH, "ibwxfr", 0);
327                 if (i == EINTR) {
328                         ib_set_errno(ib->ap, i);
329                         break;
330                 }
331                 if (u->rreg[ISR1] & IXR1_ERR) {
332                         ib_set_error(ib->ap, EABO);     /* XXX ? */
333                         break;
334                 }
335         }
336         ib->mode = BUSY;
337         ib->buf = NULL;
338         upd7210_wr(u, IMR1, 0);
339         upd7210_wr(u, IMR2, 0);
340         if (u->use_fifo)
341                 bus_write_1(u->reg_res[0], imr3, 0x00);
342 }
343
344 static void
345 config_eos(struct upd7210 *u, struct handle *h)
346 {
347         int i;
348
349         i = 0;
350         if (h->eos & REOS) {
351                 upd7210_wr(u, EOSR, h->eos & 0xff);
352                 i |= AUXA_REOS;
353         }
354         if (h->eos & XEOS) {
355                 upd7210_wr(u, EOSR, h->eos & 0xff);
356                 i |= AUXA_XEOS;
357         }
358         if (h->eos & BIN)
359                 i |= AUXA_BIN;
360         upd7210_wr(u, AUXRA, C_AUXA | i);
361 }
362
363 /*
364  * Look up the handle, and set the deadline if the handle has a timeout.
365  */
366 static int
367 gethandle(struct upd7210 *u, struct ibarg *ap, struct handle **hp)
368 {
369         struct ibfoo *ib;
370         struct handle *h;
371
372         KASSERT(ap->__field & __F_HANDLE, ("gethandle without __F_HANDLE"));
373         ib = u->ibfoo;
374         LIST_FOREACH(h, &ib->handles, list) {
375                 if (h->handle == ap->handle) {
376                         *hp = h;
377                         return (0);
378                 }
379         }
380         ib_set_error(ap, EARG);
381         return (1);
382 }
383
384 static int
385 pio_cmd(struct upd7210 *u, u_char *cmd, int len)
386 {
387         struct ibfoo *ib;
388
389         ib = u->ibfoo;
390
391         if (ib->rdh != NULL || ib->wrh != NULL) {
392                 upd7210_take_ctrl_async(u);
393                 ib->rdh = NULL;
394                 ib->wrh = NULL;
395         }
396         mtx_lock(&u->mutex);
397         ib->buf = cmd;
398         ib->buflen = len;
399         if (u->use_fifo) {
400                 /* TNT5004 or TNT4882 in FIFO mode */
401                 ib->mode = FIFO_CMD;
402                 upd7210_wr(u, AUXMR, 0x51);             /* holdoff immediately */
403                 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
404                 bus_write_1(u->reg_res[0], cfg, 0x80); /* CMD, xfer OUT, 8-bit FIFO */
405                 bus_write_1(u->reg_res[0], imr3, 0x19); /* STOP IE, NFF IE, DONE IE */
406                 bus_write_1(u->reg_res[0], cnt0, -len);
407                 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
408                 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
409                 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
410                 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
411         } else {
412                 ib->mode = PIO_CMD;
413                 upd7210_wr(u, IMR2, IXR2_CO);
414                 gpib_ib_irq(u, 0);
415         }
416
417         gpib_ib_wait_xfer(u, ib);
418
419         if (u->use_fifo)
420                 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
421
422         mtx_unlock(&u->mutex);
423         return (len - ib->buflen);
424 }
425
426 static int
427 pio_odata(struct upd7210 *u, u_char *data, int len)
428 {
429         struct ibfoo *ib;
430
431         ib = u->ibfoo;
432
433         if (len == 0)
434                 return (0);
435         mtx_lock(&u->mutex);
436         ib->buf = data;
437         ib->buflen = len;
438         if (u->use_fifo) {
439                 /* TNT5004 or TNT4882 in FIFO mode */
440                 ib->mode = FIFO_ODATA;
441                 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
442                 if (ib->doeoi)
443                         bus_write_1(u->reg_res[0], cfg, 0x08); /* CCEN */
444                 else
445                         bus_write_1(u->reg_res[0], cfg, 0x00); /* xfer OUT, 8-bit FIFO */
446                 bus_write_1(u->reg_res[0], imr3, 0x19); /* STOP IE, NFF IE, DONE IE */
447                 bus_write_1(u->reg_res[0], cnt0, -len);
448                 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
449                 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
450                 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
451                 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
452         } else {
453                 ib->mode = PIO_ODATA;
454                 upd7210_wr(u, IMR1, IXR1_DO);
455         }
456
457         gpib_ib_wait_xfer(u, ib);
458
459         if (u->use_fifo)
460                 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
461
462         mtx_unlock(&u->mutex);
463         return (len - ib->buflen);
464 }
465
466 static int
467 pio_idata(struct upd7210 *u, u_char *data, int len)
468 {
469         struct ibfoo *ib;
470
471         ib = u->ibfoo;
472
473         mtx_lock(&u->mutex);
474         ib->buf = data;
475         ib->buflen = len;
476         if (u->use_fifo) {
477                 /* TNT5004 or TNT4882 in FIFO mode */
478                 ib->mode = FIFO_IDATA;
479                 bus_write_1(u->reg_res[0], cmdr, 0x10); /* reset FIFO */
480                 bus_write_1(u->reg_res[0], cfg, 0x20); /* xfer IN, 8-bit FIFO */
481                 bus_write_1(u->reg_res[0], cnt0, -len);
482                 bus_write_1(u->reg_res[0], cnt1, (-len) >> 8);
483                 bus_write_1(u->reg_res[0], cnt2, (-len) >> 16);
484                 bus_write_1(u->reg_res[0], cnt3, (-len) >> 24);
485                 bus_write_1(u->reg_res[0], cmdr, 0x04); /* GO */
486                 upd7210_wr(u, AUXMR, AUXMR_RFD);
487                 bus_write_1(u->reg_res[0], imr3, 0x15); /* STOP IE, NEF IE, DONE IE */
488         } else {
489                 ib->mode = PIO_IDATA;
490                 upd7210_wr(u, IMR1, IXR1_DI);
491         }
492
493         gpib_ib_wait_xfer(u, ib);
494
495         if (u->use_fifo)
496                 bus_write_1(u->reg_res[0], cmdr, 0x08); /* STOP */
497
498         mtx_unlock(&u->mutex);
499         return (len - ib->buflen);
500 }
501
502 static int
503 dma_idata(struct upd7210 *u, u_char *data, int len)
504 {
505         int j;
506         struct ibfoo *ib;
507
508         KASSERT(u->dmachan >= 0, ("Bogus dmachan %d", u->dmachan));
509         ib = u->ibfoo;
510         ib->mode = DMA_IDATA;
511         mtx_lock(&Giant);
512         isa_dmastart(ISADMA_READ, data, len, u->dmachan);
513         mtx_unlock(&Giant);
514         mtx_lock(&u->mutex);
515         upd7210_wr(u, IMR1, IXR1_ENDRX);
516         upd7210_wr(u, IMR2, IMR2_DMAI);
517         gpib_ib_wait_xfer(u, ib);
518         mtx_unlock(&u->mutex);
519         mtx_lock(&Giant);
520         j = isa_dmastatus(u->dmachan);
521         isa_dmadone(ISADMA_READ, data, len, u->dmachan);
522         mtx_unlock(&Giant);
523         return (len - j);
524 }
525
526 static int
527 ib_send_msg(struct ibfoo *ib, int msg)
528 {
529         u_char buf[10];
530         int i, j;
531
532         i = 0;
533         buf[i++] = UNT;
534         buf[i++] = UNL;
535         buf[i++] = LAD | ib->h->pad;
536         if (ib->h->sad)
537                 buf[i++] = LAD | TAD | ib->h->sad;
538         buf[i++] = TAD | 0;
539         buf[i++] = msg;
540         j = pio_cmd(ib->u, buf, i);
541         if (i != j)
542                 ib_set_error(ib->ap, EABO); /* XXX ? */
543         return (0);
544 }
545
546 static int
547 ibask(struct ibfoo *ib)
548 {       /* XXX */
549
550         ibdebug = ib->ap->option;
551         return (0);
552 }
553
554 #define ibbna NULL
555 #define ibcac NULL
556
557 static int
558 ibclr(struct ibfoo *ib)
559 {
560
561         return (ib_send_msg(ib, SDC));
562 }
563
564 #define ibcmd NULL
565 #define ibcmda NULL
566 #define ibconfig NULL
567
568 static int
569 ibdev(struct ibfoo *ib)
570 {       /* TBD */
571         struct handle *h;
572
573         h = malloc(sizeof *h, M_IBFOO, M_ZERO | M_WAITOK);
574         h->handle = alloc_unr(ib->unrhdr);
575         h->kind = H_DEV;
576         h->pad = ib->ap->pad;
577         h->sad = ib->ap->sad;
578         h->timeout = timeouts[ib->ap->tmo];
579         h->eot = ib->ap->eot;
580         h->eos = ib->ap->eos;
581         mtx_lock(&ib->u->mutex);
582         LIST_INSERT_HEAD(&ib->handles, h, list);
583         mtx_unlock(&ib->u->mutex);
584         ib->ap->__retval = h->handle;
585         return (0);
586 }
587
588 #define ibdiag NULL
589
590 static int
591 ibdma(struct ibfoo *ib)
592 {
593
594         if (ib->u->dmachan < 0 && ib->ap->v)
595                 return (ib_set_error(ib->ap, EARG));
596         ib->h->dma = ib->ap->v;
597         return (0);
598 }
599
600 static int
601 ibeos(struct ibfoo *ib)
602 {
603
604         ib->ap->__iberr = ib->h->eos;
605         ib->h->eos = ib->ap->eos;
606         if (ib->rdh == ib->h)
607                 config_eos(ib->u, ib->h);
608         return (0);
609 }
610
611 static int
612 ibeot(struct ibfoo *ib)
613 {
614
615         ib->h->eot = ib->ap->eot;
616         return (0);
617 }
618
619 #define ibevent NULL
620 #define ibfind NULL
621 #define ibgts NULL
622 #define ibist NULL
623 #define iblines NULL
624 #define ibllo NULL
625 #define ibln NULL
626
627 static int
628 ibloc(struct ibfoo *ib)
629 {       /* XXX */
630
631         if (ib->h->kind == H_BOARD)
632                 return (EOPNOTSUPP); /* XXX */
633         return (ib_send_msg(ib, GTL));
634 }
635
636 static int
637 ibonl(struct ibfoo *ib)
638 {       /* XXX */
639
640         if (ib->ap->v)
641                 return (EOPNOTSUPP);    /* XXX */
642         mtx_lock(&ib->u->mutex);
643         LIST_REMOVE(ib->h, list);
644         mtx_unlock(&ib->u->mutex);
645         free(ib->h, M_IBFOO);
646         ib->h = NULL;
647         return (0);
648 }
649
650 static int
651 ibpad(struct ibfoo *ib)
652 {
653
654         ib->h->pad = ib->ap->pad;
655         return (0);
656 }
657
658 #define ibpct NULL
659 #define ibpoke NULL
660 #define ibppc NULL
661
662 static int
663 ibrd(struct ibfoo *ib)
664 {       /* TBD */
665         u_char buf[10], *bp;
666         int i, j, error, bl, bc;
667         u_char *dp;
668
669         if (ib->h->kind == H_BOARD)
670                 return (EOPNOTSUPP); /* XXX */
671         bl = ib->ap->cnt;
672         if (bl > PAGE_SIZE)
673                 bl = PAGE_SIZE;
674         bp = malloc(bl, M_IBFOO, M_WAITOK);
675
676         if (ib->rdh != ib->h) {
677                 i = 0;
678                 buf[i++] = UNT;
679                 buf[i++] = UNL;
680                 buf[i++] = LAD | 0;
681                 buf[i++] = TAD | ib->h->pad;
682                 if (ib->h->sad)
683                         buf[i++] = ib->h->sad;
684                 i = pio_cmd(ib->u, buf, i);
685                 config_eos(ib->u, ib->h);
686                 ib->rdh = ib->h;
687                 ib->wrh = NULL;
688         }
689         upd7210_goto_standby(ib->u);
690         dp = ib->ap->buffer;
691         bc = ib->ap->cnt;
692         error = 0;
693         while (bc > 0 && ib->ap->__iberr == 0) {
694                 j = imin(bc, PAGE_SIZE);
695                 if (ib->h->dma)
696                         i = dma_idata(ib->u, bp, j);
697                 else
698                         i = pio_idata(ib->u, bp, j);
699                 error = copyout(bp, dp , i);
700                 if (error)
701                         break;
702                 ib->ap->__ibcnt += i;
703                 if (i != j)
704                         break;
705                 bc -= i;
706                 dp += i;
707         }
708         upd7210_take_ctrl_async(ib->u);
709         free(bp, M_IBFOO);
710         return (error);
711 }
712
713 #define ibrda NULL
714 #define ibrdf NULL
715 #define ibrdkey NULL
716 #define ibrpp NULL
717 #define ibrsc NULL
718 #define ibrsp NULL
719 #define ibrsv NULL
720
721 static int
722 ibsad(struct ibfoo *ib)
723 {
724
725         ib->h->sad = ib->ap->sad;
726         return (0);
727 }
728
729 #define ibsgnl NULL
730
731 static int
732 ibsic(struct ibfoo *ib)
733 {       /* TBD */
734
735         upd7210_wr(ib->u, AUXMR, AUXMR_SIFC);
736         DELAY(100);
737         upd7210_wr(ib->u, AUXMR, AUXMR_CIFC);
738         return (0);
739 }
740
741 #define ibsre NULL
742 #define ibsrq NULL
743 #define ibstop NULL
744
745 static int
746 ibtmo(struct ibfoo *ib)
747 {
748
749         ib->h->timeout = timeouts[ib->ap->tmo];
750         return (0);
751 }
752
753 #define ibtrap NULL
754
755 static int
756 ibtrg(struct ibfoo *ib)
757 {
758
759         return (ib_send_msg(ib, GET));
760 }
761
762 #define ibwait NULL
763
764 static int
765 ibwrt(struct ibfoo *ib)
766 {       /* XXX */
767         u_char buf[10], *bp;
768         int i;
769
770         if (ib->h->kind == H_BOARD)
771                 return (EOPNOTSUPP);
772         bp = malloc(ib->ap->cnt, M_IBFOO, M_WAITOK);
773         /* XXX: bigger than PAGE_SIZE handling */
774         i = copyin(ib->ap->buffer, bp, ib->ap->cnt);
775         if (i) {
776                 free(bp, M_IBFOO);
777                 return (i);
778         }
779         if (ib->wrh != ib->h) {
780                 i = 0;
781                 buf[i++] = UNT;
782                 buf[i++] = UNL;
783                 buf[i++] = LAD | ib->h->pad;
784                 if (ib->h->sad)
785                         buf[i++] = LAD | TAD | ib->h->sad;
786                 buf[i++] = TAD | 0;
787                 i = pio_cmd(ib->u, buf, i);
788                 ib->rdh = NULL;
789                 ib->wrh = ib->h;
790                 config_eos(ib->u, ib->h);
791         }
792         upd7210_goto_standby(ib->u);
793         ib->doeoi = ib->h->eot;
794         i = pio_odata(ib->u, bp, ib->ap->cnt);
795         upd7210_take_ctrl_async(ib->u);
796         ib->ap->__ibcnt = i;
797         free(bp, M_IBFOO);
798         return (0);
799 }
800
801 #define ibwrta NULL
802 #define ibwrtf NULL
803 #define ibwrtkey NULL
804 #define ibxtrc NULL
805
806 static struct ibhandler {
807         const char      *name;
808         enum h_kind     kind;
809         ibhandler_t     *func;
810         u_int           args;
811 } ibhandlers[] = {
812         [__ID_IBASK] =          { "ibask",      H_EITHER,       ibask,          __F_HANDLE | __F_OPTION | __F_RETVAL },
813         [__ID_IBBNA] =          { "ibbna",      H_DEV,          ibbna,          __F_HANDLE | __F_BDNAME },
814         [__ID_IBCAC] =          { "ibcac",      H_BOARD,        ibcac,          __F_HANDLE | __F_V },
815         [__ID_IBCLR] =          { "ibclr",      H_DEV,          ibclr,          __F_HANDLE },
816         [__ID_IBCMD] =          { "ibcmd",      H_BOARD,        ibcmd,          __F_HANDLE | __F_BUFFER | __F_CNT },
817         [__ID_IBCMDA] =         { "ibcmda",     H_BOARD,        ibcmda,         __F_HANDLE | __F_BUFFER | __F_CNT },
818         [__ID_IBCONFIG] =       { "ibconfig",   H_EITHER,       ibconfig,       __F_HANDLE | __F_OPTION | __F_VALUE },
819         [__ID_IBDEV] =          { "ibdev",      0,              ibdev,          __F_BOARDID | __F_PAD | __F_SAD | __F_TMO | __F_EOT | __F_EOS },
820         [__ID_IBDIAG] =         { "ibdiag",     H_EITHER,       ibdiag,         __F_HANDLE | __F_BUFFER | __F_CNT },
821         [__ID_IBDMA] =          { "ibdma",      H_EITHER,       ibdma,          __F_HANDLE | __F_V },
822         [__ID_IBEOS] =          { "ibeos",      H_EITHER,       ibeos,          __F_HANDLE | __F_EOS },
823         [__ID_IBEOT] =          { "ibeot",      H_EITHER,       ibeot,          __F_HANDLE | __F_EOT },
824         [__ID_IBEVENT] =        { "ibevent",    H_BOARD,        ibevent,        __F_HANDLE | __F_EVENT },
825         [__ID_IBFIND] =         { "ibfind",     0,              ibfind,         __F_BDNAME },
826         [__ID_IBGTS] =          { "ibgts",      H_BOARD,        ibgts,          __F_HANDLE | __F_V },
827         [__ID_IBIST] =          { "ibist",      H_BOARD,        ibist,          __F_HANDLE | __F_V },
828         [__ID_IBLINES] =        { "iblines",    H_BOARD,        iblines,        __F_HANDLE | __F_LINES },
829         [__ID_IBLLO] =          { "ibllo",      H_EITHER,       ibllo,          __F_HANDLE },
830         [__ID_IBLN] =           { "ibln",       H_BOARD,        ibln,           __F_HANDLE | __F_PADVAL | __F_SADVAL | __F_LISTENFLAG },
831         [__ID_IBLOC] =          { "ibloc",      H_EITHER,       ibloc,          __F_HANDLE },
832         [__ID_IBONL] =          { "ibonl",      H_EITHER,       ibonl,          __F_HANDLE | __F_V },
833         [__ID_IBPAD] =          { "ibpad",      H_EITHER,       ibpad,          __F_HANDLE | __F_PAD },
834         [__ID_IBPCT] =          { "ibpct",      H_DEV,          ibpct,          __F_HANDLE },
835         [__ID_IBPOKE] =         { "ibpoke",     H_EITHER,       ibpoke,         __F_HANDLE | __F_OPTION | __F_VALUE },
836         [__ID_IBPPC] =          { "ibppc",      H_EITHER,       ibppc,          __F_HANDLE | __F_V },
837         [__ID_IBRD] =           { "ibrd",       H_EITHER,       ibrd,           __F_HANDLE | __F_BUFFER | __F_CNT },
838         [__ID_IBRDA] =          { "ibrda",      H_EITHER,       ibrda,          __F_HANDLE | __F_BUFFER | __F_CNT },
839         [__ID_IBRDF] =          { "ibrdf",      H_EITHER,       ibrdf,          __F_HANDLE | __F_FLNAME },
840         [__ID_IBRDKEY] =        { "ibrdkey",    H_EITHER,       ibrdkey,        __F_HANDLE | __F_BUFFER | __F_CNT },
841         [__ID_IBRPP] =          { "ibrpp",      H_EITHER,       ibrpp,          __F_HANDLE | __F_PPR },
842         [__ID_IBRSC] =          { "ibrsc",      H_BOARD,        ibrsc,          __F_HANDLE | __F_V },
843         [__ID_IBRSP] =          { "ibrsp",      H_DEV,          ibrsp,          __F_HANDLE | __F_SPR },
844         [__ID_IBRSV] =          { "ibrsv",      H_EITHER,       ibrsv,          __F_HANDLE | __F_V },
845         [__ID_IBSAD] =          { "ibsad",      H_EITHER,       ibsad,          __F_HANDLE | __F_SAD },
846         [__ID_IBSGNL] =         { "ibsgnl",     H_EITHER,       ibsgnl,         __F_HANDLE | __F_V },
847         [__ID_IBSIC] =          { "ibsic",      H_BOARD,        ibsic,          __F_HANDLE },
848         [__ID_IBSRE] =          { "ibsre",      H_BOARD,        ibsre,          __F_HANDLE | __F_V },
849         [__ID_IBSRQ] =          { "ibsrq",      H_EITHER,       ibsrq,          __F_FUNC },
850         [__ID_IBSTOP] =         { "ibstop",     H_EITHER,       ibstop,         __F_HANDLE },
851         [__ID_IBTMO] =          { "ibtmo",      H_EITHER,       ibtmo,          __F_HANDLE | __F_TMO },
852         [__ID_IBTRAP] =         { "ibtrap",     H_EITHER,       ibtrap,         __F_MASK | __F_MODE },
853         [__ID_IBTRG] =          { "ibtrg",      H_DEV,          ibtrg,          __F_HANDLE },
854         [__ID_IBWAIT] =         { "ibwait",     H_EITHER,       ibwait,         __F_HANDLE | __F_MASK },
855         [__ID_IBWRT] =          { "ibwrt",      H_EITHER,       ibwrt,          __F_HANDLE | __F_BUFFER | __F_CNT },
856         [__ID_IBWRTA] =         { "ibwrta",     H_EITHER,       ibwrta,         __F_HANDLE | __F_BUFFER | __F_CNT },
857         [__ID_IBWRTF] =         { "ibwrtf",     H_EITHER,       ibwrtf,         __F_HANDLE | __F_FLNAME },
858         [__ID_IBWRTKEY] =       { "ibwrtkey",   H_EITHER,       ibwrtkey,       __F_HANDLE | __F_BUFFER | __F_CNT },
859         [__ID_IBXTRC] =         { "ibxtrc",     H_EITHER,       ibxtrc,         __F_HANDLE | __F_BUFFER | __F_CNT },
860 };
861
862 static const u_int max_ibhandler = sizeof ibhandlers / sizeof ibhandlers[0];
863
864 static void
865 ib_dump_args(struct ibhandler *ih, struct ibarg *ap)
866 {
867
868         if (ih->name != NULL)
869                 printf("%s(", ih->name);
870         else
871                 printf("ibinvalid(");
872         printf("[0x%x]", ap->__field);
873         if (ap->__field & __F_HANDLE)   printf(" handle=%d", ap->handle);
874         if (ap->__field & __F_EOS)      printf(" eos=0x%x", ap->eos);
875         if (ap->__field & __F_EOT)      printf(" eot=%d", ap->eot);
876         if (ap->__field & __F_TMO)      printf(" tmo=%d", ap->tmo);
877         if (ap->__field & __F_PAD)      printf(" pad=0x%x", ap->pad);
878         if (ap->__field & __F_SAD)      printf(" sad=0x%x", ap->sad);
879         if (ap->__field & __F_BUFFER)   printf(" buffer=%p", ap->buffer);
880         if (ap->__field & __F_CNT)      printf(" cnt=%ld", ap->cnt);
881         if (ap->__field & __F_V)        printf(" v=%d/0x%x", ap->v, ap->v);
882         /* XXX more ... */
883         printf(")\n");
884 }
885
886 static int
887 gpib_ib_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
888 {
889         struct upd7210 *u;
890         struct ibfoo *ib;
891         int error = 0;
892
893         u = dev->si_drv1;
894
895         mtx_lock(&u->mutex);
896         if (u->busy) {
897                 mtx_unlock(&u->mutex);
898                 return (EBUSY);
899         }
900         u->busy = 1;
901         mtx_unlock(&u->mutex);
902
903         if (u->dmachan >= 0) {
904                 mtx_lock(&Giant);
905                 error = isa_dma_acquire(u->dmachan);
906                 if (!error) {
907                         error = isa_dma_init(u->dmachan, PAGE_SIZE, M_WAITOK);
908                         if (error)
909                                 isa_dma_release(u->dmachan);
910                 }
911                 mtx_unlock(&Giant);
912         }
913
914         if (error) {
915                 mtx_lock(&u->mutex);
916                 u->busy = 0;
917                 mtx_unlock(&u->mutex);
918                 return (error);
919         }
920
921         ib = malloc(sizeof *ib, M_IBFOO, M_WAITOK | M_ZERO);
922         LIST_INIT(&ib->handles);
923         callout_init(&ib->callout, CALLOUT_MPSAFE);
924         ib->unrhdr = new_unrhdr(0, INT_MAX, NULL);
925         dev->si_drv2 = ib;
926         ib->u = u;
927         u->ibfoo = ib;
928         u->irq = gpib_ib_irq;
929
930         upd7210_wr(u, AUXMR, AUXMR_CRST);
931         DELAY(10000);
932         DELAY(1000);
933         upd7210_wr(u, IMR1, 0x00);
934         upd7210_wr(u, IMR2, 0x00);
935         upd7210_wr(u, SPMR, 0x00);
936         upd7210_wr(u, ADR, 0x00);
937         upd7210_wr(u, ADR, ADR_ARS | ADR_DL | ADR_DT);
938         upd7210_wr(u, ADMR, ADMR_ADM0 | ADMR_TRM0 | ADMR_TRM1);
939         upd7210_wr(u, EOSR, 0x00);
940         upd7210_wr(u, AUXMR, C_ICR | 8);
941         upd7210_wr(u, AUXMR, C_PPR | PPR_U);
942         upd7210_wr(u, AUXMR, C_AUXA);
943         upd7210_wr(u, AUXMR, C_AUXB + 3);
944         upd7210_wr(u, AUXMR, C_AUXE + 0);
945         upd7210_wr(u, AUXMR, AUXMR_PON);
946         if (u->use_fifo) {
947                 bus_write_1(u->reg_res[0], imr3, 0x00);
948                 bus_write_1(u->reg_res[0], cmdr, 0x22); /* soft reset */
949                 bus_write_1(u->reg_res[0], cmdr, 0x03); /* set system
950                                                          * controller bit */
951         }
952         upd7210_wr(u, AUXMR, AUXMR_CIFC);
953         DELAY(100);
954         upd7210_wr(u, AUXMR, AUXMR_SIFC);
955         upd7210_wr(u, AUXMR, AUXMR_SREN);
956         return (0);
957 }
958
959 static int
960 gpib_ib_close(struct cdev *dev, int oflags, int devtype, struct thread *td)
961 {
962         struct upd7210 *u;
963         struct ibfoo *ib;
964
965         u = dev->si_drv1;
966         ib = dev->si_drv2;
967         /* XXX: assert pointer consistency */
968
969         u->ibfoo = NULL;
970         /* XXX: free handles */
971         dev->si_drv2 = NULL;
972         free(ib, M_IBFOO);
973
974         if (u->dmachan >= 0) {
975                 mtx_lock(&Giant);
976                 isa_dma_release(u->dmachan);
977                 mtx_unlock(&Giant);
978         }
979         mtx_lock(&u->mutex);
980         u->busy = 0;
981         ibdebug = 0;
982         upd7210_wr(u, IMR1, 0x00);
983         upd7210_wr(u, IMR2, 0x00);
984         if (u->use_fifo) {
985                 bus_write_1(u->reg_res[0], imr3, 0x00);
986                 bus_write_1(u->reg_res[0], cmdr, 0x02); /* clear system
987                                                          * controller bit */
988         }
989         upd7210_wr(u, AUXMR, AUXMR_CRST);
990         DELAY(10000);
991         mtx_unlock(&u->mutex);
992         return (0);
993 }
994
995 static int
996 gpib_ib_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
997 {
998         struct ibarg *ap;
999         struct ibhandler *ih;
1000         struct handle *h;
1001         struct upd7210 *u;
1002         struct ibfoo *ib;
1003         int error;
1004         struct timeval deadline, tv;
1005
1006         u = dev->si_drv1;
1007         ib = u->ibfoo;
1008
1009         /* We only support a single ioctl, everything else is a mistake */
1010         if (cmd != GPIB_IBFOO)
1011                 return (ENOIOCTL);
1012
1013         /* Check the identifier and field-bitmap in the arguments.  */
1014         ap = (void *)data;
1015         if (ap->__ident < 0 || ap->__ident >= max_ibhandler)
1016                 return (EINVAL);
1017         ih = &ibhandlers[ap->__ident];
1018         if (ap->__field != ih->args)
1019                 return (EINVAL);
1020
1021         if (ibdebug)
1022                 ib_dump_args(ih, ap);
1023
1024         if (ih->func == NULL)
1025                 return (EOPNOTSUPP);
1026
1027         ap->__iberr = 0;
1028         ap->__ibsta = 0;
1029         ap->__ibcnt = 0;
1030         ap->retval = 0;
1031
1032         if (ap->__field & __F_TMO) {
1033                 if (ap->tmo < 0 || ap->tmo >= max_timeouts)
1034                         return (ib_set_error(ap, EARG));
1035         }
1036
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));
1041         }
1042         if (ap->__field & __F_PAD) {
1043                 if (ap->pad < 0 || ap->pad > 30)
1044                         return (ib_set_error(ap, EARG));
1045         }
1046         if (ap->__field & __F_SAD) {
1047                 if (ap->sad != 0 && (ap->sad < 0x60 || ap->sad > 126))
1048                         return (ib_set_error(ap, EARG));
1049         }
1050         
1051
1052         mtx_lock(&u->mutex);
1053
1054         
1055         /* Find the handle, if any */
1056         h = NULL;
1057         if ((ap->__field & __F_HANDLE) && gethandle(u, ap, &h)) {
1058                 mtx_unlock(&u->mutex);
1059                 return (0);
1060         }
1061
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));
1066         }
1067
1068         /* Set up handle and deadline */
1069         if (h != NULL && timevalisset(&h->timeout)) {
1070                 getmicrouptime(&deadline);
1071                 timevaladd(&deadline, &h->timeout);
1072         } else {
1073                 timevalclear(&deadline);
1074         }
1075
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);
1080                 if (error == 0)
1081                         continue;
1082                 mtx_unlock(&u->mutex);
1083                 if (error == EINTR)
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));
1089                 }
1090                 mtx_lock(&u->mutex);
1091         }
1092         u->busy = 2;
1093         mtx_unlock(&u->mutex);
1094
1095         /* Hand over deadline handling to the callout routine */
1096         ib->ap = ap;
1097         ib->h = h;
1098         ib->mode = BUSY;
1099         ib->deadline = deadline;
1100         callout_reset(&ib->callout, hz / 5, gpib_ib_timeout, u);
1101
1102         error = ih->func(ib);
1103
1104         /* Release card */
1105         ib->mode = IDLE;
1106         ib->ap = NULL;
1107         ib->h = NULL;
1108         timevalclear(&deadline);
1109         callout_stop(&ib->callout);
1110
1111         mtx_lock(&u->mutex);
1112         u->busy = 1;
1113         wakeup(ib);
1114         mtx_unlock(&u->mutex);
1115
1116         if (error) 
1117                 return(ib_set_errno(ap, error));
1118         return (0);
1119 }
1120
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,
1127 };