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