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