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