| 1 | /* asc.c - device driver for hand scanners |
| 2 | * |
| 3 | * Current version supports: |
| 4 | * |
| 5 | * - AmiScan (Mustek) Color and BW hand scanners (GI1904 chipset) |
| 6 | * |
| 7 | * Copyright (c) 1995 Gunther Schadow. All rights reserved. |
| 8 | * Copyright (c) 1995,1996,1997 Luigi Rizzo. All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by Gunther Schadow |
| 21 | * and Luigi Rizzo. |
| 22 | * 4. The name of the author may not be used to endorse or promote products |
| 23 | * derived from this software without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | /* |
| 37 | * $FreeBSD: src/sys/i386/isa/asc.c,v 1.42.2.2 2001/03/01 03:22:39 jlemon Exp $ |
| 38 | * $DragonFly: src/sys/platform/pc32/isa/asc.c,v 1.10 2005/10/13 00:02:47 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 inthand2_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_intr = 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(void *arg) |
| 501 | { |
| 502 | struct asc_unit *scu = unittab + unit; |
| 503 | int chan_bit = 0x01 << scu->dma_num; |
| 504 | int unit = (int)arg; |
| 505 | |
| 506 | scu->icnt++; |
| 507 | /* ignore stray interrupts... */ |
| 508 | if ((scu->flags & (OPEN |READING)) != (OPEN | READING) ) { |
| 509 | /* must be after closing... */ |
| 510 | scu->flags &= ~(OPEN | READING | DMA_ACTIVE | SLEEPING | SEL_COLL); |
| 511 | return; |
| 512 | } |
| 513 | if ( (scu->flags & DMA_ACTIVE) && (inb(DMA1_READY) & chan_bit) != 0) { |
| 514 | outb( ASC_CMD, ASC_STANDBY); |
| 515 | scu->flags &= ~DMA_ACTIVE; |
| 516 | /* bounce buffers... */ |
| 517 | isa_dmadone(ISADMA_READ, scu->sbuf.base+scu->sbuf.wptr, |
| 518 | scu->linesize, scu->dma_num); |
| 519 | scu->sbuf.wptr += scu->linesize; |
| 520 | if (scu->sbuf.wptr >= scu->sbuf.size) scu->sbuf.wptr=0; |
| 521 | scu->sbuf.count += scu->linesize; |
| 522 | if (scu->flags & SLEEPING) { |
| 523 | scu->flags &= ~SLEEPING; |
| 524 | wakeup((caddr_t)scu); |
| 525 | } |
| 526 | if (scu->sbuf.size - scu->sbuf.count >= scu->linesize) { |
| 527 | dma_restart(scu); |
| 528 | } |
| 529 | if (scu->selp.si_pid) { |
| 530 | selwakeup(&scu->selp); |
| 531 | scu->selp.si_pid=(pid_t)0; |
| 532 | scu->selp.si_flags = 0; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /************************************************************************** |
| 538 | *** |
| 539 | *** ascopen |
| 540 | *** set open flag, set modes according to minor number |
| 541 | *** FOR RELEASE: |
| 542 | *** don't switch scanner on, wait until first read or ioctls go before |
| 543 | ***/ |
| 544 | |
| 545 | STATIC int |
| 546 | ascopen(dev_t dev, int flags, int fmt, struct thread *td) |
| 547 | { |
| 548 | struct asc_unit *scu; |
| 549 | int unit; |
| 550 | |
| 551 | unit = UNIT(minor(dev)) & UNIT_MASK; |
| 552 | if ( unit >= NASC ) |
| 553 | { |
| 554 | #ifdef ASCDEBUG |
| 555 | /* XXX lprintf isn't valid here since there is no scu. */ |
| 556 | printf("asc%d.open: unconfigured unit number (max %d)\n", unit, NASC); |
| 557 | #endif |
| 558 | return ENXIO; |
| 559 | } |
| 560 | scu = unittab + unit; |
| 561 | if ( !( scu->flags & ATTACHED ) ) |
| 562 | { |
| 563 | lprintf("asc%d.open: unit was not attached successfully 0x%04x\n", |
| 564 | unit, scu->flags); |
| 565 | return ENXIO; |
| 566 | } |
| 567 | |
| 568 | if ( minor(dev) & DBUG_MASK ) |
| 569 | scu->flags |= FLAG_DEBUG; |
| 570 | else |
| 571 | scu->flags &= ~FLAG_DEBUG; |
| 572 | |
| 573 | switch(minor(dev) & FRMT_MASK) { |
| 574 | case FRMT_PBM: |
| 575 | scu->flags |= PBM_MODE; |
| 576 | lprintf("asc%d.open: pbm mode\n", unit); |
| 577 | break; |
| 578 | case FRMT_RAW: |
| 579 | lprintf("asc%d.open: raw mode\n", unit); |
| 580 | scu->flags &= ~PBM_MODE; |
| 581 | break; |
| 582 | default: |
| 583 | lprintf("asc%d.open: gray maps are not yet supported", unit); |
| 584 | return ENXIO; |
| 585 | } |
| 586 | |
| 587 | lprintf("asc%d.open: minor %d icnt %ld\n", unit, minor(dev), scu->icnt); |
| 588 | |
| 589 | if ( scu->flags & OPEN ) { |
| 590 | lprintf("asc%d.open: already open", unit); |
| 591 | return EBUSY; |
| 592 | } |
| 593 | if (isa_dma_acquire(scu->dma_num)) |
| 594 | return(EBUSY); |
| 595 | |
| 596 | scu->flags = ATTACHED | OPEN; |
| 597 | |
| 598 | asc_reset(scu); |
| 599 | get_resolution(scu); |
| 600 | return SUCCESS; |
| 601 | } |
| 602 | |
| 603 | static int |
| 604 | asc_startread(struct asc_unit *scu) |
| 605 | { |
| 606 | /*** from here on, things can be delayed to the first read/ioctl ***/ |
| 607 | /*** this was done in sub_12... ***/ |
| 608 | scu->cfg_byte= scu->cmd_byte=0; /* init scanner */ |
| 609 | outb(ASC_CMD, scu->cmd_byte); |
| 610 | /*** this was done in sub_16, set scan len... ***/ |
| 611 | outb(ASC_BOH, scu->portf_byte ); |
| 612 | if (geomtab[scu->geometry].g_res==0) { /* color */ |
| 613 | scu->cmd_byte = 0x00 ; |
| 614 | } else { |
| 615 | scu->cmd_byte = 0x90 ; |
| 616 | } |
| 617 | outb(ASC_CMD, scu->cmd_byte); |
| 618 | outb(ASC_LEN_L, scu->linesize & 0xff /* len_low */); |
| 619 | outb(ASC_LEN_H, (scu->linesize >>8) & 0xff /* len_high */); |
| 620 | /*** this was done in sub_21, config DMA ... ***/ |
| 621 | scu->cfg_byte |= scu->dma_byte; |
| 622 | outb(ASC_CFG, scu->cfg_byte); |
| 623 | /*** sub_22: enable int on the scanner ***/ |
| 624 | scu->cfg_byte |= scu->int_byte; |
| 625 | outb(ASC_CFG, scu->cfg_byte); |
| 626 | /*** sub_28: light on etc...***/ |
| 627 | scu->cmd_byte = ASC_STANDBY; |
| 628 | outb(ASC_CMD, scu->cmd_byte); |
| 629 | tsleep((caddr_t)scu, PCATCH, "ascstrd", hz/10); /* sleep .1 sec */ |
| 630 | return SUCCESS; |
| 631 | } |
| 632 | |
| 633 | /************************************************************************** |
| 634 | *** |
| 635 | *** ascclose |
| 636 | *** turn off scanner, release the buffer |
| 637 | *** should probably terminate dma ops, release int and dma. lr 12mar95 |
| 638 | ***/ |
| 639 | |
| 640 | STATIC int |
| 641 | ascclose(dev_t dev, int flags, int fmt, struct thread *td) |
| 642 | { |
| 643 | int unit = UNIT(minor(dev)); |
| 644 | struct asc_unit *scu = unittab + unit; |
| 645 | |
| 646 | lprintf("asc%d.close: minor %d\n", |
| 647 | unit, minor(dev)); |
| 648 | |
| 649 | if ( unit >= NASC || !( scu->flags & ATTACHED ) ) { |
| 650 | lprintf("asc%d.close: unit was not attached successfully 0x%04x\n", |
| 651 | unit, scu->flags); |
| 652 | return ENXIO; |
| 653 | } |
| 654 | /* all this is in sub_29... */ |
| 655 | /* cli(); */ |
| 656 | outb(ASC_CFG, 0 ); /* don't save in CFG byte!!! */ |
| 657 | scu->cmd_byte &= ~ASC_LIGHT_ON; |
| 658 | outb(ASC_CMD, scu->cmd_byte);/* light off */ |
| 659 | tsleep((caddr_t)scu, PCATCH, "ascclo", hz/2); /* sleep 1/2 sec */ |
| 660 | scu->cfg_byte &= ~ scu->dma_byte ; /* disable scanner dma */ |
| 661 | scu->cfg_byte &= ~ scu->int_byte ; /* disable scanner int */ |
| 662 | outb(ASC_CFG, scu->cfg_byte); |
| 663 | /* --- disable dma controller ? --- */ |
| 664 | isa_dma_release(scu->dma_num); |
| 665 | /* --- disable interrupts on the controller (sub_24) --- */ |
| 666 | |
| 667 | scu->sbuf.size = INVALID; |
| 668 | scu->sbuf.rptr = INVALID; |
| 669 | |
| 670 | scu->flags &= ~(FLAG_DEBUG | OPEN | READING); |
| 671 | |
| 672 | return SUCCESS; |
| 673 | } |
| 674 | |
| 675 | static void |
| 676 | pbm_init(struct asc_unit *scu) |
| 677 | { |
| 678 | int width = geomtab[scu->geometry].dpl; |
| 679 | int l= sprintf(scu->sbuf.base,"P4 %d %d\n", width, scu->height); |
| 680 | char *p; |
| 681 | |
| 682 | scu->bcount = scu->height * width / 8 + l; |
| 683 | |
| 684 | /* move header to end of sbuf */ |
| 685 | scu->sbuf.rptr=scu->sbuf.size-l; |
| 686 | bcopy(scu->sbuf.base, scu->sbuf.base+scu->sbuf.rptr,l); |
| 687 | scu->sbuf.count = l; |
| 688 | if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */ |
| 689 | for(p = scu->sbuf.base + scu->sbuf.rptr; l; p++, l--) |
| 690 | *p = ~*p; |
| 691 | } |
| 692 | } |
| 693 | /************************************************************************** |
| 694 | *** |
| 695 | *** ascread |
| 696 | ***/ |
| 697 | |
| 698 | STATIC int |
| 699 | ascread(dev_t dev, struct uio *uio, int ioflag) |
| 700 | { |
| 701 | int unit = UNIT(minor(dev)); |
| 702 | struct asc_unit *scu = unittab + unit; |
| 703 | size_t nbytes; |
| 704 | int res; |
| 705 | unsigned char *p; |
| 706 | |
| 707 | lprintf("asc%d.read: minor %d icnt %ld\n", unit, minor(dev), scu->icnt); |
| 708 | |
| 709 | if ( unit >= NASC || !( scu->flags & ATTACHED ) ) { |
| 710 | lprintf("asc%d.read: unit was not attached successfully 0x%04x\n", |
| 711 | unit, scu->flags); |
| 712 | return ENXIO; |
| 713 | } |
| 714 | |
| 715 | if ( !(scu->flags & READING) ) { /*** first read... ***/ |
| 716 | /* allocate a buffer for reading data and init things */ |
| 717 | if ( (res = buffer_allocate(scu)) == SUCCESS ) scu->flags |= READING; |
| 718 | else return res; |
| 719 | asc_startread(scu); |
| 720 | if ( scu->flags & PBM_MODE ) { /* initialize for pbm mode */ |
| 721 | pbm_init(scu); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | lprintf("asc%d.read(before): " |
| 726 | "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n", |
| 727 | unit, scu->sbuf.size, scu->sbuf.rptr, |
| 728 | scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags, |
| 729 | scu->icnt); |
| 730 | |
| 731 | crit_enter(); |
| 732 | if ( scu->sbuf.count == 0 ) { /* no data avail., must wait */ |
| 733 | if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu); |
| 734 | scu->flags |= SLEEPING; |
| 735 | res = tsleep((caddr_t)scu, PCATCH, "ascread", 0); |
| 736 | scu->flags &= ~SLEEPING; |
| 737 | if ( res == 0 ) res = SUCCESS; |
| 738 | } |
| 739 | crit_exit(); |
| 740 | if (scu->flags & FLAG_DEBUG) |
| 741 | tsleep((caddr_t)scu, PCATCH, "ascdly",hz); |
| 742 | lprintf("asc%d.read(after): " |
| 743 | "sz 0x%x, rptr 0x%x, wptr 0x%x, cnt 0x%x bcnt 0x%x flags 0x%x icnt %ld\n", |
| 744 | unit, scu->sbuf.size, scu->sbuf.rptr, |
| 745 | scu->sbuf.wptr, scu->sbuf.count, scu->bcount,scu->flags,scu->icnt); |
| 746 | |
| 747 | /* first, not more than available... */ |
| 748 | nbytes = min( uio->uio_resid, scu->sbuf.count ); |
| 749 | /* second, contiguous data... */ |
| 750 | nbytes = min( nbytes, (scu->sbuf.size - scu->sbuf.rptr) ); |
| 751 | /* third, one line (will remove this later, XXX) */ |
| 752 | nbytes = min( nbytes, scu->linesize ); |
| 753 | if ( (scu->flags & PBM_MODE) ) |
| 754 | nbytes = min( nbytes, scu->bcount ); |
| 755 | lprintf("asc%d.read: transferring 0x%x bytes\n", unit, nbytes); |
| 756 | if (geomtab[scu->geometry].g_res!=0) { /* BW scanner */ |
| 757 | lprintf("asc%d.read: invert buffer\n",unit); |
| 758 | for(p = scu->sbuf.base + scu->sbuf.rptr, res=nbytes; res; p++, res--) |
| 759 | *p = ~*p; |
| 760 | } |
| 761 | res = uiomove(scu->sbuf.base + scu->sbuf.rptr, nbytes, uio); |
| 762 | if ( res != SUCCESS ) { |
| 763 | lprintf("asc%d.read: uiomove failed %d", unit, res); |
| 764 | return res; |
| 765 | } |
| 766 | |
| 767 | crit_enter(); |
| 768 | scu->sbuf.rptr += nbytes; |
| 769 | if (scu->sbuf.rptr >= scu->sbuf.size) scu->sbuf.rptr=0; |
| 770 | scu->sbuf.count -= nbytes; |
| 771 | /* having moved some data, can read mode */ |
| 772 | if (!(scu->flags & DMA_ACTIVE)) dma_restart(scu); |
| 773 | crit_exit(); |
| 774 | if ( scu->flags & PBM_MODE ) scu->bcount -= nbytes; |
| 775 | |
| 776 | lprintf("asc%d.read: size 0x%x, pointer 0x%x, bcount 0x%x, ok\n", |
| 777 | unit, scu->sbuf.size, scu->sbuf.rptr, scu->bcount); |
| 778 | |
| 779 | return SUCCESS; |
| 780 | } |
| 781 | |
| 782 | /************************************************************************** |
| 783 | *** |
| 784 | *** ascioctl |
| 785 | ***/ |
| 786 | |
| 787 | STATIC int |
| 788 | ascioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) |
| 789 | { |
| 790 | int unit = UNIT(minor(dev)); |
| 791 | struct asc_unit *scu = unittab + unit; |
| 792 | |
| 793 | lprintf("asc%d.ioctl: minor %d\n", |
| 794 | unit, minor(dev)); |
| 795 | |
| 796 | if ( unit >= NASC || !( scu->flags & ATTACHED ) ) { |
| 797 | lprintf("asc%d.ioctl: unit was not attached successfully 0x%04x\n", |
| 798 | unit, scu->flags); |
| 799 | return ENXIO; |
| 800 | } |
| 801 | switch(cmd) { |
| 802 | case ASC_GRES: |
| 803 | asc_reset(scu); |
| 804 | get_resolution(scu); |
| 805 | *(int *)data=geomtab[scu->geometry].dpi; |
| 806 | lprintf("asc%d.ioctl:ASC_GRES %ddpi\n", unit, *(int *)data); |
| 807 | return SUCCESS; |
| 808 | case ASC_GWIDTH: |
| 809 | *(int *)data=geomtab[scu->geometry].dpl; |
| 810 | lprintf("asc%d.ioctl:ASC_GWIDTH %d\n", unit, *(int *)data); |
| 811 | return SUCCESS; |
| 812 | case ASC_GHEIGHT: |
| 813 | *(int *)data=scu->height; |
| 814 | lprintf("asc%d.ioctl:ASC_GHEIGHT %d\n", unit, *(int *)data); |
| 815 | return SUCCESS; |
| 816 | case ASC_SHEIGHT: |
| 817 | lprintf("asc%d.ioctl:ASC_SHEIGHT %d\n", unit, *(int *)data); |
| 818 | if ( scu->flags & READING ) { |
| 819 | lprintf("asc%d:ioctl on already reading unit\n", unit); |
| 820 | return EBUSY; |
| 821 | } |
| 822 | scu->height=*(int *)data; |
| 823 | return SUCCESS; |
| 824 | #if 0 |
| 825 | case ASC_GBLEN: |
| 826 | *(int *)data=scu->blen; |
| 827 | lprintf("asc%d.ioctl:ASC_GBLEN %d\n", unit, *(int *)data); |
| 828 | return SUCCESS; |
| 829 | case ASC_SBLEN: |
| 830 | lprintf("asc%d.ioctl:ASC_SBLEN %d\n", unit, *(int *)data); |
| 831 | if (*(int *)data * geomtab[scu->geometry].dpl / 8 > MAX_BUFSIZE) |
| 832 | { |
| 833 | lprintf("asc%d:ioctl buffer size too high\n", unit); |
| 834 | return ENOMEM; |
| 835 | } |
| 836 | scu->blen=*(int *)data; |
| 837 | return SUCCESS; |
| 838 | case ASC_GBTIME: |
| 839 | *(int *)data = scu->btime / hz; |
| 840 | lprintf("asc%d.ioctl:ASC_GBTIME %d\n", unit, *(int *)data); |
| 841 | return SUCCESS; |
| 842 | case ASC_SBTIME: |
| 843 | scu->btime = *(int *)data * hz; |
| 844 | lprintf("asc%d.ioctl:ASC_SBTIME %d\n", unit, *(int *)data); |
| 845 | return SUCCESS; |
| 846 | #endif |
| 847 | default: return ENOTTY; |
| 848 | } |
| 849 | return SUCCESS; |
| 850 | } |
| 851 | |
| 852 | STATIC int |
| 853 | ascpoll(dev_t dev, int events, struct thread *td) |
| 854 | { |
| 855 | int unit = UNIT(minor(dev)); |
| 856 | struct asc_unit *scu = unittab + unit; |
| 857 | struct proc *p; |
| 858 | struct proc *p1; |
| 859 | int revents = 0; |
| 860 | |
| 861 | p = td->td_proc; |
| 862 | KKASSERT(p); |
| 863 | |
| 864 | crit_enter(); |
| 865 | |
| 866 | if (events & (POLLIN | POLLRDNORM)) { |
| 867 | if (scu->sbuf.count >0) |
| 868 | revents |= events & (POLLIN | POLLRDNORM); |
| 869 | else { |
| 870 | if (!(scu->flags & DMA_ACTIVE)) |
| 871 | dma_restart(scu); |
| 872 | |
| 873 | if (scu->selp.si_pid && (p1=pfind(scu->selp.si_pid)) |
| 874 | && p1->p_wchan == (caddr_t)&selwait) |
| 875 | scu->selp.si_flags = SI_COLL; |
| 876 | else |
| 877 | scu->selp.si_pid = p->p_pid; |
| 878 | } |
| 879 | } |
| 880 | crit_exit(); |
| 881 | return (revents); |
| 882 | } |