Home | History | Annotate | Line # | Download | only in scsipi
ss_scanjet.c revision 1.3
      1 /*	$NetBSD: ss_scanjet.c,v 1.3 1996/03/30 21:47:07 christos 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/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/fcntl.h>
     40 #include <sys/errno.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/malloc.h>
     43 #include <sys/buf.h>
     44 #include <sys/proc.h>
     45 #include <sys/user.h>
     46 #include <sys/device.h>
     47 #include <sys/conf.h>		/* for cdevsw */
     48 #include <sys/scanio.h>
     49 
     50 #include <scsi/scsi_all.h>
     51 #include <scsi/scsi_scanner.h>
     52 #include <scsi/scsiconf.h>
     53 #include <scsi/ssvar.h>
     54 
     55 #define SCANJET_RETRIES 4
     56 
     57 int scanjet_get_params __P((struct ss_softc *));
     58 int scanjet_set_params __P((struct ss_softc *, struct scan_io *));
     59 int scanjet_trigger_scanner __P((struct ss_softc *));
     60 int scanjet_read __P((struct ss_softc *, struct buf *));
     61 
     62 /* only used internally */
     63 int scanjet_write __P((struct ss_softc *ss, char *buf, u_int size, int flags));
     64 int scanjet_set_window __P((struct ss_softc *ss));
     65 void scanjet_compute_sizes __P((struct ss_softc *));
     66 
     67 /*
     68  * structure for the special handlers
     69  */
     70 struct ss_special scanjet_special = {
     71 	scanjet_set_params,
     72 	scanjet_trigger_scanner,
     73 	scanjet_get_params,
     74 	NULL,			/* no special minphys */
     75 	scanjet_read,		/* scsi 6-byte read */
     76 	NULL,			/* no "rewind" code (yet?) */
     77 	NULL,			/* no adf support right now */
     78 	NULL			/* no adf support right now */
     79 };
     80 
     81 /*
     82  * scanjet_attach: attach special functions to ss
     83  */
     84 void
     85 scanjet_attach(ss, sa)
     86 	struct ss_softc *ss;
     87 	struct scsibus_attach_args *sa;
     88 {
     89 #ifdef SCSIDEBUG
     90 	struct scsi_link *sc_link = sa->sa_sc_link;
     91 #endif
     92 
     93 	SC_DEBUG(sc_link, SDEV_DB1, ("scanjet_attach: start\n"));
     94 	ss->sio.scan_scanner_type = 0;
     95 
     96 	printf("\n%s: ", ss->sc_dev.dv_xname);
     97 
     98 	/* first, check the model (which determines nothing yet) */
     99 
    100 	if (!bcmp(sa->sa_inqbuf->product, "C1750A", 6)) {
    101 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    102 		printf("HP ScanJet IIc\n");
    103 	}
    104 	if (!bcmp(sa->sa_inqbuf->product, "C2500A", 6)) {
    105 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
    106 		printf("HP ScanJet IIcx\n");
    107 	}
    108 
    109 	SC_DEBUG(sc_link, SDEV_DB1, ("scanjet_attach: scanner_type = %d\n",
    110 	    ss->sio.scan_scanner_type));
    111 
    112 	/* now install special handlers */
    113 	ss->special = &scanjet_special;
    114 
    115 	/*
    116 	 * populate the scanio struct with legal values
    117 	 */
    118 	ss->sio.scan_width		= 1200;
    119 	ss->sio.scan_height		= 1200;
    120 	ss->sio.scan_x_resolution	= 100;
    121 	ss->sio.scan_y_resolution	= 100;
    122 	ss->sio.scan_x_origin		= 0;
    123 	ss->sio.scan_y_origin		= 0;
    124 	ss->sio.scan_brightness		= 128;
    125 	ss->sio.scan_contrast		= 128;
    126 	ss->sio.scan_quality		= 100;
    127 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
    128 
    129 	scanjet_compute_sizes(ss);
    130 }
    131 
    132 int
    133 scanjet_get_params(ss)
    134 	struct ss_softc *ss;
    135 {
    136 
    137 	return (0);
    138 }
    139 
    140 /*
    141  * check the parameters if the scanjet is capable of fulfilling it
    142  * but don't send the command to the scanner in case the user wants
    143  * to change parameters by more than one call
    144  */
    145 int
    146 scanjet_set_params(ss, sio)
    147 	struct ss_softc *ss;
    148 	struct scan_io *sio;
    149 {
    150 #if 0
    151 	int error;
    152 
    153 	/*
    154 	 * if the scanner is triggered, then rewind it
    155 	 */
    156 	if (ss->flags & SSF_TRIGGERED) {
    157 		error = scanjet_rewind_scanner(ss);
    158 		if (error)
    159 			return (error);
    160 	}
    161 #endif
    162 
    163 	/* size constraints... */
    164 	if (sio->scan_width == 0				 ||
    165 	    sio->scan_x_origin + sio->scan_width > 10200 || /* 8.5" */
    166 	    sio->scan_height == 0				 ||
    167 	    sio->scan_y_origin + sio->scan_height > 16800)  /* 14" */
    168 		return (EINVAL);
    169 
    170 	/* resolution (dpi)... */
    171 	if (sio->scan_x_resolution < 100 ||
    172 	    sio->scan_x_resolution > 400 ||
    173 	    sio->scan_y_resolution < 100 ||
    174 	    sio->scan_y_resolution > 400)
    175 		return (EINVAL);
    176 
    177 	switch (sio->scan_image_mode) {
    178 	case SIM_BINARY_MONOCHROME:
    179 	case SIM_DITHERED_MONOCHROME:
    180 	case SIM_GRAYSCALE:
    181 	case SIM_COLOR:
    182 		break;
    183 	default:
    184 		return (EINVAL);
    185 	}
    186 
    187 	/* change ss_softc to the new values, but save ro-variables */
    188 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
    189 	bcopy(sio, &ss->sio, sizeof(struct scan_io));
    190 
    191 	scanjet_compute_sizes(ss);
    192 
    193 	return (0);
    194 }
    195 
    196 /*
    197  * trigger the scanner to start a scan operation
    198  * this includes sending the mode- and window-data,
    199  * and starting the scanner
    200  */
    201 int
    202 scanjet_trigger_scanner(ss)
    203 	struct ss_softc *ss;
    204 {
    205 	char escape_codes[20];
    206 #ifdef SCSIDEBUG
    207 	struct scsi_link *sc_link = ss->sc_link;
    208 #endif
    209 	int error;
    210 
    211 	scanjet_compute_sizes(ss);
    212 
    213 	/* send parameters */
    214 	error = scanjet_set_window(ss);
    215 	if (error) {
    216 		uprintf("set window failed\n");
    217 		return (error);
    218 	}
    219 
    220 	/* send "trigger" operation */
    221 	strcpy(escape_codes, "\033*f0S");
    222 	error = scanjet_write(ss, escape_codes, strlen(escape_codes), 0);
    223 	if (error) {
    224 		uprintf("trigger failed\n");
    225 		return (error);
    226 	}
    227 
    228 	return (0);
    229 }
    230 
    231 int
    232 scanjet_read(ss, bp)
    233 	struct ss_softc *ss;
    234 	struct buf *bp;
    235 {
    236 	struct scsi_rw_scanner cmd;
    237 	struct scsi_link *sc_link = ss->sc_link;
    238 
    239 	/*
    240 	 *  Fill out the scsi command
    241 	 */
    242 	bzero(&cmd, sizeof(cmd));
    243 	cmd.opcode = READ;
    244 
    245 	/*
    246 	 * Handle "fixed-block-mode" tape drives by using the
    247 	 * block count instead of the length.
    248 	 */
    249 	_lto3b(bp->b_bcount, cmd.len);
    250 
    251 	/*
    252 	 * go ask the adapter to do all this for us
    253 	 */
    254 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, sizeof(cmd),
    255 	    (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp,
    256 	    SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED)
    257 		printf("%s: not queued\n", ss->sc_dev.dv_xname);
    258 	else {
    259 		ss->sio.scan_window_size -= bp->b_bcount;
    260 		if (ss->sio.scan_window_size < 0)
    261 			ss->sio.scan_window_size = 0;
    262 	}
    263 
    264 	return (0);
    265 }
    266 
    267 
    268 /*
    269  * Do a synchronous write.  Used to send control messages.
    270  */
    271 int
    272 scanjet_write(ss, buf, size, flags)
    273 	struct ss_softc *ss;
    274 	char *buf;
    275 	u_int size;
    276 	int flags;
    277 {
    278 	struct scsi_rw_scanner cmd;
    279 
    280 	/*
    281 	 * If it's a null transfer, return immediatly
    282 	 */
    283 	if (size == 0)
    284 		return (0);
    285 	bzero(&cmd, sizeof(cmd));
    286 	cmd.opcode = WRITE;
    287 	_lto3b(size, cmd.len);
    288 	return (scsi_scsi_cmd(ss->sc_link, (struct scsi_generic *) &cmd,
    289 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
    290 	    flags | SCSI_DATA_OUT));
    291 }
    292 
    293 #ifdef SCANJETDEBUG
    294 static void show_es(char *es)
    295 {
    296   char *p = es;
    297   while (*p) {
    298     if (*p == '\033')
    299       printf("[Esc]");
    300     else
    301       printf("%c", *p);
    302     ++p;
    303   }
    304   printf("\n");
    305 }
    306 #endif
    307 
    308 /*
    309  * simulate SCSI_SET_WINDOW for ScanJets
    310  */
    311 int
    312 scanjet_set_window(ss)
    313 	struct ss_softc *ss;
    314 {
    315 	char escape_codes[128], *p;
    316 
    317 	p = escape_codes;
    318 
    319 	sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4);
    320 	p += strlen(p);
    321 	sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4);
    322 	p += strlen(p);
    323 	sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
    324 	p += strlen(p);
    325 	sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
    326 	p += strlen(p);
    327 	sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution);
    328 	p += strlen(p);
    329 	sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution);
    330 	p += strlen(p);
    331 
    332 	switch (ss->sio.scan_image_mode) {
    333 	case SIM_BINARY_MONOCHROME:
    334 		/* use "line art" mode */
    335 		strcpy(p, "\033*a0T");
    336 		p += strlen(p);
    337 		/* make image data be "min-is-white ala PBM */
    338 		strcpy(p, "\033*a0I");
    339 		p += strlen(p);
    340 		break;
    341 	case SIM_DITHERED_MONOCHROME:
    342 		/* use dithered mode */
    343 		strcpy(p, "\033*a3T");
    344 		p += strlen(p);
    345 		/* make image data be "min-is-white ala PBM */
    346 		strcpy(p, "\033*a0I");
    347 		p += strlen(p);
    348 		break;
    349 	case SIM_GRAYSCALE:
    350 		/* use grayscale mode */
    351 		strcpy(p, "\033*a4T");
    352 		p += strlen(p);
    353 		/* make image data be "min-is-black ala PGM */
    354 		strcpy(p, "\033*a1I");
    355 		p += strlen(p);
    356 		break;
    357 	case SIM_COLOR:
    358 		/* use RGB color mode */
    359 		strcpy(p, "\033*a5T");
    360 		p += strlen(p);
    361 		/* make image data be "min-is-black ala PPM */
    362 		strcpy(p, "\033*a1I");
    363 		p += strlen(p);
    364 		/* use pass-through matrix (disable NTSC) */
    365 		strcpy(p, "\033*u2T");
    366 		p += strlen(p);
    367 	}
    368 
    369 	sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
    370 	p += strlen(p);
    371 	sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
    372 	p += strlen(p);
    373 	sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
    374 	p += strlen(p);
    375 
    376 	return (scanjet_write(ss, escape_codes, p - escape_codes, 0));
    377 }
    378 
    379 void
    380 scanjet_compute_sizes(ss)
    381 	struct ss_softc *ss;
    382 {
    383 	int r = 0;		/* round up by r 1/1200" */
    384 
    385 	/*
    386 	 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
    387 	 * as its base unit of measurement.  PINT uses 1/1200" (yes I know
    388 	 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
    389 	 */
    390 	ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
    391 	ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
    392 
    393 	switch (ss->sio.scan_image_mode) {
    394 	case SIM_BINARY_MONOCHROME:
    395 	case SIM_DITHERED_MONOCHROME:
    396 		ss->sio.scan_bits_per_pixel = 1;
    397 		break;
    398 	case SIM_GRAYSCALE:
    399 		r = 600;
    400 		ss->sio.scan_bits_per_pixel = 8;
    401 		break;
    402 	case SIM_COLOR:
    403 		r = 600;
    404 		ss->sio.scan_bits_per_pixel = 24;
    405 		break;
    406 	}
    407 
    408 	ss->sio.scan_pixels_per_line =
    409 		(ss->sio.scan_width * ss->sio.scan_x_resolution + r) / 1200;
    410 	if (ss->sio.scan_bits_per_pixel == 1)
    411 		/* pad to byte boundary: */
    412 		ss->sio.scan_pixels_per_line =
    413 		    (ss->sio.scan_pixels_per_line + 7) & 0xfffffff8;
    414 
    415 	ss->sio.scan_lines =
    416 	    (ss->sio.scan_height * ss->sio.scan_y_resolution + r) / 1200;
    417 	ss->sio.scan_window_size = ss->sio.scan_lines *
    418 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    419 }
    420