1 /* $NetBSD: ss_scanjet.c,v 1.16 1998/04/22 19:44:19 pk 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 <dev/scsipi/scsi_all.h> 51 #include <dev/scsipi/scsipi_all.h> 52 #include <dev/scsipi/scsi_scanner.h> 53 #include <dev/scsipi/scsiconf.h> 54 #include <dev/scsipi/ssvar.h> 55 56 #define SCANJET_RETRIES 4 57 58 int scanjet_get_params __P((struct ss_softc *)); 59 int scanjet_set_params __P((struct ss_softc *, struct scan_io *)); 60 int scanjet_trigger_scanner __P((struct ss_softc *)); 61 int scanjet_read __P((struct ss_softc *, struct buf *)); 62 63 /* only used internally */ 64 int scanjet_ctl_write __P((struct ss_softc *, char *, u_int, int)); 65 int scanjet_ctl_read __P((struct ss_softc *, char *, u_int, int)); 66 int scanjet_set_window __P((struct ss_softc *)); 67 int scanjet_compute_sizes __P((struct ss_softc *)); 68 /* Maybe move to libkern? */ 69 __inline static int atoi __P((const 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 scsipibus_attach_args *sa; 93 { 94 #ifdef SCSIDEBUG 95 struct scsipi_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 if (!bcmp(sa->sa_inqbuf.product, "C5110A", 6)) { 119 ss->sio.scan_scanner_type = HP_SCANJET_IIC; 120 printf("HP ScanJet 5p"); 121 } 122 123 SC_DEBUG(sc_link, SDEV_DB1, ("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 bcopy(sio, &ss->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 strcpy(escape_codes, "\033*f0S"); 255 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0); 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_link *sc_link = ss->sc_link; 271 272 /* 273 * Fill out the scsi command 274 */ 275 bzero(&cmd, sizeof(cmd)); 276 cmd.opcode = READ; 277 278 /* 279 * Handle "fixed-block-mode" tape drives by using the 280 * block count instead of the length. 281 */ 282 _lto3b(bp->b_bcount, cmd.len); 283 284 /* 285 * go ask the adapter to do all this for us 286 */ 287 if (scsipi_command(sc_link, 288 (struct scsipi_generic *) &cmd, sizeof(cmd), 289 (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp, 290 SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED) 291 printf("%s: not queued\n", ss->sc_dev.dv_xname); 292 else { 293 ss->sio.scan_window_size -= bp->b_bcount; 294 if (ss->sio.scan_window_size < 0) 295 ss->sio.scan_window_size = 0; 296 } 297 298 return (0); 299 } 300 301 302 /* 303 * Do a synchronous write. Used to send control messages. 304 */ 305 int 306 scanjet_ctl_write(ss, buf, size, flags) 307 struct ss_softc *ss; 308 char *buf; 309 u_int size; 310 int flags; 311 { 312 struct scsi_rw_scanner cmd; 313 314 if ((ss->flags & SSF_AUTOCONF) != 0) 315 flags |= SCSI_AUTOCONF; 316 317 bzero(&cmd, sizeof(cmd)); 318 cmd.opcode = WRITE; 319 _lto3b(size, cmd.len); 320 return (scsipi_command(ss->sc_link, 321 (struct scsipi_generic *) &cmd, 322 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL, 323 flags | SCSI_DATA_OUT)); 324 } 325 326 327 /* 328 * Do a synchronous read. Used to read responses to control messages. 329 */ 330 int 331 scanjet_ctl_read(ss, buf, size, flags) 332 struct ss_softc *ss; 333 char *buf; 334 u_int size; 335 int flags; 336 { 337 struct scsi_rw_scanner cmd; 338 339 if ((ss->flags & SSF_AUTOCONF) != 0) 340 flags |= SCSI_AUTOCONF; 341 342 bzero(&cmd, sizeof(cmd)); 343 cmd.opcode = READ; 344 _lto3b(size, cmd.len); 345 return (scsipi_command(ss->sc_link, 346 (struct scsipi_generic *) &cmd, 347 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL, 348 flags | SCSI_DATA_IN)); 349 } 350 351 352 #ifdef SCANJETDEBUG 353 static void show_es(char *es) 354 { 355 char *p = es; 356 while (*p) { 357 if (*p == '\033') 358 printf("[Esc]"); 359 else 360 printf("%c", *p); 361 ++p; 362 } 363 printf("\n"); 364 } 365 #endif 366 367 /* 368 * simulate SCSI_SET_WINDOW for ScanJets 369 */ 370 int 371 scanjet_set_window(ss) 372 struct ss_softc *ss; 373 { 374 char escape_codes[128], *p; 375 376 p = escape_codes; 377 378 p += sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4); 379 p += sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4); 380 p += sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4); 381 p += sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4); 382 p += sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution); 383 p += sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution); 384 385 switch (ss->sio.scan_image_mode) { 386 case SIM_BINARY_MONOCHROME: 387 ss->sio.scan_bits_per_pixel = 1; 388 /* use "line art" mode */ 389 strcpy(p, "\033*a0T"); 390 p += strlen(p); 391 /* make image data be "min-is-white ala PBM */ 392 strcpy(p, "\033*a0I"); 393 p += strlen(p); 394 break; 395 case SIM_DITHERED_MONOCHROME: 396 ss->sio.scan_bits_per_pixel = 1; 397 /* use dithered mode */ 398 strcpy(p, "\033*a3T"); 399 p += strlen(p); 400 /* make image data be "min-is-white ala PBM */ 401 strcpy(p, "\033*a0I"); 402 p += strlen(p); 403 break; 404 case SIM_GRAYSCALE: 405 ss->sio.scan_bits_per_pixel = 8; 406 /* use grayscale mode */ 407 strcpy(p, "\033*a4T"); 408 p += strlen(p); 409 /* make image data be "min-is-black ala PGM */ 410 strcpy(p, "\033*a1I"); 411 p += strlen(p); 412 break; 413 case SIM_COLOR: 414 ss->sio.scan_bits_per_pixel = 24; 415 /* use RGB color mode */ 416 strcpy(p, "\033*a5T"); 417 p += strlen(p); 418 /* make image data be "min-is-black ala PPM */ 419 strcpy(p, "\033*a1I"); 420 p += strlen(p); 421 /* use pass-through matrix (disable NTSC) */ 422 strcpy(p, "\033*u2T"); 423 p += strlen(p); 424 break; 425 } 426 427 p += sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel); 428 p += sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128); 429 p += sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128); 430 431 return (scanjet_ctl_write(ss, escape_codes, p - escape_codes, 0)); 432 } 433 434 /* atoi() is from /sys/arch/amiga/dev/ite.c 435 and is only used in scanjet_compute_sizes */ 436 437 __inline static int 438 atoi(cp) 439 const char *cp; 440 { 441 int n; 442 443 for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++) 444 n = n * 10 + *cp - '0'; 445 446 return (n); 447 } 448 449 int 450 scanjet_compute_sizes(ss) 451 struct ss_softc *ss; 452 { 453 int error; 454 static char *wfail = "%s: interrogate write failed\n"; 455 static char *rfail = "%s: interrogate read failed\n"; 456 static char *dfail = "%s: bad data returned\n"; 457 char escape_codes[20]; 458 char response[20]; 459 char *p; 460 461 /* 462 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200" 463 * as its base unit of measurement. PINT uses 1/1200" (yes I know 464 * ScanJet II's use decipoints as well but 1200 % 720 != 0) 465 */ 466 ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc; 467 ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc; 468 469 switch (ss->sio.scan_image_mode) { 470 case SIM_BINARY_MONOCHROME: 471 case SIM_DITHERED_MONOCHROME: 472 strcpy(escape_codes, "\033*s1025E"); /* bytes wide */ 473 break; 474 case SIM_GRAYSCALE: 475 case SIM_COLOR: 476 strcpy(escape_codes, "\033*s1024E"); /* pixels wide */ 477 break; 478 } 479 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0); 480 if (error) { 481 uprintf(wfail, ss->sc_dev.dv_xname); 482 return (error); 483 } 484 error = scanjet_ctl_read(ss, response, 20, 0); 485 if (error) { 486 uprintf(rfail, ss->sc_dev.dv_xname); 487 return (error); 488 } 489 p = strchr(response, 'd'); 490 if (p == 0) { 491 uprintf(dfail, ss->sc_dev.dv_xname); 492 return (EIO); 493 } 494 ss->sio.scan_pixels_per_line = atoi(p + 1); 495 if (ss->sio.scan_image_mode < SIM_GRAYSCALE) 496 ss->sio.scan_pixels_per_line *= 8; 497 498 strcpy(escape_codes, "\033*s1026E"); /* pixels high */ 499 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0); 500 if (error) { 501 uprintf(wfail, ss->sc_dev.dv_xname); 502 return (error); 503 } 504 error = scanjet_ctl_read(ss, response, 20, 0); 505 if (error) { 506 uprintf(rfail, ss->sc_dev.dv_xname); 507 return (error); 508 } 509 p = strchr(response, 'd'); 510 if (p == 0) { 511 uprintf(dfail, ss->sc_dev.dv_xname); 512 return (EIO); 513 } 514 ss->sio.scan_lines = atoi(p + 1); 515 516 ss->sio.scan_window_size = ss->sio.scan_lines * 517 ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8); 518 519 return (0); 520 } 521