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