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