Home | History | Annotate | Line # | Download | only in scsipi
ss_mustek.c revision 1.1
      1 /*	$NetBSD: ss_mustek.c,v 1.1 1996/02/18 20:32:47 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Joachim Koenig-Baltes.  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 Joachim Koenig-Baltes.
     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 driver for MUSTEK flatbed scanners MFS 06000CX and MFS 12000CX
     34  * these scanners come with their own scsi card, containing an NCR53C400
     35  * SCSI controller chip. I'm in the progress of writing a driver for this
     36  * card to work under NetBSD-current. I've hooked it up to a Seagate ST01
     37  * hostadapter in the meantime, giving 350KB/sec for higher resolutions!
     38  *
     39  * I tried to connect it to my Adaptec 1542B, but with no success. It seems,
     40  * it does not like synchronous negotiation between Hostadapter and other
     41  * targets, but I could not turn this off for the 1542B.
     42  *
     43  * There is also an other reason why you would not like to connect it to your
     44  * favourite SCSI host adapter: The Mustek DOES NOT DISCONNECT. It will block
     45  * other traffic from the bus while a transfer is active.
     46  */
     47 
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/kernel.h>
     51 #include <sys/systm.h>
     52 #include <sys/fcntl.h>
     53 #include <sys/errno.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/buf.h>
     57 #include <sys/proc.h>
     58 #include <sys/user.h>
     59 #include <sys/device.h>
     60 #include <sys/conf.h>		/* for cdevsw */
     61 #include <sys/scanio.h>
     62 
     63 #include <scsi/scsi_all.h>
     64 #include <scsi/scsi_scanner.h>
     65 #include <scsi/scsiconf.h>
     66 #include <scsi/ssvar.h>
     67 #include <scsi/ss_mustek.h>
     68 
     69 #define MUSTEK_RETRIES 4
     70 
     71 int mustek_get_params __P((struct ss_softc *));
     72 int mustek_set_params __P((struct ss_softc *, struct scan_io *));
     73 int mustek_trigger_scanner __P((struct ss_softc *));
     74 void mustek_minphys __P((struct ss_softc *, struct buf *));
     75 int mustek_read __P((struct ss_softc *, struct buf *));
     76 int mustek_rewind_scanner __P((struct ss_softc *));
     77 
     78 /* only used internally */
     79 int mustek_get_status __P((struct ss_softc *, int, int));
     80 void mustek_compute_sizes __P((struct ss_softc *));
     81 
     82 /*
     83  * structure for the special handlers
     84  */
     85 struct ss_special mustek_special = {
     86 	mustek_set_params,
     87 	mustek_trigger_scanner,
     88 	mustek_get_params,
     89 	mustek_minphys,
     90 	mustek_read,
     91 	mustek_rewind_scanner,
     92 	NULL,			/* no adf support right now */
     93 	NULL			/* no adf support right now */
     94 };
     95 
     96 /*
     97  * mustek_attach: attach special functions to ss
     98  */
     99 void
    100 mustek_attach(ss, sa)
    101 	struct ss_softc *ss;
    102 	struct scsibus_attach_args *sa;
    103 {
    104 	struct scsi_link *sc_link = sa->sa_sc_link;
    105 
    106 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_attach: start\n"));
    107 	ss->sio.scan_scanner_type = 0;
    108 
    109 	/* first, check the model which determines resolutions */
    110 	if (!bcmp(sa->sa_inqbuf->product, "MFS-06000CX", 11)) {
    111 		ss->sio.scan_scanner_type = MUSTEK_06000CX;
    112 		printf(": Mustek 6000CX Flatbed 3-pass color scanner, 3 - 600 dpi\n");
    113 	}
    114 	if (!bcmp(sa->sa_inqbuf->product, "MFS-12000CX", 11)) {
    115 		ss->sio.scan_scanner_type = MUSTEK_12000CX;
    116 		printf(": Mustek 12000CX Flatbed 3-pass color scanner, 6 - 1200 dpi\n");
    117 	}
    118 
    119 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_attach: scanner_type = %d\n",
    120 	    ss->sio.scan_scanner_type));
    121 
    122 	/* install special handlers */
    123 	ss->special = &mustek_special;
    124 
    125 	/*
    126 	 * populate the scanio struct with legal values
    127 	 * the default should come from user space
    128 	 */
    129 	ss->sio.scan_width		= 1200;
    130 	ss->sio.scan_height		= 1200;
    131 	ss->sio.scan_x_resolution	= 99;
    132 	ss->sio.scan_y_resolution	= 99;
    133 	ss->sio.scan_x_origin		= 0;
    134 	ss->sio.scan_y_origin		= 0;
    135 	ss->sio.scan_brightness		= 100;
    136 	ss->sio.scan_contrast		= 100;
    137 	ss->sio.scan_quality		= 100;
    138 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
    139 
    140 	mustek_compute_sizes(ss);
    141 }
    142 
    143 int
    144 mustek_get_params (ss)
    145 	struct ss_softc *ss;
    146 {
    147 
    148 	return (0);
    149 }
    150 
    151 /*
    152  * check the parameters if the mustek is capable of fulfilling it
    153  * but don't send the command to the scanner in case the user wants
    154  * to change parameters by more than one call
    155  */
    156 int
    157 mustek_set_params(ss, sio)
    158 	struct ss_softc *ss;
    159 	struct scan_io *sio;
    160 {
    161 	int error;
    162 
    163 	/*
    164 	 * if the scanner is triggered, then rewind it
    165 	 */
    166 	if (ss->flags & SSF_TRIGGERED) {
    167 		error = mustek_rewind_scanner(ss);
    168 		if (error)
    169 			return (error);
    170 	}
    171 
    172 	/* size constraints: 8.5" horizontally and 14" vertically */
    173 #ifdef MUSTEK_INCH_SPEC
    174 	/* sizes must be a multiple of 1/8" */
    175 	sio->scan_x_origin -= sio->scan_x_origin % 150;
    176 	sio->scan_y_origin -= sio->scan_y_origin % 150;
    177 	sio->scan_width -= sio->scan_width % 150;
    178 	sio->scan_height -= sio->scan_height % 150;
    179 #endif
    180 	if (sio->scan_width == 0 ||
    181 	    sio->scan_x_origin + sio->scan_width > 10200 ||
    182 	    sio->scan_height == 0 ||
    183 	    sio->scan_y_origin + sio->scan_height > 16800)
    184 		return (EINVAL);
    185 
    186 	/*
    187 	 * for now, only realize the values for the MUSTEK_06000CX
    188 	 * in the future, values for the MUSTEK_12000CX will be implemented
    189 	 */
    190 
    191 	/*
    192 	 * resolution (dpi) must be <= 300 and a multiple of 3 or
    193 	 * between 300 and 600 and a multiple of 30
    194 	 */
    195 	sio->scan_x_resolution -= sio->scan_x_resolution <= 300 ?
    196 	    sio->scan_x_resolution % 3 : sio->scan_x_resolution % 30;
    197 	sio->scan_y_resolution -= sio->scan_y_resolution <= 300 ?
    198 	    sio->scan_y_resolution % 3 : sio->scan_y_resolution % 30;
    199 	if (sio->scan_x_resolution < 3 || sio->scan_x_resolution > 600 ||
    200 	    sio->scan_x_resolution != sio->scan_y_resolution)
    201 		return (EINVAL);
    202 
    203 	/* assume brightness values are between 64 and 136 in steps of 3 */
    204 	sio->scan_brightness -= (sio->scan_brightness - 64) % 3;
    205 	if (sio->scan_brightness < 64 || sio->scan_brightness > 136)
    206 		return (EINVAL);
    207 
    208 	/* contrast values must be between 16 and 184 in steps of 7 */
    209 	sio->scan_contrast -= (sio->scan_contrast - 16) % 7;
    210 	if (sio->scan_contrast < 16 || sio->scan_contrast > 184)
    211 		return (EINVAL);
    212 
    213 	/*
    214 	 * velocity: between 0 (fast) and 4 (slow) which will be mapped
    215 	 * to 100% = 4, 80% = 3, 60% = 2, 40% = 1, 20% = 0
    216 	 * must be a multiple of 20
    217 	 */
    218 	sio->scan_quality -= sio->scan_quality % 20;
    219 	if (sio->scan_quality < 20 || sio->scan_quality > 100)
    220 		return (EINVAL);
    221 
    222 	switch (sio->scan_image_mode) {
    223 	case SIM_BINARY_MONOCHROME:
    224 	case SIM_DITHERED_MONOCHROME:
    225 	case SIM_GRAYSCALE:
    226 	case SIM_RED:
    227 	case SIM_GREEN:
    228 	case SIM_BLUE:
    229 		break;
    230 	default:
    231 		return (EINVAL);
    232 	}
    233 
    234 	/* change ss_softc to the new values, but save ro-variables */
    235 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
    236 	bcopy(sio, &ss->sio, sizeof(struct scan_io));
    237 
    238 	mustek_compute_sizes(ss);
    239 
    240 	return (0);
    241 }
    242 
    243 /*
    244  * trim the requested transfer to a multiple of the line size
    245  * this is called only from ssread() which guarantees, scanner is triggered
    246  * In the future, it will trim the transfer to not read to much at a time
    247  * because the mustek cannot disconnect. It will be calculated by the
    248  * resolution, the velocity and the number of bytes per line.
    249  */
    250 void
    251 mustek_minphys(ss, bp)
    252 	struct ss_softc *ss;
    253 	struct buf *bp;
    254 {
    255 	struct scsi_link *sc_link = ss->sc_link;
    256 
    257 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_minphys: before: %d\n",
    258 	    bp->b_bcount));
    259 	bp->b_bcount -= bp->b_bcount %
    260 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    261 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_minphys: after:  %d\n",
    262 	    bp->b_bcount));
    263 }
    264 
    265 /*
    266  * trigger the scanner to start a scan operation
    267  * this includes sending the mode- and window-data, starting the scanner
    268  * and getting the image size info
    269  */
    270 int
    271 mustek_trigger_scanner(ss)
    272 	struct ss_softc *ss;
    273 {
    274 	struct mustek_mode_select_cmd mode_cmd;
    275 	struct mustek_mode_select_data mode_data;
    276 	struct mustek_set_window_cmd window_cmd;
    277 	struct mustek_set_window_data window_data;
    278 	struct mustek_start_scan_cmd start_scan_cmd;
    279 	struct scsi_link *sc_link = ss->sc_link;
    280 #ifndef MUSTEK_INCH_SPEC
    281 	int pixel_tlx, pixel_tly, pixel_brx, pixel_bry, paperlength;
    282 #endif
    283 	int error;
    284 
    285 	mustek_compute_sizes(ss);
    286 
    287 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_trigger_scanner\n"));
    288 
    289 	/*
    290 	 * set the window params and send the scsi command
    291 	 */
    292 	bzero(&window_cmd, sizeof(window_cmd));
    293 	window_cmd.opcode = MUSTEK_SET_WINDOW;
    294 	window_cmd.length = sizeof(window_data);
    295 
    296 	bzero(&window_data, sizeof(window_data));
    297 	window_data.frame_header = MUSTEK_LINEART_BACKGROUND | MUSTEK_UNIT_SPEC;
    298 #ifdef MUSTEK_INCH_SPEC
    299 	/* the positional values are all 1 byte because 256 / 8 = 32" */
    300 	window_data.frame_tl_x_0 = ss->sio.scan_x_origin / 150;
    301 	window_data.frame_tl_x_1 = 0;
    302 	window_data.frame_tl_y_0 = ss->sio.scan_y_origin / 150;
    303 	window_data.frame_tl_y_1 = 0;
    304 	window_data.frame_br_x_0 = (ss->sio.scan_x_origin +
    305 				    ss->sio.scan_width) / 150;
    306 	window_data.frame_br_x_1 = 0;
    307 	window_data.frame_br_y_0 = (ss->sio.scan_y_origin +
    308 				    ss->sio.scan_height) / 150;
    309 	window_data.frame_br_y_1 = 0;
    310 #else
    311 	pixel_tlx = (ss->sio.scan_x_origin * ss->sio.scan_x_resolution) / 1200;
    312 	window_data.frame_tl_x_0 = pixel_tlx & 0xff;
    313 	window_data.frame_tl_x_1 = (pixel_tlx >> 8) & 0xff;
    314 	pixel_tly = (ss->sio.scan_y_origin * ss->sio.scan_y_resolution) / 1200;
    315 	window_data.frame_tl_y_0 = pixel_tly & 0xff;
    316 	window_data.frame_tl_y_1 = (pixel_tly >> 8) & 0xff;
    317 	pixel_brx = pixel_tlx +
    318 	    (ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
    319 	window_data.frame_br_x_0 = pixel_brx & 0xff;
    320 	window_data.frame_br_x_1 = (pixel_brx >> 8) & 0xff;
    321 	pixel_bry = pixel_tly +
    322 	    (ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
    323 	window_data.frame_br_y_0 = pixel_bry & 0xff;
    324 	window_data.frame_br_y_1 = (pixel_bry >> 8) & 0xff;
    325 #endif
    326 
    327 #if MUSTEK_WINDOWS >= 1
    328 	window_data.window1_header = MUSTEK_WINDOW_MASK | MUSTEK_UNIT_SPEC;
    329 	window_data.window1_tl_x_0 = window_data.frame_tl_x_0;
    330 	window_data.window1_tl_x_1 = window_data.frame_tl_x_1;
    331 	window_data.window1_tl_y_0 = window_data.frame_tl_y_0;
    332 	window_data.window1_tl_y_1 = window_data.frame_tl_y_1;
    333 	window_data.window1_br_x_0 = window_data.frame_br_x_0;
    334 	window_data.window1_br_x_1 = window_data.frame_br_x_1;
    335 	window_data.window1_br_y_0 = window_data.frame_br_y_0;
    336 	window_data.window1_br_y_1 = window_data.frame_br_y_1;
    337 #endif
    338 
    339 	/* send the set window command to the scanner */
    340 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_set_parms: set_window\n"));
    341 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &window_cmd,
    342 	    sizeof(window_cmd), (u_char *) &window_data, sizeof(window_data),
    343 	    MUSTEK_RETRIES, 5000, NULL, SCSI_DATA_OUT);
    344 	if (error)
    345 		return (error);
    346 
    347 	/*
    348 	 * do what it takes to actualize the mode
    349 	 */
    350 	bzero(&mode_cmd, sizeof(mode_cmd));
    351 	mode_cmd.opcode = MUSTEK_MODE_SELECT;
    352 	mode_cmd.length_0 = sizeof(mode_data);
    353 
    354 	bzero(&mode_data, sizeof(mode_data));
    355 	mode_data.mode =
    356 	    MUSTEK_MODE_MASK | MUSTEK_HT_PATTERN_BUILTIN | MUSTEK_UNIT_SPEC;
    357 	if (ss->sio.scan_x_resolution <= 300) {
    358 		mode_data.resolution = ss->sio.scan_x_resolution / 3;
    359 	} else {
    360 		/*
    361 		 * the resolution values is computed by modulo 100, but not
    362 		 * for 600dpi, where the value is 100 (a bit tricky, but ...)
    363 		 */
    364 		mode_data.resolution =
    365 		    ((ss->sio.scan_x_resolution - 1) % 100) + 1;
    366 	}
    367 	mode_data.brightness = (ss->sio.scan_brightness - 64) / 3;
    368 	mode_data.contrast = (ss->sio.scan_contrast - 16) / 7;
    369 	mode_data.grain = 0;
    370 	mode_data.velocity = ss->sio.scan_quality / 20 - 1;
    371 #ifdef MUSTEK_INCH_SPEC
    372 	mode_data.paperlength_0 = 14 * 8;	/* 14" */
    373 	mode_data.paperlength_1 = 0;
    374 #else
    375 	paperlength = 14 * ss->sio.scan_y_resolution; /* 14" */
    376 	mode_data.paperlength_0 = paperlength & 0xff;
    377 	mode_data.paperlength_1 = (paperlength >> 8) & 0xff;
    378 #endif
    379 
    380 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_trigger_scanner: mode_select\n"));
    381 	/* send the command to the scanner */
    382 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &mode_cmd,
    383 	    sizeof(mode_cmd), (u_char *) &mode_data, sizeof(mode_data),
    384 	    MUSTEK_RETRIES, 5000, NULL, SCSI_DATA_OUT);
    385 	if (error)
    386 		return (error);
    387 
    388 	/*
    389 	 * now construct and send the start command
    390 	 */
    391 	bzero(&start_scan_cmd,sizeof(start_scan_cmd));
    392 	start_scan_cmd.opcode = MUSTEK_START_STOP;
    393 	start_scan_cmd.mode = MUSTEK_SCAN_START;
    394 	if (ss->sio.scan_x_resolution <= 300)
    395 		start_scan_cmd.mode |= MUSTEK_RES_STEP_1;
    396 	else
    397 		start_scan_cmd.mode |= MUSTEK_RES_STEP_10;
    398 	switch (ss->sio.scan_image_mode) {
    399 	case SIM_BINARY_MONOCHROME:
    400 	case SIM_DITHERED_MONOCHROME:
    401 		start_scan_cmd.mode |= MUSTEK_BIT_MODE | MUSTEK_GRAY_FILTER;
    402 		break;
    403 	case SIM_GRAYSCALE:
    404 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GRAY_FILTER;
    405 		break;
    406 	case SIM_RED:
    407 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_RED_FILTER;
    408 		break;
    409 	case SIM_GREEN:
    410 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GREEN_FILTER;
    411 		break;
    412 	case SIM_BLUE:
    413 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_BLUE_FILTER;
    414 		break;
    415 	}
    416 
    417 	/* send the command to the scanner */
    418 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_trigger_scanner: start_scan\n"));
    419 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &start_scan_cmd,
    420 	    sizeof(start_scan_cmd), NULL, 0,
    421 	    MUSTEK_RETRIES, 5000, NULL, 0);
    422 	if (error)
    423 		return (error);
    424 
    425 	/*
    426 	 * now check if scanner ready this time with update of size info
    427 	 * we wait here so that if the user issues a read directly afterwards,
    428 	 * the scanner will respond directly (otherwise we had to sleep with
    429 	 * a buffer locked in memory)
    430 	 */
    431 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_trigger_scanner: get_status\n"));
    432 	error = mustek_get_status(ss, 60, 1);
    433 	if (error)
    434 		return (error);
    435 
    436 	return (0);
    437 }
    438 
    439 /*
    440  * stop a scan operation in progress
    441  */
    442 int
    443 mustek_rewind_scanner(ss)
    444 	struct ss_softc *ss;
    445 {
    446 	struct mustek_start_scan_cmd cmd;
    447 	struct scsi_link *sc_link = ss->sc_link;
    448 	int error;
    449 
    450 	if (ss->sio.scan_window_size != 0) {
    451 		/*
    452 		 * only if not all data has been read, the scanner has to be
    453 		 * stopped
    454 		 */
    455 		bzero(&cmd, sizeof(cmd));
    456 		cmd.opcode = MUSTEK_START_STOP;
    457 		cmd.mode = MUSTEK_SCAN_STOP;
    458 
    459 		/* send the command to the scanner */
    460 		SC_DEBUG(sc_link, SDEV_DB1,
    461 		    ("mustek_rewind_scanner: stop_scan\n"));
    462 		error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    463 		    sizeof(cmd), NULL, 0, MUSTEK_RETRIES, 5000, NULL, 0);
    464 		if (error)
    465 			return (error);
    466 	}
    467 
    468 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_rewind_scanner: end\n"));
    469 
    470 	return (0);
    471 }
    472 
    473 /*
    474  * read the requested number of bytes/lines from the scanner
    475  */
    476 int
    477 mustek_read(ss, bp)
    478 	struct ss_softc *ss;
    479 	struct buf *bp;
    480 {
    481 	struct mustek_read_cmd cmd;
    482 	struct scsi_link *sc_link = ss->sc_link;
    483 	u_long lines_to_read;
    484 
    485 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_read: start\n"));
    486 
    487 	bzero(&cmd, sizeof(cmd));
    488 	cmd.opcode = MUSTEK_READ;
    489 
    490 	/* instead of the bytes, the mustek wants the number of lines */
    491 	lines_to_read = bp->b_bcount /
    492 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    493 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_read: read %d lines\n",
    494 	    lines_to_read));
    495 	cmd.length_0 = lines_to_read & 0xff;
    496 	cmd.length_1 = (lines_to_read >> 8) & 0xff;
    497 	cmd.length_2 = (lines_to_read >> 16) & 0xff;
    498 
    499 	/*
    500 	 * go ask the adapter to do all this for us
    501 	 */
    502 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd, sizeof(cmd),
    503 	    (u_char *) bp->b_data, bp->b_bcount, MUSTEK_RETRIES, 10000, bp,
    504 	    SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED)
    505 		printf("%s: not queued\n", ss->sc_dev.dv_xname);
    506 	else {
    507 		ss->sio.scan_lines -= lines_to_read;
    508 		ss->sio.scan_window_size -= bp->b_bcount;
    509 	}
    510 
    511 	return (0);
    512 }
    513 
    514 /*
    515  * check if the scanner is ready to take commands
    516  *   wait timeout seconds and try only every second
    517  *   if update, then update picture size info
    518  *
    519  *   returns EBUSY if scanner not ready
    520  */
    521 int
    522 mustek_get_status(ss, timeout, update)
    523 	struct ss_softc *ss;
    524 	int timeout, update;
    525 {
    526 	struct mustek_get_status_cmd cmd;
    527 	struct mustek_get_status_data data;
    528 	struct scsi_link *sc_link = ss->sc_link;
    529 	int error, lines, bytes_per_line;
    530 
    531 	bzero(&cmd, sizeof(cmd));
    532 	cmd.opcode = MUSTEK_GET_STATUS;
    533 	cmd.length = sizeof(data);
    534 
    535 	while (1) {
    536 		SC_DEBUG(sc_link, SDEV_DB1, ("mustek_get_status: stat_cmd\n"));
    537 		error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    538 		    sizeof(cmd), (u_char *) &data, sizeof(data), MUSTEK_RETRIES,
    539 		    5000, NULL, SCSI_DATA_IN);
    540 		if (error)
    541 			return (error);
    542 		if ((data.ready_busy == MUSTEK_READY) ||
    543 		    (timeout-- <= 0))
    544 			break;
    545 		/* please wait a second */
    546 		tsleep((caddr_t)mustek_get_status, PRIBIO + 1, "mtkrdy", hz);
    547 	}
    548 
    549 	if (update) {
    550 		bytes_per_line =
    551 		    (data.bytes_per_line_1 << 8) |
    552 		    data.bytes_per_line_0;
    553 		lines =
    554 		    (data.lines_2 << 16) |
    555 		    (data.lines_1 << 8) |
    556 		    data.lines_0;
    557 		if (lines != ss->sio.scan_lines) {
    558 			printf("mustek: lines actual(%d) != computed(%d)\n",
    559 			    lines, ss->sio.scan_lines);
    560 			return (EIO);
    561 		}
    562 		if (bytes_per_line * lines != ss->sio.scan_window_size) {
    563 			printf("mustek: win-size actual(%d) != computed(%d)\n",
    564 			    bytes_per_line * lines, ss->sio.scan_window_size);
    565 		    return (EIO);
    566 		}
    567 
    568 		SC_DEBUG(sc_link, SDEV_DB1,
    569 		    ("mustek_get_size: bpl=%d, lines=%d\n",
    570 		    (ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8,
    571 		    ss->sio.scan_lines));
    572 		SC_DEBUG(sc_link, SDEV_DB1, ("window size = %d\n",
    573 		    ss->sio.scan_window_size));
    574 	}
    575 
    576 	SC_DEBUG(sc_link, SDEV_DB1, ("mustek_get_status: end\n"));
    577 	if (data.ready_busy == MUSTEK_READY)
    578 		return (0);
    579 	else
    580 		return (EBUSY);
    581 }
    582 
    583 /*
    584  * mustek_compute_sizes: compute window_size and lines for the picture
    585  *   this function is called from different places in the code
    586  */
    587 void
    588 mustek_compute_sizes(ss)
    589 	struct ss_softc *ss;
    590 {
    591 
    592 	switch (ss->sio.scan_image_mode) {
    593 	case SIM_BINARY_MONOCHROME:
    594 	case SIM_DITHERED_MONOCHROME:
    595 		ss->sio.scan_bits_per_pixel = 1;
    596 		break;
    597 	case SIM_GRAYSCALE:
    598 	case SIM_RED:
    599 	case SIM_GREEN:
    600 	case SIM_BLUE:
    601 		ss->sio.scan_bits_per_pixel = 8;
    602 		break;
    603 	}
    604 
    605 	/*
    606 	 * horizontal number of bytes is always a multiple of 2,
    607 	 * in 8-bit mode at least
    608 	 */
    609 	ss->sio.scan_pixels_per_line =
    610 	    (ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
    611 	if (ss->sio.scan_bits_per_pixel == 1)
    612 		/* make it a multiple of 16, and thus of 2 bytes */
    613 		ss->sio.scan_pixels_per_line =
    614 		    (ss->sio.scan_pixels_per_line + 15) & 0xfffffff0;
    615 	else
    616 		ss->sio.scan_pixels_per_line =
    617 		    (ss->sio.scan_pixels_per_line + 1) & 0xfffffffe;
    618 
    619 	ss->sio.scan_lines =
    620 	    (ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
    621 	ss->sio.scan_window_size = ss->sio.scan_lines *
    622 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    623 }
    624