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