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