ss_scanjet.c revision 1.8 1 /* $NetBSD: ss_scanjet.c,v 1.8 1996/10/12 23:23:22 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 p += sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4);
362 p += sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4);
363 p += sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4);
364 p += sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4);
365 p += sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution);
366 p += sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution);
367
368 switch (ss->sio.scan_image_mode) {
369 case SIM_BINARY_MONOCHROME:
370 ss->sio.scan_bits_per_pixel = 1;
371 /* use "line art" mode */
372 strcpy(p, "\033*a0T");
373 p += strlen(p);
374 /* make image data be "min-is-white ala PBM */
375 strcpy(p, "\033*a0I");
376 p += strlen(p);
377 break;
378 case SIM_DITHERED_MONOCHROME:
379 ss->sio.scan_bits_per_pixel = 1;
380 /* use dithered mode */
381 strcpy(p, "\033*a3T");
382 p += strlen(p);
383 /* make image data be "min-is-white ala PBM */
384 strcpy(p, "\033*a0I");
385 p += strlen(p);
386 break;
387 case SIM_GRAYSCALE:
388 ss->sio.scan_bits_per_pixel = 8;
389 /* use grayscale mode */
390 strcpy(p, "\033*a4T");
391 p += strlen(p);
392 /* make image data be "min-is-black ala PGM */
393 strcpy(p, "\033*a1I");
394 p += strlen(p);
395 break;
396 case SIM_COLOR:
397 ss->sio.scan_bits_per_pixel = 24;
398 /* use RGB color mode */
399 strcpy(p, "\033*a5T");
400 p += strlen(p);
401 /* make image data be "min-is-black ala PPM */
402 strcpy(p, "\033*a1I");
403 p += strlen(p);
404 /* use pass-through matrix (disable NTSC) */
405 strcpy(p, "\033*u2T");
406 p += strlen(p);
407 break;
408 }
409
410 p += sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel);
411 p += sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128);
412 p += sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128);
413
414 return (scanjet_ctl_write(ss, escape_codes, p - escape_codes, 0));
415 }
416
417 /* atoi() and strchr() are from /sys/arch/amiga/dev/ite.c
418 and are only used in scanjet_compute_sizes */
419
420 __inline static int
421 atoi(cp)
422 const char *cp;
423 {
424 int n;
425
426 for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
427 n = n * 10 + *cp - '0';
428
429 return (n);
430 }
431
432 __inline static char *
433 strchr(cp, ch)
434 const char *cp;
435 char ch;
436 {
437 while (*cp && *cp != ch) cp++;
438 return (*cp ? (char *)cp : 0);
439 }
440
441 int
442 scanjet_compute_sizes(ss)
443 struct ss_softc *ss;
444 {
445 int error;
446 static char *wfail = "%s: interrogate write failed\n";
447 static char *rfail = "%s: interrogate read failed\n";
448 static char *dfail = "%s: bad data returned\n";
449 char escape_codes[20];
450 char response[20];
451 char *p;
452
453 /*
454 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200"
455 * as its base unit of measurement. PINT uses 1/1200" (yes I know
456 * ScanJet II's use decipoints as well but 1200 % 720 != 0)
457 */
458 ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc;
459 ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc;
460
461 switch (ss->sio.scan_image_mode) {
462 case SIM_BINARY_MONOCHROME:
463 case SIM_DITHERED_MONOCHROME:
464 strcpy(escape_codes, "\033*s1025E"); /* bytes wide */
465 break;
466 case SIM_GRAYSCALE:
467 case SIM_COLOR:
468 strcpy(escape_codes, "\033*s1024E"); /* pixels wide */
469 break;
470 }
471 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0);
472 if (error) {
473 uprintf(wfail, ss->sc_dev.dv_xname);
474 return (error);
475 }
476 error = scanjet_ctl_read(ss, response, 20, 0);
477 if (error) {
478 uprintf(rfail, ss->sc_dev.dv_xname);
479 return (error);
480 }
481 p = strchr(response, 'd');
482 if (p == 0) {
483 uprintf(dfail, ss->sc_dev.dv_xname);
484 return (EIO);
485 }
486 ss->sio.scan_pixels_per_line = atoi(p + 1);
487 if (ss->sio.scan_image_mode < SIM_GRAYSCALE)
488 ss->sio.scan_pixels_per_line *= 8;
489
490 strcpy(escape_codes, "\033*s1026E"); /* pixels high */
491 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0);
492 if (error) {
493 uprintf(wfail, ss->sc_dev.dv_xname);
494 return (error);
495 }
496 error = scanjet_ctl_read(ss, response, 20, 0);
497 if (error) {
498 uprintf(rfail, ss->sc_dev.dv_xname);
499 return (error);
500 }
501 p = strchr(response, 'd');
502 if (p == 0) {
503 uprintf(dfail, ss->sc_dev.dv_xname);
504 return (EIO);
505 }
506 ss->sio.scan_lines = atoi(p + 1);
507
508 ss->sio.scan_window_size = ss->sio.scan_lines *
509 ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8);
510
511 return (0);
512 }
513