Home | History | Annotate | Line # | Download | only in scsipi
ss_scanjet.c revision 1.48.4.2
      1  1.48.4.2      yamt /*	$NetBSD: ss_scanjet.c,v 1.48.4.2 2010/03/11 15:04:03 yamt 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.26     lukem 
     36      1.26     lukem #include <sys/cdefs.h>
     37  1.48.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: ss_scanjet.c,v 1.48.4.2 2010/03/11 15:04:03 yamt Exp $");
     38       1.1   mycroft 
     39       1.1   mycroft #include <sys/param.h>
     40       1.1   mycroft #include <sys/systm.h>
     41       1.1   mycroft #include <sys/fcntl.h>
     42       1.1   mycroft #include <sys/errno.h>
     43       1.1   mycroft #include <sys/ioctl.h>
     44       1.1   mycroft #include <sys/malloc.h>
     45       1.1   mycroft #include <sys/buf.h>
     46      1.37      yamt #include <sys/bufq.h>
     47       1.1   mycroft #include <sys/proc.h>
     48       1.1   mycroft #include <sys/device.h>
     49       1.1   mycroft #include <sys/conf.h>		/* for cdevsw */
     50       1.1   mycroft #include <sys/scanio.h>
     51      1.31    bouyer #include <sys/kernel.h>
     52       1.1   mycroft 
     53      1.36   mycroft #include <dev/scsipi/scsipi_all.h>
     54      1.12    bouyer #include <dev/scsipi/scsi_all.h>
     55      1.12    bouyer #include <dev/scsipi/scsi_scanner.h>
     56      1.36   mycroft #include <dev/scsipi/scsipiconf.h>
     57      1.32    bouyer #include <dev/scsipi/scsipi_base.h>
     58      1.12    bouyer #include <dev/scsipi/ssvar.h>
     59       1.1   mycroft 
     60       1.1   mycroft #define SCANJET_RETRIES 4
     61       1.1   mycroft 
     62      1.30   thorpej static int	scanjet_get_params(struct ss_softc *);
     63      1.30   thorpej static int	scanjet_set_params(struct ss_softc *, struct scan_io *);
     64      1.30   thorpej static int	scanjet_trigger_scanner(struct ss_softc *);
     65      1.30   thorpej static int	scanjet_read(struct ss_softc *, struct buf *);
     66       1.1   mycroft 
     67       1.1   mycroft /* only used internally */
     68      1.40   reinoud static int	scanjet_ctl_write(struct ss_softc *, char *, u_int);
     69      1.40   reinoud static int	scanjet_ctl_read(struct ss_softc *, char *, u_int);
     70      1.30   thorpej static int	scanjet_set_window(struct ss_softc *);
     71      1.30   thorpej static int	scanjet_compute_sizes(struct ss_softc *);
     72       1.1   mycroft 
     73       1.1   mycroft /*
     74       1.1   mycroft  * structure for the special handlers
     75       1.1   mycroft  */
     76      1.30   thorpej static struct ss_special scanjet_special = {
     77       1.1   mycroft 	scanjet_set_params,
     78       1.1   mycroft 	scanjet_trigger_scanner,
     79       1.1   mycroft 	scanjet_get_params,
     80       1.1   mycroft 	NULL,			/* no special minphys */
     81       1.1   mycroft 	scanjet_read,		/* scsi 6-byte read */
     82       1.1   mycroft 	NULL,			/* no "rewind" code (yet?) */
     83       1.1   mycroft 	NULL,			/* no adf support right now */
     84       1.1   mycroft 	NULL			/* no adf support right now */
     85       1.1   mycroft };
     86       1.1   mycroft 
     87       1.1   mycroft /*
     88       1.1   mycroft  * scanjet_attach: attach special functions to ss
     89       1.1   mycroft  */
     90       1.1   mycroft void
     91      1.30   thorpej scanjet_attach(struct ss_softc *ss, struct scsipibus_attach_args *sa)
     92       1.1   mycroft {
     93       1.5   mycroft 	int error;
     94       1.1   mycroft 
     95      1.22    bouyer 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("scanjet_attach: start\n"));
     96       1.1   mycroft 	ss->sio.scan_scanner_type = 0;
     97       1.1   mycroft 
     98      1.48    cegger 	printf("%s: ", device_xname(&ss->sc_dev));
     99       1.3  christos 
    100       1.1   mycroft 	/* first, check the model (which determines nothing yet) */
    101       1.1   mycroft 
    102      1.25   thorpej 	if (!memcmp(sa->sa_inqbuf.product, "C1750A", 6)) {
    103       1.1   mycroft 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    104       1.8  christos 		printf("HP ScanJet IIc");
    105      1.28       chs 	} else if (!memcmp(sa->sa_inqbuf.product, "C2500A", 6)) {
    106       1.1   mycroft 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    107       1.8  christos 		printf("HP ScanJet IIcx");
    108      1.28       chs 	} else if (!memcmp(sa->sa_inqbuf.product, "C2520A", 6)) {
    109      1.28       chs 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    110      1.28       chs 		printf("HP ScanJet 4c");
    111      1.28       chs 	} else if (!memcmp(sa->sa_inqbuf.product, "C1130A", 6)) {
    112       1.9   thorpej 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    113       1.9   thorpej 		printf("HP ScanJet 4p");
    114      1.28       chs 	} else if (!memcmp(sa->sa_inqbuf.product, "C5110A", 6)) {
    115      1.15  augustss 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    116      1.15  augustss 		printf("HP ScanJet 5p");
    117      1.28       chs 	} else {
    118      1.28       chs 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    119      1.28       chs 		printf("HP ScanJet (unknown model)");
    120       1.1   mycroft 	}
    121       1.1   mycroft 
    122      1.22    bouyer 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
    123      1.22    bouyer 	    ("scanjet_attach: scanner_type = %d\n",
    124       1.1   mycroft 	    ss->sio.scan_scanner_type));
    125       1.1   mycroft 
    126       1.1   mycroft 	/* now install special handlers */
    127       1.1   mycroft 	ss->special = &scanjet_special;
    128       1.1   mycroft 
    129       1.1   mycroft 	/*
    130       1.1   mycroft 	 * populate the scanio struct with legal values
    131       1.1   mycroft 	 */
    132       1.1   mycroft 	ss->sio.scan_width		= 1200;
    133       1.1   mycroft 	ss->sio.scan_height		= 1200;
    134       1.1   mycroft 	ss->sio.scan_x_resolution	= 100;
    135       1.1   mycroft 	ss->sio.scan_y_resolution	= 100;
    136       1.1   mycroft 	ss->sio.scan_x_origin		= 0;
    137       1.1   mycroft 	ss->sio.scan_y_origin		= 0;
    138       1.3  christos 	ss->sio.scan_brightness		= 128;
    139       1.3  christos 	ss->sio.scan_contrast		= 128;
    140       1.1   mycroft 	ss->sio.scan_quality		= 100;
    141       1.1   mycroft 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
    142       1.1   mycroft 
    143       1.5   mycroft 	error = scanjet_set_window(ss);
    144       1.5   mycroft 	if (error) {
    145       1.8  christos 		printf(" set_window failed\n");
    146       1.5   mycroft 		return;
    147       1.5   mycroft 	}
    148       1.5   mycroft 	error = scanjet_compute_sizes(ss);
    149       1.5   mycroft 	if (error) {
    150       1.8  christos 		printf(" compute_sizes failed\n");
    151       1.5   mycroft 		return;
    152       1.5   mycroft 	}
    153       1.5   mycroft 
    154       1.8  christos 	printf("\n");
    155       1.1   mycroft }
    156       1.1   mycroft 
    157      1.30   thorpej static int
    158      1.47  christos scanjet_get_params(struct ss_softc *ss)
    159       1.1   mycroft {
    160       1.1   mycroft 
    161       1.1   mycroft 	return (0);
    162       1.1   mycroft }
    163       1.1   mycroft 
    164       1.1   mycroft /*
    165       1.1   mycroft  * check the parameters if the scanjet is capable of fulfilling it
    166       1.1   mycroft  * but don't send the command to the scanner in case the user wants
    167       1.1   mycroft  * to change parameters by more than one call
    168       1.1   mycroft  */
    169      1.30   thorpej static int
    170      1.30   thorpej scanjet_set_params(struct ss_softc *ss, struct scan_io *sio)
    171       1.1   mycroft {
    172       1.1   mycroft 	int error;
    173       1.1   mycroft 
    174       1.5   mycroft #if 0
    175       1.1   mycroft 	/*
    176       1.1   mycroft 	 * if the scanner is triggered, then rewind it
    177       1.1   mycroft 	 */
    178       1.1   mycroft 	if (ss->flags & SSF_TRIGGERED) {
    179       1.1   mycroft 		error = scanjet_rewind_scanner(ss);
    180       1.1   mycroft 		if (error)
    181       1.1   mycroft 			return (error);
    182       1.1   mycroft 	}
    183       1.1   mycroft #endif
    184       1.1   mycroft 
    185       1.1   mycroft 	/* size constraints... */
    186       1.1   mycroft 	if (sio->scan_width == 0				 ||
    187       1.1   mycroft 	    sio->scan_x_origin + sio->scan_width > 10200 || /* 8.5" */
    188       1.1   mycroft 	    sio->scan_height == 0				 ||
    189       1.1   mycroft 	    sio->scan_y_origin + sio->scan_height > 16800)  /* 14" */
    190       1.1   mycroft 		return (EINVAL);
    191       1.1   mycroft 
    192       1.1   mycroft 	/* resolution (dpi)... */
    193       1.1   mycroft 	if (sio->scan_x_resolution < 100 ||
    194       1.1   mycroft 	    sio->scan_x_resolution > 400 ||
    195       1.1   mycroft 	    sio->scan_y_resolution < 100 ||
    196       1.1   mycroft 	    sio->scan_y_resolution > 400)
    197       1.1   mycroft 		return (EINVAL);
    198       1.1   mycroft 
    199       1.1   mycroft 	switch (sio->scan_image_mode) {
    200       1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    201       1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    202       1.1   mycroft 	case SIM_GRAYSCALE:
    203       1.1   mycroft 	case SIM_COLOR:
    204       1.1   mycroft 		break;
    205       1.1   mycroft 	default:
    206       1.1   mycroft 		return (EINVAL);
    207       1.1   mycroft 	}
    208       1.1   mycroft 
    209       1.1   mycroft 	/* change ss_softc to the new values, but save ro-variables */
    210       1.1   mycroft 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
    211      1.24   thorpej 	memcpy(&ss->sio, sio, sizeof(struct scan_io));
    212       1.1   mycroft 
    213       1.5   mycroft 	error = scanjet_set_window(ss);
    214       1.5   mycroft 	if (error) {
    215      1.48    cegger 		uprintf("%s: set_window failed\n", device_xname(&ss->sc_dev));
    216       1.5   mycroft 		return (error);
    217       1.5   mycroft 	}
    218       1.5   mycroft 	error = scanjet_compute_sizes(ss);
    219       1.5   mycroft 	if (error) {
    220      1.48    cegger 		uprintf("%s: compute_sizes failed\n", device_xname(&ss->sc_dev));
    221       1.5   mycroft 		return (error);
    222       1.5   mycroft 	}
    223       1.1   mycroft 
    224       1.1   mycroft 	return (0);
    225       1.1   mycroft }
    226       1.1   mycroft 
    227       1.1   mycroft /*
    228       1.1   mycroft  * trigger the scanner to start a scan operation
    229       1.1   mycroft  * this includes sending the mode- and window-data,
    230       1.1   mycroft  * and starting the scanner
    231       1.1   mycroft  */
    232      1.30   thorpej static int
    233      1.30   thorpej scanjet_trigger_scanner(struct ss_softc *ss)
    234       1.1   mycroft {
    235       1.1   mycroft 	char escape_codes[20];
    236       1.1   mycroft 	int error;
    237       1.1   mycroft 
    238       1.1   mycroft 	error = scanjet_set_window(ss);
    239       1.1   mycroft 	if (error) {
    240      1.48    cegger 		uprintf("%s: set_window failed\n", device_xname(&ss->sc_dev));
    241       1.5   mycroft 		return (error);
    242       1.5   mycroft 	}
    243       1.5   mycroft 	error = scanjet_compute_sizes(ss);
    244       1.5   mycroft 	if (error) {
    245      1.48    cegger 		uprintf("%s: compute_sizes failed\n", device_xname(&ss->sc_dev));
    246       1.1   mycroft 		return (error);
    247       1.1   mycroft 	}
    248       1.1   mycroft 
    249       1.1   mycroft 	/* send "trigger" operation */
    250      1.29    itojun 	strlcpy(escape_codes, "\033*f0S", sizeof(escape_codes));
    251      1.17   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
    252       1.1   mycroft 	if (error) {
    253      1.48    cegger 		uprintf("%s: trigger_scanner failed\n", device_xname(&ss->sc_dev));
    254       1.1   mycroft 		return (error);
    255       1.1   mycroft 	}
    256      1.41     perry 
    257       1.1   mycroft 	return (0);
    258       1.1   mycroft }
    259       1.1   mycroft 
    260      1.30   thorpej static int
    261      1.30   thorpej scanjet_read(struct ss_softc *ss, struct buf *bp)
    262       1.1   mycroft {
    263       1.1   mycroft 	struct scsi_rw_scanner cmd;
    264      1.32    bouyer 	struct scsipi_xfer *xs;
    265      1.22    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    266      1.17   mycroft 	int error;
    267       1.1   mycroft 
    268       1.1   mycroft 	/*
    269       1.1   mycroft 	 *  Fill out the scsi command
    270       1.1   mycroft 	 */
    271      1.23   thorpej 	memset(&cmd, 0, sizeof(cmd));
    272       1.1   mycroft 	cmd.opcode = READ;
    273       1.1   mycroft 
    274       1.1   mycroft 	/*
    275       1.1   mycroft 	 * Handle "fixed-block-mode" tape drives by using the
    276       1.1   mycroft 	 * block count instead of the length.
    277       1.1   mycroft 	 */
    278       1.2   mycroft 	_lto3b(bp->b_bcount, cmd.len);
    279       1.1   mycroft 
    280       1.1   mycroft 	/*
    281       1.1   mycroft 	 * go ask the adapter to do all this for us
    282       1.1   mycroft 	 */
    283      1.32    bouyer 	xs = scsipi_make_xs(periph,
    284      1.13     enami 	    (struct scsipi_generic *) &cmd, sizeof(cmd),
    285      1.40   reinoud 	    (u_char *) bp->b_data, bp->b_bcount,
    286      1.32    bouyer 	    SCANJET_RETRIES, 100000, bp,
    287      1.18   thorpej 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
    288      1.32    bouyer 	if (xs == NULL) {
    289      1.32    bouyer 		/*
    290      1.32    bouyer 		 * out of memory. Keep this buffer in the queue, and
    291      1.32    bouyer 		 * retry later.
    292      1.32    bouyer 		 */
    293      1.32    bouyer 		callout_reset(&ss->sc_callout, hz / 2, ssrestart,
    294      1.32    bouyer 		    periph);
    295      1.32    bouyer 		return(0);
    296       1.1   mycroft 	}
    297      1.31    bouyer #ifdef DIAGNOSTIC
    298  1.48.4.1      yamt 	if (bufq_get(ss->buf_queue) != bp)
    299      1.31    bouyer 		panic("ssstart(): dequeued wrong buf");
    300      1.31    bouyer #else
    301  1.48.4.1      yamt 	bufq_get(ss->buf_queue);
    302      1.31    bouyer #endif
    303      1.33   mycroft 	error = scsipi_execute_xs(xs);
    304      1.32    bouyer 	/* with a scsipi_xfer preallocated, scsipi_command can't fail */
    305      1.32    bouyer 	KASSERT(error == 0);
    306      1.32    bouyer 	ss->sio.scan_window_size -= bp->b_bcount;
    307      1.45  christos #if 0
    308      1.32    bouyer 	if (ss->sio.scan_window_size < 0)
    309      1.32    bouyer 		ss->sio.scan_window_size = 0;
    310      1.45  christos #endif
    311       1.1   mycroft 	return (0);
    312       1.1   mycroft }
    313       1.1   mycroft 
    314       1.1   mycroft 
    315       1.1   mycroft /*
    316       1.1   mycroft  * Do a synchronous write.  Used to send control messages.
    317       1.1   mycroft  */
    318      1.41     perry static int
    319      1.42  christos scanjet_ctl_write(struct ss_softc *ss, char *tbuf, u_int size)
    320       1.1   mycroft {
    321       1.1   mycroft 	struct scsi_rw_scanner cmd;
    322      1.17   mycroft 	int flags;
    323       1.1   mycroft 
    324      1.17   mycroft 	flags = 0;
    325      1.16        pk 	if ((ss->flags & SSF_AUTOCONF) != 0)
    326      1.18   thorpej 		flags |= XS_CTL_DISCOVERY;
    327      1.16        pk 
    328      1.23   thorpej 	memset(&cmd, 0, sizeof(cmd));
    329       1.1   mycroft 	cmd.opcode = WRITE;
    330       1.2   mycroft 	_lto3b(size, cmd.len);
    331      1.35   mycroft 
    332      1.34   mycroft 	return (scsipi_command(ss->sc_periph,
    333      1.42  christos 	    (void *)&cmd, sizeof(cmd), (void *)tbuf, size, 0, 100000, NULL,
    334  1.48.4.2      yamt 	    flags | XS_CTL_DATA_OUT));
    335       1.1   mycroft }
    336       1.1   mycroft 
    337       1.5   mycroft 
    338       1.5   mycroft /*
    339       1.5   mycroft  * Do a synchronous read.  Used to read responses to control messages.
    340       1.5   mycroft  */
    341      1.30   thorpej static int
    342      1.42  christos scanjet_ctl_read(struct ss_softc *ss, char *tbuf, u_int size)
    343       1.5   mycroft {
    344       1.5   mycroft 	struct scsi_rw_scanner cmd;
    345      1.17   mycroft 	int flags;
    346      1.16        pk 
    347      1.17   mycroft 	flags = 0;
    348      1.16        pk 	if ((ss->flags & SSF_AUTOCONF) != 0)
    349      1.18   thorpej 		flags |= XS_CTL_DISCOVERY;
    350       1.5   mycroft 
    351      1.23   thorpej 	memset(&cmd, 0, sizeof(cmd));
    352       1.5   mycroft 	cmd.opcode = READ;
    353       1.5   mycroft 	_lto3b(size, cmd.len);
    354      1.35   mycroft 
    355      1.34   mycroft 	return (scsipi_command(ss->sc_periph,
    356      1.42  christos 	    (void *)&cmd, sizeof(cmd), (void *)tbuf, size, 0, 100000, NULL,
    357  1.48.4.2      yamt 	    flags | XS_CTL_DATA_IN));
    358       1.5   mycroft }
    359       1.5   mycroft 
    360       1.5   mycroft 
    361       1.1   mycroft #ifdef SCANJETDEBUG
    362      1.30   thorpej static void
    363      1.30   thorpej show_es(char *es)
    364       1.1   mycroft {
    365      1.13     enami 	char *p = es;
    366      1.13     enami 	while (*p) {
    367      1.13     enami 		if (*p == '\033')
    368      1.13     enami 			printf("[Esc]");
    369      1.13     enami 		else
    370      1.13     enami 			printf("%c", *p);
    371      1.13     enami 		++p;
    372      1.13     enami 	}
    373      1.13     enami 	printf("\n");
    374       1.1   mycroft }
    375       1.1   mycroft #endif
    376       1.1   mycroft 
    377      1.41     perry /*
    378       1.1   mycroft  * simulate SCSI_SET_WINDOW for ScanJets
    379       1.1   mycroft  */
    380      1.30   thorpej static int
    381      1.30   thorpej scanjet_set_window(struct ss_softc *ss)
    382       1.1   mycroft {
    383      1.29    itojun 	char escape_codes[128], *p, *ep;
    384       1.1   mycroft 
    385       1.1   mycroft 	p = escape_codes;
    386      1.29    itojun 	ep = &escape_codes[128];
    387       1.1   mycroft 
    388      1.29    itojun 	p += snprintf(p, ep - p, "\033*f%ldP", ss->sio.scan_width / 4);
    389      1.29    itojun 	p += snprintf(p, ep - p, "\033*f%ldQ", ss->sio.scan_height / 4);
    390      1.29    itojun 	p += snprintf(p, ep - p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
    391      1.29    itojun 	p += snprintf(p, ep - p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
    392      1.29    itojun 	p += snprintf(p, ep - p, "\033*a%dR", ss->sio.scan_x_resolution);
    393      1.29    itojun 	p += snprintf(p, ep - p, "\033*a%dS", ss->sio.scan_y_resolution);
    394      1.41     perry 
    395       1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    396       1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    397       1.5   mycroft 		ss->sio.scan_bits_per_pixel = 1;
    398       1.1   mycroft 		/* use "line art" mode */
    399      1.29    itojun 		strlcpy(p, "\033*a0T", ep - p);
    400       1.1   mycroft 		p += strlen(p);
    401       1.1   mycroft 		/* make image data be "min-is-white ala PBM */
    402      1.29    itojun 		strlcpy(p, "\033*a0I", ep - p);
    403       1.1   mycroft 		p += strlen(p);
    404       1.1   mycroft 		break;
    405       1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    406       1.5   mycroft 		ss->sio.scan_bits_per_pixel = 1;
    407       1.1   mycroft 		/* use dithered mode */
    408      1.29    itojun 		strlcpy(p, "\033*a3T", ep - p);
    409       1.1   mycroft 		p += strlen(p);
    410       1.1   mycroft 		/* make image data be "min-is-white ala PBM */
    411      1.29    itojun 		strlcpy(p, "\033*a0I", ep - p);
    412       1.1   mycroft 		p += strlen(p);
    413       1.1   mycroft 		break;
    414       1.1   mycroft 	case SIM_GRAYSCALE:
    415       1.5   mycroft 		ss->sio.scan_bits_per_pixel = 8;
    416       1.1   mycroft 		/* use grayscale mode */
    417      1.29    itojun 		strlcpy(p, "\033*a4T", ep - p);
    418       1.1   mycroft 		p += strlen(p);
    419       1.1   mycroft 		/* make image data be "min-is-black ala PGM */
    420      1.29    itojun 		strlcpy(p, "\033*a1I", ep - p);
    421       1.1   mycroft 		p += strlen(p);
    422       1.1   mycroft 		break;
    423       1.1   mycroft 	case SIM_COLOR:
    424       1.5   mycroft 		ss->sio.scan_bits_per_pixel = 24;
    425       1.1   mycroft 		/* use RGB color mode */
    426      1.29    itojun 		strlcpy(p, "\033*a5T", ep - p);
    427       1.1   mycroft 		p += strlen(p);
    428       1.1   mycroft 		/* make image data be "min-is-black ala PPM */
    429      1.29    itojun 		strlcpy(p, "\033*a1I", ep - p);
    430       1.1   mycroft 		p += strlen(p);
    431       1.1   mycroft 		/* use pass-through matrix (disable NTSC) */
    432      1.29    itojun 		strlcpy(p, "\033*u2T", ep - p);
    433       1.1   mycroft 		p += strlen(p);
    434       1.5   mycroft 		break;
    435       1.1   mycroft 	}
    436       1.1   mycroft 
    437      1.29    itojun 	p += snprintf(p, ep - p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
    438      1.29    itojun 	p += snprintf(p, ep - p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
    439      1.29    itojun 	p += snprintf(p, ep - p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
    440       1.1   mycroft 
    441      1.17   mycroft 	return (scanjet_ctl_write(ss, escape_codes, p - escape_codes));
    442       1.5   mycroft }
    443       1.5   mycroft 
    444      1.30   thorpej static int
    445      1.30   thorpej scanjet_compute_sizes(struct ss_softc *ss)
    446       1.1   mycroft {
    447       1.5   mycroft 	int error;
    448      1.22    bouyer 	static const char *wfail = "%s: interrogate write failed\n";
    449      1.22    bouyer 	static const char *rfail = "%s: interrogate read failed\n";
    450      1.22    bouyer 	static const char *dfail = "%s: bad data returned\n";
    451       1.5   mycroft 	char escape_codes[20];
    452       1.5   mycroft 	char response[20];
    453       1.5   mycroft 	char *p;
    454       1.1   mycroft 
    455       1.1   mycroft 	/*
    456       1.1   mycroft 	 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
    457       1.1   mycroft 	 * as its base unit of measurement.  PINT uses 1/1200" (yes I know
    458       1.1   mycroft 	 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
    459       1.1   mycroft 	 */
    460       1.1   mycroft 	ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
    461       1.1   mycroft 	ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
    462       1.1   mycroft 
    463       1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    464       1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    465       1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    466      1.29    itojun 		/* bytes wide */
    467      1.29    itojun 		strlcpy(escape_codes, "\033*s1025E", sizeof(escape_codes));
    468       1.1   mycroft 		break;
    469       1.1   mycroft 	case SIM_GRAYSCALE:
    470       1.1   mycroft 	case SIM_COLOR:
    471      1.29    itojun 		/* pixels wide */
    472      1.29    itojun 		strlcpy(escape_codes, "\033*s1024E", sizeof(escape_codes));
    473       1.1   mycroft 		break;
    474       1.1   mycroft 	}
    475      1.17   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
    476       1.5   mycroft 	if (error) {
    477      1.48    cegger 		uprintf(wfail, device_xname(&ss->sc_dev));
    478       1.5   mycroft 		return (error);
    479       1.5   mycroft 	}
    480      1.17   mycroft 	error = scanjet_ctl_read(ss, response, 20);
    481       1.5   mycroft 	if (error) {
    482      1.48    cegger 		uprintf(rfail, device_xname(&ss->sc_dev));
    483       1.5   mycroft 		return (error);
    484       1.5   mycroft 	}
    485       1.5   mycroft 	p = strchr(response, 'd');
    486       1.5   mycroft 	if (p == 0) {
    487      1.48    cegger 		uprintf(dfail, device_xname(&ss->sc_dev));
    488       1.5   mycroft 		return (EIO);
    489       1.5   mycroft 	}
    490      1.28       chs 	ss->sio.scan_pixels_per_line = strtoul(p + 1, NULL, 10);
    491       1.5   mycroft 	if (ss->sio.scan_image_mode < SIM_GRAYSCALE)
    492       1.5   mycroft 		ss->sio.scan_pixels_per_line *= 8;
    493       1.1   mycroft 
    494      1.29    itojun 	/* pixels high */
    495      1.29    itojun 	strlcpy(escape_codes, "\033*s1026E", sizeof(escape_codes));
    496      1.17   mycroft 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
    497       1.5   mycroft 	if (error) {
    498      1.48    cegger 		uprintf(wfail, device_xname(&ss->sc_dev));
    499       1.5   mycroft 		return (error);
    500       1.5   mycroft 	}
    501      1.17   mycroft 	error = scanjet_ctl_read(ss, response, 20);
    502       1.5   mycroft 	if (error) {
    503      1.48    cegger 		uprintf(rfail, device_xname(&ss->sc_dev));
    504       1.5   mycroft 		return (error);
    505       1.5   mycroft 	}
    506       1.5   mycroft 	p = strchr(response, 'd');
    507       1.5   mycroft 	if (p == 0) {
    508      1.48    cegger 		uprintf(dfail, device_xname(&ss->sc_dev));
    509       1.5   mycroft 		return (EIO);
    510       1.5   mycroft 	}
    511      1.28       chs 	ss->sio.scan_lines = strtoul(p + 1, NULL, 10);
    512       1.1   mycroft 
    513       1.1   mycroft 	ss->sio.scan_window_size = ss->sio.scan_lines *
    514       1.1   mycroft 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    515       1.5   mycroft 
    516       1.5   mycroft 	return (0);
    517       1.1   mycroft }
    518