Home | History | Annotate | Line # | Download | only in scsipi
ss_scanjet.c revision 1.4
      1 /*	$NetBSD: ss_scanjet.c,v 1.4 1996/05/05 19:52:58 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 	int error;
    207 
    208 	scanjet_compute_sizes(ss);
    209 
    210 	/* send parameters */
    211 	error = scanjet_set_window(ss);
    212 	if (error) {
    213 		uprintf("set window failed\n");
    214 		return (error);
    215 	}
    216 
    217 	/* send "trigger" operation */
    218 	strcpy(escape_codes, "\033*f0S");
    219 	error = scanjet_write(ss, escape_codes, strlen(escape_codes), 0);
    220 	if (error) {
    221 		uprintf("trigger failed\n");
    222 		return (error);
    223 	}
    224 
    225 	return (0);
    226 }
    227 
    228 int
    229 scanjet_read(ss, bp)
    230 	struct ss_softc *ss;
    231 	struct buf *bp;
    232 {
    233 	struct scsi_rw_scanner cmd;
    234 	struct scsi_link *sc_link = ss->sc_link;
    235 
    236 	/*
    237 	 *  Fill out the scsi command
    238 	 */
    239 	bzero(&cmd, sizeof(cmd));
    240 	cmd.opcode = READ;
    241 
    242 	/*
    243 	 * Handle "fixed-block-mode" tape drives by using the
    244 	 * block count instead of the length.
    245 	 */
    246 	_lto3b(bp->b_bcount, cmd.len);
    247 
    248 	/*
    249 	 * go ask the adapter to do all this for us
    250 	 */
    251 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, sizeof(cmd),
    252 	    (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp,
    253 	    SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED)
    254 		printf("%s: not queued\n", ss->sc_dev.dv_xname);
    255 	else {
    256 		ss->sio.scan_window_size -= bp->b_bcount;
    257 		if (ss->sio.scan_window_size < 0)
    258 			ss->sio.scan_window_size = 0;
    259 	}
    260 
    261 	return (0);
    262 }
    263 
    264 
    265 /*
    266  * Do a synchronous write.  Used to send control messages.
    267  */
    268 int
    269 scanjet_write(ss, buf, size, flags)
    270 	struct ss_softc *ss;
    271 	char *buf;
    272 	u_int size;
    273 	int flags;
    274 {
    275 	struct scsi_rw_scanner cmd;
    276 
    277 	/*
    278 	 * If it's a null transfer, return immediatly
    279 	 */
    280 	if (size == 0)
    281 		return (0);
    282 	bzero(&cmd, sizeof(cmd));
    283 	cmd.opcode = WRITE;
    284 	_lto3b(size, cmd.len);
    285 	return (scsi_scsi_cmd(ss->sc_link, (struct scsi_generic *) &cmd,
    286 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
    287 	    flags | SCSI_DATA_OUT));
    288 }
    289 
    290 #ifdef SCANJETDEBUG
    291 static void show_es(char *es)
    292 {
    293   char *p = es;
    294   while (*p) {
    295     if (*p == '\033')
    296       printf("[Esc]");
    297     else
    298       printf("%c", *p);
    299     ++p;
    300   }
    301   printf("\n");
    302 }
    303 #endif
    304 
    305 /*
    306  * simulate SCSI_SET_WINDOW for ScanJets
    307  */
    308 int
    309 scanjet_set_window(ss)
    310 	struct ss_softc *ss;
    311 {
    312 	char escape_codes[128], *p;
    313 
    314 	p = escape_codes;
    315 
    316 	sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4);
    317 	p += strlen(p);
    318 	sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4);
    319 	p += strlen(p);
    320 	sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
    321 	p += strlen(p);
    322 	sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
    323 	p += strlen(p);
    324 	sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution);
    325 	p += strlen(p);
    326 	sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution);
    327 	p += strlen(p);
    328 
    329 	switch (ss->sio.scan_image_mode) {
    330 	case SIM_BINARY_MONOCHROME:
    331 		/* use "line art" mode */
    332 		strcpy(p, "\033*a0T");
    333 		p += strlen(p);
    334 		/* make image data be "min-is-white ala PBM */
    335 		strcpy(p, "\033*a0I");
    336 		p += strlen(p);
    337 		break;
    338 	case SIM_DITHERED_MONOCHROME:
    339 		/* use dithered mode */
    340 		strcpy(p, "\033*a3T");
    341 		p += strlen(p);
    342 		/* make image data be "min-is-white ala PBM */
    343 		strcpy(p, "\033*a0I");
    344 		p += strlen(p);
    345 		break;
    346 	case SIM_GRAYSCALE:
    347 		/* use grayscale mode */
    348 		strcpy(p, "\033*a4T");
    349 		p += strlen(p);
    350 		/* make image data be "min-is-black ala PGM */
    351 		strcpy(p, "\033*a1I");
    352 		p += strlen(p);
    353 		break;
    354 	case SIM_COLOR:
    355 		/* use RGB color mode */
    356 		strcpy(p, "\033*a5T");
    357 		p += strlen(p);
    358 		/* make image data be "min-is-black ala PPM */
    359 		strcpy(p, "\033*a1I");
    360 		p += strlen(p);
    361 		/* use pass-through matrix (disable NTSC) */
    362 		strcpy(p, "\033*u2T");
    363 		p += strlen(p);
    364 	}
    365 
    366 	sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
    367 	p += strlen(p);
    368 	sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
    369 	p += strlen(p);
    370 	sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
    371 	p += strlen(p);
    372 
    373 	return (scanjet_write(ss, escape_codes, p - escape_codes, 0));
    374 }
    375 
    376 void
    377 scanjet_compute_sizes(ss)
    378 	struct ss_softc *ss;
    379 {
    380 	int r = 0;		/* round up by r 1/1200" */
    381 
    382 	/*
    383 	 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
    384 	 * as its base unit of measurement.  PINT uses 1/1200" (yes I know
    385 	 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
    386 	 */
    387 	ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
    388 	ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
    389 
    390 	switch (ss->sio.scan_image_mode) {
    391 	case SIM_BINARY_MONOCHROME:
    392 	case SIM_DITHERED_MONOCHROME:
    393 		ss->sio.scan_bits_per_pixel = 1;
    394 		break;
    395 	case SIM_GRAYSCALE:
    396 		r = 600;
    397 		ss->sio.scan_bits_per_pixel = 8;
    398 		break;
    399 	case SIM_COLOR:
    400 		r = 600;
    401 		ss->sio.scan_bits_per_pixel = 24;
    402 		break;
    403 	}
    404 
    405 	ss->sio.scan_pixels_per_line =
    406 		(ss->sio.scan_width * ss->sio.scan_x_resolution + r) / 1200;
    407 	if (ss->sio.scan_bits_per_pixel == 1)
    408 		/* pad to byte boundary: */
    409 		ss->sio.scan_pixels_per_line =
    410 		    (ss->sio.scan_pixels_per_line + 7) & 0xfffffff8;
    411 
    412 	ss->sio.scan_lines =
    413 	    (ss->sio.scan_height * ss->sio.scan_y_resolution + r) / 1200;
    414 	ss->sio.scan_window_size = ss->sio.scan_lines *
    415 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    416 }
    417