Home | History | Annotate | Line # | Download | only in scsipi
ss_scanjet.c revision 1.6
      1  1.6  christos /*	$NetBSD: ss_scanjet.c,v 1.6 1996/05/18 22:58:01 christos Exp $	*/
      2  1.1   mycroft 
      3  1.1   mycroft /*
      4  1.1   mycroft  * Copyright (c) 1995 Kenneth Stailey.  All rights reserved.
      5  1.1   mycroft  *
      6  1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      7  1.1   mycroft  * modification, are permitted provided that the following conditions
      8  1.1   mycroft  * are met:
      9  1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     10  1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     11  1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     14  1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     15  1.1   mycroft  *    must display the following acknowledgement:
     16  1.1   mycroft  *      This product includes software developed by Kenneth Stailey.
     17  1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18  1.1   mycroft  *    derived from this software without specific prior written permission.
     19  1.1   mycroft  *
     20  1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1   mycroft  */
     31  1.1   mycroft 
     32  1.1   mycroft /*
     33  1.1   mycroft  * special functions for the HP ScanJet IIc and IIcx
     34  1.1   mycroft  */
     35  1.1   mycroft 
     36  1.1   mycroft #include <sys/types.h>
     37  1.1   mycroft #include <sys/param.h>
     38  1.1   mycroft #include <sys/systm.h>
     39  1.1   mycroft #include <sys/fcntl.h>
     40  1.1   mycroft #include <sys/errno.h>
     41  1.1   mycroft #include <sys/ioctl.h>
     42  1.1   mycroft #include <sys/malloc.h>
     43  1.1   mycroft #include <sys/buf.h>
     44  1.1   mycroft #include <sys/proc.h>
     45  1.1   mycroft #include <sys/user.h>
     46  1.1   mycroft #include <sys/device.h>
     47  1.1   mycroft #include <sys/conf.h>		/* for cdevsw */
     48  1.1   mycroft #include <sys/scanio.h>
     49  1.1   mycroft 
     50  1.1   mycroft #include <scsi/scsi_all.h>
     51  1.1   mycroft #include <scsi/scsi_scanner.h>
     52  1.1   mycroft #include <scsi/scsiconf.h>
     53  1.1   mycroft #include <scsi/ssvar.h>
     54  1.1   mycroft 
     55  1.1   mycroft #define SCANJET_RETRIES 4
     56  1.1   mycroft 
     57  1.1   mycroft int scanjet_get_params __P((struct ss_softc *));
     58  1.1   mycroft int scanjet_set_params __P((struct ss_softc *, struct scan_io *));
     59  1.1   mycroft int scanjet_trigger_scanner __P((struct ss_softc *));
     60  1.1   mycroft int scanjet_read __P((struct ss_softc *, struct buf *));
     61  1.1   mycroft 
     62  1.1   mycroft /* only used internally */
     63  1.5   mycroft int scanjet_ctl_write __P((struct ss_softc *, char *, u_int, int));
     64  1.5   mycroft int scanjet_ctl_read __P((struct ss_softc *, char *, u_int, int));
     65  1.5   mycroft int scanjet_set_window __P((struct ss_softc *));
     66  1.5   mycroft int scanjet_compute_sizes __P((struct ss_softc *));
     67  1.6  christos /* Maybe move to libkern? */
     68  1.6  christos __inline static int atoi __P((const char *));
     69  1.6  christos __inline static char *strchr __P((const char *, char));
     70  1.6  christos 
     71  1.1   mycroft 
     72  1.1   mycroft /*
     73  1.1   mycroft  * structure for the special handlers
     74  1.1   mycroft  */
     75  1.1   mycroft struct ss_special scanjet_special = {
     76  1.1   mycroft 	scanjet_set_params,
     77  1.1   mycroft 	scanjet_trigger_scanner,
     78  1.1   mycroft 	scanjet_get_params,
     79  1.1   mycroft 	NULL,			/* no special minphys */
     80  1.1   mycroft 	scanjet_read,		/* scsi 6-byte read */
     81  1.1   mycroft 	NULL,			/* no "rewind" code (yet?) */
     82  1.1   mycroft 	NULL,			/* no adf support right now */
     83  1.1   mycroft 	NULL			/* no adf support right now */
     84  1.1   mycroft };
     85  1.1   mycroft 
     86  1.1   mycroft /*
     87  1.1   mycroft  * scanjet_attach: attach special functions to ss
     88  1.1   mycroft  */
     89  1.1   mycroft void
     90  1.1   mycroft scanjet_attach(ss, sa)
     91  1.1   mycroft 	struct ss_softc *ss;
     92  1.1   mycroft 	struct scsibus_attach_args *sa;
     93  1.1   mycroft {
     94  1.3  christos #ifdef SCSIDEBUG
     95  1.1   mycroft 	struct scsi_link *sc_link = sa->sa_sc_link;
     96  1.3  christos #endif
     97  1.5   mycroft 	int error;
     98  1.1   mycroft 
     99  1.1   mycroft 	SC_DEBUG(sc_link, SDEV_DB1, ("scanjet_attach: start\n"));
    100  1.1   mycroft 	ss->sio.scan_scanner_type = 0;
    101  1.1   mycroft 
    102  1.3  christos 	printf("\n%s: ", ss->sc_dev.dv_xname);
    103  1.3  christos 
    104  1.1   mycroft 	/* first, check the model (which determines nothing yet) */
    105  1.1   mycroft 
    106  1.1   mycroft 	if (!bcmp(sa->sa_inqbuf->product, "C1750A", 6)) {
    107  1.1   mycroft 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    108  1.5   mycroft 		printf("HP ScanJet IIc");
    109  1.1   mycroft 	}
    110  1.1   mycroft 	if (!bcmp(sa->sa_inqbuf->product, "C2500A", 6)) {
    111  1.1   mycroft 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    112  1.5   mycroft 		printf("HP ScanJet IIcx");
    113  1.1   mycroft 	}
    114  1.1   mycroft 
    115  1.3  christos 	SC_DEBUG(sc_link, SDEV_DB1, ("scanjet_attach: scanner_type = %d\n",
    116  1.1   mycroft 	    ss->sio.scan_scanner_type));
    117  1.1   mycroft 
    118  1.1   mycroft 	/* now install special handlers */
    119  1.1   mycroft 	ss->special = &scanjet_special;
    120  1.1   mycroft 
    121  1.1   mycroft 	/*
    122  1.1   mycroft 	 * populate the scanio struct with legal values
    123  1.1   mycroft 	 */
    124  1.1   mycroft 	ss->sio.scan_width		= 1200;
    125  1.1   mycroft 	ss->sio.scan_height		= 1200;
    126  1.1   mycroft 	ss->sio.scan_x_resolution	= 100;
    127  1.1   mycroft 	ss->sio.scan_y_resolution	= 100;
    128  1.1   mycroft 	ss->sio.scan_x_origin		= 0;
    129  1.1   mycroft 	ss->sio.scan_y_origin		= 0;
    130  1.3  christos 	ss->sio.scan_brightness		= 128;
    131  1.3  christos 	ss->sio.scan_contrast		= 128;
    132  1.1   mycroft 	ss->sio.scan_quality		= 100;
    133  1.1   mycroft 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
    134  1.1   mycroft 
    135  1.5   mycroft 	error = scanjet_set_window(ss);
    136  1.5   mycroft 	if (error) {
    137  1.5   mycroft 		printf(" set_window failed\n");
    138  1.5   mycroft 		return;
    139  1.5   mycroft 	}
    140  1.5   mycroft 	error = scanjet_compute_sizes(ss);
    141  1.5   mycroft 	if (error) {
    142  1.5   mycroft 		printf(" compute_sizes failed\n");
    143  1.5   mycroft 		return;
    144  1.5   mycroft 	}
    145  1.5   mycroft 
    146  1.5   mycroft 	printf("\n");
    147  1.1   mycroft }
    148  1.1   mycroft 
    149  1.1   mycroft int
    150  1.1   mycroft scanjet_get_params(ss)
    151  1.1   mycroft 	struct ss_softc *ss;
    152  1.1   mycroft {
    153  1.1   mycroft 
    154  1.1   mycroft 	return (0);
    155  1.1   mycroft }
    156  1.1   mycroft 
    157  1.1   mycroft /*
    158  1.1   mycroft  * check the parameters if the scanjet is capable of fulfilling it
    159  1.1   mycroft  * but don't send the command to the scanner in case the user wants
    160  1.1   mycroft  * to change parameters by more than one call
    161  1.1   mycroft  */
    162  1.1   mycroft int
    163  1.1   mycroft scanjet_set_params(ss, sio)
    164  1.1   mycroft 	struct ss_softc *ss;
    165  1.1   mycroft 	struct scan_io *sio;
    166  1.1   mycroft {
    167  1.1   mycroft 	int error;
    168  1.1   mycroft 
    169  1.5   mycroft #if 0
    170  1.1   mycroft 	/*
    171  1.1   mycroft 	 * if the scanner is triggered, then rewind it
    172  1.1   mycroft 	 */
    173  1.1   mycroft 	if (ss->flags & SSF_TRIGGERED) {
    174  1.1   mycroft 		error = scanjet_rewind_scanner(ss);
    175  1.1   mycroft 		if (error)
    176  1.1   mycroft 			return (error);
    177  1.1   mycroft 	}
    178  1.1   mycroft #endif
    179  1.1   mycroft 
    180  1.1   mycroft 	/* size constraints... */
    181  1.1   mycroft 	if (sio->scan_width == 0				 ||
    182  1.1   mycroft 	    sio->scan_x_origin + sio->scan_width > 10200 || /* 8.5" */
    183  1.1   mycroft 	    sio->scan_height == 0				 ||
    184  1.1   mycroft 	    sio->scan_y_origin + sio->scan_height > 16800)  /* 14" */
    185  1.1   mycroft 		return (EINVAL);
    186  1.1   mycroft 
    187  1.1   mycroft 	/* resolution (dpi)... */
    188  1.1   mycroft 	if (sio->scan_x_resolution < 100 ||
    189  1.1   mycroft 	    sio->scan_x_resolution > 400 ||
    190  1.1   mycroft 	    sio->scan_y_resolution < 100 ||
    191  1.1   mycroft 	    sio->scan_y_resolution > 400)
    192  1.1   mycroft 		return (EINVAL);
    193  1.1   mycroft 
    194  1.1   mycroft 	switch (sio->scan_image_mode) {
    195  1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    196  1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    197  1.1   mycroft 	case SIM_GRAYSCALE:
    198  1.1   mycroft 	case SIM_COLOR:
    199  1.1   mycroft 		break;
    200  1.1   mycroft 	default:
    201  1.1   mycroft 		return (EINVAL);
    202  1.1   mycroft 	}
    203  1.1   mycroft 
    204  1.1   mycroft 	/* change ss_softc to the new values, but save ro-variables */
    205  1.1   mycroft 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
    206  1.1   mycroft 	bcopy(sio, &ss->sio, sizeof(struct scan_io));
    207  1.1   mycroft 
    208  1.5   mycroft 	error = scanjet_set_window(ss);
    209  1.5   mycroft 	if (error) {
    210  1.5   mycroft 		uprintf("%s: set_window failed\n", ss->sc_dev.dv_xname);
    211  1.5   mycroft 		return (error);
    212  1.5   mycroft 	}
    213  1.5   mycroft 	error = scanjet_compute_sizes(ss);
    214  1.5   mycroft 	if (error) {
    215  1.5   mycroft 		uprintf("%s: compute_sizes failed\n", ss->sc_dev.dv_xname);
    216  1.5   mycroft 		return (error);
    217  1.5   mycroft 	}
    218  1.1   mycroft 
    219  1.1   mycroft 	return (0);
    220  1.1   mycroft }
    221  1.1   mycroft 
    222  1.1   mycroft /*
    223  1.1   mycroft  * trigger the scanner to start a scan operation
    224  1.1   mycroft  * this includes sending the mode- and window-data,
    225  1.1   mycroft  * and starting the scanner
    226  1.1   mycroft  */
    227  1.1   mycroft int
    228  1.1   mycroft scanjet_trigger_scanner(ss)
    229  1.1   mycroft 	struct ss_softc *ss;
    230  1.1   mycroft {
    231  1.1   mycroft 	char escape_codes[20];
    232  1.1   mycroft 	int error;
    233  1.1   mycroft 
    234  1.1   mycroft 	error = scanjet_set_window(ss);
    235  1.1   mycroft 	if (error) {
    236  1.5   mycroft 		uprintf("%s: set_window failed\n", ss->sc_dev.dv_xname);
    237  1.5   mycroft 		return (error);
    238  1.5   mycroft 	}
    239  1.5   mycroft 	error = scanjet_compute_sizes(ss);
    240  1.5   mycroft 	if (error) {
    241  1.5   mycroft 		uprintf("%s: compute_sizes failed\n", ss->sc_dev.dv_xname);
    242  1.1   mycroft 		return (error);
    243  1.1   mycroft 	}
    244  1.1   mycroft 
    245  1.1   mycroft 	/* send "trigger" operation */
    246  1.1   mycroft 	strcpy(escape_codes, "\033*f0S");
    247  1.5   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0);
    248  1.1   mycroft 	if (error) {
    249  1.5   mycroft 		uprintf("%s: trigger_scanner failed\n", ss->sc_dev.dv_xname);
    250  1.1   mycroft 		return (error);
    251  1.1   mycroft 	}
    252  1.1   mycroft 
    253  1.1   mycroft 	return (0);
    254  1.1   mycroft }
    255  1.1   mycroft 
    256  1.1   mycroft int
    257  1.1   mycroft scanjet_read(ss, bp)
    258  1.1   mycroft 	struct ss_softc *ss;
    259  1.1   mycroft 	struct buf *bp;
    260  1.1   mycroft {
    261  1.1   mycroft 	struct scsi_rw_scanner cmd;
    262  1.1   mycroft 	struct scsi_link *sc_link = ss->sc_link;
    263  1.1   mycroft 
    264  1.1   mycroft 	/*
    265  1.1   mycroft 	 *  Fill out the scsi command
    266  1.1   mycroft 	 */
    267  1.1   mycroft 	bzero(&cmd, sizeof(cmd));
    268  1.1   mycroft 	cmd.opcode = READ;
    269  1.1   mycroft 
    270  1.1   mycroft 	/*
    271  1.1   mycroft 	 * Handle "fixed-block-mode" tape drives by using the
    272  1.1   mycroft 	 * block count instead of the length.
    273  1.1   mycroft 	 */
    274  1.2   mycroft 	_lto3b(bp->b_bcount, cmd.len);
    275  1.1   mycroft 
    276  1.1   mycroft 	/*
    277  1.1   mycroft 	 * go ask the adapter to do all this for us
    278  1.1   mycroft 	 */
    279  1.1   mycroft 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, sizeof(cmd),
    280  1.1   mycroft 	    (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp,
    281  1.1   mycroft 	    SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED)
    282  1.1   mycroft 		printf("%s: not queued\n", ss->sc_dev.dv_xname);
    283  1.1   mycroft 	else {
    284  1.1   mycroft 		ss->sio.scan_window_size -= bp->b_bcount;
    285  1.1   mycroft 		if (ss->sio.scan_window_size < 0)
    286  1.1   mycroft 			ss->sio.scan_window_size = 0;
    287  1.1   mycroft 	}
    288  1.1   mycroft 
    289  1.1   mycroft 	return (0);
    290  1.1   mycroft }
    291  1.1   mycroft 
    292  1.1   mycroft 
    293  1.1   mycroft /*
    294  1.1   mycroft  * Do a synchronous write.  Used to send control messages.
    295  1.1   mycroft  */
    296  1.1   mycroft int
    297  1.5   mycroft scanjet_ctl_write(ss, buf, size, flags)
    298  1.1   mycroft 	struct ss_softc *ss;
    299  1.1   mycroft 	char *buf;
    300  1.1   mycroft 	u_int size;
    301  1.1   mycroft 	int flags;
    302  1.1   mycroft {
    303  1.1   mycroft 	struct scsi_rw_scanner cmd;
    304  1.1   mycroft 
    305  1.1   mycroft 	bzero(&cmd, sizeof(cmd));
    306  1.1   mycroft 	cmd.opcode = WRITE;
    307  1.2   mycroft 	_lto3b(size, cmd.len);
    308  1.1   mycroft 	return (scsi_scsi_cmd(ss->sc_link, (struct scsi_generic *) &cmd,
    309  1.1   mycroft 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
    310  1.1   mycroft 	    flags | SCSI_DATA_OUT));
    311  1.1   mycroft }
    312  1.1   mycroft 
    313  1.5   mycroft 
    314  1.5   mycroft /*
    315  1.5   mycroft  * Do a synchronous read.  Used to read responses to control messages.
    316  1.5   mycroft  */
    317  1.5   mycroft int
    318  1.5   mycroft scanjet_ctl_read(ss, buf, size, flags)
    319  1.5   mycroft 	struct ss_softc *ss;
    320  1.5   mycroft 	char *buf;
    321  1.5   mycroft 	u_int size;
    322  1.5   mycroft 	int flags;
    323  1.5   mycroft {
    324  1.5   mycroft 	struct scsi_rw_scanner cmd;
    325  1.5   mycroft 
    326  1.5   mycroft 	bzero(&cmd, sizeof(cmd));
    327  1.5   mycroft 	cmd.opcode = READ;
    328  1.5   mycroft 	_lto3b(size, cmd.len);
    329  1.5   mycroft 	return (scsi_scsi_cmd(ss->sc_link, (struct scsi_generic *) &cmd,
    330  1.5   mycroft 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
    331  1.5   mycroft 	    flags | SCSI_DATA_IN));
    332  1.5   mycroft }
    333  1.5   mycroft 
    334  1.5   mycroft 
    335  1.1   mycroft #ifdef SCANJETDEBUG
    336  1.1   mycroft static void show_es(char *es)
    337  1.1   mycroft {
    338  1.1   mycroft   char *p = es;
    339  1.1   mycroft   while (*p) {
    340  1.1   mycroft     if (*p == '\033')
    341  1.1   mycroft       printf("[Esc]");
    342  1.1   mycroft     else
    343  1.1   mycroft       printf("%c", *p);
    344  1.1   mycroft     ++p;
    345  1.1   mycroft   }
    346  1.1   mycroft   printf("\n");
    347  1.1   mycroft }
    348  1.1   mycroft #endif
    349  1.1   mycroft 
    350  1.1   mycroft /*
    351  1.1   mycroft  * simulate SCSI_SET_WINDOW for ScanJets
    352  1.1   mycroft  */
    353  1.1   mycroft int
    354  1.1   mycroft scanjet_set_window(ss)
    355  1.1   mycroft 	struct ss_softc *ss;
    356  1.1   mycroft {
    357  1.1   mycroft 	char escape_codes[128], *p;
    358  1.1   mycroft 
    359  1.1   mycroft 	p = escape_codes;
    360  1.1   mycroft 
    361  1.3  christos 	sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4);
    362  1.1   mycroft 	p += strlen(p);
    363  1.3  christos 	sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4);
    364  1.1   mycroft 	p += strlen(p);
    365  1.3  christos 	sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
    366  1.1   mycroft 	p += strlen(p);
    367  1.3  christos 	sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
    368  1.1   mycroft 	p += strlen(p);
    369  1.1   mycroft 	sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution);
    370  1.1   mycroft 	p += strlen(p);
    371  1.1   mycroft 	sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution);
    372  1.1   mycroft 	p += strlen(p);
    373  1.1   mycroft 
    374  1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    375  1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    376  1.5   mycroft 		ss->sio.scan_bits_per_pixel = 1;
    377  1.1   mycroft 		/* use "line art" mode */
    378  1.1   mycroft 		strcpy(p, "\033*a0T");
    379  1.1   mycroft 		p += strlen(p);
    380  1.1   mycroft 		/* make image data be "min-is-white ala PBM */
    381  1.1   mycroft 		strcpy(p, "\033*a0I");
    382  1.1   mycroft 		p += strlen(p);
    383  1.1   mycroft 		break;
    384  1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    385  1.5   mycroft 		ss->sio.scan_bits_per_pixel = 1;
    386  1.1   mycroft 		/* use dithered mode */
    387  1.1   mycroft 		strcpy(p, "\033*a3T");
    388  1.1   mycroft 		p += strlen(p);
    389  1.1   mycroft 		/* make image data be "min-is-white ala PBM */
    390  1.1   mycroft 		strcpy(p, "\033*a0I");
    391  1.1   mycroft 		p += strlen(p);
    392  1.1   mycroft 		break;
    393  1.1   mycroft 	case SIM_GRAYSCALE:
    394  1.5   mycroft 		ss->sio.scan_bits_per_pixel = 8;
    395  1.1   mycroft 		/* use grayscale mode */
    396  1.1   mycroft 		strcpy(p, "\033*a4T");
    397  1.1   mycroft 		p += strlen(p);
    398  1.1   mycroft 		/* make image data be "min-is-black ala PGM */
    399  1.1   mycroft 		strcpy(p, "\033*a1I");
    400  1.1   mycroft 		p += strlen(p);
    401  1.1   mycroft 		break;
    402  1.1   mycroft 	case SIM_COLOR:
    403  1.5   mycroft 		ss->sio.scan_bits_per_pixel = 24;
    404  1.1   mycroft 		/* use RGB color mode */
    405  1.1   mycroft 		strcpy(p, "\033*a5T");
    406  1.1   mycroft 		p += strlen(p);
    407  1.1   mycroft 		/* make image data be "min-is-black ala PPM */
    408  1.1   mycroft 		strcpy(p, "\033*a1I");
    409  1.1   mycroft 		p += strlen(p);
    410  1.1   mycroft 		/* use pass-through matrix (disable NTSC) */
    411  1.1   mycroft 		strcpy(p, "\033*u2T");
    412  1.1   mycroft 		p += strlen(p);
    413  1.5   mycroft 		break;
    414  1.1   mycroft 	}
    415  1.1   mycroft 
    416  1.1   mycroft 	sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
    417  1.1   mycroft 	p += strlen(p);
    418  1.1   mycroft 	sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
    419  1.1   mycroft 	p += strlen(p);
    420  1.1   mycroft 	sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
    421  1.1   mycroft 	p += strlen(p);
    422  1.1   mycroft 
    423  1.5   mycroft 	return (scanjet_ctl_write(ss, escape_codes, p - escape_codes, 0));
    424  1.5   mycroft }
    425  1.5   mycroft 
    426  1.5   mycroft /* atoi() and strchr() are from /sys/arch/amiga/dev/ite.c
    427  1.5   mycroft    and are only used in scanjet_compute_sizes */
    428  1.5   mycroft 
    429  1.6  christos __inline static int
    430  1.5   mycroft atoi(cp)
    431  1.5   mycroft 	const char *cp;
    432  1.5   mycroft {
    433  1.5   mycroft 	int n;
    434  1.5   mycroft 
    435  1.5   mycroft 	for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
    436  1.5   mycroft 		n = n * 10 + *cp - '0';
    437  1.5   mycroft 
    438  1.5   mycroft 	return (n);
    439  1.5   mycroft }
    440  1.5   mycroft 
    441  1.6  christos __inline static char *
    442  1.5   mycroft strchr(cp, ch)
    443  1.5   mycroft 	const char *cp;
    444  1.5   mycroft 	char ch;
    445  1.5   mycroft {
    446  1.5   mycroft 	while (*cp && *cp != ch) cp++;
    447  1.5   mycroft 	return (*cp ? (char *)cp : 0);
    448  1.1   mycroft }
    449  1.1   mycroft 
    450  1.5   mycroft int
    451  1.1   mycroft scanjet_compute_sizes(ss)
    452  1.1   mycroft 	struct ss_softc *ss;
    453  1.1   mycroft {
    454  1.5   mycroft 	int error;
    455  1.5   mycroft 	static char *wfail = "%s: interrogate write failed\n";
    456  1.5   mycroft 	static char *rfail = "%s: interrogate read failed\n";
    457  1.5   mycroft 	static char *dfail = "%s: bad data returned\n";
    458  1.5   mycroft 	char escape_codes[20];
    459  1.5   mycroft 	char response[20];
    460  1.5   mycroft 	char *p;
    461  1.1   mycroft 
    462  1.1   mycroft 	/*
    463  1.1   mycroft 	 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
    464  1.1   mycroft 	 * as its base unit of measurement.  PINT uses 1/1200" (yes I know
    465  1.1   mycroft 	 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
    466  1.1   mycroft 	 */
    467  1.1   mycroft 	ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
    468  1.1   mycroft 	ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
    469  1.1   mycroft 
    470  1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    471  1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    472  1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    473  1.5   mycroft 		strcpy(escape_codes, "\033*s1025E"); /* bytes wide */
    474  1.1   mycroft 		break;
    475  1.1   mycroft 	case SIM_GRAYSCALE:
    476  1.1   mycroft 	case SIM_COLOR:
    477  1.5   mycroft 		strcpy(escape_codes, "\033*s1024E"); /* pixels wide */
    478  1.1   mycroft 		break;
    479  1.1   mycroft 	}
    480  1.5   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0);
    481  1.5   mycroft 	if (error) {
    482  1.5   mycroft 		uprintf(wfail, ss->sc_dev.dv_xname);
    483  1.5   mycroft 		return (error);
    484  1.5   mycroft 	}
    485  1.5   mycroft 	error = scanjet_ctl_read(ss, response, 20, 0);
    486  1.5   mycroft 	if (error) {
    487  1.5   mycroft 		uprintf(rfail, ss->sc_dev.dv_xname);
    488  1.5   mycroft 		return (error);
    489  1.5   mycroft 	}
    490  1.5   mycroft 	p = strchr(response, 'd');
    491  1.5   mycroft 	if (p == 0) {
    492  1.5   mycroft 		uprintf(dfail, ss->sc_dev.dv_xname);
    493  1.5   mycroft 		return (EIO);
    494  1.5   mycroft 	}
    495  1.5   mycroft 	ss->sio.scan_pixels_per_line = atoi(p + 1);
    496  1.5   mycroft 	if (ss->sio.scan_image_mode < SIM_GRAYSCALE)
    497  1.5   mycroft 		ss->sio.scan_pixels_per_line *= 8;
    498  1.1   mycroft 
    499  1.5   mycroft 	strcpy(escape_codes, "\033*s1026E"); /* pixels high */
    500  1.5   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0);
    501  1.5   mycroft 	if (error) {
    502  1.5   mycroft 		uprintf(wfail, ss->sc_dev.dv_xname);
    503  1.5   mycroft 		return (error);
    504  1.5   mycroft 	}
    505  1.5   mycroft 	error = scanjet_ctl_read(ss, response, 20, 0);
    506  1.5   mycroft 	if (error) {
    507  1.5   mycroft 		uprintf(rfail, ss->sc_dev.dv_xname);
    508  1.5   mycroft 		return (error);
    509  1.5   mycroft 	}
    510  1.5   mycroft 	p = strchr(response, 'd');
    511  1.5   mycroft 	if (p == 0) {
    512  1.5   mycroft 		uprintf(dfail, ss->sc_dev.dv_xname);
    513  1.5   mycroft 		return (EIO);
    514  1.5   mycroft 	}
    515  1.5   mycroft 	ss->sio.scan_lines = atoi(p + 1);
    516  1.1   mycroft 
    517  1.1   mycroft 	ss->sio.scan_window_size = ss->sio.scan_lines *
    518  1.1   mycroft 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    519  1.5   mycroft 
    520  1.5   mycroft 	return (0);
    521  1.1   mycroft }
    522