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