2016-11-20 18:37:19 +03:00
|
|
|
/* $NetBSD: ss_mustek.c,v 1.43 2016/11/20 15:37:19 mlelstv Exp $ */
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Joachim Koenig-Baltes. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Joachim Koenig-Baltes.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* special driver for MUSTEK flatbed scanners MFS 06000CX and MFS 12000CX
|
|
|
|
* these scanners come with their own scsi card, containing an NCR53C400
|
|
|
|
* SCSI controller chip. I'm in the progress of writing a driver for this
|
|
|
|
* card to work under NetBSD-current. I've hooked it up to a Seagate ST01
|
|
|
|
* hostadapter in the meantime, giving 350KB/sec for higher resolutions!
|
|
|
|
*
|
|
|
|
* I tried to connect it to my Adaptec 1542B, but with no success. It seems,
|
|
|
|
* it does not like synchronous negotiation between Hostadapter and other
|
|
|
|
* targets, but I could not turn this off for the 1542B.
|
|
|
|
*
|
|
|
|
* There is also an other reason why you would not like to connect it to your
|
|
|
|
* favourite SCSI host adapter: The Mustek DOES NOT DISCONNECT. It will block
|
|
|
|
* other traffic from the bus while a transfer is active.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 09:54:32 +03:00
|
|
|
#include <sys/cdefs.h>
|
2016-11-20 18:37:19 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ss_mustek.c,v 1.43 2016/11/20 15:37:19 mlelstv Exp $");
|
2001-11-13 09:54:32 +03:00
|
|
|
|
1996-02-18 23:32:40 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/buf.h>
|
2004-10-28 11:07:35 +04:00
|
|
|
#include <sys/bufq.h>
|
1996-02-18 23:32:40 +03:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h> /* for cdevsw */
|
|
|
|
#include <sys/scanio.h>
|
|
|
|
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <dev/scsipi/scsipi_all.h>
|
2004-09-18 04:21:03 +04:00
|
|
|
#include <dev/scsipi/scsi_all.h>
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <dev/scsipi/scsi_scanner.h>
|
2004-09-18 04:21:03 +04:00
|
|
|
#include <dev/scsipi/scsipiconf.h>
|
2004-09-09 23:35:30 +04:00
|
|
|
#include <dev/scsipi/scsipi_base.h>
|
1997-08-27 15:22:52 +04:00
|
|
|
#include <dev/scsipi/ssvar.h>
|
|
|
|
#include <dev/scsipi/ss_mustek.h>
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
#define MUSTEK_RETRIES 4
|
|
|
|
|
2004-08-22 02:02:31 +04:00
|
|
|
static int mustek_get_params(struct ss_softc *);
|
|
|
|
static int mustek_set_params(struct ss_softc *, struct scan_io *);
|
|
|
|
static int mustek_trigger_scanner(struct ss_softc *);
|
|
|
|
static void mustek_minphys(struct ss_softc *, struct buf *);
|
|
|
|
static int mustek_read(struct ss_softc *, struct buf *);
|
|
|
|
static int mustek_rewind_scanner(struct ss_softc *);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/* only used internally */
|
2004-08-22 02:02:31 +04:00
|
|
|
static int mustek_get_status(struct ss_softc *, int, int);
|
|
|
|
static void mustek_compute_sizes(struct ss_softc *);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* structure for the special handlers */
|
2004-08-22 02:02:31 +04:00
|
|
|
static struct ss_special mustek_special = {
|
1996-02-18 23:32:40 +03:00
|
|
|
mustek_set_params,
|
|
|
|
mustek_trigger_scanner,
|
|
|
|
mustek_get_params,
|
|
|
|
mustek_minphys,
|
|
|
|
mustek_read,
|
|
|
|
mustek_rewind_scanner,
|
|
|
|
NULL, /* no adf support right now */
|
|
|
|
NULL /* no adf support right now */
|
|
|
|
};
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* mustek_attach: attach special functions to ss */
|
1996-02-18 23:32:40 +03:00
|
|
|
void
|
2004-08-22 02:02:31 +04:00
|
|
|
mustek_attach(struct ss_softc *ss, struct scsipibus_attach_args *sa)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
2001-04-25 21:53:04 +04:00
|
|
|
#ifdef SCSIPI_DEBUG
|
|
|
|
struct scsipi_periph *periph = sa->sa_periph;
|
1996-03-31 00:47:00 +03:00
|
|
|
#endif
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_attach: start\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
ss->sio.scan_scanner_type = 0;
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
printf("\n%s: ", device_xname(ss->sc_dev));
|
1996-03-31 00:47:00 +03:00
|
|
|
|
1996-02-18 23:32:40 +03:00
|
|
|
/* first, check the model which determines resolutions */
|
2001-07-18 22:27:08 +04:00
|
|
|
if (!memcmp(sa->sa_inqbuf.product, "MFS-06000CX", 11)) {
|
1996-02-18 23:32:40 +03:00
|
|
|
ss->sio.scan_scanner_type = MUSTEK_06000CX;
|
2012-02-28 15:41:00 +04:00
|
|
|
printf("Mustek 6000CX Flatbed 3-pass color scanner, "
|
|
|
|
"3 - 600 dpi\n");
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
2001-07-18 22:27:08 +04:00
|
|
|
if (!memcmp(sa->sa_inqbuf.product, "MFS-12000CX", 11)) {
|
1996-02-18 23:32:40 +03:00
|
|
|
ss->sio.scan_scanner_type = MUSTEK_12000CX;
|
2012-02-28 15:41:00 +04:00
|
|
|
printf("Mustek 12000CX Flatbed 3-pass color scanner, "
|
|
|
|
"6 - 1200 dpi\n");
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_attach: scanner_type = %d\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
ss->sio.scan_scanner_type));
|
|
|
|
|
|
|
|
/* install special handlers */
|
|
|
|
ss->special = &mustek_special;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* populate the scanio struct with legal values
|
|
|
|
* the default should come from user space
|
|
|
|
*/
|
|
|
|
ss->sio.scan_width = 1200;
|
|
|
|
ss->sio.scan_height = 1200;
|
|
|
|
ss->sio.scan_x_resolution = 99;
|
|
|
|
ss->sio.scan_y_resolution = 99;
|
|
|
|
ss->sio.scan_x_origin = 0;
|
|
|
|
ss->sio.scan_y_origin = 0;
|
|
|
|
ss->sio.scan_brightness = 100;
|
|
|
|
ss->sio.scan_contrast = 100;
|
|
|
|
ss->sio.scan_quality = 100;
|
|
|
|
ss->sio.scan_image_mode = SIM_GRAYSCALE;
|
|
|
|
|
|
|
|
mustek_compute_sizes(ss);
|
|
|
|
}
|
|
|
|
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
mustek_get_params (struct ss_softc *ss)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the parameters if the mustek is capable of fulfilling it
|
|
|
|
* but don't send the command to the scanner in case the user wants
|
|
|
|
* to change parameters by more than one call
|
|
|
|
*/
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
|
|
|
mustek_set_params(struct ss_softc *ss, struct scan_io *sio)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* if the scanner is triggered, then rewind it */
|
1996-02-18 23:32:40 +03:00
|
|
|
if (ss->flags & SSF_TRIGGERED) {
|
|
|
|
error = mustek_rewind_scanner(ss);
|
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* size constraints: 8.5" horizontally and 14" vertically */
|
|
|
|
#ifdef MUSTEK_INCH_SPEC
|
|
|
|
/* sizes must be a multiple of 1/8" */
|
|
|
|
sio->scan_x_origin -= sio->scan_x_origin % 150;
|
|
|
|
sio->scan_y_origin -= sio->scan_y_origin % 150;
|
|
|
|
sio->scan_width -= sio->scan_width % 150;
|
|
|
|
sio->scan_height -= sio->scan_height % 150;
|
|
|
|
#endif
|
|
|
|
if (sio->scan_width == 0 ||
|
|
|
|
sio->scan_x_origin + sio->scan_width > 10200 ||
|
|
|
|
sio->scan_height == 0 ||
|
|
|
|
sio->scan_y_origin + sio->scan_height > 16800)
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* for now, only realize the values for the MUSTEK_06000CX
|
|
|
|
* in the future, values for the MUSTEK_12000CX will be implemented
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* resolution (dpi) must be <= 300 and a multiple of 3 or
|
|
|
|
* between 300 and 600 and a multiple of 30
|
|
|
|
*/
|
|
|
|
sio->scan_x_resolution -= sio->scan_x_resolution <= 300 ?
|
|
|
|
sio->scan_x_resolution % 3 : sio->scan_x_resolution % 30;
|
|
|
|
sio->scan_y_resolution -= sio->scan_y_resolution <= 300 ?
|
|
|
|
sio->scan_y_resolution % 3 : sio->scan_y_resolution % 30;
|
|
|
|
if (sio->scan_x_resolution < 3 || sio->scan_x_resolution > 600 ||
|
|
|
|
sio->scan_x_resolution != sio->scan_y_resolution)
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/* assume brightness values are between 64 and 136 in steps of 3 */
|
|
|
|
sio->scan_brightness -= (sio->scan_brightness - 64) % 3;
|
|
|
|
if (sio->scan_brightness < 64 || sio->scan_brightness > 136)
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/* contrast values must be between 16 and 184 in steps of 7 */
|
|
|
|
sio->scan_contrast -= (sio->scan_contrast - 16) % 7;
|
|
|
|
if (sio->scan_contrast < 16 || sio->scan_contrast > 184)
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* velocity: between 0 (fast) and 4 (slow) which will be mapped
|
|
|
|
* to 100% = 4, 80% = 3, 60% = 2, 40% = 1, 20% = 0
|
|
|
|
* must be a multiple of 20
|
|
|
|
*/
|
|
|
|
sio->scan_quality -= sio->scan_quality % 20;
|
|
|
|
if (sio->scan_quality < 20 || sio->scan_quality > 100)
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
switch (sio->scan_image_mode) {
|
|
|
|
case SIM_BINARY_MONOCHROME:
|
|
|
|
case SIM_DITHERED_MONOCHROME:
|
|
|
|
case SIM_GRAYSCALE:
|
|
|
|
case SIM_RED:
|
|
|
|
case SIM_GREEN:
|
|
|
|
case SIM_BLUE:
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-28 15:41:00 +04:00
|
|
|
return EINVAL;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* change ss_softc to the new values, but save ro-variables */
|
|
|
|
sio->scan_scanner_type = ss->sio.scan_scanner_type;
|
2001-07-18 22:25:41 +04:00
|
|
|
memcpy(&ss->sio, sio, sizeof(struct scan_io));
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
mustek_compute_sizes(ss);
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trim the requested transfer to a multiple of the line size
|
|
|
|
* this is called only from ssread() which guarantees, scanner is triggered
|
|
|
|
* In the future, it will trim the transfer to not read to much at a time
|
|
|
|
* because the mustek cannot disconnect. It will be calculated by the
|
|
|
|
* resolution, the velocity and the number of bytes per line.
|
|
|
|
*/
|
2004-08-22 02:02:31 +04:00
|
|
|
static void
|
|
|
|
mustek_minphys(struct ss_softc *ss, struct buf *bp)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
2001-04-25 21:53:04 +04:00
|
|
|
#ifdef SCSIPI_DEBUG
|
|
|
|
struct scsipi_periph *periph = ss->sc_periph;
|
1996-03-31 00:47:00 +03:00
|
|
|
#endif
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2004-10-17 21:49:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_minphys: before: %d\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
bp->b_bcount));
|
|
|
|
bp->b_bcount -= bp->b_bcount %
|
|
|
|
((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
|
2004-10-17 21:49:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_minphys: after: %d\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
bp->b_bcount));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trigger the scanner to start a scan operation
|
|
|
|
* this includes sending the mode- and window-data, starting the scanner
|
|
|
|
* and getting the image size info
|
|
|
|
*/
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
|
|
|
mustek_trigger_scanner(struct ss_softc *ss)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
struct mustek_mode_select_cmd mode_cmd;
|
|
|
|
struct mustek_mode_select_data mode_data;
|
|
|
|
struct mustek_set_window_cmd window_cmd;
|
|
|
|
struct mustek_set_window_data window_data;
|
|
|
|
struct mustek_start_scan_cmd start_scan_cmd;
|
2001-04-25 21:53:04 +04:00
|
|
|
struct scsipi_periph *periph = ss->sc_periph;
|
1996-02-18 23:32:40 +03:00
|
|
|
int pixel_tlx, pixel_tly, pixel_brx, pixel_bry, paperlength;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mustek_compute_sizes(ss);
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* set the window params and send the scsi command */
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&window_cmd, 0, sizeof(window_cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
window_cmd.opcode = MUSTEK_SET_WINDOW;
|
|
|
|
window_cmd.length = sizeof(window_data);
|
|
|
|
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&window_data, 0, sizeof(window_data));
|
1996-03-19 06:05:15 +03:00
|
|
|
window_data.frame.header = MUSTEK_LINEART_BACKGROUND | MUSTEK_UNIT_SPEC;
|
1996-02-18 23:32:40 +03:00
|
|
|
#ifdef MUSTEK_INCH_SPEC
|
|
|
|
/* the positional values are all 1 byte because 256 / 8 = 32" */
|
1996-03-19 06:05:15 +03:00
|
|
|
pixel_tlx = ss->sio.scan_x_origin / 150;
|
|
|
|
pixel_tly = ss->sio.scan_y_origin / 150;
|
|
|
|
pixel_brx = pixel_tlx + ss->sio.scan_width / 150;
|
|
|
|
pixel_bry = pixel_tly + ss->sio.scan_height / 150;
|
1996-02-18 23:32:40 +03:00
|
|
|
#else
|
|
|
|
pixel_tlx = (ss->sio.scan_x_origin * ss->sio.scan_x_resolution) / 1200;
|
|
|
|
pixel_tly = (ss->sio.scan_y_origin * ss->sio.scan_y_resolution) / 1200;
|
|
|
|
pixel_brx = pixel_tlx +
|
|
|
|
(ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
|
|
|
|
pixel_bry = pixel_tly +
|
|
|
|
(ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
|
|
|
|
#endif
|
1996-03-19 06:05:15 +03:00
|
|
|
_lto2l(pixel_tlx, window_data.frame.tl_x);
|
|
|
|
_lto2l(pixel_tly, window_data.frame.tl_y);
|
|
|
|
_lto2l(pixel_brx, window_data.frame.br_x);
|
|
|
|
_lto2l(pixel_bry, window_data.frame.br_y);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
#if MUSTEK_WINDOWS >= 1
|
1996-03-19 06:05:15 +03:00
|
|
|
window_data.window1 = window_data.frame;
|
|
|
|
window_data.window1.header = MUSTEK_WINDOW_MASK | MUSTEK_UNIT_SPEC;
|
1996-02-18 23:32:40 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* send the set window command to the scanner */
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_set_parms: set_window\n"));
|
2004-09-18 04:08:16 +04:00
|
|
|
error = scsipi_command(periph, (void *)&window_cmd, sizeof(window_cmd),
|
|
|
|
(void *)&window_data, sizeof(window_data),
|
2009-10-22 01:11:57 +04:00
|
|
|
MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_OUT);
|
1996-02-18 23:32:40 +03:00
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do what it takes to actualize the mode
|
|
|
|
*/
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&mode_cmd, 0, sizeof(mode_cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
mode_cmd.opcode = MUSTEK_MODE_SELECT;
|
1996-03-19 06:05:15 +03:00
|
|
|
_lto2b(sizeof(mode_data), mode_cmd.length);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&mode_data, 0, sizeof(mode_data));
|
1996-02-18 23:32:40 +03:00
|
|
|
mode_data.mode =
|
|
|
|
MUSTEK_MODE_MASK | MUSTEK_HT_PATTERN_BUILTIN | MUSTEK_UNIT_SPEC;
|
2012-02-28 15:41:00 +04:00
|
|
|
if (ss->sio.scan_x_resolution <= 300)
|
1996-02-18 23:32:40 +03:00
|
|
|
mode_data.resolution = ss->sio.scan_x_resolution / 3;
|
2012-02-28 15:41:00 +04:00
|
|
|
else
|
1996-02-18 23:32:40 +03:00
|
|
|
/*
|
|
|
|
* the resolution values is computed by modulo 100, but not
|
|
|
|
* for 600dpi, where the value is 100 (a bit tricky, but ...)
|
|
|
|
*/
|
|
|
|
mode_data.resolution =
|
|
|
|
((ss->sio.scan_x_resolution - 1) % 100) + 1;
|
2012-02-28 15:41:00 +04:00
|
|
|
|
1996-02-18 23:32:40 +03:00
|
|
|
mode_data.brightness = (ss->sio.scan_brightness - 64) / 3;
|
|
|
|
mode_data.contrast = (ss->sio.scan_contrast - 16) / 7;
|
|
|
|
mode_data.grain = 0;
|
|
|
|
mode_data.velocity = ss->sio.scan_quality / 20 - 1;
|
|
|
|
#ifdef MUSTEK_INCH_SPEC
|
1996-03-19 06:05:15 +03:00
|
|
|
paperlength = 14 * 8; /* 14" */
|
1996-02-18 23:32:40 +03:00
|
|
|
#else
|
1996-03-19 06:05:15 +03:00
|
|
|
paperlength = 14 * ss->sio.scan_y_resolution; /* 14" */
|
1996-02-18 23:32:40 +03:00
|
|
|
#endif
|
1996-03-19 06:05:15 +03:00
|
|
|
_lto2l(paperlength, mode_data.paperlength);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: mode_select\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
/* send the command to the scanner */
|
2004-09-18 04:08:16 +04:00
|
|
|
error = scsipi_command(periph, (void *)&mode_cmd, sizeof(mode_cmd),
|
|
|
|
(void *)&mode_data, sizeof(mode_data),
|
2009-10-22 01:11:57 +04:00
|
|
|
MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_OUT);
|
1996-02-18 23:32:40 +03:00
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now construct and send the start command
|
|
|
|
*/
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&start_scan_cmd, 0, sizeof(start_scan_cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
start_scan_cmd.opcode = MUSTEK_START_STOP;
|
|
|
|
start_scan_cmd.mode = MUSTEK_SCAN_START;
|
|
|
|
if (ss->sio.scan_x_resolution <= 300)
|
|
|
|
start_scan_cmd.mode |= MUSTEK_RES_STEP_1;
|
|
|
|
else
|
|
|
|
start_scan_cmd.mode |= MUSTEK_RES_STEP_10;
|
|
|
|
switch (ss->sio.scan_image_mode) {
|
|
|
|
case SIM_BINARY_MONOCHROME:
|
|
|
|
case SIM_DITHERED_MONOCHROME:
|
|
|
|
start_scan_cmd.mode |= MUSTEK_BIT_MODE | MUSTEK_GRAY_FILTER;
|
|
|
|
break;
|
|
|
|
case SIM_GRAYSCALE:
|
|
|
|
start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GRAY_FILTER;
|
|
|
|
break;
|
|
|
|
case SIM_RED:
|
|
|
|
start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_RED_FILTER;
|
|
|
|
break;
|
|
|
|
case SIM_GREEN:
|
|
|
|
start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_GREEN_FILTER;
|
|
|
|
break;
|
|
|
|
case SIM_BLUE:
|
|
|
|
start_scan_cmd.mode |= MUSTEK_GRAY_MODE | MUSTEK_BLUE_FILTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the command to the scanner */
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: start_scan\n"));
|
2004-09-18 03:43:17 +04:00
|
|
|
error = scsipi_command(periph,
|
2012-02-28 15:41:00 +04:00
|
|
|
(void *)&start_scan_cmd, sizeof(start_scan_cmd), NULL, 0,
|
1996-02-18 23:32:40 +03:00
|
|
|
MUSTEK_RETRIES, 5000, NULL, 0);
|
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* now check if scanner ready this time with update of size info
|
|
|
|
* we wait here so that if the user issues a read directly afterwards,
|
|
|
|
* the scanner will respond directly (otherwise we had to sleep with
|
|
|
|
* a buffer locked in memory)
|
|
|
|
*/
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_trigger_scanner: get_status\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
error = mustek_get_status(ss, 60, 1);
|
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* stop a scan operation in progress */
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
|
|
|
mustek_rewind_scanner(struct ss_softc *ss)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
struct mustek_start_scan_cmd cmd;
|
2001-04-25 21:53:04 +04:00
|
|
|
struct scsipi_periph *periph = ss->sc_periph;
|
1996-02-18 23:32:40 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (ss->sio.scan_window_size != 0) {
|
|
|
|
/*
|
|
|
|
* only if not all data has been read, the scanner has to be
|
|
|
|
* stopped
|
|
|
|
*/
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
cmd.opcode = MUSTEK_START_STOP;
|
|
|
|
cmd.mode = MUSTEK_SCAN_STOP;
|
|
|
|
|
|
|
|
/* send the command to the scanner */
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1,
|
1996-02-18 23:32:40 +03:00
|
|
|
("mustek_rewind_scanner: stop_scan\n"));
|
2004-09-18 03:43:17 +04:00
|
|
|
error = scsipi_command(periph,
|
2004-09-18 04:08:16 +04:00
|
|
|
(void *)&cmd, sizeof(cmd),
|
|
|
|
NULL, 0,
|
|
|
|
MUSTEK_RETRIES, 5000, NULL, 0);
|
1996-02-18 23:32:40 +03:00
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_rewind_scanner: end\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* read the requested number of bytes/lines from the scanner */
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
|
|
|
mustek_read(struct ss_softc *ss, struct buf *bp)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
struct mustek_read_cmd cmd;
|
2004-09-09 23:35:30 +04:00
|
|
|
struct scsipi_xfer *xs;
|
2001-04-25 21:53:04 +04:00
|
|
|
struct scsipi_periph *periph = ss->sc_periph;
|
2005-02-01 03:19:34 +03:00
|
|
|
u_long lines_to_read;
|
2013-10-25 20:01:56 +04:00
|
|
|
int error __diagused;
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_read: start\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
cmd.opcode = MUSTEK_READ;
|
|
|
|
|
|
|
|
/* instead of the bytes, the mustek wants the number of lines */
|
|
|
|
lines_to_read = bp->b_bcount /
|
|
|
|
((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_read: read %ld lines\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
lines_to_read));
|
1996-03-19 06:05:15 +03:00
|
|
|
_lto3b(lines_to_read, cmd.length);
|
1996-02-18 23:32:40 +03:00
|
|
|
|
2012-02-28 15:41:00 +04:00
|
|
|
/* go ask the adapter to do all this for us */
|
2016-11-20 18:37:19 +03:00
|
|
|
xs = scsipi_make_xs_locked(periph,
|
1997-10-01 05:18:38 +04:00
|
|
|
(struct scsipi_generic *) &cmd, sizeof(cmd),
|
2005-02-01 03:19:34 +03:00
|
|
|
(u_char *) bp->b_data, bp->b_bcount,
|
2004-09-09 23:35:30 +04:00
|
|
|
MUSTEK_RETRIES, 10000, bp,
|
1999-10-01 02:57:52 +04:00
|
|
|
XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
|
2004-09-09 23:35:30 +04:00
|
|
|
if (xs == NULL) {
|
|
|
|
/*
|
|
|
|
* out of memory. Keep this buffer in the queue, and
|
|
|
|
* retry later.
|
|
|
|
*/
|
|
|
|
callout_reset(&ss->sc_callout, hz / 2, ssrestart,
|
|
|
|
periph);
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.
*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.
Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
2004-08-28 00:37:28 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
2009-01-13 16:33:58 +03:00
|
|
|
if (bufq_get(ss->buf_queue) != bp)
|
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.
*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.
Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
2004-08-28 00:37:28 +04:00
|
|
|
panic("ssstart(): dequeued wrong buf");
|
|
|
|
#else
|
2009-01-13 16:33:58 +03:00
|
|
|
bufq_get(ss->buf_queue);
|
Improve handling of memory shortage, to fix problems like:
sd3(mpt0:0:1:0): unable to allocate scsipi_xfer
sd3: not queued, error 12
Havard Eidnes's analysis of this problem is that the scsipi_xfer pool is
competing for resources with other pools, including the the inode and vnode
pools which can grow quite large.
*_scsipi_cmd(): don't biodone the buffer if scsipi_make_xs() fails, let the
caller deal with the problem
start function of block devices drivers: dequeue the buffer after the
scsipi_command() call. If scsipi_command() fails with ENOMEM don't dequeue
the buffer, and schedule a callout to call the start function after
some delay.
scsipi_init(): prime the scsipi_xfer_pool with one page. This ensure that
there is always some scsipi_xfer to play with. If scsipi_command() fails
because of pool_get(), we're sure there will be resources available later,
when the pending commands have completed.
Reviewed by Jason Thorpe and Havard Eidnes.
Todo: remove the "unable to allocate scsipi_xfer" and "not queued, error %d"
printfs, but I choose to keep them for now, to help make sure the code does
what it should.
2004-08-28 00:37:28 +04:00
|
|
|
#endif
|
2004-09-18 03:35:13 +04:00
|
|
|
error = scsipi_execute_xs(xs);
|
2004-09-09 23:35:30 +04:00
|
|
|
/* with a scsipi_xfer preallocated, scsipi_command can't fail */
|
|
|
|
KASSERT(error == 0);
|
|
|
|
ss->sio.scan_lines -= lines_to_read;
|
2006-09-03 10:22:05 +04:00
|
|
|
#if 0
|
2004-09-09 23:35:30 +04:00
|
|
|
if (ss->sio.scan_lines < 0)
|
|
|
|
ss->sio.scan_lines = 0;
|
2006-09-03 10:22:05 +04:00
|
|
|
#endif
|
2004-09-09 23:35:30 +04:00
|
|
|
ss->sio.scan_window_size -= bp->b_bcount;
|
2006-09-03 10:22:05 +04:00
|
|
|
#if 0
|
2004-09-09 23:35:30 +04:00
|
|
|
if (ss->sio.scan_window_size < 0)
|
|
|
|
ss->sio.scan_window_size = 0;
|
2006-09-03 10:22:05 +04:00
|
|
|
#endif
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the scanner is ready to take commands
|
|
|
|
* wait timeout seconds and try only every second
|
|
|
|
* if update, then update picture size info
|
|
|
|
*
|
|
|
|
* returns EBUSY if scanner not ready
|
|
|
|
*/
|
2004-08-22 02:02:31 +04:00
|
|
|
static int
|
|
|
|
mustek_get_status(struct ss_softc *ss, int timeout, int update)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
struct mustek_get_status_cmd cmd;
|
|
|
|
struct mustek_get_status_data data;
|
2001-04-25 21:53:04 +04:00
|
|
|
struct scsipi_periph *periph = ss->sc_periph;
|
1996-02-18 23:32:40 +03:00
|
|
|
int error, lines, bytes_per_line;
|
|
|
|
|
2001-07-18 22:21:04 +04:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
1996-02-18 23:32:40 +03:00
|
|
|
cmd.opcode = MUSTEK_GET_STATUS;
|
|
|
|
cmd.length = sizeof(data);
|
|
|
|
|
|
|
|
while (1) {
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_get_status: stat_cmd\n"));
|
2004-09-18 04:08:16 +04:00
|
|
|
error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
|
|
|
|
(void *)&data, sizeof(data),
|
2009-10-22 01:11:57 +04:00
|
|
|
MUSTEK_RETRIES, 5000, NULL, XS_CTL_DATA_IN);
|
1996-02-18 23:32:40 +03:00
|
|
|
if (error)
|
2012-02-28 15:41:00 +04:00
|
|
|
return error;
|
1996-02-18 23:32:40 +03:00
|
|
|
if ((data.ready_busy == MUSTEK_READY) ||
|
|
|
|
(timeout-- <= 0))
|
|
|
|
break;
|
|
|
|
/* please wait a second */
|
2016-11-20 18:37:19 +03:00
|
|
|
kpause("mtkrdy", false, hz, NULL);
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (update) {
|
1996-03-19 06:05:15 +03:00
|
|
|
bytes_per_line = _2ltol(data.bytes_per_line);
|
|
|
|
lines = _3ltol(data.lines);
|
1996-02-18 23:32:40 +03:00
|
|
|
if (lines != ss->sio.scan_lines) {
|
1996-10-13 03:23:13 +04:00
|
|
|
printf("mustek: lines actual(%d) != computed(%ld)\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
lines, ss->sio.scan_lines);
|
2012-02-28 15:41:00 +04:00
|
|
|
return EIO;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
if (bytes_per_line * lines != ss->sio.scan_window_size) {
|
1996-10-13 03:23:13 +04:00
|
|
|
printf("mustek: win-size actual(%d) != computed(%ld)\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
bytes_per_line * lines, ss->sio.scan_window_size);
|
2012-02-28 15:41:00 +04:00
|
|
|
return EIO;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1,
|
1996-03-31 00:47:00 +03:00
|
|
|
("mustek_get_size: bpl=%ld, lines=%ld\n",
|
2012-02-28 15:41:00 +04:00
|
|
|
(ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel)
|
|
|
|
/ 8, ss->sio.scan_lines));
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("window size = %ld\n",
|
1996-02-18 23:32:40 +03:00
|
|
|
ss->sio.scan_window_size));
|
|
|
|
}
|
|
|
|
|
2001-04-25 21:53:04 +04:00
|
|
|
SC_DEBUG(periph, SCSIPI_DB1, ("mustek_get_status: end\n"));
|
1996-02-18 23:32:40 +03:00
|
|
|
if (data.ready_busy == MUSTEK_READY)
|
2012-02-28 15:41:00 +04:00
|
|
|
return 0;
|
1996-02-18 23:32:40 +03:00
|
|
|
else
|
2012-02-28 15:41:00 +04:00
|
|
|
return EBUSY;
|
1996-02-18 23:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mustek_compute_sizes: compute window_size and lines for the picture
|
|
|
|
* this function is called from different places in the code
|
|
|
|
*/
|
2004-08-22 02:02:31 +04:00
|
|
|
static void
|
|
|
|
mustek_compute_sizes(struct ss_softc *ss)
|
1996-02-18 23:32:40 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (ss->sio.scan_image_mode) {
|
|
|
|
case SIM_BINARY_MONOCHROME:
|
|
|
|
case SIM_DITHERED_MONOCHROME:
|
|
|
|
ss->sio.scan_bits_per_pixel = 1;
|
|
|
|
break;
|
|
|
|
case SIM_GRAYSCALE:
|
|
|
|
case SIM_RED:
|
|
|
|
case SIM_GREEN:
|
|
|
|
case SIM_BLUE:
|
|
|
|
ss->sio.scan_bits_per_pixel = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* horizontal number of bytes is always a multiple of 2,
|
|
|
|
* in 8-bit mode at least
|
|
|
|
*/
|
|
|
|
ss->sio.scan_pixels_per_line =
|
|
|
|
(ss->sio.scan_width * ss->sio.scan_x_resolution) / 1200;
|
|
|
|
if (ss->sio.scan_bits_per_pixel == 1)
|
|
|
|
/* make it a multiple of 16, and thus of 2 bytes */
|
|
|
|
ss->sio.scan_pixels_per_line =
|
|
|
|
(ss->sio.scan_pixels_per_line + 15) & 0xfffffff0;
|
|
|
|
else
|
|
|
|
ss->sio.scan_pixels_per_line =
|
|
|
|
(ss->sio.scan_pixels_per_line + 1) & 0xfffffffe;
|
|
|
|
|
|
|
|
ss->sio.scan_lines =
|
|
|
|
(ss->sio.scan_height * ss->sio.scan_y_resolution) / 1200;
|
1997-10-01 05:18:38 +04:00
|
|
|
ss->sio.scan_window_size = ss->sio.scan_lines *
|
1996-02-18 23:32:40 +03:00
|
|
|
((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
|
|
|
|
}
|