1 /* asc.c - device driver for hand scanners
3 * Current version supports:
5 * - AmiScan (Mustek) Color and BW hand scanners (GI1904 chipset)
7 * Copyright (c) 1995 Gunther Schadow. All rights reserved.
8 * Copyright (c) 1995,1996,1997 Luigi Rizzo. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Gunther Schadow
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * $FreeBSD: src/sys/i386/isa/asc.c,v 1.42.2.2 2001/03/01 03:22:39 jlemon Exp $
38 * $DragonFly: src/sys/platform/pc32/isa/asc.c,v 1.21 2008/08/02 01:14:43 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/event.h>
51 #include <sys/selinfo.h>
53 #include <sys/thread2.h>
56 #include <machine/asc_ioctl.h>
58 #include <bus/isa/isa.h>
59 #include <bus/isa/isa_device.h>
60 #include <bus/isa/isavar.h>
64 *** CONSTANTS & DEFINES
69 #define PROBE_SUCCESS IO_ASCSIZE
71 #define ATTACH_SUCCESS 1
76 #define DMA1_READY 0x08
79 # define lprintf if(scu->flags & FLAG_DEBUG) kprintf
81 # define lprintf (void)
84 #define TIMEOUT (hz*15) /* timeout while reading a buffer - default value */
87 *** LAYOUT OF THE MINOR NUMBER
90 #define UNIT_MASK 0xc0 /* unit asc0 .. asc3 */
91 #define UNIT(x) (x >> 6)
92 #define DBUG_MASK 0x20
93 #define FRMT_MASK 0x18 /* output format */
94 #define FRMT_RAW 0x00 /* output bits as read from scanner */
95 #define FRMT_GRAY 0x1 /* output gray mode for color scanner */
96 #define FRMT_PBM 0x08 /* output pbm format */
100 *** THE GEMOMETRY TABLE
103 #define GREY_LINE 826 /* 825, or 826 , or 550 ??? */
104 static const struct asc_geom {
105 int dpi; /* dots per inch */
106 int dpl; /* dots per line */
107 int bpl; /* bytes per line */
108 int g_res; /* get resolution value (ASC_STAT) */
110 { 800, 3312, 414, ASC_RES_800},
111 { 700, 2896, 362, ASC_RES_700},
112 { 600, 2480, 310, ASC_RES_600},
113 { 500, 1656, 258, ASC_RES_500},
114 { 400, 1656, 207, ASC_RES_400},
115 { 300, 1240, 155, ASC_RES_300},
116 { 200, 832, 104, ASC_RES_200},
117 { 100, 416, 52, ASC_RES_100},
118 { 200, 3*GREY_LINE, 3*GREY_LINE, 0 /* returned by color scanner */},
119 { 200, GREY_LINE, GREY_LINE, 0 /* color scanner, grey mode */},
120 { INVALID, 416, 52, INVALID } /* terminator */
124 *** THE TABLE OF UNITS
130 size_t wptr; /* only changed in ascintr */
136 long thedev; /* XXX */
137 int base; /* base address */
138 int dma_num; /* dma number */
139 char dma_byte; /* mask of byte for setting DMA value */
140 char int_byte; /* mask of byte for setting int value */
141 char cfg_byte; /* mirror of byte written to config reg (ASC_CFG). */
142 char cmd_byte; /* mirror of byte written to cmd port (ASC_CMD)*/
145 #define ATTACHED 0x01
148 #define DMA_ACTIVE 0x08
149 #define SLEEPING 0x10
150 #define SEL_COLL 0x20
151 #define PBM_MODE 0x40
152 #define FLAG_DEBUG 0x80
153 int geometry; /* resolution as geomtab index */
154 int linesize; /* length of one scan line (from geom.table) */
155 int blen; /* length of buffer in lines */
156 int btime; /* timeout of buffer in seconds/hz */
158 long icnt; /* interrupt count XXX for debugging */
160 int height; /* height, for pnm modes */
161 size_t bcount; /* bytes to read, for pnm modes */
164 static struct asc_unit unittab[NASC];
166 /*** I could not find a reasonable buffer size limit other than by
167 *** experiments. MAXPHYS is obviously too much, while DEV_BSIZE and
168 *** PAGE_SIZE are really too small. There must be something wrong
169 *** with isa_dmastart/isa_dmarangecheck HELP!!!
171 *** Note, must be DEFAULT_BLEN * samples_per_line <= MAX_BUFSIZE
173 #define MAX_BUFSIZE 0xb000 /* XXX was 0x3000 */
174 #define DEFAULT_BLEN 16
177 *** THE PER-DRIVER RECORD FOR ISA.C
179 static int ascprobe (struct isa_device *isdp);
180 static int ascattach(struct isa_device *isdp);
181 struct isa_driver ascdriver = { ascprobe, ascattach, "asc" };
183 static void ascintr(void *);
185 static d_open_t ascopen;
186 static d_close_t ascclose;
187 static d_read_t ascread;
188 static d_ioctl_t ascioctl;
189 static d_poll_t ascpoll;
190 static d_kqfilter_t asckqfilter;
192 static void ascfilter_detach(struct knote *kn);
193 static int ascfilter(struct knote *kn, long hint);
195 #define CDEV_MAJOR 71
197 static struct dev_ops asc_ops = {
198 { "asc", CDEV_MAJOR, D_KQFILTER },
204 .d_kqfilter = asckqfilter
207 #define STATIC static
210 *** LOCALLY USED SUBROUTINES
216 *** read resolution from the scanner
219 get_resolution(struct asc_unit *scu)
224 scu->cmd_byte = ASC_STANDBY;
225 outb(ASC_CMD, scu->cmd_byte);
226 tsleep((caddr_t)scu, PCATCH, "ascres", hz/10);
227 for(delay= 100; (res=inb(ASC_STAT)) & ASC_RDY_FLAG; delay--)
229 i = tsleep((caddr_t)scu, PCATCH, "ascres0", 1);
230 if ( ( i == 0 ) || ( i == EWOULDBLOCK ) )
236 lprintf("asc.get_resolution: timeout completing command\n");
239 /* ... actual read resolution... */
241 for (i=0; geomtab[i].dpi != INVALID; i++) {
242 if (geomtab[i].g_res == res) break;
244 if (geomtab[i].dpi==INVALID) {
245 scu->geometry= i; /* INVALID; */
246 lprintf("asc.get_resolution: wrong resolution\n");
248 lprintf("asc.get_resolution: %d dpi\n",geomtab[i].dpi);
251 scu->portf_byte=0; /* default */
252 if (geomtab[scu->geometry].g_res==0 && !(scu->thedev&FRMT_GRAY)) {
253 /* color scanner seems to require this */
255 /* scu->geometry++; */
257 scu->linesize = geomtab[scu->geometry].bpl;
258 scu->height = geomtab[scu->geometry].dpl; /* default... */
263 *** allocate/reallocate a buffer
264 *** Now just checks that the preallocated buffer is large enough.
268 buffer_allocate(struct asc_unit *scu)
272 size = scu->blen * scu->linesize;
274 lprintf("asc.buffer_allocate: need 0x%x bytes\n", size);
276 if ( size > MAX_BUFSIZE ) {
278 size= ( (MAX_BUFSIZE+scu->linesize-1) / scu->linesize)*scu->linesize;
279 lprintf("asc.buffer_allocate: 0x%x bytes are too much, try 0x%x\n",
284 scu->sbuf.size = size;
287 scu->sbuf.count = 0; /* available data for reading */
289 lprintf("asc.buffer_allocate: ok\n");
295 *** invoked locally to start dma. Must run in a critical section
298 dma_restart(struct asc_unit *scu)
300 unsigned char al=scu->cmd_byte;
302 if (geomtab[scu->geometry].g_res==0) {/* color */
303 isa_dmastart(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
304 scu->linesize + 90 /* XXX */ , scu->dma_num);
306 * looks like we have to set and then clear this
307 * bit to enable the scanner to send interrupts
309 outb( ASC_CMD, al |= 4 ); /* seems to disable interrupts */
311 outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
313 outb( ASC_CMD, al &= 0xfb );
315 } else { /* normal */
316 isa_dmastart(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
317 scu->linesize, scu->dma_num);
318 /*** this is done in sub_20, after dmastart ? ***/
320 outb( ASC_CMD, al |= 4 );
321 outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
322 outb( ASC_CMD, al &= 0xfb );
325 outb( ASC_CMD, ASC_OPERATE);
328 scu->flags |= DMA_ACTIVE;
332 *** the main functions
336 *** resets the scanner and the config bytes...
339 asc_reset(struct asc_unit *scu)
341 scu->cfg_byte = 0 ; /* clear... */
342 scu->cmd_byte = 0 ; /* clear... */
344 outb(ASC_CFG,scu->cfg_byte); /* for safety, do this here */
345 outb(ASC_CMD,scu->cmd_byte); /* probably not needed */
346 tsleep((caddr_t)scu, PCATCH, "ascres", hz/10); /* sleep .1 sec */
348 scu->blen = DEFAULT_BLEN;
349 scu->btime = TIMEOUT;
350 scu->height = 0 ; /* don't know better... */
352 /**************************************************************************
355 *** read status port and check for proper configuration:
356 *** - if address group matches (status byte has reasonable value)
357 *** cannot check interrupt/dma, only clear the config byte.
360 ascprobe (struct isa_device *isdp)
362 int unit = isdp->id_unit;
363 struct asc_unit *scu = unittab + unit;
366 scu->base = isdp->id_iobase; /*** needed by the following macros ***/
367 scu->flags = FLAG_DEBUG;
369 if ( isdp->id_iobase < 0 ) {
370 lprintf("asc%d.probe: no iobase given\n", unit);
374 if ((stb=inb(ASC_PROBE)) != ASC_PROBE_VALUE) {
375 lprintf("asc%d.probe: failed, got 0x%02x instead of 0x%02x\n",
376 unit, stb, ASC_PROBE_VALUE);
382 * the new AmiScan Color board uses int 10,11,12 instead of 3,5,10
383 * respectively. This means that the driver must act accordingly.
384 * Unfortunately there is no easy way of telling which board one has,
385 * other than trying to get an interrupt and noticing that it is
386 * missing. use "option ASC_NEW_BOARD" if you have a new board.
400 switch(ffs(isdp->id_irq) - 1) {
402 scu->int_byte = ASC_CNF_IRQ3;
405 scu->int_byte = ASC_CNF_IRQ5;
408 scu->int_byte = ASC_CNF_IRQ10;
413 lprintf("asc%d.probe: warning - going interruptless\n", unit);
417 lprintf("asc%d.probe: unsupported INT %d (only 3, 5, 10)\n",
418 unit, ffs(isdp->id_irq) - 1 );
421 scu->dma_num = isdp->id_drq;
422 switch(scu->dma_num) {
424 scu->dma_byte = ASC_CNF_DMA1;
427 scu->dma_byte = ASC_CNF_DMA3;
430 lprintf("asc%d.probe: unsupported DMA %d (only 1 or 3)\n",
435 /* lprintf("asc%d.probe: ok\n", unit); */
437 scu->flags &= ~FLAG_DEBUG;
439 return PROBE_SUCCESS;
442 /**************************************************************************
445 *** finish initialization of unit structure, get geometry value (?)
449 ascattach(struct isa_device *isdp)
451 int unit = isdp->id_unit;
452 struct asc_unit *scu = unittab + unit;
454 isdp->id_intr = (inthand2_t *)ascintr;
455 scu->flags |= FLAG_DEBUG;
456 kprintf("asc%d: [GI1904/Trust Ami-Scan Grey/Color]\n", unit);
459 * Initialize buffer structure.
460 * XXX this must be done early to give a good chance of getting a
461 * contiguous buffer. This wastes memory.
463 scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
464 0ul, 0xfffffful, 1ul, 0x10000ul);
465 if ( scu->sbuf.base == NULL )
467 lprintf("asc%d.attach: buffer allocation failed\n", unit);
468 return ATTACH_FAIL; /* XXX attach must not fail */
470 scu->sbuf.size = INVALID;
471 scu->sbuf.rptr = INVALID;
473 scu->flags |= ATTACHED;
474 /* lprintf("asc%d.attach: ok\n", unit); */
475 scu->flags &= ~FLAG_DEBUG;
477 scu->selp.si_flags=0;
478 scu->selp.si_pid=(pid_t)0;
481 make_dev(&asc_ops, unit<<6, ASC_UID, ASC_GID, 0666, "asc%d", unit);
482 make_dev(&asc_ops, ((unit<<6) + FRMT_PBM),
483 ASC_UID, ASC_GID, 0666, "asc%dp", unit);
484 make_dev(&asc_ops, ((unit<<6) + DBUG_MASK),
485 ASC_UID, ASC_GID, 0666, "asc%dd", unit);
486 make_dev(&asc_ops, ((unit<<6) + DBUG_MASK+FRMT_PBM),
487 ASC_UID, ASC_GID, 0666, "asc%dpd", unit);
488 return ATTACH_SUCCESS;
491 /**************************************************************************
494 *** the interrupt routine, at the end of DMA...
500 struct asc_unit *scu = unittab + unit;
501 int chan_bit = 0x01 << scu->dma_num;
504 /* ignore stray interrupts... */
505 if ((scu->flags & (OPEN |READING)) != (OPEN | READING) ) {
506 /* must be after closing... */
507 scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL);
510 if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) {
511 outb( ASC_CMD, ASC_STANDBY);
512 scu->flags &= ~DMA_ACTIVE;
513 /* bounce buffers... */
514 isa_dmadone(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
515 scu->linesize, scu->dma_num);
516 scu->sbuf.wptr += scu->linesize;
517 if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0;
518 scu->sbuf.count += scu->linesize;
519 if (scu->flags & SLEEPING) {
520 scu->flags &= ~SLEEPING;
521 wakeup((caddr_t)scu);
523 if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) {
526 if (scu->selp.si_pid) {
527 selwakeup(&scu->selp);
528 scu->selp.si_pid=(pid_t)0;
529 scu->selp.si_flags = 0;
534 /**************************************************************************
537 *** set open flag, set modes according to minor number
539 *** don't switch scanner on, wait until first read or ioctls go before
543 ascopen(struct dev_open_args *ap)
545 cdev_t dev = ap->a_head.a_dev;
546 struct asc_unit *scu;
549 unit = UNIT(minor(dev)) & UNIT_MASK;
553 /* XXX lprintf isn't valid here since there is no scu. */
554 kprintf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC);
558 scu = unittab + unit;
559 if ( !( scu->flags & ATTACHED ) )
561 lprintf("asc%d.open: unit was not attached successfully 0x%04x\n",
566 if ( minor(dev) & DBUG_MASK )
567 scu->flags |= FLAG_DEBUG;
569 scu->flags &= ~FLAG_DEBUG;
571 switch(minor(dev) & FRMT_MASK) {
573 scu->flags |= PBM_MODE;
574 lprintf("asc%d.open: pbm mode\n", unit);
577 lprintf("asc%d.open: raw mode\n", unit);
578 scu->flags &= ~PBM_MODE;
581 lprintf("asc%d.open: gray maps are not yet supported", unit);
585 lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
587 if ( scu->flags & OPEN ) {
588 lprintf("asc%d.open: already open", unit);
591 if (isa_dma_acquire(scu->dma_num))
594 scu->flags = ATTACHED | OPEN;
602 asc_startread(struct asc_unit *scu)
604 /*** from here on, things can be delayed to the first read/ioctl ***/
605 /*** this was done in sub_12... ***/
606 scu->cfg_byte= scu->cmd_byte=0; /* init scanner */
607 outb(ASC_CMD, scu->cmd_byte);
608 /*** this was done in sub_16, set scan len... ***/
609 outb(ASC_BOH, scu->portf_byte );
610 if (geomtab[scu->geometry].g_res==0) { /* color */
611 scu->cmd_byte = 0x00 ;
613 scu->cmd_byte = 0x90 ;
615 outb(ASC_CMD, scu->cmd_byte);
616 outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */);
617 outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */);
618 /*** this was done in sub_21, config DMA ... ***/
619 scu->cfg_byte |= scu->dma_byte;
620 outb(ASC_CFG, scu->cfg_byte);
621 /*** sub_22: enable int on the scanner ***/
622 scu->cfg_byte |= scu->int_byte;
623 outb(ASC_CFG, scu->cfg_byte);
624 /*** sub_28: light on etc...***/
625 scu->cmd_byte = ASC_STANDBY;
626 outb(ASC_CMD, scu->cmd_byte);
627 tsleep((caddr_t)scu, PCATCH, "ascstrd", hz/10); /* sleep .1 sec */
631 /**************************************************************************
634 *** turn off scanner, release the buffer
635 *** should probably terminate dma ops, release int and dma. lr 12mar95
639 ascclose(struct dev_close_args *ap)
641 cdev_t dev = ap->a_head.a_dev;
642 int unit = UNIT(minor(dev));
643 struct asc_unit *scu = unittab + unit;
645 lprintf("asc%d.close: minor %d\n",
648 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
649 lprintf("asc%d.close: unit was not attached successfully 0x%04x\n",
653 /* all this is in sub_29... */
655 outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */
656 scu->cmd_byte &= ~ASC_LIGHT_ON;
657 outb(ASC_CMD, scu->cmd_byte);/* light off */
658 tsleep((caddr_t)scu, PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */
659 scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */
660 scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */
661 outb(ASC_CFG, scu->cfg_byte);
662 /* --- disable dma controller ? --- */
663 isa_dma_release(scu->dma_num);
664 /* --- disable interrupts on the controller (sub_24) --- */
666 scu->sbuf.size = INVALID;
667 scu->sbuf.rptr = INVALID;
669 scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
675 pbm_init(struct asc_unit *scu)
677 int width = geomtab[scu->geometry].dpl;
678 int l= ksprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
681 scu->bcount = scu->height * width / 8 + l;
683 /* move header to end of sbuf */
684 scu->sbuf.rptr=scu->sbuf.size-l;
685 bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l);
687 if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
688 for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--)
692 /**************************************************************************
698 ascread(struct dev_read_args *ap)
700 cdev_t dev = ap->a_head.a_dev;
701 struct uio *uio = ap->a_uio;
702 int unit = UNIT(minor(dev));
703 struct asc_unit *scu = unittab + unit;
708 lprintf("asc%d.read: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
710 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
711 lprintf("asc%d.read: unit was not attached successfully 0x%04x\n",
716 if ( !(scu->flags & READING) ) { /*** first read... ***/
717 /* allocate a buffer for reading data and init things */
718 if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING;
721 if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */
726 lprintf("asc%d.read(before): "
727 "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
728 unit, scu->sbuf.size, scu->sbuf.rptr,
729 scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,
733 if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */
734 if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
735 scu->flags |= SLEEPING;
736 res = tsleep((caddr_t)scu, PCATCH, "ascread", 0);
737 scu->flags &= ~SLEEPING;
738 if ( res == 0 ) res = SUCCESS;
741 if (scu->flags & FLAG_DEBUG)
742 tsleep((caddr_t)scu, PCATCH, "ascdly",hz);
743 lprintf("asc%d.read(after): "
744 "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
745 unit, scu->sbuf.size, scu->sbuf.rptr,
746 scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt);
748 /* first, not more than available... */
749 nbytes = szmin(uio->uio_resid, scu->sbuf.count);
750 /* second, contiguous data... */
751 nbytes = szmin( nbytes, (scu->sbuf.size - scu->sbuf.rptr) );
752 /* third, one line (will remove this later, XXX) */
753 nbytes = szmin( nbytes, scu->linesize );
754 if ( (scu->flags & PBM_MODE) )
755 nbytes = szmin( nbytes, scu->bcount );
756 lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes);
757 if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
758 lprintf("asc%d.read: invert buffer\n",unit);
759 for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--)
762 res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio);
763 if ( res != SUCCESS ) {
764 lprintf("asc%d.read: uiomove failed %d", unit, res);
769 scu->sbuf.rptr += nbytes;
770 if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0;
771 scu->sbuf.count -= nbytes;
772 /* having moved some data, can read mode */
773 if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
775 if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
777 lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
778 unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount);
783 /**************************************************************************
789 ascioctl(struct dev_ioctl_args *ap)
791 cdev_t dev = ap->a_head.a_dev;
792 caddr_t data = ap->a_data;
793 int unit = UNIT(minor(dev));
794 struct asc_unit *scu = unittab + unit;
796 lprintf("asc%d.ioctl: minor %d\n",
799 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
800 lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n",
808 *(int *)data=geomtab[scu->geometry].dpi;
809 lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data);
812 *(int *)data=geomtab[scu->geometry].dpl;
813 lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data);
816 *(int *)data=scu->height;
817 lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data);
820 lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data);
821 if ( scu->flags & READING ) {
822 lprintf("asc%d:ioctl on already reading unit\n", unit);
825 scu->height=*(int *)data;
829 *(int *)data=scu->blen;
830 lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data);
833 lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data);
834 if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
836 lprintf("asc%d:ioctl buffer size too high\n", unit);
839 scu->blen=*(int *)data;
842 *(int *)data = scu->btime / hz;
843 lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data);
846 scu->btime = *(int *)data * hz;
847 lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data);
850 default: return ENOTTY;
856 ascpoll(struct dev_poll_args *ap)
858 cdev_t dev = ap->a_head.a_dev;
859 int unit = UNIT(minor(dev));
860 struct asc_unit *scu = unittab + unit;
865 if (ap->a_events & (POLLIN | POLLRDNORM)) {
866 if (scu->sbuf.count >0)
867 revents |= ap->a_events & (POLLIN | POLLRDNORM);
869 if (!(scu->flags & DMA_ACTIVE))
872 selrecord(curthread, &scu->selp);
876 ap->a_events = revents;
880 static struct filterops ascfiltops =
881 { 1, NULL, ascfilter_detach, ascfilter };
884 asckqfilter(struct dev_kqfilter_args *ap)
886 cdev_t dev = ap->a_head.a_dev;
887 int unit = UNIT(minor(dev));
888 struct asc_unit *scu = unittab + unit;
889 struct knote *kn = ap->a_kn;
894 switch (kn->kn_filter) {
896 kn->kn_fop = &ascfiltops;
897 kn->kn_hook = (caddr_t)scu;
905 klist = &scu->selp.si_note;
906 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
913 ascfilter_detach(struct knote *kn)
915 struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
919 klist = &scu->selp.si_note;
920 SLIST_REMOVE(klist, kn, knote, kn_selnext);
925 ascfilter(struct knote *kn, long hint)
927 struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
931 if (scu->sbuf.count >0)
934 if (!(scu->flags & DMA_ACTIVE))