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