]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cmx/cmx.c
Merge libc++ trunk r366426, resolve conflicts, and add FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / sys / dev / cmx / cmx.c
1 /*-
2  * Copyright (c) 2006-2007 Daniel Roethlisberger <daniel@roe.ch>
3  * Copyright (c) 2000-2004 OMNIKEY GmbH (www.omnikey.com)
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 unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33  * OMNIKEY CardMan 4040 a.k.a. CardMan eXtended (cmx) driver.
34  * This is a PCMCIA based smartcard reader which seems to work
35  * like an I/O port mapped USB CCID smartcard device.
36  *
37  * I/O originally based on Linux driver version 1.1.0 by OMNIKEY.
38  * Dual GPL/BSD.  Almost all of the code has been rewritten.
39  * $Omnikey: cm4040_cs.c,v 1.7 2004/10/04 09:08:50 jp Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/lock.h>
45 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/poll.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/uio.h>
53 #include <sys/selinfo.h>
54
55 #include <sys/module.h>
56 #include <sys/bus.h>
57
58 #include <machine/bus.h>
59 #include <machine/resource.h>
60 #include <sys/rman.h>
61
62 #include <dev/cmx/cmxvar.h>
63 #include <dev/cmx/cmxreg.h>
64
65 #ifdef CMX_DEBUG
66 #define DEBUG_printf(dev, fmt, args...) \
67         device_printf(dev, "%s: " fmt, __FUNCTION__, ##args)
68 #else
69 #define DEBUG_printf(dev, fmt, args...)
70 #endif
71
72 #define SPIN_COUNT                              1000
73 #define WAIT_TICKS                              (hz/100)
74 #define POLL_TICKS                              (hz/10)
75
76 /* possibly bogus */
77 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT        (150*hz)
78 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT       (35*hz)
79 #define CCID_DRIVER_MINIMUM_TIMEOUT             (3*hz)
80
81 #ifdef CMX_DEBUG
82 static char     BSRBITS[] = "\020"
83         "\01BULK_OUT_FULL"              /* 0x01 */
84         "\02BULK_IN_FULL"               /* 0x02 */
85         "\03(0x04)";                    /* 0x04 */
86 #ifdef CMX_INTR
87 static char     SCRBITS[] = "\020"
88         "\01POWER_DOWN"                 /* 0x01 */
89         "\02PULSE_INTERRUPT"            /* 0x02 */
90         "\03HOST_TO_READER_DONE"        /* 0x04 */
91         "\04READER_TO_HOST_DONE"        /* 0x08 */
92         "\05ACK_NOTIFY"                 /* 0x10 */
93         "\06EN_NOTIFY"                  /* 0x20 */
94         "\07ABORT"                      /* 0x40 */
95         "\10HOST_TO_READER_START";      /* 0x80 */
96 #endif /* CMX_INTR */
97 static char     POLLBITS[] = "\020"
98         "\01POLLIN"                     /* 0x0001 */
99         "\02POLLPRI"                    /* 0x0002 */
100         "\03POLLOUT"                    /* 0x0004 */
101         "\04POLLERR"                    /* 0x0008 */
102         "\05POLLHUP"                    /* 0x0010 */
103         "\06POLLINVAL"                  /* 0x0020 */
104         "\07POLLRDNORM"                 /* 0x0040 */
105         "\10POLLRDBAND"                 /* 0x0080 */
106         "\11POLLWRBAND";                /* 0x0100 */
107 static char     MODEBITS[] = "\020"
108         "\01READ"                       /* 0x0001 */
109         "\02WRITE"                      /* 0x0002 */
110         "\03NONBLOCK"                   /* 0x0004 */
111         "\04APPEND"                     /* 0x0008 */
112         "\05SHLOCK"                     /* 0x0010 */
113         "\06EXLOCK"                     /* 0x0020 */
114         "\07ASYNC"                      /* 0x0040 */
115         "\10FSYNC"                      /* 0x0080 */
116         "\11NOFOLLOW"                   /* 0x0100 */
117         "\12CREAT"                      /* 0x0200 */
118         "\13TRUNK"                      /* 0x0400 */
119         "\14EXCL"                       /* 0x0800 */
120         "\15(0x1000)"                   /* 0x1000 */
121         "\16(0x2000)"                   /* 0x2000 */
122         "\17HASLOCK"                    /* 0x4000 */
123         "\20NOCTTY"                     /* 0x8000 */
124         "\21DIRECT";                    /* 0x00010000 */
125 #endif /* CMX_DEBUG */
126
127 devclass_t cmx_devclass;
128
129 static d_open_t         cmx_open;
130 static d_close_t        cmx_close;
131 static d_read_t         cmx_read;
132 static d_write_t        cmx_write;
133 static d_poll_t         cmx_poll;
134 #ifdef CMX_INTR
135 static void             cmx_intr(void *arg);
136 #endif
137
138 static struct cdevsw cmx_cdevsw = {
139         .d_version =    D_VERSION,
140         .d_open =       cmx_open,
141         .d_close =      cmx_close,
142         .d_read =       cmx_read,
143         .d_write =      cmx_write,
144         .d_poll =       cmx_poll,
145         .d_name =       "cmx",
146 };
147
148 /*
149  * Initialize the softc structure.  Must be called from
150  * the bus specific device allocation routine.
151  */
152 void
153 cmx_init_softc(device_t dev)
154 {
155         struct cmx_softc *sc = device_get_softc(dev);
156         sc->dev = dev;
157         sc->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
158 }
159
160 /*
161  * Allocate driver resources.  Must be called from the
162  * bus specific device allocation routine.  Caller must
163  * ensure to call cmx_release_resources to free the
164  * resources when detaching.
165  * Return zero if successful, and ENOMEM if the resources
166  * could not be allocated.
167  */
168 int
169 cmx_alloc_resources(device_t dev)
170 {
171         struct cmx_softc *sc = device_get_softc(dev);
172 #ifdef CMX_INTR
173         int rv;
174 #endif
175
176         sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
177                         &sc->ioport_rid, RF_ACTIVE);
178         if (!sc->ioport) {
179                 device_printf(dev, "failed to allocate io port\n");
180                 return ENOMEM;
181         }
182         sc->bst = rman_get_bustag(sc->ioport);
183         sc->bsh = rman_get_bushandle(sc->ioport);
184
185 #ifdef CMX_INTR
186         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
187                         &sc->irq_rid, RF_ACTIVE);
188         if (!sc->irq) {
189                 device_printf(dev, "failed to allocate irq\n");
190                 return ENOMEM;
191         }
192         if ((rv = bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY,
193                         cmx_intr, sc, &sc->ih)) != 0) {
194                 device_printf(dev, "failed to set up irq\n");
195                 return ENOMEM;
196         }
197 #endif
198
199         mtx_init(&sc->mtx, device_get_nameunit(dev),
200                         "cmx softc lock",
201                         MTX_DEF | MTX_RECURSE);
202         callout_init_mtx(&sc->ch, &sc->mtx, 0);
203
204         return 0;
205 }
206
207 /*
208  * Release the resources allocated by cmx_allocate_resources.
209  */
210 void
211 cmx_release_resources(device_t dev)
212 {
213         struct cmx_softc *sc = device_get_softc(dev);
214
215         mtx_destroy(&sc->mtx);
216
217 #ifdef CMX_INTR
218         if (sc->ih) {
219                 bus_teardown_intr(dev, sc->irq, sc->ih);
220                 sc->ih = NULL;
221         }
222         if (sc->irq) {
223                 bus_release_resource(dev, SYS_RES_IRQ,
224                                 sc->irq_rid, sc->irq);
225                 sc->irq = NULL;
226         }
227 #endif
228
229         if (sc->ioport) {
230                 bus_deactivate_resource(dev, SYS_RES_IOPORT,
231                                 sc->ioport_rid, sc->ioport);
232                 bus_release_resource(dev, SYS_RES_IOPORT,
233                                 sc->ioport_rid, sc->ioport);
234                 sc->ioport = NULL;
235         }
236         return;
237 }
238
239 /*
240  * Bus independent device attachment routine.  Creates the
241  * character device node.
242  */
243 int
244 cmx_attach(device_t dev)
245 {
246         struct cmx_softc *sc = device_get_softc(dev);
247
248         if (!sc || sc->dying)
249                 return ENXIO;
250
251         sc->cdev = make_dev(&cmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
252                             "cmx%d", device_get_unit(dev));
253         if (!sc->cdev) {
254                 device_printf(dev, "failed to create character device\n");
255                 return ENOMEM;
256         }
257         sc->cdev->si_drv1 = sc;
258
259         return 0;
260 }
261
262 /*
263  * Bus independent device detachment routine.  Makes sure all
264  * allocated resources are freed, callouts disabled and waiting
265  * processes unblocked.
266  */
267 int
268 cmx_detach(device_t dev)
269 {
270         struct cmx_softc *sc = device_get_softc(dev);
271
272         DEBUG_printf(dev, "called\n");
273
274         sc->dying = 1;
275
276         CMX_LOCK(sc);
277         if (sc->polling) {
278                 DEBUG_printf(sc->dev, "disabling polling\n");
279                 callout_stop(&sc->ch);
280                 sc->polling = 0;
281                 CMX_UNLOCK(sc);
282                 callout_drain(&sc->ch);
283                 selwakeuppri(&sc->sel, PZERO);
284         } else {
285                 CMX_UNLOCK(sc);
286         }
287
288         wakeup(sc);
289         destroy_dev(sc->cdev);
290
291         DEBUG_printf(dev, "releasing resources\n");
292         cmx_release_resources(dev);
293         return 0;
294 }
295
296 /*
297  * Wait for buffer status register events.  If test is non-zero,
298  * wait until flags are set, otherwise wait until flags are unset.
299  * Will spin SPIN_COUNT times, then sleep until timeout is reached.
300  * Returns zero if event happened, EIO if the timeout was reached,
301  * and ENXIO if the device was detached in the meantime.  When that
302  * happens, the caller must quit immediately, since a detach is
303  * in progress.
304  */
305 static inline int
306 cmx_wait_BSR(struct cmx_softc *sc, uint8_t flags, int test)
307 {
308         int rv;
309
310         for (int i = 0; i < SPIN_COUNT; i++) {
311                 if (cmx_test_BSR(sc, flags, test))
312                         return 0;
313         }
314
315         for (int i = 0; i * WAIT_TICKS < sc->timeout; i++) {
316                 if (cmx_test_BSR(sc, flags, test))
317                         return 0;
318                 rv = tsleep(sc, PWAIT|PCATCH, "cmx", WAIT_TICKS);
319                 /*
320                  * Currently, the only reason for waking up with
321                  * rv == 0 is when we are detaching, in which
322                  * case sc->dying is always 1.
323                  */
324                 if (sc->dying)
325                         return ENXIO;
326                 if (rv != EAGAIN)
327                         return rv;
328         }
329
330         /* timeout */
331         return EIO;
332 }
333
334 /*
335  * Set the sync control register to val.  Before and after writing
336  * to the SCR, we wait for the BSR to not signal BULK_OUT_FULL.
337  * Returns zero if successful, or whatever errors cmx_wait_BSR can
338  * return.  ENXIO signals that the device has been detached in the
339  * meantime, and that we should leave the kernel immediately.
340  */
341 static inline int
342 cmx_sync_write_SCR(struct cmx_softc *sc, uint8_t val)
343 {
344         int rv = 0;
345
346         if ((rv = cmx_wait_BSR(sc, BSR_BULK_OUT_FULL, 0)) != 0) {
347                 return rv;
348         }
349
350         cmx_write_SCR(sc, val);
351
352         if ((rv = cmx_wait_BSR(sc, BSR_BULK_OUT_FULL, 0)) != 0) {
353                 return rv;
354         }
355
356         return 0;
357 }
358
359 /*
360  * Returns a suitable timeout value based on the given command byte.
361  * Some commands appear to need longer timeout values than others.
362  */
363 static inline unsigned long
364 cmx_timeout_by_cmd(uint8_t cmd)
365 {
366         switch (cmd) {
367         case CMD_PC_TO_RDR_XFRBLOCK:
368         case CMD_PC_TO_RDR_SECURE:
369         case CMD_PC_TO_RDR_TEST_SECURE:
370         case CMD_PC_TO_RDR_OK_SECURE:
371                 return CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
372
373         case CMD_PC_TO_RDR_ICCPOWERON:
374                 return CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
375
376         case CMD_PC_TO_RDR_GETSLOTSTATUS:
377         case CMD_PC_TO_RDR_ICCPOWEROFF:
378         case CMD_PC_TO_RDR_GETPARAMETERS:
379         case CMD_PC_TO_RDR_RESETPARAMETERS:
380         case CMD_PC_TO_RDR_SETPARAMETERS:
381         case CMD_PC_TO_RDR_ESCAPE:
382         case CMD_PC_TO_RDR_ICCCLOCK:
383         default:
384                 return CCID_DRIVER_MINIMUM_TIMEOUT;
385         }
386 }
387
388 /*
389  * Periodical callout routine, polling the reader for data
390  * availability.  If the reader signals data ready for reading,
391  * wakes up the processes which are waiting in select()/poll().
392  * Otherwise, reschedules itself with a delay of POLL_TICKS.
393  */
394 static void
395 cmx_tick(void *xsc)
396 {
397         struct cmx_softc *sc = xsc;
398         uint8_t bsr;
399
400         CMX_LOCK(sc);
401         if (sc->polling && !sc->dying) {
402                 bsr = cmx_read_BSR(sc);
403                 DEBUG_printf(sc->dev, "BSR=%b\n", bsr, BSRBITS);
404                 if (cmx_test(bsr, BSR_BULK_IN_FULL, 1)) {
405                         sc->polling = 0;
406                         selwakeuppri(&sc->sel, PZERO);
407                 } else {
408                         callout_reset(&sc->ch, POLL_TICKS, cmx_tick, sc);
409                 }
410         }
411         CMX_UNLOCK(sc);
412 }
413
414 /*
415  * Open the character device.  Only a single process may open the
416  * device at a time.
417  */
418 static int
419 cmx_open(struct cdev *cdev, int flags, int fmt, struct thread *td)
420 {
421         struct cmx_softc *sc = cdev->si_drv1;
422
423         if (sc == NULL || sc->dying)
424                 return ENXIO;
425
426         CMX_LOCK(sc);
427         if (sc->open) {
428                 CMX_UNLOCK(sc);
429                 return EBUSY;
430         }
431         sc->open = 1;
432         CMX_UNLOCK(sc);
433
434         DEBUG_printf(sc->dev, "open (flags=%b thread=%p)\n",
435                         flags, MODEBITS, td);
436         return 0;
437 }
438
439 /*
440  * Close the character device.
441  */
442 static int
443 cmx_close(struct cdev *cdev, int flags, int fmt, struct thread *td)
444 {
445         struct cmx_softc *sc = cdev->si_drv1;
446
447         if (sc == NULL || sc->dying)
448                 return ENXIO;
449
450         CMX_LOCK(sc);
451         if (!sc->open) {
452                 CMX_UNLOCK(sc);
453                 return EINVAL;
454         }
455         if (sc->polling) {
456                 DEBUG_printf(sc->dev, "disabling polling\n");
457                 callout_stop(&sc->ch);
458                 sc->polling = 0;
459                 CMX_UNLOCK(sc);
460                 callout_drain(&sc->ch);
461                 selwakeuppri(&sc->sel, PZERO);
462                 CMX_LOCK(sc);
463         }
464         sc->open = 0;
465         CMX_UNLOCK(sc);
466
467         DEBUG_printf(sc->dev, "close (flags=%b thread=%p)\n",
468                         flags, MODEBITS, td);
469         return 0;
470 }
471
472 /*
473  * Read from the character device.
474  * Returns zero if successful, ENXIO if dying, EINVAL if an attempt
475  * was made to read less than CMX_MIN_RDLEN bytes or less than the
476  * device has available, or any of the errors that cmx_sync_write_SCR
477  * can return.  Partial reads are not supported.
478  */
479 static int
480 cmx_read(struct cdev *cdev, struct uio *uio, int flag)
481 {
482         struct cmx_softc *sc = cdev->si_drv1;
483         unsigned long bytes_left;
484         uint8_t uc;
485         int rv, amnt, offset;
486
487         if (sc == NULL || sc->dying)
488                 return ENXIO;
489
490         DEBUG_printf(sc->dev, "called (len=%d flag=%b)\n",
491                 uio->uio_resid, flag, MODEBITS);
492
493         CMX_LOCK(sc);
494         if (sc->polling) {
495                 DEBUG_printf(sc->dev, "disabling polling\n");
496                 callout_stop(&sc->ch);
497                 sc->polling = 0;
498                 CMX_UNLOCK(sc);
499                 callout_drain(&sc->ch);
500                 selwakeuppri(&sc->sel, PZERO);
501         } else {
502                 CMX_UNLOCK(sc);
503         }
504
505         if (uio->uio_resid == 0) {
506                 return 0;
507         }
508
509         if (uio->uio_resid < CMX_MIN_RDLEN) {
510                 return EINVAL;
511         }
512
513         if (flag & O_NONBLOCK) {
514                 if (cmx_test_BSR(sc, BSR_BULK_IN_FULL, 0)) {
515                         return EAGAIN;
516                 }
517         }
518
519         for (int i = 0; i < 5; i++) {
520                 if ((rv = cmx_wait_BSR(sc, BSR_BULK_IN_FULL, 1)) != 0) {
521                         return rv;
522                 }
523                 sc->buf[i] = cmx_read_DTR(sc);
524                 DEBUG_printf(sc->dev, "buf[%02x]=%02x\n", i, sc->buf[i]);
525         }
526
527         bytes_left = CMX_MIN_RDLEN +
528                         (0x000000FF&((char)sc->buf[1])) +
529                         (0x0000FF00&((char)sc->buf[2] << 8)) +
530                         (0x00FF0000&((char)sc->buf[3] << 16)) +
531                         (0xFF000000&((char)sc->buf[4] << 24));
532         DEBUG_printf(sc->dev, "msgsz=%lu\n", bytes_left);
533
534         if (uio->uio_resid < bytes_left) {
535                 return EINVAL;
536         }
537
538         offset = 5; /* prefetched header */
539         while (bytes_left > 0) {
540                 amnt = MIN(bytes_left, sizeof(sc->buf));
541
542                 for (int i = offset; i < amnt; i++) {
543                         if ((rv = cmx_wait_BSR(sc, BSR_BULK_IN_FULL, 1))!=0) {
544                                 return rv;
545                         }
546                         sc->buf[i] = cmx_read_DTR(sc);
547                         DEBUG_printf(sc->dev, "buf[%02x]=%02x\n",
548                                         i, sc->buf[i]);
549                 }
550
551                 if ((rv = uiomove(sc->buf, amnt, uio)) != 0) {
552                         DEBUG_printf(sc->dev, "uiomove failed (%d)\n", rv);
553                         return rv;
554                 }
555
556                 if (offset)
557                         offset = 0;
558                 bytes_left -= amnt;
559         }
560
561         if ((rv = cmx_wait_BSR(sc, BSR_BULK_IN_FULL, 1)) != 0) {
562                 return rv;
563         }
564
565         if ((rv = cmx_sync_write_SCR(sc, SCR_READER_TO_HOST_DONE)) != 0) {
566                 return rv;
567         }
568
569         uc = cmx_read_DTR(sc);
570         DEBUG_printf(sc->dev, "success (DTR=%02x)\n", uc);
571         return 0;
572 }
573
574 /*
575  * Write to the character device.
576  * Returns zero if successful, NXIO if dying, EINVAL if less data
577  * written than CMX_MIN_WRLEN, or any of the errors that cmx_sync_SCR
578  * can return.
579  */
580 static int
581 cmx_write(struct cdev *cdev, struct uio *uio, int flag)
582 {
583         struct cmx_softc *sc = cdev->si_drv1;
584         int rv, amnt;
585
586         if (sc == NULL || sc->dying)
587                 return ENXIO;
588
589         DEBUG_printf(sc->dev, "called (len=%d flag=%b)\n",
590                         uio->uio_resid, flag, MODEBITS);
591
592         if (uio->uio_resid == 0) {
593                 return 0;
594         }
595
596         if (uio->uio_resid < CMX_MIN_WRLEN) {
597                 return EINVAL;
598         }
599
600         if ((rv = cmx_sync_write_SCR(sc, SCR_HOST_TO_READER_START)) != 0) {
601                 return rv;
602         }
603
604         sc->timeout = 0;
605         while (uio->uio_resid > 0) {
606                 amnt = MIN(uio->uio_resid, sizeof(sc->buf));
607
608                 if ((rv = uiomove(sc->buf, amnt, uio)) != 0) {
609                         DEBUG_printf(sc->dev, "uiomove failed (%d)\n", rv);
610                         /* wildly guessed attempt to notify device */
611                         sc->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
612                         cmx_sync_write_SCR(sc, SCR_HOST_TO_READER_DONE);
613                         return rv;
614                 }
615
616                 if (sc->timeout == 0) {
617                         sc->timeout = cmx_timeout_by_cmd(sc->buf[0]);
618                         DEBUG_printf(sc->dev, "cmd=%02x timeout=%lu\n",
619                                         sc->buf[0], sc->timeout);
620                 }
621
622                 for (int i = 0; i < amnt; i++) {
623                         if ((rv = cmx_wait_BSR(sc, BSR_BULK_OUT_FULL, 0))!=0) {
624                                 return rv;
625                         }
626                         cmx_write_DTR(sc, sc->buf[i]);
627                         DEBUG_printf(sc->dev, "buf[%02x]=%02x\n",
628                                         i, sc->buf[i]);
629                 }
630         }
631
632         if ((rv = cmx_sync_write_SCR(sc, SCR_HOST_TO_READER_DONE)) != 0) {
633                 return rv;
634         }
635
636         DEBUG_printf(sc->dev, "success\n");
637         return 0;
638 }
639
640 /*
641  * Poll handler.  Writing is always possible, reading is only possible
642  * if BSR_BULK_IN_FULL is set.  Will start the cmx_tick callout and
643  * set sc->polling.
644  */
645 static int
646 cmx_poll(struct cdev *cdev, int events, struct thread *td)
647 {
648         struct cmx_softc *sc = cdev->si_drv1;
649         int revents = 0;
650         uint8_t bsr = 0;
651
652         if (sc == NULL || sc->dying)
653                 return ENXIO;
654
655         bsr = cmx_read_BSR(sc);
656         DEBUG_printf(sc->dev, "called (events=%b BSR=%b)\n",
657                         events, POLLBITS, bsr, BSRBITS);
658
659         revents = events & (POLLOUT | POLLWRNORM);
660         if (events & (POLLIN | POLLRDNORM)) {
661                 if (cmx_test(bsr, BSR_BULK_IN_FULL, 1)) {
662                         revents |= events & (POLLIN | POLLRDNORM);
663                 } else {
664                         selrecord(td, &sc->sel);
665                         CMX_LOCK(sc);
666                         if (!sc->polling) {
667                                 DEBUG_printf(sc->dev, "enabling polling\n");
668                                 sc->polling = 1;
669                                 callout_reset(&sc->ch, POLL_TICKS,
670                                                 cmx_tick, sc);
671                         } else {
672                                 DEBUG_printf(sc->dev, "already polling\n");
673                         }
674                         CMX_UNLOCK(sc);
675                 }
676         }
677
678         DEBUG_printf(sc->dev, "success (revents=%b)\n", revents, POLLBITS);
679
680         return revents;
681 }
682
683 #ifdef CMX_INTR
684 /*
685  * Interrupt handler.  Currently has no function except to
686  * print register status (if debugging is also enabled).
687  */
688 static void
689 cmx_intr(void *arg)
690 {
691         struct cmx_softc *sc = (struct cmx_softc *)arg;
692
693         if (sc == NULL || sc->dying)
694                 return;
695
696         DEBUG_printf(sc->dev, "received interrupt (SCR=%b BSR=%b)\n",
697                         cmx_read_SCR(sc), SCRBITS,
698                         cmx_read_BSR(sc), BSRBITS);
699
700         return;
701 }
702 #endif
703