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