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