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