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