kernel - Fix kqfilter error return codes
[dragonfly.git] / sys / platform / pc64 / 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  * Copyright (c) 2008 The DragonFly Project.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Gunther Schadow
22  *      and Luigi Rizzo.
23  * 4. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  * $FreeBSD: src/sys/i386/isa/asc.c,v 1.42.2.2 2001/03/01 03:22:39 jlemon Exp $
39  * $DragonFly: src/sys/platform/pc64/isa/asc.c,v 1.1 2008/08/29 17:07:19 dillon Exp $
40  */
41
42 #include "use_asc.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/poll.h>
51 #include <sys/event.h>
52 #include <sys/selinfo.h>
53 #include <sys/uio.h>
54 #include <sys/thread2.h>
55
56 #include <machine/asc_ioctl.h>
57
58 #include <bus/isa/isa.h>
59 #include <bus/isa/isa_device.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_poll_t         ascpoll;
189 static d_kqfilter_t     asckqfilter;
190
191 static void ascfilter_detach(struct knote *kn);
192 static int ascfilter(struct knote *kn, long hint);
193
194 #define CDEV_MAJOR 71
195
196 static struct dev_ops asc_ops = {
197         { "asc", CDEV_MAJOR, D_KQFILTER },
198         .d_open =       ascopen,
199         .d_close =      ascclose,
200         .d_read =       ascread,
201         .d_ioctl =      ascioctl,
202         .d_poll =       ascpoll,
203         .d_kqfilter =   asckqfilter
204 };
205
206 #define STATIC static
207
208 /***
209  *** LOCALLY USED SUBROUTINES
210  ***
211  ***/
212
213 /***
214  *** get_resolution
215  ***    read resolution from the scanner
216  ***/
217 static void
218 get_resolution(struct asc_unit *scu)
219 {
220     int res, i, delay;
221
222     res=0;
223     scu->cmd_byte = ASC_STANDBY;
224     outb(ASC_CMD, scu->cmd_byte);
225     tsleep((caddr_t)scu, PCATCH, "ascres", hz/10);
226     for(delay= 100; (res=inb(ASC_STAT)) & ASC_RDY_FLAG; delay--)
227     {
228         i = tsleep((caddr_t)scu, PCATCH, "ascres0", 1);
229         if ( ( i == 0 ) || ( i == EWOULDBLOCK ) )
230             i = SUCCESS;
231         else
232             break;
233     }
234     if (delay==0) {
235         lprintf("asc.get_resolution: timeout completing command\n");
236         return /*  -1 */;
237     }
238     /* ... actual read resolution... */
239     res &= ASC_RES_MASK;
240     for (i=0; geomtab[i].dpi != INVALID; i++) {
241         if (geomtab[i].g_res == res) break;
242     }
243     if (geomtab[i].dpi==INVALID) {
244         scu->geometry= i; /* INVALID; */
245         lprintf("asc.get_resolution: wrong resolution\n");
246     } else {
247         lprintf("asc.get_resolution: %d dpi\n",geomtab[i].dpi);
248         scu->geometry = i;
249     }
250     scu->portf_byte=0; /* default */
251     if (geomtab[scu->geometry].g_res==0 && !(scu->thedev&FRMT_GRAY)) {
252         /* color scanner seems to require this */
253         scu->portf_byte=2;
254         /* scu->geometry++; */
255     }
256     scu->linesize = geomtab[scu->geometry].bpl;
257     scu->height = geomtab[scu->geometry].dpl; /* default... */
258 }
259
260 /***
261  *** buffer_allocate
262  ***    allocate/reallocate a buffer
263  ***    Now just checks that the preallocated buffer is large enough.
264  ***/
265
266 static int
267 buffer_allocate(struct asc_unit *scu)
268 {
269   size_t size, size1;
270
271   size = scu->blen * scu->linesize;
272
273   lprintf("asc.buffer_allocate: need 0x%x bytes\n", size);
274
275   if ( size > MAX_BUFSIZE ) {
276       size1=size;
277       size= ( (MAX_BUFSIZE+scu->linesize-1) / scu->linesize)*scu->linesize;
278       lprintf("asc.buffer_allocate: 0x%x bytes are too much, try 0x%x\n",
279           size1, size);
280       return ENOMEM;
281   }
282
283   scu->sbuf.size = size;
284   scu->sbuf.rptr  = 0;
285   scu->sbuf.wptr  = 0;
286   scu->sbuf.count  = 0; /* available data for reading */
287
288   lprintf("asc.buffer_allocate: ok\n");
289
290   return SUCCESS;
291 }
292
293 /*** dma_restart
294  ***    invoked locally to start dma. Must run in a critical section
295  ***/
296 static void
297 dma_restart(struct asc_unit *scu)
298 {
299     unsigned char al=scu->cmd_byte;
300
301     if (geomtab[scu->geometry].g_res==0) {/* color */
302         isa_dmastart(BUF_CMD_READ, 0, scu->sbuf.base+scu->sbuf.wptr,
303             scu->linesize + 90 /* XXX */ , scu->dma_num);
304         /*
305          * looks like we have to set and then clear this
306          * bit to enable the scanner to send interrupts
307          */
308         outb( ASC_CMD, al |= 4 ); /* seems to disable interrupts */
309 #if 0
310         outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
311 #endif
312         outb( ASC_CMD, al &= 0xfb );
313         scu->cmd_byte = al;
314     } else {                                    /* normal */
315     isa_dmastart(BUF_CMD_READ, 0, scu->sbuf.base+scu->sbuf.wptr,
316         scu->linesize, scu->dma_num);
317     /*** this is done in sub_20, after dmastart ? ***/  
318 #if 0
319     outb( ASC_CMD, al |= 4 );
320     outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
321     outb( ASC_CMD, al &= 0xfb );
322     scu->cmd_byte = al;
323 #else
324     outb( ASC_CMD, ASC_OPERATE); 
325 #endif
326     }
327     scu->flags |= DMA_ACTIVE;
328 }
329
330 /***
331  *** the main functions
332  ***/
333
334 /*** asc_reset
335  ***    resets the scanner and the config bytes...
336  ***/
337 static void
338 asc_reset(struct asc_unit *scu)
339 {
340   scu->cfg_byte = 0 ; /* clear... */
341   scu->cmd_byte = 0 ; /* clear... */
342
343   outb(ASC_CFG,scu->cfg_byte);  /* for safety, do this here */
344   outb(ASC_CMD,scu->cmd_byte);  /* probably not needed */
345   tsleep((caddr_t)scu, PCATCH, "ascres", hz/10); /* sleep .1 sec */
346
347   scu->blen = DEFAULT_BLEN;
348   scu->btime = TIMEOUT;
349   scu->height = 0 ; /* don't know better... */
350 }
351 /**************************************************************************
352  ***
353  *** ascprobe
354  ***    read status port and check for proper configuration:
355  ***    - if address group matches (status byte has reasonable value)
356  ***      cannot check interrupt/dma, only clear the config byte.
357  ***/
358 static int
359 ascprobe (struct isa_device *isdp)
360 {
361   int unit = isdp->id_unit;
362   struct asc_unit *scu = unittab + unit;
363   int stb;
364
365   scu->base = isdp->id_iobase; /*** needed by the following macros ***/
366   scu->flags = FLAG_DEBUG;
367
368   if ( isdp->id_iobase < 0 ) {
369       lprintf("asc%d.probe: no iobase given\n", unit);
370       return PROBE_FAIL;
371   }
372
373   if ((stb=inb(ASC_PROBE)) != ASC_PROBE_VALUE) {
374       lprintf("asc%d.probe: failed, got 0x%02x instead of 0x%02x\n",
375           unit, stb, ASC_PROBE_VALUE);
376       return PROBE_FAIL;
377   }
378
379 /*
380  * NOTE NOTE NOTE
381  * the new AmiScan Color board uses int 10,11,12 instead of 3,5,10
382  * respectively. This means that the driver must act accordingly.
383  * Unfortunately there is no easy way of telling which board one has,
384  * other than trying to get an interrupt and noticing that it is
385  * missing. use "option ASC_NEW_BOARD" if you have a new board.
386  *
387  */
388
389 #if ASC_NEW_BOARD
390 #define ASC_IRQ_A       10
391 #define ASC_IRQ_B       11
392 #define ASC_IRQ_C       12
393 #else
394 #define ASC_IRQ_A       3
395 #define ASC_IRQ_B       5
396 #define ASC_IRQ_C       10
397 #endif
398
399   switch(ffs(isdp->id_irq) - 1) {
400     case ASC_IRQ_A :
401       scu->int_byte = ASC_CNF_IRQ3;
402       break;
403     case ASC_IRQ_B :
404       scu->int_byte = ASC_CNF_IRQ5;
405       break;
406     case ASC_IRQ_C :
407       scu->int_byte = ASC_CNF_IRQ10;
408       break;
409 #if 0
410     case -1:
411       scu->int_byte = 0;
412       lprintf("asc%d.probe: warning - going interruptless\n", unit);
413       break;
414 #endif
415     default:
416       lprintf("asc%d.probe: unsupported INT %d (only 3, 5, 10)\n",
417                 unit, ffs(isdp->id_irq) - 1 );
418       return PROBE_FAIL;
419   }
420   scu->dma_num = isdp->id_drq;
421   switch(scu->dma_num) {
422     case 1:
423       scu->dma_byte = ASC_CNF_DMA1;
424       break;
425     case 3:
426       scu->dma_byte = ASC_CNF_DMA3;
427       break;
428     default:
429       lprintf("asc%d.probe: unsupported DMA %d (only 1 or 3)\n", 
430                 unit, scu->dma_num);
431       return PROBE_FAIL;
432   }
433   asc_reset(scu);
434 /*  lprintf("asc%d.probe: ok\n", unit); */
435
436   scu->flags &= ~FLAG_DEBUG;
437   scu->icnt = 0;
438   return PROBE_SUCCESS;
439 }
440
441 /**************************************************************************
442  ***
443  *** ascattach
444  ***    finish initialization of unit structure, get geometry value (?)
445  ***/
446
447 static int
448 ascattach(struct isa_device *isdp)
449 {
450   int unit = isdp->id_unit;
451   struct asc_unit *scu = unittab + unit;
452
453   isdp->id_intr = (inthand2_t *)ascintr;
454   scu->flags |= FLAG_DEBUG;
455   kprintf("asc%d: [GI1904/Trust Ami-Scan Grey/Color]\n", unit);
456
457   /*
458    * Initialize buffer structure.
459    * XXX this must be done early to give a good chance of getting a
460    * contiguous buffer.  This wastes memory.
461    */
462   scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
463                                 0ul, 0xfffffful, 1ul, 0x10000ul);
464   if ( scu->sbuf.base == NULL )
465     {
466       lprintf("asc%d.attach: buffer allocation failed\n", unit);
467       return ATTACH_FAIL;       /* XXX attach must not fail */
468     }
469   scu->sbuf.size = INVALID;
470   scu->sbuf.rptr  = INVALID;
471
472   scu->flags |= ATTACHED;
473 /*  lprintf("asc%d.attach: ok\n", unit); */
474   scu->flags &= ~FLAG_DEBUG;
475
476     scu->selp.si_flags=0;
477     scu->selp.si_pid=(pid_t)0;
478 #define ASC_UID 0
479 #define ASC_GID 13
480   make_dev(&asc_ops, unit<<6, ASC_UID, ASC_GID, 0666, "asc%d", unit);
481   make_dev(&asc_ops, ((unit<<6) + FRMT_PBM),
482            ASC_UID,  ASC_GID, 0666, "asc%dp", unit);
483   make_dev(&asc_ops, ((unit<<6) + DBUG_MASK),
484            ASC_UID,  ASC_GID, 0666, "asc%dd", unit);
485   make_dev(&asc_ops, ((unit<<6) + DBUG_MASK+FRMT_PBM), 
486            ASC_UID, ASC_GID, 0666, "asc%dpd", unit);
487   return ATTACH_SUCCESS;
488 }
489
490 /**************************************************************************
491  ***
492  *** ascintr
493  ***    the interrupt routine, at the end of DMA...
494  ***/
495 static void
496 ascintr(void *arg)
497 {
498     int unit = (int)arg;
499     struct asc_unit *scu = unittab + unit;
500     int chan_bit = 0x01 << scu->dma_num;
501
502     scu->icnt++;
503     /* ignore stray interrupts... */
504     if ((scu->flags & (OPEN |READING)) != (OPEN | READING) ) {
505         /* must be after closing... */
506         scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL);
507         return;
508     }
509     if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) {
510         outb( ASC_CMD, ASC_STANDBY);
511         scu->flags &= ~DMA_ACTIVE;
512                 /* bounce buffers... */
513         isa_dmadone(BUF_CMD_READ, 0, scu->sbuf.base+scu->sbuf.wptr,
514             scu->linesize, scu->dma_num);
515         scu->sbuf.wptr += scu->linesize;
516         if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0;
517         scu->sbuf.count += scu->linesize;
518         if (scu->flags & SLEEPING) {
519             scu->flags &= ~SLEEPING;
520             wakeup((caddr_t)scu);
521         }
522         if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) {
523             dma_restart(scu);
524         }
525         if (scu->selp.si_pid) {
526             selwakeup(&scu->selp);
527             scu->selp.si_pid=(pid_t)0;
528             scu->selp.si_flags = 0;
529         }
530     }
531 }
532
533 /**************************************************************************
534  ***
535  *** ascopen
536  ***    set open flag, set modes according to minor number
537  ***    FOR RELEASE:
538  ***    don't switch scanner on, wait until first read or ioctls go before
539  ***/
540
541 STATIC int
542 ascopen(struct dev_open_args *ap)
543 {
544   cdev_t dev = ap->a_head.a_dev;
545   struct asc_unit *scu;
546   int unit;
547
548   unit = UNIT(minor(dev)) & UNIT_MASK;
549   if ( unit >= NASC )
550     {
551 #ifdef ASCDEBUG
552       /* XXX lprintf isn't valid here since there is no scu. */
553       kprintf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC);
554 #endif
555       return ENXIO;
556     }
557   scu = unittab + unit;
558   if ( !( scu->flags & ATTACHED ) )
559     {
560       lprintf("asc%d.open: unit was not attached successfully 0x%04x\n",
561              unit, scu->flags);
562       return ENXIO;
563     }
564
565   if ( minor(dev) & DBUG_MASK )
566     scu->flags |= FLAG_DEBUG;
567   else
568     scu->flags &= ~FLAG_DEBUG;
569
570   switch(minor(dev) & FRMT_MASK) {
571   case FRMT_PBM:
572     scu->flags |= PBM_MODE;
573     lprintf("asc%d.open: pbm mode\n", unit);
574     break;
575   case FRMT_RAW:
576     lprintf("asc%d.open: raw mode\n", unit);
577     scu->flags &= ~PBM_MODE;
578     break;
579   default:
580     lprintf("asc%d.open: gray maps are not yet supported", unit);
581     return ENXIO;
582   }
583   
584   lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
585
586   if ( scu->flags & OPEN ) {
587       lprintf("asc%d.open: already open", unit);
588       return EBUSY;
589   }
590   if (isa_dma_acquire(scu->dma_num))
591       return(EBUSY);
592
593   scu->flags = ATTACHED | OPEN;      
594
595   asc_reset(scu);
596   get_resolution(scu);
597   return SUCCESS;
598 }
599
600 static int
601 asc_startread(struct asc_unit *scu)
602 {
603     /*** from here on, things can be delayed to the first read/ioctl ***/
604     /*** this was done in sub_12... ***/
605   scu->cfg_byte= scu->cmd_byte=0;       /* init scanner */
606   outb(ASC_CMD, scu->cmd_byte);
607     /*** this was done in sub_16, set scan len... ***/
608   outb(ASC_BOH, scu->portf_byte );
609   if (geomtab[scu->geometry].g_res==0) {                /* color */
610         scu->cmd_byte = 0x00 ;
611   } else {
612   scu->cmd_byte = 0x90 ;
613   }
614   outb(ASC_CMD, scu->cmd_byte);
615   outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */);
616   outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */);
617     /*** this was done in sub_21, config DMA ... ***/
618   scu->cfg_byte |= scu->dma_byte;
619   outb(ASC_CFG, scu->cfg_byte);
620     /*** sub_22: enable int on the scanner ***/
621   scu->cfg_byte |= scu->int_byte;
622   outb(ASC_CFG, scu->cfg_byte);
623     /*** sub_28: light on etc...***/
624   scu->cmd_byte = ASC_STANDBY;
625   outb(ASC_CMD, scu->cmd_byte);
626   tsleep((caddr_t)scu, PCATCH, "ascstrd", hz/10); /* sleep .1 sec */
627   return SUCCESS;
628 }
629
630 /**************************************************************************
631  ***
632  *** ascclose
633  ***    turn off scanner, release the buffer
634  ***    should probably terminate dma ops, release int and dma. lr 12mar95
635  ***/
636
637 STATIC int
638 ascclose(struct dev_close_args *ap)
639 {
640   cdev_t dev = ap->a_head.a_dev;
641   int unit = UNIT(minor(dev));
642   struct asc_unit *scu = unittab + unit;
643
644   lprintf("asc%d.close: minor %d\n",
645          unit, minor(dev));
646
647   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
648       lprintf("asc%d.close: unit was not attached successfully 0x%04x\n",
649              unit, scu->flags);
650       return ENXIO;
651   }
652     /* all this is in sub_29... */
653   /* cli(); */
654   outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */
655   scu->cmd_byte &= ~ASC_LIGHT_ON;
656   outb(ASC_CMD, scu->cmd_byte);/* light off */
657   tsleep((caddr_t)scu, PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */
658   scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */
659   scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */
660   outb(ASC_CFG, scu->cfg_byte);
661     /* --- disable dma controller ? --- */
662   isa_dma_release(scu->dma_num);
663     /* --- disable interrupts on the controller (sub_24) --- */
664
665   scu->sbuf.size = INVALID;
666   scu->sbuf.rptr  = INVALID;
667
668   scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
669   
670   return SUCCESS;
671 }
672
673 static void
674 pbm_init(struct asc_unit *scu)
675 {
676     int width = geomtab[scu->geometry].dpl;
677     int l= ksprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
678     char *p;
679
680     scu->bcount = scu->height * width / 8 + l;
681
682       /* move header to end of sbuf */
683     scu->sbuf.rptr=scu->sbuf.size-l;
684     bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l);
685     scu->sbuf.count = l;
686     if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
687     for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--)
688         *p = ~*p;
689 }
690 }
691 /**************************************************************************
692  ***
693  *** ascread
694  ***/
695
696 STATIC int
697 ascread(struct dev_read_args *ap)
698 {
699   cdev_t dev = ap->a_head.a_dev;
700   struct uio *uio = ap->a_uio;
701   int unit = UNIT(minor(dev));
702   struct asc_unit *scu = unittab + unit;
703   size_t nbytes;
704   int res;
705   unsigned char *p;
706   
707   lprintf("asc%d.read: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
708
709   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
710       lprintf("asc%d.read: unit was not attached successfully 0x%04x\n",
711              unit, scu->flags);
712       return ENXIO;
713   }
714
715   if ( !(scu->flags & READING) ) { /*** first read... ***/
716         /* allocate a buffer for reading data and init things */
717       if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING;
718       else return res;
719       asc_startread(scu);
720       if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */
721           pbm_init(scu);
722       }
723   }
724   
725   lprintf("asc%d.read(before): "
726       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
727           unit, scu->sbuf.size, scu->sbuf.rptr,
728           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,
729           scu->icnt);
730
731   crit_enter();
732   if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */
733       if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
734       scu->flags |= SLEEPING;
735       res = tsleep((caddr_t)scu, PCATCH, "ascread", 0);
736       scu->flags &= ~SLEEPING;
737       if ( res == 0 ) res = SUCCESS;
738   }
739   crit_exit();
740   if (scu->flags & FLAG_DEBUG)
741       tsleep((caddr_t)scu, PCATCH, "ascdly",hz);
742   lprintf("asc%d.read(after): "
743       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
744           unit, scu->sbuf.size, scu->sbuf.rptr,
745           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt);
746
747         /* first, not more than available... */
748   nbytes = szmin(uio->uio_resid, scu->sbuf.count);
749         /* second, contiguous data... */
750   nbytes = szmin(nbytes, (scu->sbuf.size - scu->sbuf.rptr));
751         /* third, one line (will remove this later, XXX) */
752   nbytes = szmin(nbytes, scu->linesize);
753   if ( (scu->flags & PBM_MODE) )
754       nbytes = szmin(nbytes, scu->bcount);
755   lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes);
756   if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
757   lprintf("asc%d.read: invert buffer\n",unit);
758   for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--)
759         *p = ~*p;
760   }
761   res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio);
762   if ( res != SUCCESS ) {
763       lprintf("asc%d.read: uiomove failed %d", unit, res);
764       return res;
765   }
766   
767   crit_enter();
768   scu->sbuf.rptr += nbytes;
769   if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0;
770   scu->sbuf.count -= nbytes;
771         /* having moved some data, can read mode */
772   if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
773   crit_exit();
774   if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
775   
776   lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
777           unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount);
778   
779   return SUCCESS;
780 }
781
782 /**************************************************************************
783  ***
784  *** ascioctl
785  ***/
786
787 STATIC int
788 ascioctl(struct dev_ioctl_args *ap)
789 {
790   cdev_t dev = ap->a_head.a_dev;
791   caddr_t data = ap->a_data;
792   int unit = UNIT(minor(dev));
793   struct asc_unit *scu = unittab + unit;
794
795   lprintf("asc%d.ioctl: minor %d\n",
796          unit, minor(dev));
797
798   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
799       lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n",
800              unit, scu->flags);
801       return ENXIO;
802   }
803   switch(ap->a_cmd) {
804   case ASC_GRES:
805     asc_reset(scu);
806     get_resolution(scu);
807     *(int *)data=geomtab[scu->geometry].dpi;
808     lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data);
809     return SUCCESS;    
810   case ASC_GWIDTH:
811     *(int *)data=geomtab[scu->geometry].dpl;
812     lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data);
813     return SUCCESS;    
814   case ASC_GHEIGHT:
815     *(int *)data=scu->height;
816     lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data);
817     return SUCCESS;
818   case ASC_SHEIGHT:
819     lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data);
820     if ( scu->flags & READING ) { 
821         lprintf("asc%d:ioctl on already reading unit\n", unit);
822         return EBUSY;
823     }
824     scu->height=*(int *)data;
825     return SUCCESS;
826 #if 0  
827   case ASC_GBLEN:
828     *(int *)data=scu->blen;
829     lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data);
830     return SUCCESS;
831   case ASC_SBLEN:
832     lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data);
833     if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
834       {
835         lprintf("asc%d:ioctl buffer size too high\n", unit);
836         return ENOMEM;
837       }
838     scu->blen=*(int *)data;
839     return SUCCESS;
840   case ASC_GBTIME:
841     *(int *)data = scu->btime / hz;
842     lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data);
843     return SUCCESS;
844   case ASC_SBTIME:
845     scu->btime = *(int *)data * hz;
846     lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data);
847     return SUCCESS;
848 #endif
849   default: return ENOTTY;
850   }
851   return SUCCESS;
852 }
853
854 STATIC int
855 ascpoll(struct dev_poll_args *ap)
856 {
857     cdev_t dev = ap->a_head.a_dev;
858     int unit = UNIT(minor(dev));
859     struct asc_unit *scu = unittab + unit;
860     int revents = 0;
861
862     crit_enter();
863
864     if (ap->a_events & (POLLIN | POLLRDNORM)) {
865         if (scu->sbuf.count >0)
866             revents |= ap->a_events & (POLLIN | POLLRDNORM);
867         else {
868             if (!(scu->flags & DMA_ACTIVE))
869                 dma_restart(scu);
870
871             selrecord(curthread, &scu->selp);
872         }
873     }
874     crit_exit();
875     ap->a_events = revents;
876     return (0);
877 }
878
879 static struct filterops ascfiltops =
880     { 1, NULL, ascfilter_detach, ascfilter };
881
882 STATIC int
883 asckqfilter(struct dev_kqfilter_args *ap)
884 {
885     cdev_t dev = ap->a_head.a_dev;
886     int unit = UNIT(minor(dev));
887     struct asc_unit *scu = unittab + unit;
888     struct knote *kn = ap->a_kn;
889     struct klist *klist;
890
891     ap->a_result = 0;
892
893     switch (kn->kn_filter) {
894     case EVFILT_READ:
895         kn->kn_fop = &ascfiltops;
896         kn->kn_hook = (caddr_t)scu;
897         break;
898     default:
899         ap->a_result = EOPNOTSUPP;
900         return (0);
901     }
902
903     crit_enter();
904     klist = &scu->selp.si_note;
905     SLIST_INSERT_HEAD(klist, kn, kn_selnext);
906     crit_exit();
907
908     return (0);
909 }
910
911 STATIC void
912 ascfilter_detach(struct knote *kn)
913 {
914     struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
915     struct klist *klist;
916
917     crit_enter();
918     klist = &scu->selp.si_note;
919     SLIST_REMOVE(klist, kn, knote, kn_selnext);
920     crit_exit();
921 }
922
923 STATIC int
924 ascfilter(struct knote *kn, long hint)
925 {
926     struct asc_unit *scu = (struct asc_unit *)kn->kn_hook;
927     int ready = 0;
928
929     crit_enter();
930     if (scu->sbuf.count >0)
931         ready = 1;
932     else {
933         if (!(scu->flags & DMA_ACTIVE))
934             dma_restart(scu);
935     }
936     crit_exit();
937
938     return (ready);
939 }