Merge from vendor branch LESS:
[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.6 2003/08/07 21:17:23 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/poll.h>
50 #include <sys/select.h>
51 #include <sys/uio.h>
52
53 #include <machine/asc_ioctl.h>
54
55 #include <bus/isa/i386/isa.h>
56 #include <bus/isa/i386/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) printf
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 selinfo selp;
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 ointhand2_t      ascintr;
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_poll_t         ascpoll;
186
187 #define CDEV_MAJOR 71
188
189 static struct cdevsw asc_cdevsw = {
190         /* name */      "asc",
191         /* maj */       CDEV_MAJOR,
192         /* flags */     0,
193         /* port */      NULL,
194         /* autoq */     0,
195
196         /* open */      ascopen,
197         /* close */     ascclose,
198         /* read */      ascread,
199         /* write */     nowrite,
200         /* ioctl */     ascioctl,
201         /* poll */      ascpoll,
202         /* mmap */      nommap,
203         /* strategy */  nostrategy,
204         /* dump */      nodump,
205         /* psize */     nopsize
206 };
207
208 #define STATIC static
209
210 /***
211  *** LOCALLY USED SUBROUTINES
212  ***
213  ***/
214
215 /***
216  *** get_resolution
217  ***    read resolution from the scanner
218  ***/
219 static void
220 get_resolution(struct asc_unit *scu)
221 {
222     int res, i, delay;
223
224     res=0;
225     scu->cmd_byte = ASC_STANDBY;
226     outb(ASC_CMD, scu->cmd_byte);
227     tsleep((caddr_t)scu, PCATCH, "ascres", hz/10);
228     for(delay= 100; (res=inb(ASC_STAT)) & ASC_RDY_FLAG; delay--)
229     {
230         i = tsleep((caddr_t)scu, PCATCH, "ascres0", 1);
231         if ( ( i == 0 ) || ( i == EWOULDBLOCK ) )
232             i = SUCCESS;
233         else
234             break;
235     }
236     if (delay==0) {
237         lprintf("asc.get_resolution: timeout completing command\n");
238         return /*  -1 */;
239     }
240     /* ... actual read resolution... */
241     res &= ASC_RES_MASK;
242     for (i=0; geomtab[i].dpi != INVALID; i++) {
243         if (geomtab[i].g_res == res) break;
244     }
245     if (geomtab[i].dpi==INVALID) {
246         scu->geometry= i; /* INVALID; */
247         lprintf("asc.get_resolution: wrong resolution\n");
248     } else {
249         lprintf("asc.get_resolution: %d dpi\n",geomtab[i].dpi);
250         scu->geometry = i;
251     }
252     scu->portf_byte=0; /* default */
253     if (geomtab[scu->geometry].g_res==0 && !(scu->thedev&FRMT_GRAY)) {
254         /* color scanner seems to require this */
255         scu->portf_byte=2;
256         /* scu->geometry++; */
257     }
258     scu->linesize = geomtab[scu->geometry].bpl;
259     scu->height = geomtab[scu->geometry].dpl; /* default... */
260 }
261
262 /***
263  *** buffer_allocate
264  ***    allocate/reallocate a buffer
265  ***    Now just checks that the preallocated buffer is large enough.
266  ***/
267
268 static int
269 buffer_allocate(struct asc_unit *scu)
270 {
271   size_t size, size1;
272
273   size = scu->blen * scu->linesize;
274
275   lprintf("asc.buffer_allocate: need 0x%x bytes\n", size);
276
277   if ( size > MAX_BUFSIZE ) {
278       size1=size;
279       size= ( (MAX_BUFSIZE+scu->linesize-1) / scu->linesize)*scu->linesize;
280       lprintf("asc.buffer_allocate: 0x%x bytes are too much, try 0x%x\n",
281           size1, size);
282       return ENOMEM;
283   }
284
285   scu->sbuf.size = size;
286   scu->sbuf.rptr  = 0;
287   scu->sbuf.wptr  = 0;
288   scu->sbuf.count  = 0; /* available data for reading */
289
290   lprintf("asc.buffer_allocate: ok\n");
291
292   return SUCCESS;
293 }
294
295 /*** dma_restart
296  ***    invoked locally to start dma. Must run in a critical section
297  ***/
298 static void
299 dma_restart(struct asc_unit *scu)
300 {
301     unsigned char al=scu->cmd_byte;
302
303     if (geomtab[scu->geometry].g_res==0) {/* color */
304         isa_dmastart(ISADMA_READ, scu->sbuf.base+scu->sbuf.wptr,
305             scu->linesize + 90 /* XXX */ , scu->dma_num);
306         /*
307          * looks like we have to set and then clear this
308          * bit to enable the scanner to send interrupts
309          */
310         outb( ASC_CMD, al |= 4 ); /* seems to disable interrupts */
311 #if 0
312         outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
313 #endif
314         outb( ASC_CMD, al &= 0xfb );
315         scu->cmd_byte = al;
316     } else {                                    /* normal */
317     isa_dmastart(ISADMA_READ, scu->sbuf.base+scu->sbuf.wptr,
318         scu->linesize, scu->dma_num);
319     /*** this is done in sub_20, after dmastart ? ***/  
320 #if 0
321     outb( ASC_CMD, al |= 4 );
322     outb( ASC_CMD, al |= 8 ); /* ??? seems useless */
323     outb( ASC_CMD, al &= 0xfb );
324     scu->cmd_byte = al;
325 #else
326     outb( ASC_CMD, ASC_OPERATE); 
327 #endif
328     }
329     scu->flags |= DMA_ACTIVE;
330 }
331
332 /***
333  *** the main functions
334  ***/
335
336 /*** asc_reset
337  ***    resets the scanner and the config bytes...
338  ***/
339 static void
340 asc_reset(struct asc_unit *scu)
341 {
342   scu->cfg_byte = 0 ; /* clear... */
343   scu->cmd_byte = 0 ; /* clear... */
344
345   outb(ASC_CFG,scu->cfg_byte);  /* for safety, do this here */
346   outb(ASC_CMD,scu->cmd_byte);  /* probably not needed */
347   tsleep((caddr_t)scu, PCATCH, "ascres", hz/10); /* sleep .1 sec */
348
349   scu->blen = DEFAULT_BLEN;
350   scu->btime = TIMEOUT;
351   scu->height = 0 ; /* don't know better... */
352 }
353 /**************************************************************************
354  ***
355  *** ascprobe
356  ***    read status port and check for proper configuration:
357  ***    - if address group matches (status byte has reasonable value)
358  ***      cannot check interrupt/dma, only clear the config byte.
359  ***/
360 static int
361 ascprobe (struct isa_device *isdp)
362 {
363   int unit = isdp->id_unit;
364   struct asc_unit *scu = unittab + unit;
365   int stb;
366   static int once;
367
368   if (!once++)
369       cdevsw_add(&asc_cdevsw);
370
371   scu->base = isdp->id_iobase; /*** needed by the following macros ***/
372   scu->flags = FLAG_DEBUG;
373
374   if ( isdp->id_iobase < 0 ) {
375       lprintf("asc%d.probe: no iobase given\n", unit);
376       return PROBE_FAIL;
377   }
378
379   if ((stb=inb(ASC_PROBE)) != ASC_PROBE_VALUE) {
380       lprintf("asc%d.probe: failed, got 0x%02x instead of 0x%02x\n",
381           unit, stb, ASC_PROBE_VALUE);
382       return PROBE_FAIL;
383   }
384
385 /*
386  * NOTE NOTE NOTE
387  * the new AmiScan Color board uses int 10,11,12 instead of 3,5,10
388  * respectively. This means that the driver must act accordingly.
389  * Unfortunately there is no easy way of telling which board one has,
390  * other than trying to get an interrupt and noticing that it is
391  * missing. use "option ASC_NEW_BOARD" if you have a new board.
392  *
393  */
394
395 #if ASC_NEW_BOARD
396 #define ASC_IRQ_A       10
397 #define ASC_IRQ_B       11
398 #define ASC_IRQ_C       12
399 #else
400 #define ASC_IRQ_A       3
401 #define ASC_IRQ_B       5
402 #define ASC_IRQ_C       10
403 #endif
404
405   switch(ffs(isdp->id_irq) - 1) {
406     case ASC_IRQ_A :
407       scu->int_byte = ASC_CNF_IRQ3;
408       break;
409     case ASC_IRQ_B :
410       scu->int_byte = ASC_CNF_IRQ5;
411       break;
412     case ASC_IRQ_C :
413       scu->int_byte = ASC_CNF_IRQ10;
414       break;
415 #if 0
416     case -1:
417       scu->int_byte = 0;
418       lprintf("asc%d.probe: warning - going interruptless\n", unit);
419       break;
420 #endif
421     default:
422       lprintf("asc%d.probe: unsupported INT %d (only 3, 5, 10)\n",
423                 unit, ffs(isdp->id_irq) - 1 );
424       return PROBE_FAIL;
425   }
426   scu->dma_num = isdp->id_drq;
427   switch(scu->dma_num) {
428     case 1:
429       scu->dma_byte = ASC_CNF_DMA1;
430       break;
431     case 3:
432       scu->dma_byte = ASC_CNF_DMA3;
433       break;
434     default:
435       lprintf("asc%d.probe: unsupported DMA %d (only 1 or 3)\n", 
436                 unit, scu->dma_num);
437       return PROBE_FAIL;
438   }
439   asc_reset(scu);
440 /*  lprintf("asc%d.probe: ok\n", unit); */
441
442   scu->flags &= ~FLAG_DEBUG;
443   scu->icnt = 0;
444   return PROBE_SUCCESS;
445 }
446
447 /**************************************************************************
448  ***
449  *** ascattach
450  ***    finish initialization of unit structure, get geometry value (?)
451  ***/
452
453 static int
454 ascattach(struct isa_device *isdp)
455 {
456   int unit = isdp->id_unit;
457   struct asc_unit *scu = unittab + unit;
458
459   isdp->id_ointr = ascintr;
460   scu->flags |= FLAG_DEBUG;
461   printf("asc%d: [GI1904/Trust Ami-Scan Grey/Color]\n", unit);
462
463   /*
464    * Initialize buffer structure.
465    * XXX this must be done early to give a good chance of getting a
466    * contiguous buffer.  This wastes memory.
467    */
468   scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
469                                 0ul, 0xfffffful, 1ul, 0x10000ul);
470   if ( scu->sbuf.base == NULL )
471     {
472       lprintf("asc%d.attach: buffer allocation failed\n", unit);
473       return ATTACH_FAIL;       /* XXX attach must not fail */
474     }
475   scu->sbuf.size = INVALID;
476   scu->sbuf.rptr  = INVALID;
477
478   scu->flags |= ATTACHED;
479 /*  lprintf("asc%d.attach: ok\n", unit); */
480   scu->flags &= ~FLAG_DEBUG;
481
482     scu->selp.si_flags=0;
483     scu->selp.si_pid=(pid_t)0;
484 #define ASC_UID 0
485 #define ASC_GID 13
486   make_dev(&asc_cdevsw, unit<<6, ASC_UID, ASC_GID, 0666, "asc%d", unit);
487   make_dev(&asc_cdevsw, ((unit<<6) + FRMT_PBM),
488     ASC_UID,  ASC_GID, 0666, "asc%dp", unit);
489   make_dev(&asc_cdevsw, ((unit<<6) + DBUG_MASK),
490     ASC_UID,  ASC_GID, 0666, "asc%dd", unit);
491   make_dev(&asc_cdevsw, ((unit<<6) + DBUG_MASK+FRMT_PBM), 
492     ASC_UID, ASC_GID, 0666, "asc%dpd", unit);
493   return ATTACH_SUCCESS;
494 }
495
496 /**************************************************************************
497  ***
498  *** ascintr
499  ***    the interrupt routine, at the end of DMA...
500  ***/
501 static void
502 ascintr(int unit)
503 {
504     struct asc_unit *scu = unittab + unit;
505     int chan_bit = 0x01 << scu->dma_num;
506
507     scu->icnt++;
508     /* ignore stray interrupts... */
509     if ((scu->flags & (OPEN |READING)) != (OPEN | READING) ) {
510         /* must be after closing... */
511         scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL);
512         return;
513     }
514     if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) {
515         outb( ASC_CMD, ASC_STANDBY);
516         scu->flags &= ~DMA_ACTIVE;
517                 /* bounce buffers... */
518         isa_dmadone(ISADMA_READ, scu->sbuf.base+scu->sbuf.wptr,
519             scu->linesize, scu->dma_num);
520         scu->sbuf.wptr += scu->linesize;
521         if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0;
522         scu->sbuf.count += scu->linesize;
523         if (scu->flags & SLEEPING) {
524             scu->flags &= ~SLEEPING;
525             wakeup((caddr_t)scu);
526         }
527         if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) {
528             dma_restart(scu);
529         }
530         if (scu->selp.si_pid) {
531             selwakeup(&scu->selp);
532             scu->selp.si_pid=(pid_t)0;
533             scu->selp.si_flags = 0;
534         }
535     }
536 }
537
538 /**************************************************************************
539  ***
540  *** ascopen
541  ***    set open flag, set modes according to minor number
542  ***    FOR RELEASE:
543  ***    don't switch scanner on, wait until first read or ioctls go before
544  ***/
545
546 STATIC int
547 ascopen(dev_t dev, int flags, int fmt, struct thread *td)
548 {
549   struct asc_unit *scu;
550   int unit;
551
552   unit = UNIT(minor(dev)) & UNIT_MASK;
553   if ( unit >= NASC )
554     {
555 #ifdef ASCDEBUG
556       /* XXX lprintf isn't valid here since there is no scu. */
557       printf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC);
558 #endif
559       return ENXIO;
560     }
561   scu = unittab + unit;
562   if ( !( scu->flags & ATTACHED ) )
563     {
564       lprintf("asc%d.open: unit was not attached successfully 0x%04x\n",
565              unit, scu->flags);
566       return ENXIO;
567     }
568
569   if ( minor(dev) & DBUG_MASK )
570     scu->flags |= FLAG_DEBUG;
571   else
572     scu->flags &= ~FLAG_DEBUG;
573
574   switch(minor(dev) & FRMT_MASK) {
575   case FRMT_PBM:
576     scu->flags |= PBM_MODE;
577     lprintf("asc%d.open: pbm mode\n", unit);
578     break;
579   case FRMT_RAW:
580     lprintf("asc%d.open: raw mode\n", unit);
581     scu->flags &= ~PBM_MODE;
582     break;
583   default:
584     lprintf("asc%d.open: gray maps are not yet supported", unit);
585     return ENXIO;
586   }
587   
588   lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
589
590   if ( scu->flags & OPEN ) {
591       lprintf("asc%d.open: already open", unit);
592       return EBUSY;
593   }
594   if (isa_dma_acquire(scu->dma_num))
595       return(EBUSY);
596
597   scu->flags = ATTACHED | OPEN;      
598
599   asc_reset(scu);
600   get_resolution(scu);
601   return SUCCESS;
602 }
603
604 static int
605 asc_startread(struct asc_unit *scu)
606 {
607     /*** from here on, things can be delayed to the first read/ioctl ***/
608     /*** this was done in sub_12... ***/
609   scu->cfg_byte= scu->cmd_byte=0;       /* init scanner */
610   outb(ASC_CMD, scu->cmd_byte);
611     /*** this was done in sub_16, set scan len... ***/
612   outb(ASC_BOH, scu->portf_byte );
613   if (geomtab[scu->geometry].g_res==0) {                /* color */
614         scu->cmd_byte = 0x00 ;
615   } else {
616   scu->cmd_byte = 0x90 ;
617   }
618   outb(ASC_CMD, scu->cmd_byte);
619   outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */);
620   outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */);
621     /*** this was done in sub_21, config DMA ... ***/
622   scu->cfg_byte |= scu->dma_byte;
623   outb(ASC_CFG, scu->cfg_byte);
624     /*** sub_22: enable int on the scanner ***/
625   scu->cfg_byte |= scu->int_byte;
626   outb(ASC_CFG, scu->cfg_byte);
627     /*** sub_28: light on etc...***/
628   scu->cmd_byte = ASC_STANDBY;
629   outb(ASC_CMD, scu->cmd_byte);
630   tsleep((caddr_t)scu, PCATCH, "ascstrd", hz/10); /* sleep .1 sec */
631   return SUCCESS;
632 }
633
634 /**************************************************************************
635  ***
636  *** ascclose
637  ***    turn off scanner, release the buffer
638  ***    should probably terminate dma ops, release int and dma. lr 12mar95
639  ***/
640
641 STATIC int
642 ascclose(dev_t dev, int flags, int fmt, struct thread *td)
643 {
644   int unit = UNIT(minor(dev));
645   struct asc_unit *scu = unittab + unit;
646
647   lprintf("asc%d.close: minor %d\n",
648          unit, minor(dev));
649
650   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
651       lprintf("asc%d.close: unit was not attached successfully 0x%04x\n",
652              unit, scu->flags);
653       return ENXIO;
654   }
655     /* all this is in sub_29... */
656   /* cli(); */
657   outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */
658   scu->cmd_byte &= ~ASC_LIGHT_ON;
659   outb(ASC_CMD, scu->cmd_byte);/* light off */
660   tsleep((caddr_t)scu, PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */
661   scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */
662   scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */
663   outb(ASC_CFG, scu->cfg_byte);
664     /* --- disable dma controller ? --- */
665   isa_dma_release(scu->dma_num);
666     /* --- disable interrupts on the controller (sub_24) --- */
667
668   scu->sbuf.size = INVALID;
669   scu->sbuf.rptr  = INVALID;
670
671   scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
672   
673   return SUCCESS;
674 }
675
676 static void
677 pbm_init(struct asc_unit *scu)
678 {
679     int width = geomtab[scu->geometry].dpl;
680     int l= sprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
681     char *p;
682
683     scu->bcount = scu->height * width / 8 + l;
684
685       /* move header to end of sbuf */
686     scu->sbuf.rptr=scu->sbuf.size-l;
687     bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l);
688     scu->sbuf.count = l;
689     if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
690     for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--)
691         *p = ~*p;
692 }
693 }
694 /**************************************************************************
695  ***
696  *** ascread
697  ***/
698
699 STATIC int
700 ascread(dev_t dev, struct uio *uio, int ioflag)
701 {
702   int unit = UNIT(minor(dev));
703   struct asc_unit *scu = unittab + unit;
704   size_t nbytes;
705   int sps, res;
706   unsigned char *p;
707   
708   lprintf("asc%d.read: minor %d icnt %ld\n", unit, minor(dev), scu->icnt);
709
710   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
711       lprintf("asc%d.read: unit was not attached successfully 0x%04x\n",
712              unit, scu->flags);
713       return ENXIO;
714   }
715
716   if ( !(scu->flags & READING) ) { /*** first read... ***/
717         /* allocate a buffer for reading data and init things */
718       if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING;
719       else return res;
720       asc_startread(scu);
721       if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */
722           pbm_init(scu);
723       }
724   }
725   
726   lprintf("asc%d.read(before): "
727       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
728           unit, scu->sbuf.size, scu->sbuf.rptr,
729           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,
730           scu->icnt);
731
732   sps=spltty();
733   if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */
734       if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
735       scu->flags |= SLEEPING;
736       res = tsleep((caddr_t)scu, PCATCH, "ascread", 0);
737       scu->flags &= ~SLEEPING;
738       if ( res == 0 ) res = SUCCESS;
739   }
740   splx(sps); /* lower priority... */
741   if (scu->flags & FLAG_DEBUG)
742       tsleep((caddr_t)scu, PCATCH, "ascdly",hz);
743   lprintf("asc%d.read(after): "
744       "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n",
745           unit, scu->sbuf.size, scu->sbuf.rptr,
746           scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt);
747
748         /* first, not more than available... */
749   nbytes = min( uio->uio_resid, scu->sbuf.count );
750         /* second, contiguous data... */
751   nbytes = min( nbytes, (scu->sbuf.size - scu->sbuf.rptr) );
752         /* third, one line (will remove this later, XXX) */
753   nbytes = min( nbytes, scu->linesize );
754   if ( (scu->flags & PBM_MODE) )
755       nbytes = min( nbytes, scu->bcount );
756   lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes);
757   if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */
758   lprintf("asc%d.read: invert buffer\n",unit);
759   for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--)
760         *p = ~*p;
761   }
762   res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio);
763   if ( res != SUCCESS ) {
764       lprintf("asc%d.read: uiomove failed %d", unit, res);
765       return res;
766   }
767   
768   sps=spltty();
769   scu->sbuf.rptr += nbytes;
770   if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0;
771   scu->sbuf.count -= nbytes;
772         /* having moved some data, can read mode */
773   if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu);
774   splx(sps); /* lower priority... */
775   if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
776   
777   lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
778           unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount);
779   
780   return SUCCESS;
781 }
782
783 /**************************************************************************
784  ***
785  *** ascioctl
786  ***/
787
788 STATIC int
789 ascioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
790 {
791   int unit = UNIT(minor(dev));
792   struct asc_unit *scu = unittab + unit;
793
794   lprintf("asc%d.ioctl: minor %d\n",
795          unit, minor(dev));
796
797   if ( unit >= NASC || !( scu->flags & ATTACHED ) ) {
798       lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n",
799              unit, scu->flags);
800       return ENXIO;
801   }
802   switch(cmd) {
803   case ASC_GRES:
804     asc_reset(scu);
805     get_resolution(scu);
806     *(int *)data=geomtab[scu->geometry].dpi;
807     lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data);
808     return SUCCESS;    
809   case ASC_GWIDTH:
810     *(int *)data=geomtab[scu->geometry].dpl;
811     lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data);
812     return SUCCESS;    
813   case ASC_GHEIGHT:
814     *(int *)data=scu->height;
815     lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data);
816     return SUCCESS;
817   case ASC_SHEIGHT:
818     lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data);
819     if ( scu->flags & READING ) { 
820         lprintf("asc%d:ioctl on already reading unit\n", unit);
821         return EBUSY;
822     }
823     scu->height=*(int *)data;
824     return SUCCESS;
825 #if 0  
826   case ASC_GBLEN:
827     *(int *)data=scu->blen;
828     lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data);
829     return SUCCESS;
830   case ASC_SBLEN:
831     lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data);
832     if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
833       {
834         lprintf("asc%d:ioctl buffer size too high\n", unit);
835         return ENOMEM;
836       }
837     scu->blen=*(int *)data;
838     return SUCCESS;
839   case ASC_GBTIME:
840     *(int *)data = scu->btime / hz;
841     lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data);
842     return SUCCESS;
843   case ASC_SBTIME:
844     scu->btime = *(int *)data * hz;
845     lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data);
846     return SUCCESS;
847 #endif
848   default: return ENOTTY;
849   }
850   return SUCCESS;
851 }
852
853 STATIC int
854 ascpoll(dev_t dev, int events, struct thread *td)
855 {
856     int unit = UNIT(minor(dev));
857     struct asc_unit *scu = unittab + unit;
858     int sps;
859     struct proc *p;
860     struct proc *p1;
861     int revents = 0;
862
863     p = td->td_proc;
864     KKASSERT(p);
865
866     sps=spltty();
867
868     if (events & (POLLIN | POLLRDNORM)) {
869         if (scu->sbuf.count >0)
870             revents |= events & (POLLIN | POLLRDNORM);
871         else {
872             if (!(scu->flags & DMA_ACTIVE))
873                 dma_restart(scu);
874             
875             if (scu->selp.si_pid && (p1=pfind(scu->selp.si_pid))
876                     && p1->p_wchan == (caddr_t)&selwait)
877                 scu->selp.si_flags = SI_COLL;
878             else
879                 scu->selp.si_pid = p->p_pid;
880         }
881     }
882     splx(sps);
883     return (revents);
884 }