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 * $Id: asc.c,v 1.29 1998/03/28 10:32:59 bde Exp $
51 #include "i386/isa/isa_device.h"
52 #include "i386/isa/ascreg.h"
54 #include "machine/asc_ioctl.h"
56 #include <sys/param.h>
57 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/kernel.h>
65 #include "opt_devfs.h"
68 #include <sys/devfsext.h>
72 #include <machine/asc_ioctl.h>
74 #include <i386/isa/isa.h>
75 #include <i386/isa/isa_device.h>
76 #include <i386/isa/ascreg.h>
78 #endif /* FREEBSD_1_X */
81 *** CONSTANTS & DEFINES
86 #define PROBE_SUCCESS IO_ASCSIZE
88 #define ATTACH_SUCCESS 1
93 #define DMA1_READY 0x08
96 # define lprintf if(scu->flags & FLAG_DEBUG) printf
98 # define lprintf (void)
101 #define TIMEOUT (hz*15) /* timeout while reading a buffer - default value */
102 #define ASCPRI PRIBIO /* priority while reading a buffer */
105 *** LAYOUT OF THE MINOR NUMBER
108 #define UNIT_MASK 0xc0 /* unit asc0 .. asc3 */
109 #define UNIT(x) (x >> 6)
110 #define DBUG_MASK 0x20
111 #define FRMT_MASK 0x18 /* output format */
112 #define FRMT_RAW 0x00 /* output bits as read from scanner */
113 #define FRMT_GRAY 0x1 /* output gray mode for color scanner */
114 #define FRMT_PBM 0x08 /* output pbm format */
115 #define FRMT_PGM 0x18
118 *** THE GEMOMETRY TABLE
121 #define GREY_LINE 826 /* 825, or 826 , or 550 ??? */
122 static const struct asc_geom {
123 int dpi; /* dots per inch */
124 int dpl; /* dots per line */
125 int bpl; /* bytes per line */
126 int g_res; /* get resolution value (ASC_STAT) */
128 { 800, 3312, 414, ASC_RES_800},
129 { 700, 2896, 362, ASC_RES_700},
130 { 600, 2480, 310, ASC_RES_600},
131 { 500, 1656, 258, ASC_RES_500},
132 { 400, 1656, 207, ASC_RES_400},
133 { 300, 1240, 155, ASC_RES_300},
134 { 200, 832, 104, ASC_RES_200},
135 { 100, 416, 52, ASC_RES_100},
136 { 200, 3*GREY_LINE, 3*GREY_LINE, 0 /* returned by color scanner */},
137 { 200, GREY_LINE, GREY_LINE, 0 /* color scanner, grey mode */},
138 { INVALID, 416, 52, INVALID } /* terminator */
142 *** THE TABLE OF UNITS
148 size_t wptr; /* only changed in ascintr */
154 long thedev; /* XXX */
155 int base; /* base address */
156 int dma_num; /* dma number */
157 char dma_byte; /* mask of byte for setting DMA value */
158 char int_byte; /* mask of byte for setting int value */
159 char cfg_byte; /* mirror of byte written to config reg (ASC_CFG). */
160 char cmd_byte; /* mirror of byte written to cmd port (ASC_CMD)*/
163 #define ATTACHED 0x01
166 #define DMA_ACTIVE 0x08
167 #define SLEEPING 0x10
168 #define SEL_COLL 0x20
169 #define PBM_MODE 0x40
170 #define FLAG_DEBUG 0x80
171 int geometry; /* resolution as geomtab index */
172 int linesize; /* length of one scan line (from geom.table) */
173 int blen; /* length of buffer in lines */
174 int btime; /* timeout of buffer in seconds/hz */
176 long icnt; /* interrupt count XXX for debugging */
178 pid_t selp; /* select pointer... */
182 int height; /* height, for pnm modes */
183 size_t bcount; /* bytes to read, for pnm modes */
185 void *devfs_asc; /* storage for devfs tokens (handles) */
192 static struct asc_unit unittab[NASC];
194 /*** I could not find a reasonable buffer size limit other than by
195 *** experiments. MAXPHYS is obviously too much, while DEV_BSIZE and
196 *** PAGE_SIZE are really too small. There must be something wrong
197 *** with isa_dmastart/isa_dmarangecheck HELP!!!
199 *** Note, must be DEFAULT_BLEN * samples_per_line <= MAX_BUFSIZE
201 #define MAX_BUFSIZE 0xb000 /* XXX was 0x3000 */
202 #define DEFAULT_BLEN 16
205 *** THE PER-DRIVER RECORD FOR ISA.C
207 static int ascprobe (struct isa_device *isdp);
208 static int ascattach(struct isa_device *isdp);
209 struct isa_driver ascdriver = { ascprobe, ascattach, "asc" };
213 static d_open_t ascopen;
214 static d_close_t ascclose;
215 static d_read_t ascread;
216 static d_ioctl_t ascioctl;
217 static d_poll_t ascpoll;
219 #define CDEV_MAJOR 71
221 static struct cdevsw asc_cdevsw =
222 { ascopen, ascclose, ascread, nowrite, /*71*/
223 ascioctl, nostop, nullreset, nodevtotty, /* asc */
224 ascpoll, nommap, NULL, "asc", NULL, -1 };
226 #define STATIC static
229 #endif /* ! FREEBSD_1_X */
232 *** LOCALLY USED SUBROUTINES
238 *** read resolution from the scanner
241 get_resolution(struct asc_unit *scu)
246 scu->cmd_byte = ASC_STANDBY;
247 outb(ASC_CMD, scu->cmd_byte);
248 tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascres", hz/10);
249 for(delay= 100; (res=inb(ASC_STAT)) & ASC_RDY_FLAG; delay--)
251 i = tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascres0", 1);
252 if ( ( i == 0 ) || ( i == EWOULDBLOCK ) )
258 lprintf("asc.get_resolution: timeout completing command\n");
261 /* ... actual read resolution... */
263 for (i=0; geomtab[i].dpi != INVALID; i++) {
264 if (geomtab[i].g_res == res) break;
266 if (geomtab[i].dpi==INVALID) {
267 scu->geometry= i; /* INVALID; */
268 lprintf("asc.get_resolution: wrong resolution\n");
270 lprintf("asc.get_resolution: %d dpi\n",geomtab[i].dpi);
273 scu->portf_byte=0; /* default */
274 if (geomtab[scu->geometry].g_res==0 && !(scu->thedev&FRMT_GRAY)) {
275 /* color scanner seems to require this */
277 /* scu->geometry++; */
279 scu->linesize = geomtab[scu->geometry].bpl;
280 scu->height = geomtab[scu->geometry].dpl; /* default... */
285 *** allocate/reallocate a buffer
286 *** Now just checks that the preallocated buffer is large enough.
290 buffer_allocate(struct asc_unit *scu)
294 size = scu->blen * scu->linesize;
296 lprintf("asc.buffer_allocate: need 0x%x bytes\n", size);
298 if ( size > MAX_BUFSIZE ) {
300 size= ( (MAX_BUFSIZE+scu->linesize-1) / scu->linesize)*scu->linesize;
301 lprintf("asc.buffer_allocate: 0x%x bytes are too much, try 0x%x\n",
306 scu->sbuf.size = size;
309 scu->sbuf.count = 0; /* available data for reading */
311 lprintf("asc.buffer_allocate: ok\n");
317 *** invoked locally to start dma. Must run in a critical section
320 dma_restart(struct asc_unit *scu)
322 unsigned char al=scu->cmd_byte;
324 if (geomtab[scu->geometry].g_res==0) {/* color */
325 isa_dmastart(B_READ, scu->sbuf.base+scu->sbuf.wptr,
326 scu->linesize + 90 /* XXX */ , scu->dma_num);
328 * looks like we have to set and then clear this
329 * bit to enable the scanner to send interrupts
331 outb( ASC_CMD, al |= 4 ); /* seems to disable interrupts */
333 outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
335 outb( ASC_CMD, al &= 0xfb );
337 } else { /* normal */
338 isa_dmastart(B_READ, scu->sbuf.base+scu->sbuf.wptr,
339 scu->linesize, scu->dma_num);
340 /*** this is done in sub_20, after dmastart ? ***/
342 outb( ASC_CMD, al |= 4 );
343 outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
344 outb( ASC_CMD, al &= 0xfb );
347 outb( ASC_CMD, ASC_OPERATE);
350 scu->flags |= DMA_ACTIVE;
354 *** the main functions
358 *** resets the scanner and the config bytes...
361 asc_reset(struct asc_unit *scu)
363 scu->cfg_byte = 0 ; /* clear... */
364 scu->cmd_byte = 0 ; /* clear... */
366 outb(ASC_CFG,scu->cfg_byte); /* for safety, do this here */
367 outb(ASC_CMD,scu->cmd_byte); /* probably not needed */
368 tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascres", hz/10); /* sleep .1 sec */
370 scu->blen = DEFAULT_BLEN;
371 scu->btime = TIMEOUT;
372 scu->height = 0 ; /* don't know better... */
374 /**************************************************************************
377 *** read status port and check for proper configuration:
378 *** - if address group matches (status byte has reasonable value)
379 *** cannot check interrupt/dma, only clear the config byte.
382 ascprobe (struct isa_device *isdp)
384 int unit = isdp->id_unit;
385 struct asc_unit *scu = unittab + unit;
388 scu->base = isdp->id_iobase; /*** needed by the following macros ***/
389 scu->flags = FLAG_DEBUG;
391 if ( isdp->id_iobase < 0 ) {
392 lprintf("asc%d.probe: no iobase given\n", unit);
396 if ((stb=inb(ASC_PROBE)) != ASC_PROBE_VALUE) {
397 lprintf("asc%d.probe: failed, got 0x%02x instead of 0x%02x\n",
398 unit, stb, ASC_PROBE_VALUE);
404 * the new AmiScan Color board uses int 10,11,12 instead of 3,5,10
405 * respectively. This means that the driver must act accordingly.
406 * Unfortunately there is no easy way of telling which board one has,
407 * other than trying to get an interrupt and noticing that it is
408 * missing. use "option ASC_NEW_BOARD" if you have a new board.
422 switch(ffs(isdp->id_irq) - 1) {
424 scu->int_byte = ASC_CNF_IRQ3;
427 scu->int_byte = ASC_CNF_IRQ5;
430 scu->int_byte = ASC_CNF_IRQ10;
435 lprintf("asc%d.probe: warning - going interruptless\n", unit);
439 lprintf("asc%d.probe: unsupported INT %d (only 3, 5, 10)\n",
440 unit, ffs(isdp->id_irq) - 1 );
443 scu->dma_num = isdp->id_drq;
444 switch(scu->dma_num) {
446 scu->dma_byte = ASC_CNF_DMA1;
449 scu->dma_byte = ASC_CNF_DMA3;
452 lprintf("asc%d.probe: unsupported DMA %d (only 1 or 3)\n",
457 /* lprintf("asc%d.probe: ok\n", unit); */
459 scu->flags &= ~FLAG_DEBUG;
461 return PROBE_SUCCESS;
464 /**************************************************************************
467 *** finish initialization of unit structure, get geometry value (?)
471 ascattach(struct isa_device *isdp)
473 int unit = isdp->id_unit;
474 struct asc_unit *scu = unittab + unit;
476 scu->flags |= FLAG_DEBUG;
477 printf("asc%d: [GI1904/Trust Ami-Scan Grey/Color]\n", unit);
480 * Initialize buffer structure.
481 * XXX this must be done early to give a good chance of getting a
482 * contiguous buffer. This wastes memory.
486 * The old contigmalloc() didn't have a `low/minpa' arg, and took masks
487 * instead of multipliers for the alignments.
489 scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
490 0xfffffful, 0ul, 0xfffful);
492 scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
493 0ul, 0xfffffful, 1ul, 0x10000ul);
495 if ( scu->sbuf.base == NULL )
497 lprintf("asc%d.attach: buffer allocation failed\n", unit);
498 return ATTACH_FAIL; /* XXX attach must not fail */
500 scu->sbuf.size = INVALID;
501 scu->sbuf.rptr = INVALID;
503 scu->flags |= ATTACHED;
504 /* lprintf("asc%d.attach: ok\n", unit); */
505 scu->flags &= ~FLAG_DEBUG;
508 scu->selp = (pid_t)0;
510 scu->selp.si_flags=0;
511 scu->selp.si_pid=(pid_t)0;
517 devfs_add_devswf(&asc_cdevsw, unit<<6, DV_CHR, ASC_UID,
518 ASC_GID, 0666, "asc%d", unit);
520 devfs_add_devswf(&asc_cdevsw, ((unit<<6) + FRMT_PBM), DV_CHR,
521 ASC_UID, ASC_GID, 0666, "asc%dp", unit);
523 devfs_add_devswf(&asc_cdevsw, ((unit<<6) + DBUG_MASK), DV_CHR,
524 ASC_UID, ASC_GID, 0666, "asc%dd", unit);
526 devfs_add_devswf(&asc_cdevsw, ((unit<<6) + DBUG_MASK+FRMT_PBM),
527 DV_CHR, ASC_UID, ASC_GID, 0666, "asc%dpd",
530 return ATTACH_SUCCESS;
533 /**************************************************************************
536 *** the interrupt routine, at the end of DMA...
541 struct asc_unit *scu = unittab + unit;
542 int chan_bit = 0x01 << scu->dma_num;
545 /* ignore stray interrupts... */
546 if ( scu->flags & (OPEN |READING) != (OPEN | READING) ) {
547 /* must be after closing... */
548 scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL);
551 if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) {
552 outb( ASC_CMD, ASC_STANDBY);
553 scu->flags &= ~DMA_ACTIVE;
554 /* bounce buffers... */
555 isa_dmadone(B_READ, scu->sbuf.base+scu->sbuf.wptr,
556 scu->linesize, scu->dma_num);
557 scu->sbuf.wptr += scu->linesize;
558 if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0;
559 scu->sbuf.count += scu->linesize;
560 if (scu->flags & SLEEPING) {
561 scu->flags &= ~SLEEPING;
562 wakeup((caddr_t)scu);
564 if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) {
569 selwakeup(&scu->selp, scu->flags & SEL_COLL );
571 scu->flags &= ~SEL_COLL;
574 if (scu->selp.si_pid) {
575 selwakeup(&scu->selp);
576 scu->selp.si_pid=(pid_t)0;
577 scu->selp.si_flags = 0;
583 /**************************************************************************
586 *** set open flag, set modes according to minor number
588 *** don't switch scanner on, wait until first read or ioctls go before
592 ascopen(dev_t dev, int flags, int fmt, struct proc *p)
594 struct asc_unit *scu;
597 unit = UNIT(minor(dev)) & UNIT_MASK;
601 /* XXX lprintf isn't valid here since there is no scu. */
602 printf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC);
606 scu = unittab + unit;
607 if ( !( scu->flags & ATTACHED ) )
609 lprintf("asc%d.open: unit was not attached successfully 0x%04x\n",
614 if ( minor(dev) & DBUG_MASK )
615 scu->flags |= FLAG_DEBUG;
617 scu->flags &= ~FLAG_DEBUG;
619 switch(minor(dev) & FRMT_MASK) {
621 scu->flags |= PBM_MODE;
622 lprintf("asc%d.open: pbm mode\n", unit);
625 lprintf("asc%d.open: raw mode\n", unit);
626 scu->flags &= ~PBM_MODE;
629 lprintf("asc%d.open: gray maps are not yet supported", unit);
633 lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
635 if ( scu->flags & OPEN ) {
636 lprintf("asc%d.open: already open", unit);
639 if (isa_dma_acquire(scu->dma_num))
642 scu->flags = ATTACHED | OPEN;
650 asc_startread(struct asc_unit *scu)
652 /*** from here on, things can be delayed to the first read/ioctl ***/
653 /*** this was done in sub_12... ***/
654 scu->cfg_byte= scu->cmd_byte=0; /* init scanner */
655 outb(ASC_CMD, scu->cmd_byte);
656 /*** this was done in sub_16, set scan len... ***/
657 outb(ASC_BOH, scu->portf_byte );
658 if (geomtab[scu->geometry].g_res==0) { /* color */
659 scu->cmd_byte = 0x00 ;
661 scu->cmd_byte = 0x90 ;
663 outb(ASC_CMD, scu->cmd_byte);
664 outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */);
665 outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */);
666 /*** this was done in sub_21, config DMA ... ***/
667 scu->cfg_byte |= scu->dma_byte;
668 outb(ASC_CFG, scu->cfg_byte);
669 /*** sub_22: enable int on the scanner ***/
670 scu->cfg_byte |= scu->int_byte;
671 outb(ASC_CFG, scu->cfg_byte);
672 /*** sub_28: light on etc...***/
673 scu->cmd_byte = ASC_STANDBY;
674 outb(ASC_CMD, scu->cmd_byte);
675 tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascstrd", hz/10); /* sleep .1 sec */
679 /**************************************************************************
682 *** turn off scanner, release the buffer
683 *** should probably terminate dma ops, release int and dma. lr 12mar95
687 ascclose(dev_t dev, int flags, int fmt, struct proc *p)
689 int unit = UNIT(minor(dev));
690 struct asc_unit *scu = unittab + unit;
692 lprintf("asc%d.close: minor %d\n",
695 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
696 lprintf("asc%d.close: unit was not attached successfully 0x%04x\n",
700 /* all this is in sub_29... */
702 outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */
703 scu->cmd_byte &= ~ASC_LIGHT_ON;
704 outb(ASC_CMD, scu->cmd_byte);/* light off */
705 tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */
706 scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */
707 scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */
708 outb(ASC_CFG, scu->cfg_byte);
709 /* --- disable dma controller ? --- */
710 isa_dma_release(scu->dma_num);
711 /* --- disable interrupts on the controller (sub_24) --- */
713 scu->sbuf.size = INVALID;
714 scu->sbuf.rptr = INVALID;
716 scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
722 pbm_init(struct asc_unit *scu)
724 int width = geomtab[scu->geometry].dpl;
725 int l= sprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
728 scu->bcount = scu->height * width / 8 + l;
730 /* move header to end of sbuf */
731 scu->sbuf.rptr=scu->sbuf.size-l;
732 bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l);
734 if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
735 for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--)
739 /**************************************************************************
745 ascread(dev_t dev, struct uio *uio, int ioflag)
747 int unit = UNIT(minor(dev));
748 struct asc_unit *scu = unittab + unit;
753 lprintf("asc%d.read: minor %d icnt %d\n", unit, minor(dev), scu->icnt);
755 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
756 lprintf("asc%d.read: unit was not attached successfully 0x%04x\n",
761 if ( !(scu->flags & READING) ) { /*** first read... ***/
762 /* allocate a buffer for reading data and init things */
763 if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING;
766 if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */
771 lprintf("asc%d.read(before): "
772 "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %d\n",
773 unit, scu->sbuf.size, scu->sbuf.rptr,
774 scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,
778 if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */
779 if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
780 scu->flags |= SLEEPING;
781 res = tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascread", 0);
782 scu->flags &= ~SLEEPING;
783 if ( res == 0 ) res = SUCCESS;
785 splx(sps); /* lower priority... */
786 if (scu->flags & FLAG_DEBUG)
787 tsleep((caddr_t)scu, ASCPRI | PCATCH, "ascdly",hz);
788 lprintf("asc%d.read(after): "
789 "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %d\n",
790 unit, scu->sbuf.size, scu->sbuf.rptr,
791 scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt);
793 /* first, not more than available... */
794 nbytes = min( uio->uio_resid, scu->sbuf.count );
795 /* second, contiguous data... */
796 nbytes = min( nbytes, (scu->sbuf.size - scu->sbuf.rptr) );
797 /* third, one line (will remove this later, XXX) */
798 nbytes = min( nbytes, scu->linesize );
799 if ( (scu->flags & PBM_MODE) )
800 nbytes = min( nbytes, scu->bcount );
801 lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes);
802 if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
803 lprintf("asc%d.read: invert buffer\n",unit);
804 for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--)
807 res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio);
808 if ( res != SUCCESS ) {
809 lprintf("asc%d.read: uiomove failed %d", unit, res);
814 scu->sbuf.rptr += nbytes;
815 if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0;
816 scu->sbuf.count -= nbytes;
817 /* having moved some data, can read mode */
818 if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
819 splx(sps); /* lower priority... */
820 if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
822 lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
823 unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount);
828 /**************************************************************************
834 ascioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
836 int unit = UNIT(minor(dev));
837 struct asc_unit *scu = unittab + unit;
839 lprintf("asc%d.ioctl: minor %d\n",
842 if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
843 lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n",
851 *(int *)data=geomtab[scu->geometry].dpi;
852 lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data);
855 *(int *)data=geomtab[scu->geometry].dpl;
856 lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data);
859 *(int *)data=scu->height;
860 lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data);
863 lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data);
864 if ( scu->flags & READING ) {
865 lprintf("asc%d:ioctl on already reading unit\n", unit);
868 scu->height=*(int *)data;
872 *(int *)data=scu->blen;
873 lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data);
876 lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data);
877 if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
879 lprintf("asc%d:ioctl buffer size too high\n", unit);
882 scu->blen=*(int *)data;
885 *(int *)data = scu->btime / hz;
886 lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data);
889 scu->btime = *(int *)data * hz;
890 lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data);
893 default: return ENOTTY;
899 ascpoll(dev_t dev, int events, struct proc *p)
901 int unit = UNIT(minor(dev));
902 struct asc_unit *scu = unittab + unit;
909 if (events & (POLLIN | POLLRDNORM))
910 if (scu->sbuf.count >0)
911 revents |= events & (POLLIN | POLLRDNORM);
913 if (!(scu->flags & DMA_ACTIVE))
916 if (scu->selp.si_pid && (p1=pfind(scu->selp.si_pid))
917 && p1->p_wchan == (caddr_t)&selwait)
918 scu->selp.si_flags = SI_COLL;
920 scu->selp.si_pid = p->p_pid;
928 static asc_devsw_installed = 0;
931 asc_drvinit(void *unused)
935 if( ! asc_devsw_installed ) {
936 dev = makedev(CDEV_MAJOR,0);
937 cdevsw_add(&dev,&asc_cdevsw,NULL);
938 asc_devsw_installed = 1;
942 SYSINIT(ascdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,asc_drvinit,NULL)
945 #endif /* NASC > 0 */