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