kernel - Tear out device polling
[dragonfly.git] / sys / platform / pc32 / isa / asc.c
1 /* asc.c - device driver for hand scanners
2  *
3  * Current version supports:
4  *
5  *      - AmiScan (Mustek) Color and BW hand scanners (GI1904 chipset)
6  *
7  * Copyright (c) 1995 Gunther Schadow.  All rights reserved.
8  * Copyright (c) 1995,1996,1997 Luigi Rizzo.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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
21  *      and Luigi Rizzo.
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.
24  *
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.
35  */
36 /*
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 $
39  */
40
41 #include "use_asc.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/proc.h>
46 #include <sys/buf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/event.h>
50 #include <sys/selinfo.h>
51 #include <sys/uio.h>
52 #include <sys/thread2.h>
53 #include <sys/bus.h>
54
55 #include <machine/asc_ioctl.h>
56
57 #include <bus/isa/isa.h>
58 #include <bus/isa/isa_device.h>
59 #include <bus/isa/isavar.h>
60 #include "ascreg.h"
61
62 /***
63  *** CONSTANTS & DEFINES
64  ***
65  ***/
66
67 #define PROBE_FAIL    0
68 #define PROBE_SUCCESS IO_ASCSIZE
69 #define ATTACH_FAIL   0
70 #define ATTACH_SUCCESS 1
71 #define SUCCESS       0
72 #define FAIL         -1
73 #define INVALID       FAIL
74
75 #define DMA1_READY  0x08
76 #define ASCDEBUG
77 #ifdef ASCDEBUG
78 #       define lprintf if(scu->flags & FLAG_DEBUG) kprintf
79 #else
80 #       define lprintf (void)
81 #endif
82
83 #define TIMEOUT (hz*15)  /* timeout while reading a buffer - default value */
84
85 /***
86  *** LAYOUT OF THE MINOR NUMBER
87  ***/
88
89 #define UNIT_MASK 0xc0    /* unit asc0 .. asc3 */
90 #define UNIT(x)   (x >> 6)
91 #define DBUG_MASK 0x20
92 #define FRMT_MASK 0x18    /* output format */
93 #define FRMT_RAW  0x00    /* output bits as read from scanner */
94 #define FRMT_GRAY 0x1     /* output gray mode for color scanner */
95 #define FRMT_PBM  0x08    /* output pbm format */
96 #define FRMT_PGM  0x18
97
98 /***
99  *** THE GEMOMETRY TABLE
100  ***/
101
102 #define GREY_LINE 826 /* 825, or 826 , or 550 ??? */
103 static const struct asc_geom {
104   int dpi;     /* dots per inch */
105   int dpl;     /* dots per line */
106   int bpl;     /* bytes per line */
107   int g_res;   /* get resolution value (ASC_STAT) */
108 } geomtab[] = {
109   { 800, 3312, 414, ASC_RES_800},
110   { 700, 2896, 362, ASC_RES_700},
111   { 600, 2480, 310, ASC_RES_600},
112   { 500, 1656, 258, ASC_RES_500},
113   { 400, 1656, 207, ASC_RES_400},
114   { 300, 1240, 155, ASC_RES_300},
115   { 200, 832, 104, ASC_RES_200},
116   { 100, 416, 52, ASC_RES_100},
117   { 200, 3*GREY_LINE, 3*GREY_LINE, 0 /* returned by color scanner */},
118   { 200, GREY_LINE, GREY_LINE, 0 /* color scanner, grey mode */},
119   { INVALID, 416, 52, INVALID } /* terminator */
120 };
121
122 /***
123  *** THE TABLE OF UNITS
124  ***/
125
126 struct _sbuf {
127   size_t  size;
128   size_t  rptr;
129   size_t  wptr; /* only changed in ascintr */
130   size_t  count;
131   char   *base;
132 };
133
134 struct asc_unit {
135   long thedev;  /* XXX */
136   int base;             /* base address */
137   int dma_num;          /* dma number */
138   char    dma_byte;       /* mask of byte for setting DMA value */
139   char    int_byte;       /* mask of byte for setting int value */
140   char    cfg_byte;       /* mirror of byte written to config reg (ASC_CFG). */
141   char    cmd_byte;       /* mirror of byte written to cmd port (ASC_CMD)*/
142   char   portf_byte;
143   int flags;
144 #define ATTACHED        0x01
145 #define OPEN            0x02
146 #define READING         0x04
147 #define DMA_ACTIVE      0x08
148 #define SLEEPING        0x10
149 #define SEL_COLL        0x20
150 #define PBM_MODE        0x40
151 #define FLAG_DEBUG      0x80
152   int     geometry;       /* resolution as geomtab index */
153   int     linesize;       /* length of one scan line (from geom.table) */
154   int     blen;           /* length of buffer in lines */
155   int     btime;          /* timeout of buffer in seconds/hz */
156   struct  _sbuf sbuf;
157   long    icnt;         /* interrupt count XXX for debugging */
158   struct selinfo selp;
159   int     height;         /* height, for pnm modes */
160   size_t  bcount;         /* bytes to read, for pnm modes */
161 };
162
163 static struct asc_unit unittab[NASC];                                 
164
165 /*** I could not find a reasonable buffer size limit other than by
166  *** experiments. MAXPHYS is obviously too much, while DEV_BSIZE and
167  *** PAGE_SIZE are really too small. There must be something wrong
168  *** with isa_dmastart/isa_dmarangecheck HELP!!!
169  ***
170  *** Note, must be DEFAULT_BLEN * samples_per_line <= MAX_BUFSIZE
171  ***/
172 #define MAX_BUFSIZE 0xb000 /* XXX was 0x3000 */
173 #define DEFAULT_BLEN 16
174
175 /***
176  *** THE PER-DRIVER RECORD FOR ISA.C
177  ***/
178 static int ascprobe (struct isa_device *isdp);
179 static int ascattach(struct isa_device *isdp);
180 struct isa_driver ascdriver = { ascprobe, ascattach, "asc" };
181
182 static void             ascintr(void *);
183
184 static d_open_t         ascopen;
185 static d_close_t        ascclose;
186 static d_read_t         ascread;
187 static d_ioctl_t        ascioctl;
188 static d_kqfilter_t     asckqfilter;
189
190 static void ascfilter_detach(struct knote *kn);
191 static int ascfilter(struct knote *kn, long hint);
192
193 #define CDEV_MAJOR 71
194
195 static struct dev_ops asc_ops = {
196         { "asc", CDEV_MAJOR, D_KQFILTER },
197         .d_open =       ascopen,
198         .d_close =      ascclose,
199         .d_read =       ascread,
200         .d_ioctl =      ascioctl,
201         .d_kqfilter =   asckqfilter
202 };
203
204 #define STATIC static
205
206 /***
207  *** LOCALLY USED SUBROUTINES
208  ***
209  ***/
210
211 /***
212  *** get_resolution
213  ***    read resolution from the scanner
214  ***/
215 static void
216 get_resolution(struct asc_unit *scu)
217 {
218     int res, i, delay;
219
220     res=0;
221     scu->cmd_byte = ASC_STANDBY;
222     outb(ASC_CMD, scu->cmd_byte);
223     tsleep((caddr_t)scu, PCATCH, "ascres", hz/10);
224     for(delay= 100; (res=inb(ASC_STAT)) & ASC_RDY_FLAG; delay--)
225     {
226         i = tsleep((caddr_t)scu, PCATCH, "ascres0", 1);
227         if ( ( i == 0 ) || ( i == EWOULDBLOCK ) )
228             i = SUCCESS;
229         else
230             break;
231     }
232     if (delay==0) {
233         lprintf("asc.get_resolution: timeout completing command\n");
234         return /*  -1 */;
235     }
236     /* ... actual read resolution... */
237     res &= ASC_RES_MASK;
238     for (i=0; geomtab[i].dpi != INVALID; i++) {
239         if (geomtab[i].g_res == res) break;
240     }
241     if (geomtab[i].dpi==INVALID) {
242         scu->geometry= i; /* INVALID; */
243         lprintf("asc.get_resolution: wrong resolution\n");
244     } else {
245         lprintf("asc.get_resolution: %d dpi\n",geomtab[i].dpi);
246         scu->geometry = i;
247     }
248     scu->portf_byte=0; /* default */
249     if (geomtab[scu->geometry].g_res==0 && !(scu->thedev&FRMT_GRAY)) {
250         /* color scanner seems to require this */
251         scu->portf_byte=2;
252         /* scu->geometry++; */
253     }
254     scu->linesize = geomtab[scu->geometry].bpl;
255     scu->height = geomtab[scu->geometry].dpl; /* default... */
256 }
257
258 /***
259  *** buffer_allocate
260  ***    allocate/reallocate a buffer
261  ***    Now just checks that the preallocated buffer is large enough.
262  ***/
263
264 static int
265 buffer_allocate(struct asc_unit *scu)
266 {
267   size_t size, size1;
268
269   size = scu->blen * scu->linesize;
270
271   lprintf("asc.buffer_allocate: need 0x%x bytes\n", size);
272
273   if ( size > MAX_BUFSIZE ) {
274       size1=size;
275       size= ( (MAX_BUFSIZE+scu->linesize-1) / scu->linesize)*scu->linesize;
276       lprintf("asc.buffer_allocate: 0x%x bytes are too much, try 0x%x\n",
277           size1, size);
278       return ENOMEM;
279   }
280
281   scu->sbuf.size = size;
282   scu->sbuf.rptr  = 0;
283   scu->sbuf.wptr  = 0;
284   scu->sbuf.count  = 0; /* available data for reading */
285
286   lprintf("asc.buffer_allocate: ok\n");
287
288   return SUCCESS;
289 }
290
291 /*** dma_restart
292  ***    invoked locally to start dma. Must run in a critical section
293  ***/
294 static void
295 dma_restart(struct asc_unit *scu)
296 {
297     unsigned char al=scu->cmd_byte;
298
299     if (geomtab[scu->geometry].g_res==0) {/* color */
300         isa_dmastart(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
301             scu->linesize + 90 /* XXX */ , scu->dma_num);
302         /*
303          * looks like we have to set and then clear this
304          * bit to enable the scanner to send interrupts
305          */
306         outb( ASC_CMD, al |= 4 ); /* seems to disable interrupts */
307 #if 0
308         outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
309 #endif
310         outb( ASC_CMD, al &= 0xfb );
311         scu->cmd_byte = al;
312     } else {                                    /* normal */
313     isa_dmastart(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
314         scu->linesize, scu->dma_num);
315     /*** this is done in sub_20, after dmastart ? ***/  
316 #if 0
317     outb( ASC_CMD, al |= 4 );
318     outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
319     outb( ASC_CMD, al &= 0xfb );
320     scu->cmd_byte = al;
321 #else
322     outb( ASC_CMD, ASC_OPERATE); 
323 #endif
324     }
325     scu->flags |= DMA_ACTIVE;
326 }
327
328 /***
329  *** the main functions
330  ***/
331
332 /*** asc_reset
333  ***    resets the scanner and the config bytes...
334  ***/
335 static void
336 asc_reset(struct asc_unit *scu)
337 {
338   scu->cfg_byte = 0 ; /* clear... */
339   scu->cmd_byte = 0 ; /* clear... */
340
341   outb(ASC_CFG,scu->cfg_byte);  /* for safety, do this here */
342   outb(ASC_CMD,scu->cmd_byte);  /* probably not needed */
343   tsleep((caddr_t)scu, PCATCH, "ascres", hz/10); /* sleep .1 sec */
344
345   scu->blen = DEFAULT_BLEN;
346   scu->btime = TIMEOUT;
347   scu->height = 0 ; /* don't know better... */
348 }
349 /**************************************************************************
350  ***
351  *** ascprobe
352  ***    read status port and check for proper configuration:
353  ***    - if address group matches (status byte has reasonable value)
354  ***      cannot check interrupt/dma, only clear the config byte.
355  ***/
356 static int
357 ascprobe (struct isa_device *isdp)
358 {
359   int unit = isdp->id_unit;
360   struct asc_unit *scu = unittab + unit;
361   int stb;
362
363   scu->base = isdp->id_iobase; /*** needed by the following macros ***/
364   scu->flags = FLAG_DEBUG;
365
366   if ( isdp->id_iobase < 0 ) {
367       lprintf("asc%d.probe: no iobase given\n", unit);
368       return PROBE_FAIL;
369   }
370
371   if ((stb=inb(ASC_PROBE)) != ASC_PROBE_VALUE) {
372       lprintf("asc%d.probe: failed, got 0x%02x instead of 0x%02x\n",
373           unit, stb, ASC_PROBE_VALUE);
374       return PROBE_FAIL;
375   }
376
377 /*
378  * NOTE NOTE NOTE
379  * the new AmiScan Color board uses int 10,11,12 instead of 3,5,10
380  * respectively. This means that the driver must act accordingly.
381  * Unfortunately there is no easy way of telling which board one has,
382  * other than trying to get an interrupt and noticing that it is
383  * missing. use "option ASC_NEW_BOARD" if you have a new board.
384  *
385  */
386
387 #if ASC_NEW_BOARD
388 #define ASC_IRQ_A       10
389 #define ASC_IRQ_B       11
390 #define ASC_IRQ_C       12
391 #else
392 #define ASC_IRQ_A       3
393 #define ASC_IRQ_B       5
394 #define ASC_IRQ_C       10
395 #endif
396
397   switch(ffs(isdp->id_irq) - 1) {
398     case ASC_IRQ_A :
399       scu->int_byte = ASC_CNF_IRQ3;
400       break;
401     case ASC_IRQ_B :
402       scu->int_byte = ASC_CNF_IRQ5;
403       break;
404     case ASC_IRQ_C :
405       scu->int_byte = ASC_CNF_IRQ10;
406       break;
407 #if 0
408     case -1:
409       scu->int_byte = 0;
410       lprintf("asc%d.probe: warning - going interruptless\n", unit);
411       break;
412 #endif
413     default:
414       lprintf("asc%d.probe: unsupported INT %d (only 3, 5, 10)\n",
415                 unit, ffs(isdp->id_irq) - 1 );
416       return PROBE_FAIL;
417   }
418   scu->dma_num = isdp->id_drq;
419   switch(scu->dma_num) {
420     case 1:
421       scu->dma_byte = ASC_CNF_DMA1;
422       break;
423     case 3:
424       scu->dma_byte = ASC_CNF_DMA3;
425       break;
426     default:
427       lprintf("asc%d.probe: unsupported DMA %d (only 1 or 3)\n", 
428                 unit, scu->dma_num);
429       return PROBE_FAIL;
430   }
431   asc_reset(scu);
432 /*  lprintf("asc%d.probe: ok\n", unit); */
433
434   scu->flags &= ~FLAG_DEBUG;
435   scu->icnt = 0;
436   return PROBE_SUCCESS;
437 }
438
439 /**************************************************************************
440  ***
441  *** ascattach
442  ***    finish initialization of unit structure, get geometry value (?)
443  ***/
444
445 static int
446 ascattach(struct isa_device *isdp)
447 {
448   int unit = isdp->id_unit;
449   struct asc_unit *scu = unittab + unit;
450
451   isdp->id_intr = (inthand2_t *)ascintr;
452   scu->flags |= FLAG_DEBUG;
453   kprintf("asc%d: [GI1904/Trust Ami-Scan Grey/Color]\n", unit);
454
455   /*
456    * Initialize buffer structure.
457    * XXX this must be done early to give a good chance of getting a
458    * contiguous buffer.  This wastes memory.
459    */
460   scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
461                                 0ul, 0xfffffful, 1ul, 0x10000ul);
462   if ( scu->sbuf.base == NULL )
463     {
464       lprintf("asc%d.attach: buffer allocation failed\n", unit);
465       return ATTACH_FAIL;       /* XXX attach must not fail */
466     }
467   scu->sbuf.size = INVALID;
468   scu->sbuf.rptr  = INVALID;
469
470   scu->flags |= ATTACHED;
471 /*  lprintf("asc%d.attach: ok\n", unit); */
472   scu->flags &= ~FLAG_DEBUG;
473
474     scu->selp.si_flags=0;
475     scu->selp.si_pid=(pid_t)0;
476 #define ASC_UID 0
477 #define ASC_GID 13
478   make_dev(&asc_ops, unit<<6, ASC_UID, ASC_GID, 0666, "asc%d", unit);
479   make_dev(&asc_ops, ((unit<<6) + FRMT_PBM),
480            ASC_UID,  ASC_GID, 0666, "asc%dp", unit);
481   make_dev(&asc_ops, ((unit<<6) + DBUG_MASK),
482            ASC_UID,  ASC_GID, 0666, "asc%dd", unit);
483   make_dev(&asc_ops, ((unit<<6) + DBUG_MASK+FRMT_PBM), 
484            ASC_UID, ASC_GID, 0666, "asc%dpd", unit);
485   return ATTACH_SUCCESS;
486 }
487
488 /**************************************************************************
489  ***
490  *** ascintr
491  ***    the interrupt routine, at the end of DMA...
492  ***/
493 static void
494 ascintr(void *arg)
495 {
496     int unit = (int)arg;
497     struct asc_unit *scu = unittab + unit;
498     int chan_bit = 0x01 << scu->dma_num;
499
500     scu->icnt++;
501     /* ignore stray interrupts... */
502     if ((scu->flags & (OPEN |READING)) != (OPEN | READING) ) {
503         /* must be after closing... */
504         scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL);
505         return;
506     }
507     if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) {
508         outb( ASC_CMD, ASC_STANDBY);
509         scu->flags &= ~DMA_ACTIVE;
510                 /* bounce buffers... */
511         isa_dmadone(BUF_CMD_READ, scu->sbuf.base+scu->sbuf.wptr,
512             scu->linesize, scu->dma_num);
513         scu->sbuf.wptr += scu->linesize;
514         if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0;
515         scu->sbuf.count += scu->linesize;
516         if (scu->flags & SLEEPING) {
517             scu->flags &= ~SLEEPING;
518             wakeup((caddr_t)scu);
519         }
520         if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) {
521             dma_restart(scu);
522         }
523         if (scu->selp.si_pid) {
524             selwakeup(&scu->selp);
525             scu->selp.si_pid=(pid_t)0;
526             scu->selp.si_flags = 0;
527         }
528     }
529 }
530
531 /**************************************************************************
532  ***
533  *** ascopen
534  ***    set open flag, set modes according to minor number
535  ***    FOR RELEASE:
536  ***    don't switch scanner on, wait until first read or ioctls go before
537  ***/
538
539 STATIC int
540 ascopen(struct dev_open_args *ap)
541 {
542   cdev_t dev = ap->a_head.a_dev;
543   struct asc_unit *scu;
544   int unit;
545
546   unit = UNIT(minor(dev)) & UNIT_MASK;
547   if ( unit >= NASC )
548     {
549 #ifdef ASCDEBUG
550       /* XXX lprintf isn't valid here since there is no scu. */
551       kprintf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC);
552 #endif
553       return ENXIO;
554     }
555   scu = unittab + unit;
556   if ( !( scu->flags & ATTACHED ) )
557     {
558       lprintf("asc%d.open: unit was not attached successfully 0x%04x\n",
559              unit, scu->flags);
560       return ENXIO;
561     }
562
563   if ( minor(dev) & DBUG_MASK )
564     scu->flags |= FLAG_DEBUG;
565   else
566     scu->flags &= ~FLAG_DEBUG;
567
568   switch(minor(dev) & FRMT_MASK) {
569   case FRMT_PBM:
570     scu->flags |= PBM_MODE;
571     lprintf("asc%d.open: pbm mode\n", unit);
572     break;
573   case FRMT_RAW:
574     lprintf("asc%d.open: raw mode\n", unit);
575     scu->flags &= ~PBM_MODE;
576     break;
577   default:
578     lprintf("asc%d.open: gray maps are not yet supported", unit);
579     return ENXIO;
580   }
581   
582   lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
583
584   if ( scu->flags & OPEN ) {
585       lprintf("asc%d.open: already open", unit);
586       return EBUSY;
587   }
588   if (isa_dma_acquire(scu->dma_num))
589       return(EBUSY);
590
591   scu->flags = ATTACHED | OPEN;      
592
593   asc_reset(scu);
594   get_resolution(scu);
595   return SUCCESS;
596 }
597
598 static int
599 asc_startread(struct asc_unit *scu)
600 {
601     /*** from here on, things can be delayed to the first read/ioctl ***/
602     /*** this was done in sub_12... ***/
603   scu->cfg_byte= scu->cmd_byte=0;       /* init scanner */
604   outb(ASC_CMD, scu->cmd_byte);
605     /*** this was done in sub_16, set scan len... ***/
606   outb(ASC_BOH, scu->portf_byte );
607   if (geomtab[scu->geometry].g_res==0) {                /* color */
608         scu->cmd_byte = 0x00 ;
609   } else {
610   scu->cmd_byte = 0x90 ;
611   }
612   outb(ASC_CMD, scu->cmd_byte);
613   outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */);
614   outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */);
615     /*** this was done in sub_21, config DMA ... ***/
616   scu->cfg_byte |= scu->dma_byte;
617   outb(ASC_CFG, scu->cfg_byte);
618     /*** sub_22: enable int on the scanner ***/
619   scu->cfg_byte |= scu->int_byte;
620   outb(ASC_CFG, scu->cfg_byte);
621     /*** sub_28: light on etc...***/
622   scu->cmd_byte = ASC_STANDBY;
623   outb(ASC_CMD, scu->cmd_byte);
624   tsleep((caddr_t)scu, PCATCH, "ascstrd", hz/10); /* sleep .1 sec */
625   return SUCCESS;
626 }
627
628 /**************************************************************************
629  ***
630  *** ascclose
631  ***    turn off scanner, release the buffer
632  ***    should probably terminate dma ops, release int and dma. lr 12mar95
633  ***/
634
635 STATIC int
636 ascclose(struct dev_close_args *ap)
637 {
638   cdev_t dev = ap->a_head.a_dev;
639   int unit = UNIT(minor(dev));
640   struct asc_unit *scu = unittab + unit;
641
642   lprintf("asc%d.close: minor %d\n",
643          unit, minor(dev));
644
645   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
646       lprintf("asc%d.close: unit was not attached successfully 0x%04x\n",
647              unit, scu->flags);
648       return ENXIO;
649   }
650     /* all this is in sub_29... */
651   /* cli(); */
652   outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */
653   scu->cmd_byte &= ~ASC_LIGHT_ON;
654   outb(ASC_CMD, scu->cmd_byte);/* light off */
655   tsleep((caddr_t)scu, PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */
656   scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */
657   scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */
658   outb(ASC_CFG, scu->cfg_byte);
659     /* --- disable dma controller ? --- */
660   isa_dma_release(scu->dma_num);
661     /* --- disable interrupts on the controller (sub_24) --- */
662
663   scu->sbuf.size = INVALID;
664   scu->sbuf.rptr  = INVALID;
665
666   scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
667   
668   return SUCCESS;
669 }
670
671 static void
672 pbm_init(struct asc_unit *scu)
673 {
674     int width = geomtab[scu->geometry].dpl;
675     int l= ksprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
676     char *p;
677
678     scu->bcount = scu->height * width / 8 + l;
679
680       /* move header to end of sbuf */
681     scu->sbuf.rptr=scu->sbuf.size-l;
682     bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l);
683     scu->sbuf.count = l;
684     if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
685     for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--)
686         *p = ~*p;
687 }
688 }
689 /**************************************************************************
690  ***
691  *** ascread
692  ***/
693
694 STATIC int
695 ascread(struct dev_read_args *ap)
696 {
697   cdev_t dev = ap->a_head.a_dev;
698   struct uio *uio = ap->a_uio;
699   int unit = UNIT(minor(dev));
700   struct asc_unit *scu = unittab + unit;
701   size_t nbytes;
702   int res;
703   unsigned char *p;
704   
705   lprintf("asc%d.read: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
706
707   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
708       lprintf("asc%d.read: unit was not attached successfully 0x%04x\n",
709              unit, scu->flags);
710       return ENXIO;
711   }
712
713   if ( !(scu->flags & READING) ) { /*** first read... ***/
714         /* allocate a buffer for reading data and init things */
715       if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING;
716       else return res;
717       asc_startread(scu);
718       if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */
719           pbm_init(scu);
720       }
721   }
722   
723   lprintf("asc%d.read(before): "
724       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
725           unit, scu->sbuf.size, scu->sbuf.rptr,
726           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,
727           scu->icnt);
728
729   crit_enter();
730   if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */
731       if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
732       scu->flags |= SLEEPING;
733       res = tsleep((caddr_t)scu, PCATCH, "ascread", 0);
734       scu->flags &= ~SLEEPING;
735       if ( res == 0 ) res = SUCCESS;
736   }
737   crit_exit();
738   if (scu->flags & FLAG_DEBUG)
739       tsleep((caddr_t)scu, PCATCH, "ascdly",hz);
740   lprintf("asc%d.read(after): "
741       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
742           unit, scu->sbuf.size, scu->sbuf.rptr,
743           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt);
744
745         /* first, not more than available... */
746   nbytes = szmin(uio->uio_resid, scu->sbuf.count);
747         /* second, contiguous data... */
748   nbytes = szmin( nbytes, (scu->sbuf.size - scu->sbuf.rptr) );
749         /* third, one line (will remove this later, XXX) */
750   nbytes = szmin( nbytes, scu->linesize );
751   if ( (scu->flags & PBM_MODE) )
752       nbytes = szmin( nbytes, scu->bcount );
753   lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes);
754   if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
755   lprintf("asc%d.read: invert buffer\n",unit);
756   for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--)
757         *p = ~*p;
758   }
759   res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio);
760   if ( res != SUCCESS ) {
761       lprintf("asc%d.read: uiomove failed %d", unit, res);
762       return res;
763   }
764   
765   crit_enter();
766   scu->sbuf.rptr += nbytes;
767   if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0;
768   scu->sbuf.count -= nbytes;
769         /* having moved some data, can read mode */
770   if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
771   crit_exit();
772   if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
773   
774   lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
775           unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount);
776   
777   return SUCCESS;
778 }
779
780 /**************************************************************************
781  ***
782  *** ascioctl
783  ***/
784
785 STATIC int
786 ascioctl(struct dev_ioctl_args *ap)
787 {
788   cdev_t dev = ap->a_head.a_dev;
789   caddr_t data = ap->a_data;
790   int unit = UNIT(minor(dev));
791   struct asc_unit *scu = unittab + unit;
792
793   lprintf("asc%d.ioctl: minor %d\n",
794          unit, minor(dev));
795
796   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
797       lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n",
798              unit, scu->flags);
799       return ENXIO;
800   }
801   switch(ap->a_cmd) {
802   case ASC_GRES:
803     asc_reset(scu);
804     get_resolution(scu);
805     *(int *)data=geomtab[scu->geometry].dpi;
806     lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data);
807     return SUCCESS;    
808   case ASC_GWIDTH:
809     *(int *)data=geomtab[scu->geometry].dpl;
810     lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data);
811     return SUCCESS;    
812   case ASC_GHEIGHT:
813     *(int *)data=scu->height;
814     lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data);
815     return SUCCESS;
816   case ASC_SHEIGHT:
817     lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data);
818     if ( scu->flags & READING ) { 
819         lprintf("asc%d:ioctl on already reading unit\n", unit);
820         return EBUSY;
821     }
822     scu->height=*(int *)data;
823     return SUCCESS;
824 #if 0  
825   case ASC_GBLEN:
826     *(int *)data=scu->blen;
827     lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data);
828     return SUCCESS;
829   case ASC_SBLEN:
830     lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data);
831     if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
832       {
833         lprintf("asc%d:ioctl buffer size too high\n", unit);
834         return ENOMEM;
835       }
836     scu->blen=*(int *)data;
837     return SUCCESS;
838   case ASC_GBTIME:
839     *(int *)data = scu->btime / hz;
840     lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data);
841     return SUCCESS;
842   case ASC_SBTIME:
843     scu->btime = *(int *)data * hz;
844     lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data);
845     return SUCCESS;
846 #endif
847   default: return ENOTTY;
848   }
849   return SUCCESS;
850 }
851
852 static struct filterops ascfiltops =
853     { 1, NULL, ascfilter_detach, ascfilter };
854
855 STATIC int
856 asckqfilter(struct dev_kqfilter_args *ap)
857 {
858     cdev_t dev = ap->a_head.a_dev;
859     int unit = UNIT(minor(dev));
860     struct asc_unit *scu = unittab + unit;
861     struct knote *kn = ap->a_kn;
862     struct klist *klist;
863
864     ap->a_result = 0;
865
866     switch (kn->kn_filter) {
867     case EVFILT_READ:
868         kn->kn_fop = &ascfiltops;
869         kn->kn_hook = (caddr_t)scu;
870         break;
871     default:
872         ap->a_result = EOPNOTSUPP;
873         return (0);
874     }
875
876     crit_enter();
877     klist = &scu->selp.si_note;
878     SLIST_INSERT_HEAD(klist, kn, kn_selnext);
879     crit_exit();
880
881     return (0);
882 }
883
884 STATIC void
885 ascfilter_detach(struct knote *kn)
886 {
887     struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
888     struct klist *klist;
889
890     crit_enter();
891     klist = &scu->selp.si_note;
892     SLIST_REMOVE(klist, kn, knote, kn_selnext);
893     crit_exit();
894 }
895
896 STATIC int
897 ascfilter(struct knote *kn, long hint)
898 {
899     struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
900     int ready = 0;
901
902     crit_enter();
903     if (scu->sbuf.count >0)
904         ready = 1;
905     else {
906         if (!(scu->flags & DMA_ACTIVE))
907             dma_restart(scu);
908     }
909     crit_exit();
910
911     return (ready);
912 }