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