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