xref: /netbsd-src/sys/dev/scsipi/ss_scanjet.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: ss_scanjet.c,v 1.32 2004/09/09 19:35:33 bouyer Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Kenneth Stailey.  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 Kenneth Stailey.
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 functions for the HP ScanJet IIc and IIcx
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ss_scanjet.c,v 1.32 2004/09/09 19:35:33 bouyer Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/user.h>
48 #include <sys/device.h>
49 #include <sys/conf.h>		/* for cdevsw */
50 #include <sys/scanio.h>
51 #include <sys/kernel.h>
52 
53 #include <dev/scsipi/scsi_all.h>
54 #include <dev/scsipi/scsipi_all.h>
55 #include <dev/scsipi/scsi_scanner.h>
56 #include <dev/scsipi/scsiconf.h>
57 #include <dev/scsipi/scsipi_base.h>
58 #include <dev/scsipi/ssvar.h>
59 
60 #define SCANJET_RETRIES 4
61 
62 static int	scanjet_get_params(struct ss_softc *);
63 static int	scanjet_set_params(struct ss_softc *, struct scan_io *);
64 static int	scanjet_trigger_scanner(struct ss_softc *);
65 static int	scanjet_read(struct ss_softc *, struct buf *);
66 
67 /* only used internally */
68 static int	scanjet_ctl_write(struct ss_softc *, char *, u_int);
69 static int	scanjet_ctl_read(struct ss_softc *, char *, u_int);
70 static int	scanjet_set_window(struct ss_softc *);
71 static int	scanjet_compute_sizes(struct ss_softc *);
72 
73 /*
74  * structure for the special handlers
75  */
76 static struct ss_special scanjet_special = {
77 	scanjet_set_params,
78 	scanjet_trigger_scanner,
79 	scanjet_get_params,
80 	NULL,			/* no special minphys */
81 	scanjet_read,		/* scsi 6-byte read */
82 	NULL,			/* no "rewind" code (yet?) */
83 	NULL,			/* no adf support right now */
84 	NULL			/* no adf support right now */
85 };
86 
87 /*
88  * scanjet_attach: attach special functions to ss
89  */
90 void
91 scanjet_attach(struct ss_softc *ss, struct scsipibus_attach_args *sa)
92 {
93 	int error;
94 
95 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("scanjet_attach: start\n"));
96 	ss->sio.scan_scanner_type = 0;
97 
98 	printf("%s: ", ss->sc_dev.dv_xname);
99 
100 	/* first, check the model (which determines nothing yet) */
101 
102 	if (!memcmp(sa->sa_inqbuf.product, "C1750A", 6)) {
103 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
104 		printf("HP ScanJet IIc");
105 	} else if (!memcmp(sa->sa_inqbuf.product, "C2500A", 6)) {
106 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
107 		printf("HP ScanJet IIcx");
108 	} else if (!memcmp(sa->sa_inqbuf.product, "C2520A", 6)) {
109 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
110 		printf("HP ScanJet 4c");
111 	} else if (!memcmp(sa->sa_inqbuf.product, "C1130A", 6)) {
112 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
113 		printf("HP ScanJet 4p");
114 	} else if (!memcmp(sa->sa_inqbuf.product, "C5110A", 6)) {
115 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
116 		printf("HP ScanJet 5p");
117 	} else {
118 		ss->sio.scan_scanner_type = HP_SCANJET_IIC;
119 		printf("HP ScanJet (unknown model)");
120 	}
121 
122 	SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
123 	    ("scanjet_attach: scanner_type = %d\n",
124 	    ss->sio.scan_scanner_type));
125 
126 	/* now install special handlers */
127 	ss->special = &scanjet_special;
128 
129 	/*
130 	 * populate the scanio struct with legal values
131 	 */
132 	ss->sio.scan_width		= 1200;
133 	ss->sio.scan_height		= 1200;
134 	ss->sio.scan_x_resolution	= 100;
135 	ss->sio.scan_y_resolution	= 100;
136 	ss->sio.scan_x_origin		= 0;
137 	ss->sio.scan_y_origin		= 0;
138 	ss->sio.scan_brightness		= 128;
139 	ss->sio.scan_contrast		= 128;
140 	ss->sio.scan_quality		= 100;
141 	ss->sio.scan_image_mode		= SIM_GRAYSCALE;
142 
143 	error = scanjet_set_window(ss);
144 	if (error) {
145 		printf(" set_window failed\n");
146 		return;
147 	}
148 	error = scanjet_compute_sizes(ss);
149 	if (error) {
150 		printf(" compute_sizes failed\n");
151 		return;
152 	}
153 
154 	printf("\n");
155 }
156 
157 static int
158 scanjet_get_params(struct ss_softc *ss)
159 {
160 
161 	return (0);
162 }
163 
164 /*
165  * check the parameters if the scanjet is capable of fulfilling it
166  * but don't send the command to the scanner in case the user wants
167  * to change parameters by more than one call
168  */
169 static int
170 scanjet_set_params(struct ss_softc *ss, struct scan_io *sio)
171 {
172 	int error;
173 
174 #if 0
175 	/*
176 	 * if the scanner is triggered, then rewind it
177 	 */
178 	if (ss->flags & SSF_TRIGGERED) {
179 		error = scanjet_rewind_scanner(ss);
180 		if (error)
181 			return (error);
182 	}
183 #endif
184 
185 	/* size constraints... */
186 	if (sio->scan_width == 0				 ||
187 	    sio->scan_x_origin + sio->scan_width > 10200 || /* 8.5" */
188 	    sio->scan_height == 0				 ||
189 	    sio->scan_y_origin + sio->scan_height > 16800)  /* 14" */
190 		return (EINVAL);
191 
192 	/* resolution (dpi)... */
193 	if (sio->scan_x_resolution < 100 ||
194 	    sio->scan_x_resolution > 400 ||
195 	    sio->scan_y_resolution < 100 ||
196 	    sio->scan_y_resolution > 400)
197 		return (EINVAL);
198 
199 	switch (sio->scan_image_mode) {
200 	case SIM_BINARY_MONOCHROME:
201 	case SIM_DITHERED_MONOCHROME:
202 	case SIM_GRAYSCALE:
203 	case SIM_COLOR:
204 		break;
205 	default:
206 		return (EINVAL);
207 	}
208 
209 	/* change ss_softc to the new values, but save ro-variables */
210 	sio->scan_scanner_type = ss->sio.scan_scanner_type;
211 	memcpy(&ss->sio, sio, sizeof(struct scan_io));
212 
213 	error = scanjet_set_window(ss);
214 	if (error) {
215 		uprintf("%s: set_window failed\n", ss->sc_dev.dv_xname);
216 		return (error);
217 	}
218 	error = scanjet_compute_sizes(ss);
219 	if (error) {
220 		uprintf("%s: compute_sizes failed\n", ss->sc_dev.dv_xname);
221 		return (error);
222 	}
223 
224 	return (0);
225 }
226 
227 /*
228  * trigger the scanner to start a scan operation
229  * this includes sending the mode- and window-data,
230  * and starting the scanner
231  */
232 static int
233 scanjet_trigger_scanner(struct ss_softc *ss)
234 {
235 	char escape_codes[20];
236 	int error;
237 
238 	error = scanjet_set_window(ss);
239 	if (error) {
240 		uprintf("%s: set_window failed\n", ss->sc_dev.dv_xname);
241 		return (error);
242 	}
243 	error = scanjet_compute_sizes(ss);
244 	if (error) {
245 		uprintf("%s: compute_sizes failed\n", ss->sc_dev.dv_xname);
246 		return (error);
247 	}
248 
249 	/* send "trigger" operation */
250 	strlcpy(escape_codes, "\033*f0S", sizeof(escape_codes));
251 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
252 	if (error) {
253 		uprintf("%s: trigger_scanner failed\n", ss->sc_dev.dv_xname);
254 		return (error);
255 	}
256 
257 	return (0);
258 }
259 
260 static int
261 scanjet_read(struct ss_softc *ss, struct buf *bp)
262 {
263 	struct scsi_rw_scanner cmd;
264 	struct scsipi_xfer *xs;
265 	struct scsipi_periph *periph = ss->sc_periph;
266 	int error;
267 
268 	/*
269 	 *  Fill out the scsi command
270 	 */
271 	memset(&cmd, 0, sizeof(cmd));
272 	cmd.opcode = READ;
273 
274 	/*
275 	 * Handle "fixed-block-mode" tape drives by using the
276 	 * block count instead of the length.
277 	 */
278 	_lto3b(bp->b_bcount, cmd.len);
279 
280 	/*
281 	 * go ask the adapter to do all this for us
282 	 */
283 	xs = scsipi_make_xs(periph,
284 	    (struct scsipi_generic *) &cmd, sizeof(cmd),
285 	    (u_char *) bp->b_data, bp->b_bcount,
286 	    SCANJET_RETRIES, 100000, bp,
287 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
288 	if (xs == NULL) {
289 		/*
290 		 * out of memory. Keep this buffer in the queue, and
291 		 * retry later.
292 		 */
293 		callout_reset(&ss->sc_callout, hz / 2, ssrestart,
294 		    periph);
295 		return(0);
296 	}
297 #ifdef DIAGNOSTIC
298 	if (BUFQ_GET(&ss->buf_queue) != bp)
299 		panic("ssstart(): dequeued wrong buf");
300 #else
301 	BUFQ_GET(&ss->buf_queue);
302 #endif
303 	error = scsipi_command(periph, xs,
304 	    (struct scsipi_generic *) &cmd, sizeof(cmd),
305 	    (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp,
306 	    XS_CTL_NOSLEEP | XS_CTL_ASYNC | XS_CTL_DATA_IN);
307 	/* with a scsipi_xfer preallocated, scsipi_command can't fail */
308 	KASSERT(error == 0);
309 	ss->sio.scan_window_size -= bp->b_bcount;
310 	if (ss->sio.scan_window_size < 0)
311 		ss->sio.scan_window_size = 0;
312 	return (0);
313 }
314 
315 
316 /*
317  * Do a synchronous write.  Used to send control messages.
318  */
319 static int
320 scanjet_ctl_write(struct ss_softc *ss, char *buf, u_int size)
321 {
322 	struct scsi_rw_scanner cmd;
323 	int flags;
324 
325 	flags = 0;
326 	if ((ss->flags & SSF_AUTOCONF) != 0)
327 		flags |= XS_CTL_DISCOVERY;
328 
329 	memset(&cmd, 0, sizeof(cmd));
330 	cmd.opcode = WRITE;
331 	_lto3b(size, cmd.len);
332 	return (scsipi_command(ss->sc_periph, NULL,
333 	    (struct scsipi_generic *) &cmd,
334 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
335 	    flags | XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
336 }
337 
338 
339 /*
340  * Do a synchronous read.  Used to read responses to control messages.
341  */
342 static int
343 scanjet_ctl_read(struct ss_softc *ss, char *buf, u_int size)
344 {
345 	struct scsi_rw_scanner cmd;
346 	int flags;
347 
348 	flags = 0;
349 	if ((ss->flags & SSF_AUTOCONF) != 0)
350 		flags |= XS_CTL_DISCOVERY;
351 
352 	memset(&cmd, 0, sizeof(cmd));
353 	cmd.opcode = READ;
354 	_lto3b(size, cmd.len);
355 	return (scsipi_command(ss->sc_periph, NULL,
356 	    (struct scsipi_generic *) &cmd,
357 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
358 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK));
359 }
360 
361 
362 #ifdef SCANJETDEBUG
363 static void
364 show_es(char *es)
365 {
366 	char *p = es;
367 	while (*p) {
368 		if (*p == '\033')
369 			printf("[Esc]");
370 		else
371 			printf("%c", *p);
372 		++p;
373 	}
374 	printf("\n");
375 }
376 #endif
377 
378 /*
379  * simulate SCSI_SET_WINDOW for ScanJets
380  */
381 static int
382 scanjet_set_window(struct ss_softc *ss)
383 {
384 	char escape_codes[128], *p, *ep;
385 
386 	p = escape_codes;
387 	ep = &escape_codes[128];
388 
389 	p += snprintf(p, ep - p, "\033*f%ldP", ss->sio.scan_width / 4);
390 	p += snprintf(p, ep - p, "\033*f%ldQ", ss->sio.scan_height / 4);
391 	p += snprintf(p, ep - p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
392 	p += snprintf(p, ep - p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
393 	p += snprintf(p, ep - p, "\033*a%dR", ss->sio.scan_x_resolution);
394 	p += snprintf(p, ep - p, "\033*a%dS", ss->sio.scan_y_resolution);
395 
396 	switch (ss->sio.scan_image_mode) {
397 	case SIM_BINARY_MONOCHROME:
398 		ss->sio.scan_bits_per_pixel = 1;
399 		/* use "line art" mode */
400 		strlcpy(p, "\033*a0T", ep - p);
401 		p += strlen(p);
402 		/* make image data be "min-is-white ala PBM */
403 		strlcpy(p, "\033*a0I", ep - p);
404 		p += strlen(p);
405 		break;
406 	case SIM_DITHERED_MONOCHROME:
407 		ss->sio.scan_bits_per_pixel = 1;
408 		/* use dithered mode */
409 		strlcpy(p, "\033*a3T", ep - p);
410 		p += strlen(p);
411 		/* make image data be "min-is-white ala PBM */
412 		strlcpy(p, "\033*a0I", ep - p);
413 		p += strlen(p);
414 		break;
415 	case SIM_GRAYSCALE:
416 		ss->sio.scan_bits_per_pixel = 8;
417 		/* use grayscale mode */
418 		strlcpy(p, "\033*a4T", ep - p);
419 		p += strlen(p);
420 		/* make image data be "min-is-black ala PGM */
421 		strlcpy(p, "\033*a1I", ep - p);
422 		p += strlen(p);
423 		break;
424 	case SIM_COLOR:
425 		ss->sio.scan_bits_per_pixel = 24;
426 		/* use RGB color mode */
427 		strlcpy(p, "\033*a5T", ep - p);
428 		p += strlen(p);
429 		/* make image data be "min-is-black ala PPM */
430 		strlcpy(p, "\033*a1I", ep - p);
431 		p += strlen(p);
432 		/* use pass-through matrix (disable NTSC) */
433 		strlcpy(p, "\033*u2T", ep - p);
434 		p += strlen(p);
435 		break;
436 	}
437 
438 	p += snprintf(p, ep - p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
439 	p += snprintf(p, ep - p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
440 	p += snprintf(p, ep - p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
441 
442 	return (scanjet_ctl_write(ss, escape_codes, p - escape_codes));
443 }
444 
445 static int
446 scanjet_compute_sizes(struct ss_softc *ss)
447 {
448 	int error;
449 	static const char *wfail = "%s: interrogate write failed\n";
450 	static const char *rfail = "%s: interrogate read failed\n";
451 	static const char *dfail = "%s: bad data returned\n";
452 	char escape_codes[20];
453 	char response[20];
454 	char *p;
455 
456 	/*
457 	 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
458 	 * as its base unit of measurement.  PINT uses 1/1200" (yes I know
459 	 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
460 	 */
461 	ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
462 	ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
463 
464 	switch (ss->sio.scan_image_mode) {
465 	case SIM_BINARY_MONOCHROME:
466 	case SIM_DITHERED_MONOCHROME:
467 		/* bytes wide */
468 		strlcpy(escape_codes, "\033*s1025E", sizeof(escape_codes));
469 		break;
470 	case SIM_GRAYSCALE:
471 	case SIM_COLOR:
472 		/* pixels wide */
473 		strlcpy(escape_codes, "\033*s1024E", sizeof(escape_codes));
474 		break;
475 	}
476 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
477 	if (error) {
478 		uprintf(wfail, ss->sc_dev.dv_xname);
479 		return (error);
480 	}
481 	error = scanjet_ctl_read(ss, response, 20);
482 	if (error) {
483 		uprintf(rfail, ss->sc_dev.dv_xname);
484 		return (error);
485 	}
486 	p = strchr(response, 'd');
487 	if (p == 0) {
488 		uprintf(dfail, ss->sc_dev.dv_xname);
489 		return (EIO);
490 	}
491 	ss->sio.scan_pixels_per_line = strtoul(p + 1, NULL, 10);
492 	if (ss->sio.scan_image_mode < SIM_GRAYSCALE)
493 		ss->sio.scan_pixels_per_line *= 8;
494 
495 	/* pixels high */
496 	strlcpy(escape_codes, "\033*s1026E", sizeof(escape_codes));
497 	error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes));
498 	if (error) {
499 		uprintf(wfail, ss->sc_dev.dv_xname);
500 		return (error);
501 	}
502 	error = scanjet_ctl_read(ss, response, 20);
503 	if (error) {
504 		uprintf(rfail, ss->sc_dev.dv_xname);
505 		return (error);
506 	}
507 	p = strchr(response, 'd');
508 	if (p == 0) {
509 		uprintf(dfail, ss->sc_dev.dv_xname);
510 		return (EIO);
511 	}
512 	ss->sio.scan_lines = strtoul(p + 1, NULL, 10);
513 
514 	ss->sio.scan_window_size = ss->sio.scan_lines *
515 	    ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
516 
517 	return (0);
518 }
519