Home | History | Annotate | Line # | Download | only in scsipi
ss_mustek.c revision 1.40.16.2
      1  1.40.16.2       mrg /*	$NetBSD: ss_mustek.c,v 1.40.16.2 2012/03/06 09:56:21 mrg Exp $	*/
      2        1.1   mycroft 
      3        1.1   mycroft /*
      4        1.1   mycroft  * Copyright (c) 1995 Joachim Koenig-Baltes.  All rights reserved.
      5        1.1   mycroft  *
      6        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      7        1.1   mycroft  * modification, are permitted provided that the following conditions
      8        1.1   mycroft  * are met:
      9        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     10        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     11        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     14        1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     15        1.1   mycroft  *    must display the following acknowledgement:
     16        1.1   mycroft  *	This product includes software developed by Joachim Koenig-Baltes.
     17        1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18        1.1   mycroft  *    derived from this software without specific prior written permission.
     19        1.1   mycroft  *
     20        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21        1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22        1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23        1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24        1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25        1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26        1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27        1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28        1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29        1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30        1.1   mycroft  */
     31        1.1   mycroft 
     32        1.1   mycroft /*
     33        1.1   mycroft  * special driver for MUSTEK flatbed scanners MFS 06000CX and MFS 12000CX
     34        1.1   mycroft  * these scanners come with their own scsi card, containing an NCR53C400
     35        1.1   mycroft  * SCSI controller chip. I'm in the progress of writing a driver for this
     36        1.1   mycroft  * card to work under NetBSD-current. I've hooked it up to a Seagate ST01
     37        1.1   mycroft  * hostadapter in the meantime, giving 350KB/sec for higher resolutions!
     38        1.1   mycroft  *
     39        1.1   mycroft  * I tried to connect it to my Adaptec 1542B, but with no success. It seems,
     40        1.1   mycroft  * it does not like synchronous negotiation between Hostadapter and other
     41        1.1   mycroft  * targets, but I could not turn this off for the 1542B.
     42        1.1   mycroft  *
     43        1.1   mycroft  * There is also an other reason why you would not like to connect it to your
     44        1.1   mycroft  * favourite SCSI host adapter: The Mustek DOES NOT DISCONNECT. It will block
     45        1.1   mycroft  * other traffic from the bus while a transfer is active.
     46        1.1   mycroft  */
     47       1.17     lukem 
     48       1.17     lukem #include <sys/cdefs.h>
     49  1.40.16.2       mrg __KERNEL_RCSID(0, "$NetBSD: ss_mustek.c,v 1.40.16.2 2012/03/06 09:56:21 mrg Exp $");
     50        1.1   mycroft 
     51        1.1   mycroft #include <sys/param.h>
     52        1.1   mycroft #include <sys/kernel.h>
     53        1.1   mycroft #include <sys/systm.h>
     54        1.1   mycroft #include <sys/fcntl.h>
     55        1.1   mycroft #include <sys/errno.h>
     56        1.1   mycroft #include <sys/ioctl.h>
     57        1.1   mycroft #include <sys/malloc.h>
     58        1.1   mycroft #include <sys/buf.h>
     59       1.27      yamt #include <sys/bufq.h>
     60        1.1   mycroft #include <sys/proc.h>
     61        1.1   mycroft #include <sys/device.h>
     62        1.1   mycroft #include <sys/conf.h>		/* for cdevsw */
     63        1.1   mycroft #include <sys/scanio.h>
     64        1.1   mycroft 
     65       1.25   mycroft #include <dev/scsipi/scsipi_all.h>
     66        1.7    bouyer #include <dev/scsipi/scsi_all.h>
     67        1.7    bouyer #include <dev/scsipi/scsi_scanner.h>
     68       1.25   mycroft #include <dev/scsipi/scsipiconf.h>
     69       1.21    bouyer #include <dev/scsipi/scsipi_base.h>
     70        1.7    bouyer #include <dev/scsipi/ssvar.h>
     71        1.7    bouyer #include <dev/scsipi/ss_mustek.h>
     72        1.1   mycroft 
     73        1.1   mycroft #define MUSTEK_RETRIES 4
     74        1.1   mycroft 
     75       1.19   thorpej static int	mustek_get_params(struct ss_softc *);
     76       1.19   thorpej static int	mustek_set_params(struct ss_softc *, struct scan_io *);
     77       1.19   thorpej static int	mustek_trigger_scanner(struct ss_softc *);
     78       1.19   thorpej static void	mustek_minphys(struct ss_softc *, struct buf *);
     79       1.19   thorpej static int	mustek_read(struct ss_softc *, struct buf *);
     80       1.19   thorpej static int	mustek_rewind_scanner(struct ss_softc *);
     81        1.1   mycroft 
     82        1.1   mycroft /* only used internally */
     83       1.19   thorpej static int	mustek_get_status(struct ss_softc *, int, int);
     84       1.19   thorpej static void	mustek_compute_sizes(struct ss_softc *);
     85        1.1   mycroft 
     86  1.40.16.2       mrg /*
     87  1.40.16.2       mrg  * structure for the special handlers
     88  1.40.16.2       mrg  */
     89       1.19   thorpej static struct ss_special mustek_special = {
     90        1.1   mycroft 	mustek_set_params,
     91        1.1   mycroft 	mustek_trigger_scanner,
     92        1.1   mycroft 	mustek_get_params,
     93        1.1   mycroft 	mustek_minphys,
     94        1.1   mycroft 	mustek_read,
     95        1.1   mycroft 	mustek_rewind_scanner,
     96        1.1   mycroft 	NULL,			/* no adf support right now */
     97        1.1   mycroft 	NULL			/* no adf support right now */
     98        1.1   mycroft };
     99        1.1   mycroft 
    100  1.40.16.2       mrg /*
    101  1.40.16.2       mrg  * mustek_attach: attach special functions to ss
    102  1.40.16.2       mrg  */
    103        1.1   mycroft void
    104       1.19   thorpej mustek_attach(struct ss_softc *ss, struct scsipibus_attach_args *sa)
    105        1.1   mycroft {
    106       1.13    bouyer #ifdef SCSIPI_DEBUG
    107       1.13    bouyer 	struct scsipi_periph *periph = sa->sa_periph;
    108        1.3  christos #endif
    109        1.1   mycroft 
    110       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_attach: start\n"));
    111        1.1   mycroft 	ss->sio.scan_scanner_type = 0;
    112        1.1   mycroft 
    113  1.40.16.2       mrg 	printf("\n%s: ", device_xname(&ss->sc_dev));
    114        1.3  christos 
    115        1.1   mycroft 	/* first, check the model which determines resolutions */
    116       1.16   thorpej 	if (!memcmp(sa->sa_inqbuf.product, "MFS-06000CX", 11)) {
    117        1.1   mycroft 		ss->sio.scan_scanner_type = MUSTEK_06000CX;
    118  1.40.16.2       mrg 		printf("Mustek 6000CX Flatbed 3-pass color scanner, 3 - 600 dpi\n");
    119        1.1   mycroft 	}
    120       1.16   thorpej 	if (!memcmp(sa->sa_inqbuf.product, "MFS-12000CX", 11)) {
    121        1.1   mycroft 		ss->sio.scan_scanner_type = MUSTEK_12000CX;
    122  1.40.16.2       mrg 		printf("Mustek 12000CX Flatbed 3-pass color scanner, 6 - 1200 dpi\n");
    123        1.1   mycroft 	}
    124        1.1   mycroft 
    125       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_attach: scanner_type = %d\n",
    126        1.1   mycroft 	    ss->sio.scan_scanner_type));
    127        1.1   mycroft 
    128        1.1   mycroft 	/* install special handlers */
    129        1.1   mycroft 	ss->special = &mustek_special;
    130        1.1   mycroft 
    131        1.1   mycroft 	/*
    132        1.1   mycroft 	 * populate the scanio struct with legal values
    133        1.1   mycroft 	 * the default should come from user space
    134        1.1   mycroft 	 */
    135        1.1   mycroft 	ss->sio.scan_width		= 1200;
    136        1.1   mycroft 	ss->sio.scan_height		= 1200;
    137        1.1   mycroft 	ss->sio.scan_x_resolution	= 99;
    138        1.1   mycroft 	ss->sio.scan_y_resolution	= 99;
    139        1.1   mycroft 	ss->sio.scan_x_origin		= 0;
    140        1.1   mycroft 	ss->sio.scan_y_origin		= 0;
    141        1.1   mycroft 	ss->sio.scan_brightness		= 100;
    142        1.1   mycroft 	ss->sio.scan_contrast		= 100;
    143        1.1   mycroft 	ss->sio.scan_quality		= 100;
    144        1.1   mycroft 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
    145        1.1   mycroft 
    146        1.1   mycroft 	mustek_compute_sizes(ss);
    147        1.1   mycroft }
    148        1.1   mycroft 
    149       1.19   thorpej static int
    150       1.35  christos mustek_get_params (struct ss_softc *ss)
    151        1.1   mycroft {
    152  1.40.16.2       mrg 
    153  1.40.16.2       mrg 	return (0);
    154        1.1   mycroft }
    155        1.1   mycroft 
    156        1.1   mycroft /*
    157        1.1   mycroft  * check the parameters if the mustek is capable of fulfilling it
    158        1.1   mycroft  * but don't send the command to the scanner in case the user wants
    159        1.1   mycroft  * to change parameters by more than one call
    160        1.1   mycroft  */
    161       1.19   thorpej static int
    162       1.19   thorpej mustek_set_params(struct ss_softc *ss, struct scan_io *sio)
    163        1.1   mycroft {
    164        1.1   mycroft 	int error;
    165        1.1   mycroft 
    166  1.40.16.2       mrg 	/*
    167  1.40.16.2       mrg 	 * if the scanner is triggered, then rewind it
    168  1.40.16.2       mrg 	 */
    169        1.1   mycroft 	if (ss->flags & SSF_TRIGGERED) {
    170        1.1   mycroft 		error = mustek_rewind_scanner(ss);
    171        1.1   mycroft 		if (error)
    172  1.40.16.2       mrg 			return (error);
    173        1.1   mycroft 	}
    174        1.1   mycroft 
    175        1.1   mycroft 	/* size constraints: 8.5" horizontally and 14" vertically */
    176        1.1   mycroft #ifdef MUSTEK_INCH_SPEC
    177        1.1   mycroft 	/* sizes must be a multiple of 1/8" */
    178        1.1   mycroft 	sio->scan_x_origin -= sio->scan_x_origin % 150;
    179        1.1   mycroft 	sio->scan_y_origin -= sio->scan_y_origin % 150;
    180        1.1   mycroft 	sio->scan_width -= sio->scan_width % 150;
    181        1.1   mycroft 	sio->scan_height -= sio->scan_height % 150;
    182        1.1   mycroft #endif
    183        1.1   mycroft 	if (sio->scan_width == 0 ||
    184        1.1   mycroft 	    sio->scan_x_origin + sio->scan_width > 10200 ||
    185        1.1   mycroft 	    sio->scan_height == 0 ||
    186        1.1   mycroft 	    sio->scan_y_origin + sio->scan_height > 16800)
    187  1.40.16.2       mrg 		return (EINVAL);
    188        1.1   mycroft 
    189        1.1   mycroft 	/*
    190        1.1   mycroft 	 * for now, only realize the values for the MUSTEK_06000CX
    191        1.1   mycroft 	 * in the future, values for the MUSTEK_12000CX will be implemented
    192        1.1   mycroft 	 */
    193        1.1   mycroft 
    194        1.1   mycroft 	/*
    195        1.1   mycroft 	 * resolution (dpi) must be <= 300 and a multiple of 3 or
    196        1.1   mycroft 	 * between 300 and 600 and a multiple of 30
    197        1.1   mycroft 	 */
    198        1.1   mycroft 	sio->scan_x_resolution -= sio->scan_x_resolution <= 300 ?
    199        1.1   mycroft 	    sio->scan_x_resolution % 3 : sio->scan_x_resolution % 30;
    200        1.1   mycroft 	sio->scan_y_resolution -= sio->scan_y_resolution <= 300 ?
    201        1.1   mycroft 	    sio->scan_y_resolution % 3 : sio->scan_y_resolution % 30;
    202        1.1   mycroft 	if (sio->scan_x_resolution < 3 || sio->scan_x_resolution > 600 ||
    203        1.1   mycroft 	    sio->scan_x_resolution != sio->scan_y_resolution)
    204  1.40.16.2       mrg 		return (EINVAL);
    205        1.1   mycroft 
    206        1.1   mycroft 	/* assume brightness values are between 64 and 136 in steps of 3 */
    207        1.1   mycroft 	sio->scan_brightness -= (sio->scan_brightness - 64) % 3;
    208        1.1   mycroft 	if (sio->scan_brightness < 64 || sio->scan_brightness > 136)
    209  1.40.16.2       mrg 		return (EINVAL);
    210        1.1   mycroft 
    211        1.1   mycroft 	/* contrast values must be between 16 and 184 in steps of 7 */
    212        1.1   mycroft 	sio->scan_contrast -= (sio->scan_contrast - 16) % 7;
    213        1.1   mycroft 	if (sio->scan_contrast < 16 || sio->scan_contrast > 184)
    214  1.40.16.2       mrg 		return (EINVAL);
    215        1.1   mycroft 
    216        1.1   mycroft 	/*
    217        1.1   mycroft 	 * velocity: between 0 (fast) and 4 (slow) which will be mapped
    218        1.1   mycroft 	 * to 100% = 4, 80% = 3, 60% = 2, 40% = 1, 20% = 0
    219        1.1   mycroft 	 * must be a multiple of 20
    220        1.1   mycroft 	 */
    221        1.1   mycroft 	sio->scan_quality -= sio->scan_quality % 20;
    222        1.1   mycroft 	if (sio->scan_quality < 20 || sio->scan_quality > 100)
    223  1.40.16.2       mrg 		return (EINVAL);
    224        1.1   mycroft 
    225        1.1   mycroft 	switch (sio->scan_image_mode) {
    226        1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    227        1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    228        1.1   mycroft 	case SIM_GRAYSCALE:
    229        1.1   mycroft 	case SIM_RED:
    230        1.1   mycroft 	case SIM_GREEN:
    231        1.1   mycroft 	case SIM_BLUE:
    232        1.1   mycroft 		break;
    233        1.1   mycroft 	default:
    234  1.40.16.2       mrg 		return (EINVAL);
    235        1.1   mycroft 	}
    236        1.1   mycroft 
    237        1.1   mycroft 	/* change ss_softc to the new values, but save ro-variables */
    238        1.1   mycroft 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
    239       1.15   thorpej 	memcpy(&ss->sio, sio, sizeof(struct scan_io));
    240        1.1   mycroft 
    241        1.1   mycroft 	mustek_compute_sizes(ss);
    242        1.1   mycroft 
    243  1.40.16.2       mrg 	return (0);
    244        1.1   mycroft }
    245        1.1   mycroft 
    246        1.1   mycroft /*
    247        1.1   mycroft  * trim the requested transfer to a multiple of the line size
    248        1.1   mycroft  * this is called only from ssread() which guarantees, scanner is triggered
    249        1.1   mycroft  * In the future, it will trim the transfer to not read to much at a time
    250        1.1   mycroft  * because the mustek cannot disconnect. It will be calculated by the
    251        1.1   mycroft  * resolution, the velocity and the number of bytes per line.
    252        1.1   mycroft  */
    253       1.19   thorpej static void
    254       1.19   thorpej mustek_minphys(struct ss_softc *ss, struct buf *bp)
    255        1.1   mycroft {
    256       1.13    bouyer #ifdef SCSIPI_DEBUG
    257       1.13    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    258        1.3  christos #endif
    259        1.1   mycroft 
    260       1.26    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_minphys: before: %d\n",
    261        1.1   mycroft 	    bp->b_bcount));
    262        1.1   mycroft 	bp->b_bcount -= bp->b_bcount %
    263        1.1   mycroft 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    264       1.26    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_minphys: after:  %d\n",
    265        1.1   mycroft 	    bp->b_bcount));
    266        1.1   mycroft }
    267        1.1   mycroft 
    268        1.1   mycroft /*
    269        1.1   mycroft  * trigger the scanner to start a scan operation
    270        1.1   mycroft  * this includes sending the mode- and window-data, starting the scanner
    271        1.1   mycroft  * and getting the image size info
    272        1.1   mycroft  */
    273       1.19   thorpej static int
    274       1.19   thorpej mustek_trigger_scanner(struct ss_softc *ss)
    275        1.1   mycroft {
    276        1.1   mycroft 	struct mustek_mode_select_cmd mode_cmd;
    277        1.1   mycroft 	struct mustek_mode_select_data mode_data;
    278        1.1   mycroft 	struct mustek_set_window_cmd window_cmd;
    279        1.1   mycroft 	struct mustek_set_window_data window_data;
    280        1.1   mycroft 	struct mustek_start_scan_cmd start_scan_cmd;
    281       1.13    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    282        1.1   mycroft 	int pixel_tlx, pixel_tly, pixel_brx, pixel_bry, paperlength;
    283        1.1   mycroft 	int error;
    284        1.1   mycroft 
    285        1.1   mycroft 	mustek_compute_sizes(ss);
    286        1.1   mycroft 
    287       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner\n"));
    288        1.1   mycroft 
    289  1.40.16.2       mrg 	/*
    290  1.40.16.2       mrg 	 * set the window params and send the scsi command
    291  1.40.16.2       mrg 	 */
    292       1.14   thorpej 	memset(&window_cmd, 0, sizeof(window_cmd));
    293        1.1   mycroft 	window_cmd.opcode = MUSTEK_SET_WINDOW;
    294        1.1   mycroft 	window_cmd.length = sizeof(window_data);
    295        1.1   mycroft 
    296       1.14   thorpej 	memset(&window_data, 0, sizeof(window_data));
    297        1.2   mycroft 	window_data.frame.header = MUSTEK_LINEART_BACKGROUND | MUSTEK_UNIT_SPEC;
    298        1.1   mycroft #ifdef MUSTEK_INCH_SPEC
    299        1.1   mycroft 	/* the positional values are all 1 byte because 256 / 8 = 32" */
    300        1.2   mycroft 	pixel_tlx = ss->sio.scan_x_origin / 150;
    301        1.2   mycroft 	pixel_tly = ss->sio.scan_y_origin / 150;
    302        1.2   mycroft 	pixel_brx = pixel_tlx + ss->sio.scan_width / 150;
    303        1.2   mycroft 	pixel_bry = pixel_tly + ss->sio.scan_height / 150;
    304        1.1   mycroft #else
    305        1.1   mycroft 	pixel_tlx = (ss->sio.scan_x_origin * ss->sio.scan_x_resolution) / 1200;
    306        1.1   mycroft 	pixel_tly = (ss->sio.scan_y_origin * ss->sio.scan_y_resolution) / 1200;
    307        1.1   mycroft 	pixel_brx = pixel_tlx +
    308        1.1   mycroft 	    (ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
    309        1.1   mycroft 	pixel_bry = pixel_tly +
    310        1.1   mycroft 	    (ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
    311        1.1   mycroft #endif
    312        1.2   mycroft 	_lto2l(pixel_tlx, window_data.frame.tl_x);
    313        1.2   mycroft 	_lto2l(pixel_tly, window_data.frame.tl_y);
    314        1.2   mycroft 	_lto2l(pixel_brx, window_data.frame.br_x);
    315        1.2   mycroft 	_lto2l(pixel_bry, window_data.frame.br_y);
    316        1.1   mycroft 
    317        1.1   mycroft #if MUSTEK_WINDOWS >= 1
    318        1.2   mycroft 	window_data.window1 = window_data.frame;
    319        1.2   mycroft 	window_data.window1.header = MUSTEK_WINDOW_MASK | MUSTEK_UNIT_SPEC;
    320        1.1   mycroft #endif
    321        1.1   mycroft 
    322        1.1   mycroft 	/* send the set window command to the scanner */
    323       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_set_parms: set_window\n"));
    324       1.24   mycroft 	error = scsipi_command(periph, (void *)&window_cmd, sizeof(window_cmd),
    325       1.24   mycroft 	    (void *)&window_data, sizeof(window_data),
    326       1.39     rmind 	    MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_OUT);
    327        1.1   mycroft 	if (error)
    328  1.40.16.2       mrg 		return (error);
    329        1.1   mycroft 
    330        1.1   mycroft 	/*
    331        1.1   mycroft 	 * do what it takes to actualize the mode
    332        1.1   mycroft 	 */
    333       1.14   thorpej 	memset(&mode_cmd, 0, sizeof(mode_cmd));
    334        1.1   mycroft 	mode_cmd.opcode = MUSTEK_MODE_SELECT;
    335        1.2   mycroft 	_lto2b(sizeof(mode_data), mode_cmd.length);
    336        1.1   mycroft 
    337       1.14   thorpej 	memset(&mode_data, 0, sizeof(mode_data));
    338        1.1   mycroft 	mode_data.mode =
    339        1.1   mycroft 	    MUSTEK_MODE_MASK | MUSTEK_HT_PATTERN_BUILTIN | MUSTEK_UNIT_SPEC;
    340  1.40.16.2       mrg 	if (ss->sio.scan_x_resolution <= 300) {
    341        1.1   mycroft 		mode_data.resolution = ss->sio.scan_x_resolution / 3;
    342  1.40.16.2       mrg 	} else {
    343        1.1   mycroft 		/*
    344        1.1   mycroft 		 * the resolution values is computed by modulo 100, but not
    345        1.1   mycroft 		 * for 600dpi, where the value is 100 (a bit tricky, but ...)
    346        1.1   mycroft 		 */
    347        1.1   mycroft 		mode_data.resolution =
    348        1.1   mycroft 		    ((ss->sio.scan_x_resolution - 1) % 100) + 1;
    349  1.40.16.2       mrg 	}
    350        1.1   mycroft 	mode_data.brightness = (ss->sio.scan_brightness - 64) / 3;
    351        1.1   mycroft 	mode_data.contrast = (ss->sio.scan_contrast - 16) / 7;
    352        1.1   mycroft 	mode_data.grain = 0;
    353        1.1   mycroft 	mode_data.velocity = ss->sio.scan_quality / 20 - 1;
    354        1.1   mycroft #ifdef MUSTEK_INCH_SPEC
    355        1.2   mycroft 	paperlength = 14 * 8;	/* 14" */
    356        1.1   mycroft #else
    357        1.2   mycroft 	paperlength = 14 * ss->sio.scan_y_resolution;	/* 14" */
    358        1.1   mycroft #endif
    359        1.2   mycroft 	_lto2l(paperlength, mode_data.paperlength);
    360        1.1   mycroft 
    361       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: mode_select\n"));
    362        1.1   mycroft 	/* send the command to the scanner */
    363       1.24   mycroft 	error = scsipi_command(periph, (void *)&mode_cmd, sizeof(mode_cmd),
    364       1.24   mycroft 	    (void *)&mode_data, sizeof(mode_data),
    365       1.39     rmind 	    MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_OUT);
    366        1.1   mycroft 	if (error)
    367  1.40.16.2       mrg 		return (error);
    368        1.1   mycroft 
    369        1.1   mycroft 	/*
    370        1.1   mycroft 	 * now construct and send the start command
    371        1.1   mycroft 	 */
    372       1.14   thorpej 	memset(&start_scan_cmd, 0, sizeof(start_scan_cmd));
    373        1.1   mycroft 	start_scan_cmd.opcode = MUSTEK_START_STOP;
    374        1.1   mycroft 	start_scan_cmd.mode = MUSTEK_SCAN_START;
    375        1.1   mycroft 	if (ss->sio.scan_x_resolution <= 300)
    376        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_RES_STEP_1;
    377        1.1   mycroft 	else
    378        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_RES_STEP_10;
    379        1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    380        1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    381        1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    382        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_BIT_MODE | MUSTEK_GRAY_FILTER;
    383        1.1   mycroft 		break;
    384        1.1   mycroft 	case SIM_GRAYSCALE:
    385        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GRAY_FILTER;
    386        1.1   mycroft 		break;
    387        1.1   mycroft 	case SIM_RED:
    388        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_RED_FILTER;
    389        1.1   mycroft 		break;
    390        1.1   mycroft 	case SIM_GREEN:
    391        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GREEN_FILTER;
    392        1.1   mycroft 		break;
    393        1.1   mycroft 	case SIM_BLUE:
    394        1.1   mycroft 		start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_BLUE_FILTER;
    395        1.1   mycroft 		break;
    396        1.1   mycroft 	}
    397        1.1   mycroft 
    398        1.1   mycroft 	/* send the command to the scanner */
    399       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: start_scan\n"));
    400       1.23   mycroft 	error = scsipi_command(periph,
    401  1.40.16.2       mrg 	    (void *)&start_scan_cmd, sizeof(start_scan_cmd),
    402  1.40.16.2       mrg 	    NULL, 0,
    403        1.1   mycroft 	    MUSTEK_RETRIES, 5000, NULL, 0);
    404        1.1   mycroft 	if (error)
    405  1.40.16.2       mrg 		return (error);
    406        1.1   mycroft 
    407        1.1   mycroft 	/*
    408        1.1   mycroft 	 * now check if scanner ready this time with update of size info
    409        1.1   mycroft 	 * we wait here so that if the user issues a read directly afterwards,
    410        1.1   mycroft 	 * the scanner will respond directly (otherwise we had to sleep with
    411        1.1   mycroft 	 * a buffer locked in memory)
    412        1.1   mycroft 	 */
    413       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: get_status\n"));
    414        1.1   mycroft 	error = mustek_get_status(ss, 60, 1);
    415        1.1   mycroft 	if (error)
    416  1.40.16.2       mrg 		return (error);
    417        1.1   mycroft 
    418  1.40.16.2       mrg 	return (0);
    419        1.1   mycroft }
    420        1.1   mycroft 
    421  1.40.16.2       mrg /*
    422  1.40.16.2       mrg  * stop a scan operation in progress
    423  1.40.16.2       mrg  */
    424       1.19   thorpej static int
    425       1.19   thorpej mustek_rewind_scanner(struct ss_softc *ss)
    426        1.1   mycroft {
    427        1.1   mycroft 	struct mustek_start_scan_cmd cmd;
    428       1.13    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    429        1.1   mycroft 	int error;
    430        1.1   mycroft 
    431        1.1   mycroft 	if (ss->sio.scan_window_size != 0) {
    432        1.1   mycroft 		/*
    433        1.1   mycroft 		 * only if not all data has been read, the scanner has to be
    434        1.1   mycroft 		 * stopped
    435        1.1   mycroft 		 */
    436       1.14   thorpej 		memset(&cmd, 0, sizeof(cmd));
    437        1.1   mycroft 		cmd.opcode = MUSTEK_START_STOP;
    438        1.1   mycroft 		cmd.mode = MUSTEK_SCAN_STOP;
    439        1.1   mycroft 
    440        1.1   mycroft 		/* send the command to the scanner */
    441       1.13    bouyer 		SC_DEBUG(periph, SCSIPI_DB1,
    442        1.1   mycroft 		    ("mustek_rewind_scanner: stop_scan\n"));
    443       1.23   mycroft 		error = scsipi_command(periph,
    444       1.24   mycroft 		    (void *)&cmd, sizeof(cmd),
    445       1.24   mycroft 		    NULL, 0,
    446       1.24   mycroft 		    MUSTEK_RETRIES, 5000, NULL, 0);
    447        1.1   mycroft 		if (error)
    448  1.40.16.2       mrg 			return (error);
    449        1.1   mycroft 	}
    450        1.1   mycroft 
    451       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_rewind_scanner: end\n"));
    452        1.1   mycroft 
    453  1.40.16.2       mrg 	return (0);
    454        1.1   mycroft }
    455        1.1   mycroft 
    456  1.40.16.2       mrg /*
    457  1.40.16.2       mrg  * read the requested number of bytes/lines from the scanner
    458  1.40.16.2       mrg  */
    459       1.19   thorpej static int
    460       1.19   thorpej mustek_read(struct ss_softc *ss, struct buf *bp)
    461        1.1   mycroft {
    462        1.1   mycroft 	struct mustek_read_cmd cmd;
    463       1.21    bouyer 	struct scsipi_xfer *xs;
    464       1.13    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    465       1.30   reinoud 	u_long lines_to_read;
    466       1.10   mycroft 	int error;
    467        1.1   mycroft 
    468       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_read: start\n"));
    469        1.1   mycroft 
    470       1.14   thorpej 	memset(&cmd, 0, sizeof(cmd));
    471        1.1   mycroft 	cmd.opcode = MUSTEK_READ;
    472        1.1   mycroft 
    473        1.1   mycroft 	/* instead of the bytes, the mustek wants the number of lines */
    474        1.1   mycroft 	lines_to_read = bp->b_bcount /
    475        1.1   mycroft 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    476       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_read: read %ld lines\n",
    477        1.1   mycroft 	    lines_to_read));
    478        1.2   mycroft 	_lto3b(lines_to_read, cmd.length);
    479        1.1   mycroft 
    480  1.40.16.2       mrg 	/*
    481  1.40.16.2       mrg 	 * go ask the adapter to do all this for us
    482  1.40.16.2       mrg 	 */
    483       1.21    bouyer 	xs = scsipi_make_xs(periph,
    484        1.8     enami 	    (struct scsipi_generic *) &cmd, sizeof(cmd),
    485       1.30   reinoud 	    (u_char *) bp->b_data, bp->b_bcount,
    486       1.21    bouyer 	    MUSTEK_RETRIES, 10000, bp,
    487       1.11   thorpej 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
    488       1.21    bouyer 	if (xs == NULL) {
    489       1.21    bouyer 		/*
    490       1.21    bouyer 		 * out of memory. Keep this buffer in the queue, and
    491       1.21    bouyer 		 * retry later.
    492       1.21    bouyer 		 */
    493       1.21    bouyer 		callout_reset(&ss->sc_callout, hz / 2, ssrestart,
    494       1.21    bouyer 		    periph);
    495  1.40.16.2       mrg 		return(0);
    496        1.1   mycroft 	}
    497       1.20    bouyer #ifdef DIAGNOSTIC
    498       1.38      yamt 	if (bufq_get(ss->buf_queue) != bp)
    499       1.20    bouyer 		panic("ssstart(): dequeued wrong buf");
    500       1.20    bouyer #else
    501       1.38      yamt 	bufq_get(ss->buf_queue);
    502       1.20    bouyer #endif
    503       1.22   mycroft 	error = scsipi_execute_xs(xs);
    504       1.21    bouyer 	/* with a scsipi_xfer preallocated, scsipi_command can't fail */
    505       1.21    bouyer 	KASSERT(error == 0);
    506       1.21    bouyer 	ss->sio.scan_lines -= lines_to_read;
    507       1.33  christos #if 0
    508       1.21    bouyer 	if (ss->sio.scan_lines < 0)
    509       1.21    bouyer 		ss->sio.scan_lines = 0;
    510       1.33  christos #endif
    511       1.21    bouyer 	ss->sio.scan_window_size -= bp->b_bcount;
    512       1.33  christos #if 0
    513       1.21    bouyer 	if (ss->sio.scan_window_size < 0)
    514       1.21    bouyer 		ss->sio.scan_window_size = 0;
    515       1.33  christos #endif
    516  1.40.16.2       mrg 	return (0);
    517        1.1   mycroft }
    518        1.1   mycroft 
    519        1.1   mycroft /*
    520        1.1   mycroft  * check if the scanner is ready to take commands
    521        1.1   mycroft  *   wait timeout seconds and try only every second
    522        1.1   mycroft  *   if update, then update picture size info
    523        1.1   mycroft  *
    524        1.1   mycroft  *   returns EBUSY if scanner not ready
    525        1.1   mycroft  */
    526       1.19   thorpej static int
    527       1.19   thorpej mustek_get_status(struct ss_softc *ss, int timeout, int update)
    528        1.1   mycroft {
    529        1.1   mycroft 	struct mustek_get_status_cmd cmd;
    530        1.1   mycroft 	struct mustek_get_status_data data;
    531       1.13    bouyer 	struct scsipi_periph *periph = ss->sc_periph;
    532        1.1   mycroft 	int error, lines, bytes_per_line;
    533        1.1   mycroft 
    534       1.14   thorpej 	memset(&cmd, 0, sizeof(cmd));
    535        1.1   mycroft 	cmd.opcode = MUSTEK_GET_STATUS;
    536        1.1   mycroft 	cmd.length = sizeof(data);
    537        1.1   mycroft 
    538        1.1   mycroft 	while (1) {
    539       1.13    bouyer 		SC_DEBUG(periph, SCSIPI_DB1, ("mustek_get_status: stat_cmd\n"));
    540       1.24   mycroft 		error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
    541       1.24   mycroft 		    (void *)&data, sizeof(data),
    542       1.39     rmind 		    MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_IN);
    543        1.1   mycroft 		if (error)
    544  1.40.16.2       mrg 			return (error);
    545        1.1   mycroft 		if ((data.ready_busy == MUSTEK_READY) ||
    546        1.1   mycroft 		    (timeout-- <= 0))
    547        1.1   mycroft 			break;
    548        1.1   mycroft 		/* please wait a second */
    549       1.36  christos 		tsleep((void *)mustek_get_status, PRIBIO + 1, "mtkrdy", hz);
    550        1.1   mycroft 	}
    551        1.1   mycroft 
    552        1.1   mycroft 	if (update) {
    553        1.2   mycroft 		bytes_per_line = _2ltol(data.bytes_per_line);
    554        1.2   mycroft 		lines = _3ltol(data.lines);
    555        1.1   mycroft 		if (lines != ss->sio.scan_lines) {
    556        1.6  christos 			printf("mustek: lines actual(%d) != computed(%ld)\n",
    557        1.1   mycroft 			    lines, ss->sio.scan_lines);
    558  1.40.16.2       mrg 			return (EIO);
    559        1.1   mycroft 		}
    560        1.1   mycroft 		if (bytes_per_line * lines != ss->sio.scan_window_size) {
    561        1.6  christos 			printf("mustek: win-size actual(%d) != computed(%ld)\n",
    562        1.1   mycroft 			    bytes_per_line * lines, ss->sio.scan_window_size);
    563  1.40.16.2       mrg 		    return (EIO);
    564        1.1   mycroft 		}
    565        1.1   mycroft 
    566       1.13    bouyer 		SC_DEBUG(periph, SCSIPI_DB1,
    567        1.3  christos 		    ("mustek_get_size: bpl=%ld, lines=%ld\n",
    568  1.40.16.2       mrg 		    (ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8,
    569  1.40.16.2       mrg 		    ss->sio.scan_lines));
    570       1.13    bouyer 		SC_DEBUG(periph, SCSIPI_DB1, ("window size = %ld\n",
    571        1.1   mycroft 		    ss->sio.scan_window_size));
    572        1.1   mycroft 	}
    573        1.1   mycroft 
    574       1.13    bouyer 	SC_DEBUG(periph, SCSIPI_DB1, ("mustek_get_status: end\n"));
    575        1.1   mycroft 	if (data.ready_busy == MUSTEK_READY)
    576  1.40.16.2       mrg 		return (0);
    577        1.1   mycroft 	else
    578  1.40.16.2       mrg 		return (EBUSY);
    579        1.1   mycroft }
    580        1.1   mycroft 
    581        1.1   mycroft /*
    582        1.1   mycroft  * mustek_compute_sizes: compute window_size and lines for the picture
    583        1.1   mycroft  *   this function is called from different places in the code
    584        1.1   mycroft  */
    585       1.19   thorpej static void
    586       1.19   thorpej mustek_compute_sizes(struct ss_softc *ss)
    587        1.1   mycroft {
    588        1.1   mycroft 
    589        1.1   mycroft 	switch (ss->sio.scan_image_mode) {
    590        1.1   mycroft 	case SIM_BINARY_MONOCHROME:
    591        1.1   mycroft 	case SIM_DITHERED_MONOCHROME:
    592        1.1   mycroft 		ss->sio.scan_bits_per_pixel = 1;
    593        1.1   mycroft 		break;
    594        1.1   mycroft 	case SIM_GRAYSCALE:
    595        1.1   mycroft 	case SIM_RED:
    596        1.1   mycroft 	case SIM_GREEN:
    597        1.1   mycroft 	case SIM_BLUE:
    598        1.1   mycroft 		ss->sio.scan_bits_per_pixel = 8;
    599        1.1   mycroft 		break;
    600        1.1   mycroft 	}
    601        1.1   mycroft 
    602        1.1   mycroft 	/*
    603        1.1   mycroft 	 * horizontal number of bytes is always a multiple of 2,
    604        1.1   mycroft 	 * in 8-bit mode at least
    605        1.1   mycroft 	 */
    606        1.1   mycroft 	ss->sio.scan_pixels_per_line =
    607        1.1   mycroft 	    (ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
    608        1.1   mycroft 	if (ss->sio.scan_bits_per_pixel == 1)
    609        1.1   mycroft 		/* make it a multiple of 16, and thus of 2 bytes */
    610        1.1   mycroft 		ss->sio.scan_pixels_per_line =
    611        1.1   mycroft 		    (ss->sio.scan_pixels_per_line + 15) & 0xfffffff0;
    612        1.1   mycroft 	else
    613        1.1   mycroft 		ss->sio.scan_pixels_per_line =
    614        1.1   mycroft 		    (ss->sio.scan_pixels_per_line + 1) & 0xfffffffe;
    615        1.1   mycroft 
    616        1.1   mycroft 	ss->sio.scan_lines =
    617        1.1   mycroft 	    (ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
    618        1.8     enami 	ss->sio.scan_window_size = ss->sio.scan_lines *
    619        1.1   mycroft 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
    620        1.1   mycroft }
    621