1 /* gsc.c - device driver for handy scanners
3 * Current version supports:
7 * Copyright (c) 1995 Gunther Schadow. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gunther Schadow.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * $FreeBSD: src/sys/i386/isa/gsc.c,v 1.35.2.1 2000/08/08 19:49:53 peter Exp $
35 * $DragonFly: src/sys/dev/video/gsc/gsc.c,v 1.4 2003/07/21 05:50:40 dillon Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
48 #include <machine/gsc.h>
50 #include <i386/isa/isa.h>
51 #include <i386/isa/isa_device.h>
52 #include <i386/isa/gscreg.h>
54 /***********************************************************************
58 ***********************************************************************/
61 #define PROBE_SUCCESS IO_GSCSIZE
63 #define ATTACH_SUCCESS 1
68 #define DMA1_READY 0x08
71 #define lprintf(args) \
73 if (scu->flags & FLAG_DEBUG) \
80 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
82 #define TIMEOUT (hz*15) /* timeout while reading a buffer - default value */
83 #define LONG (hz/60) /* timesteps while reading a buffer */
85 /***********************************************************************
87 * LAYOUT OF THE MINOR NUMBER
89 ***********************************************************************/
91 #define UNIT_MASK 0xc0 /* unit gsc0 .. gsc3 */
92 #define UNIT(x) (x >> 6)
93 #define DBUG_MASK 0x20
94 #define FRMT_MASK 0x18 /* output format */
95 #define FRMT_RAW 0x00 /* output bits as read from scanner */
96 #define FRMT_GRAY 0x10 /* output graymap (not implemented yet) */
97 #define FRMT_PBM 0x08 /* output pbm format */
100 /***********************************************************************
102 * THE GEMOMETRY TABLE
104 ***********************************************************************/
106 #define GEOMTAB_SIZE 7
108 static const struct gsc_geom {
109 int dpi; /* dots per inch */
110 int dpl; /* dots per line */
111 int g_res; /* get resolution value (status flag) */
112 int s_res; /* set resolution value (control register) */
113 } geomtab[GEOMTAB_SIZE] = {
114 { 100, 424, GSC_RES_100, GSC_CNT_424},
115 { 200, 840, GSC_RES_200, GSC_CNT_840},
116 { 300, 1264, GSC_RES_300, GSC_CNT_1264},
117 { 400, 1648, GSC_RES_400, GSC_CNT_1648},
118 { -1, 1696, -1, GSC_CNT_1696},
119 { -2, 2644, -2, GSC_CNT_2544},
120 { -3, 3648, -3, GSC_CNT_3648},
123 #define NEW_GEOM { INVALID, INVALID, INVALID, INVALID }
125 /***********************************************************************
129 ***********************************************************************/
138 int channel; /* DMA channel */
139 int data; /* - video port */
140 int stat; /* - status port */
141 int ctrl; /* - control port */
142 int clrp; /* - clear port */
144 #define ATTACHED 0x01
148 #define FLAG_DEBUG 0x10
149 #define PBM_MODE 0x20
150 int geometry; /* resolution as geomtab index */
151 int blen; /* length of buffer in lines */
152 int btime; /* timeout of buffer in seconds/hz */
154 char ctrl_byte; /* the byte actually written to ctrl port */
155 int height; /* height, for pnm modes */
156 size_t bcount; /* bytes to read, for pnm modes */
157 struct _sbuf hbuf; /* buffer for pnm header data */
160 static struct gsc_unit unittab[NGSC];
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!!!
167 #define MAX_BUFSIZE 0x3000
168 #define DEFAULT_BLEN 59
170 /***********************************************************************
172 * THE PER-DRIVER RECORD FOR ISA.C
174 ***********************************************************************/
176 static int gscprobe (struct isa_device *isdp);
177 static int gscattach(struct isa_device *isdp);
179 struct isa_driver gscdriver = { gscprobe, gscattach, "gsc" };
181 static d_open_t gscopen;
182 static d_close_t gscclose;
183 static d_read_t gscread;
184 static d_ioctl_t gscioctl;
186 #define CDEV_MAJOR 47
187 static struct cdevsw gsc_cdevsw = {
189 /* maj */ CDEV_MAJOR,
195 /* close */ gscclose,
198 /* ioctl */ gscioctl,
201 /* strategy */ nostrategy,
207 /***********************************************************************
209 * LOCALLY USED SUBROUTINES
211 ***********************************************************************/
213 /***********************************************************************
215 * lookup_geometry -- lookup a record in the geometry table by pattern
217 * The caller supplies a geometry record pattern, where INVALID
218 * matches anything. Returns the index in the table or INVALID if
223 lookup_geometry(struct gsc_geom geom, const struct gsc_unit *scu)
228 for(i=0; i<GEOMTAB_SIZE; i++)
232 if ( ( ( geom.dpi != INVALID ) && ( tab.dpi == geom.dpi ) ) ||
233 ( ( geom.dpl != INVALID ) && ( tab.dpl == geom.dpl ) ) ||
234 ( ( geom.g_res != INVALID ) && ( tab.g_res == geom.g_res ) ) ||
235 ( ( geom.s_res != INVALID ) && ( tab.s_res == geom.s_res ) ) )
237 lprintf(("gsc.lookup_geometry: "
238 "geometry lookup found: %ddpi, %ddpl\n",
244 lprintf(("gsc.lookup_geometry: "
245 "geometry lookup failed on {%d, %d, 0x%02x, 0x%02x}\n",
246 geom.dpi, geom.dpl, geom.g_res, geom.s_res));
251 /***********************************************************************
253 * get_geometry -- read geometry from status port
255 * Returns the index into geometry table or INVALID if it fails to
256 * either read the status byte or lookup the record.
260 get_geometry(const struct gsc_unit *scu)
262 struct gsc_geom geom = NEW_GEOM;
264 lprintf(("gsc.get_geometry: get geometry at 0x%03x\n", scu->stat));
266 if ( ( geom.g_res = inb(scu->stat) ) == FAIL )
269 geom.g_res &= GSC_RES_MASK;
271 return lookup_geometry(geom, scu);
274 /***********************************************************************
276 * buffer_allocate -- allocate/reallocate a buffer
277 * Now just checks that the preallocated buffer is large enough.
281 buffer_allocate(struct gsc_unit *scu)
285 size = scu->blen * geomtab[scu->geometry].dpl / 8;
287 lprintf(("gsc.buffer_allocate: need 0x%x bytes\n", size));
289 if ( size > MAX_BUFSIZE )
291 lprintf(("gsc.buffer_allocate: 0x%x bytes are too much\n", size));
295 scu->sbuf.size = size;
296 scu->sbuf.poi = size;
298 lprintf(("gsc.buffer_allocate: ok\n"));
303 /***********************************************************************
305 * buffer_read -- scan a buffer
309 buffer_read(struct gsc_unit *scu)
318 lprintf(("gsc.buffer_read: begin\n"));
320 if (scu->ctrl_byte == INVALID)
322 lprintf(("gsc.buffer_read: invalid ctrl_byte\n"));
328 outb( scu->ctrl, scu->ctrl_byte | GSC_POWER_ON );
329 outb( scu->clrp, 0 );
330 stb = inb( scu->stat );
332 isa_dmastart(ISADMA_READ, scu->sbuf.base, scu->sbuf.size, scu->channel);
334 chan_bit = 0x01 << scu->channel;
336 for(delay=0; !(inb(DMA1_READY) & 0x01 << scu->channel); delay += LONG)
338 if(delay >= scu->btime)
341 lprintf(("gsc.buffer_read: timeout\n"));
345 res = tsleep((caddr_t)scu, PCATCH, "gscread", LONG);
346 if ( ( res == 0 ) || ( res == EWOULDBLOCK ) )
352 isa_dmadone(ISADMA_READ, scu->sbuf.base, scu->sbuf.size, scu->channel);
353 outb( scu->clrp, 0 );
357 lprintf(("gsc.buffer_read: aborted with %d\n", res));
361 lprintf(("gsc.buffer_read: invert buffer\n"));
362 for(p = scu->sbuf.base + scu->sbuf.size - 1; p >= scu->sbuf.base; p--)
366 lprintf(("gsc.buffer_read: ok\n"));
370 /***********************************************************************
374 ***********************************************************************/
376 /***********************************************************************
380 * read status port and check for proper configuration:
381 * - if address group matches (status byte has reasonable value)
382 * - if DMA channel matches (status byte has correct value)
386 gscprobe (struct isa_device *isdp)
388 int unit = isdp->id_unit;
389 struct gsc_unit *scu = unittab + unit;
391 struct gsc_geom geom = NEW_GEOM;
395 cdevsw_add(&gsc_cdevsw);
397 scu->flags = FLAG_DEBUG;
399 lprintf(("gsc%d.probe "
400 "on iobase 0x%03x, irq %d, drq %d, addr %p, size %d\n",
408 if ( isdp->id_iobase < 0 )
410 lprintf(("gsc%d.probe: no iobase given\n", unit));
414 stb = inb( GSC_STAT(isdp->id_iobase) );
417 lprintf(("gsc%d.probe: get status byte failed\n", unit));
421 scu->data = GSC_DATA(isdp->id_iobase);
422 scu->stat = GSC_STAT(isdp->id_iobase);
423 scu->ctrl = GSC_CTRL(isdp->id_iobase);
424 scu->clrp = GSC_CLRP(isdp->id_iobase);
427 stb = inb(scu->stat);
429 switch(stb & GSC_CNF_MASK) {
431 lprintf(("gsc%d.probe: DMA 1\n", unit));
436 lprintf(("gsc%d.probe: DMA 3\n", unit));
441 lprintf(("gsc%d.probe: IRQ 3\n", unit));
444 lprintf(("gsc%d.probe: IRQ 5\n", unit));
446 lprintf(("gsc%d.probe: sorry, can't use IRQ yet\n", unit));
449 lprintf(("gsc%d.probe: invalid status byte 0x%02x\n", unit, (u_char) stb));
453 if (isdp->id_drq < 0)
454 isdp->id_drq = scu->channel;
455 if (scu->channel != isdp->id_drq)
457 lprintf(("gsc%d.probe: drq mismatch: config: %d; hardware: %d\n",
458 unit, isdp->id_drq, scu->channel));
462 geom.g_res = stb & GSC_RES_MASK;
463 scu->geometry = lookup_geometry(geom, scu);
464 if (scu->geometry == INVALID)
466 lprintf(("gsc%d.probe: geometry lookup failed\n", unit));
471 scu->ctrl_byte = geomtab[scu->geometry].s_res;
472 outb(scu->ctrl, scu->ctrl_byte | GSC_POWER_ON);
474 lprintf(("gsc%d.probe: status 0x%02x, %ddpi\n",
475 unit, stb, geomtab[scu->geometry].dpi));
477 outb(scu->ctrl, scu->ctrl_byte & ~GSC_POWER_ON);
480 lprintf(("gsc%d.probe: ok\n", unit));
482 scu->flags &= ~FLAG_DEBUG;
484 return PROBE_SUCCESS;
487 /***********************************************************************
491 * finish initialization of unit structure
496 gscattach(struct isa_device *isdp)
498 int unit = isdp->id_unit;
499 struct gsc_unit *scu = unittab + unit;
501 scu->flags |= FLAG_DEBUG;
503 lprintf(("gsc%d.attach: "
504 "iobase 0x%03x, irq %d, drq %d, addr %p, size %d\n",
512 printf("gsc%d: GeniScan GS-4500 at %ddpi\n",
513 unit, geomtab[scu->geometry].dpi);
516 * Initialize buffer structure.
517 * XXX this must be done early to give a good chance of getting a
518 * contiguous buffer. This wastes memory.
520 scu->sbuf.base = contigmalloc((unsigned long)MAX_BUFSIZE, M_DEVBUF, M_NOWAIT,
521 0ul, 0xfffffful, 1ul, 0x10000ul);
522 if ( scu->sbuf.base == NULL )
524 lprintf(("gsc%d.attach: buffer allocation failed\n", unit));
525 return ATTACH_FAIL; /* XXX attach must not fail */
527 scu->sbuf.size = INVALID;
528 scu->sbuf.poi = INVALID;
530 scu->blen = DEFAULT_BLEN;
531 scu->btime = TIMEOUT;
533 scu->flags |= ATTACHED;
534 lprintf(("gsc%d.attach: ok\n", unit));
535 scu->flags &= ~FLAG_DEBUG;
538 make_dev(&gsc_cdevsw, unit<<6, GSC_UID, GSC_GID, 0666, "gsc%d", unit);
539 make_dev(&gsc_cdevsw, ((unit<<6) + FRMT_PBM),
540 GSC_UID, GSC_GID, 0666, "gsc%dp", unit);
541 make_dev(&gsc_cdevsw, ((unit<<6) + DBUG_MASK),
542 GSC_UID, GSC_GID, 0666, "gsc%dd", unit);
543 make_dev(&gsc_cdevsw, ((unit<<6) + DBUG_MASK+FRMT_PBM),
544 GSC_UID, GSC_GID, 0666, "gsc%dpd", unit);
546 return ATTACH_SUCCESS;
549 /***********************************************************************
554 * set modes according to minor number
555 * don't switch scanner on, wait until first read ioctls go before
559 gscopen (dev_t dev, int flags, int fmt, struct proc *p)
561 struct gsc_unit *scu;
564 unit = UNIT(minor(dev)) & UNIT_MASK;
568 /* XXX lprintf isn't valid here since there is no scu. */
569 printf("gsc%d.open: unconfigured unit number (max %d)\n", unit, NGSC);
573 scu = unittab + unit;
574 if ( !( scu->flags & ATTACHED ) )
576 lprintf(("gsc%d.open: unit was not attached successfully 0x%04x\n",
581 if ( minor(dev) & DBUG_MASK )
582 scu->flags |= FLAG_DEBUG;
584 scu->flags &= ~FLAG_DEBUG;
586 switch(minor(dev) & FRMT_MASK) {
588 scu->flags |= PBM_MODE;
589 lprintf(("gsc%d.open: pbm mode\n", unit));
592 lprintf(("gsc%d.open: raw mode\n", unit));
593 scu->flags &= ~PBM_MODE;
596 lprintf(("gsc%d.open: gray maps are not yet supported", unit));
600 lprintf(("gsc%d.open: minor %d\n",
603 if ( scu->flags & OPEN )
605 lprintf(("gsc%d.open: already open", unit));
609 if (isa_dma_acquire(scu->channel))
617 /***********************************************************************
626 gscclose (dev_t dev, int flags, int fmt, struct proc *p)
628 int unit = UNIT(minor(dev));
629 struct gsc_unit *scu = unittab + unit;
631 lprintf(("gsc%d.close: minor %d\n",
634 if ( unit >= NGSC || !( scu->flags & ATTACHED ) )
636 lprintf(("gsc%d.read: unit was not attached successfully 0x%04x\n",
641 outb(scu->ctrl, scu->ctrl_byte & ~GSC_POWER_ON);
643 scu->sbuf.size = INVALID;
644 scu->sbuf.poi = INVALID;
646 isa_dma_release(scu->channel);
648 scu->flags &= ~(FLAG_DEBUG | OPEN | READING);
653 /***********************************************************************
659 gscread (dev_t dev, struct uio *uio, int ioflag)
661 int unit = UNIT(minor(dev));
662 struct gsc_unit *scu = unittab + unit;
666 lprintf(("gsc%d.read: minor %d\n", unit, minor(dev)));
668 if ( unit >= NGSC || !( scu->flags & ATTACHED ) )
670 lprintf(("gsc%d.read: unit was not attached successfully 0x%04x\n",
675 if ( !(scu->flags & READING) )
677 res = buffer_allocate(scu);
678 if ( res == SUCCESS )
679 scu->flags |= READING;
683 scu->ctrl_byte = geomtab[scu->geometry].s_res;
685 /* initialize for pbm mode */
686 if ( scu->flags & PBM_MODE )
689 int width = geomtab[scu->geometry].dpl;
691 sprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height);
692 scu->bcount = scu->height * width / 8;
694 lprintf(("gsc%d.read: initializing pbm mode: `%s', bcount: 0x%x\n",
695 unit, scu->sbuf.base, scu->bcount));
697 /* move header to end of sbuf */
698 for(p=scu->sbuf.base; *p; p++);
699 while(--p >= scu->sbuf.base)
701 *(char *)(scu->sbuf.base + --scu->sbuf.poi) = *p;
707 lprintf(("gsc%d.read(before buffer_read): "
708 "size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
709 unit, scu->sbuf.size, scu->sbuf.poi, scu->bcount));
711 if ( scu->sbuf.poi == scu->sbuf.size )
712 if ( (res = buffer_read(scu)) != SUCCESS )
715 lprintf(("gsc%d.read(after buffer_read): "
716 "size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
717 unit, scu->sbuf.size, scu->sbuf.poi, scu->bcount));
719 nbytes = MIN( uio->uio_resid, scu->sbuf.size - scu->sbuf.poi );
721 if ( (scu->flags & PBM_MODE) )
722 nbytes = MIN( nbytes, scu->bcount );
724 lprintf(("gsc%d.read: transferring 0x%x bytes", unit, nbytes));
726 res = uiomove(scu->sbuf.base + scu->sbuf.poi, nbytes, uio);
727 if ( res != SUCCESS )
729 lprintf(("gsc%d.read: uiomove failed %d", unit, res));
733 scu->sbuf.poi += nbytes;
734 if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes;
736 lprintf(("gsc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n",
737 unit, scu->sbuf.size, scu->sbuf.poi, scu->bcount));
742 /***********************************************************************
749 gscioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
751 int unit = UNIT(minor(dev));
752 struct gsc_unit *scu = unittab + unit;
754 lprintf(("gsc%d.ioctl: minor %d\n",
757 if ( unit >= NGSC || !( scu->flags & ATTACHED ) )
759 lprintf(("gsc%d.ioctl: unit was not attached successfully 0x%04x\n",
766 lprintf(("gsc%d.ioctl:GSC_SRESSW\n", unit));
767 if ( scu->flags & READING )
769 lprintf(("gsc%d:ioctl on already reading unit\n", unit));
772 scu->geometry = get_geometry(scu);
775 *(int *)data=geomtab[scu->geometry].dpi;
776 lprintf(("gsc%d.ioctl:GSC_GRES %ddpi\n", unit, *(int *)data));
779 *(int *)data=geomtab[scu->geometry].dpl;
780 lprintf(("gsc%d.ioctl:GSC_GWIDTH %d\n", unit, *(int *)data));
784 lprintf(("gsc%d.ioctl:GSC_SRES or GSC_SWIDTH %d\n",
785 unit, *(int *)data));
787 struct gsc_geom geom = NEW_GEOM;
788 if ( cmd == GSC_SRES )
789 geom.dpi = *(int *)data;
791 geom.dpl = *(int *)data;
792 if ( ( g = lookup_geometry(geom, scu) ) == INVALID )
798 *(int *)data=scu->height;
799 lprintf(("gsc%d.ioctl:GSC_GHEIGHT %d\n", unit, *(int *)data));
802 lprintf(("gsc%d.ioctl:GSC_SHEIGHT %d\n", unit, *(int *)data));
803 if ( scu->flags & READING )
805 lprintf(("gsc%d:ioctl on already reading unit\n", unit));
808 scu->height=*(int *)data;
811 *(int *)data=scu->blen;
812 lprintf(("gsc%d.ioctl:GSC_GBLEN %d\n", unit, *(int *)data));
815 lprintf(("gsc%d.ioctl:GSC_SBLEN %d\n", unit, *(int *)data));
816 if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE)
818 lprintf(("gsc%d:ioctl buffer size too high\n", unit));
821 scu->blen=*(int *)data;
824 *(int *)data = scu->btime / hz;
825 lprintf(("gsc%d.ioctl:GSC_GBTIME %d\n", unit, *(int *)data));
828 scu->btime = *(int *)data * hz;
829 lprintf(("gsc%d.ioctl:GSC_SBTIME %d\n", unit, *(int *)data));
831 default: return ENOTTY;