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