1 /* $NetBSD: ss_scanjet.c,v 1.14 1997/10/18 19:51:11 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 <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 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 scsipi_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 (scsipi_command(sc_link, 284 (struct scsipi_generic *) &cmd, sizeof(cmd), 285 (u_char *) bp->b_data, bp->b_bcount, SCANJET_RETRIES, 100000, bp, 286 SCSI_NOSLEEP | SCSI_DATA_IN) != SUCCESSFULLY_QUEUED) 287 printf("%s: not queued\n", ss->sc_dev.dv_xname); 288 else { 289 ss->sio.scan_window_size -= bp->b_bcount; 290 if (ss->sio.scan_window_size < 0) 291 ss->sio.scan_window_size = 0; 292 } 293 294 return (0); 295 } 296 297 298 /* 299 * Do a synchronous write. Used to send control messages. 300 */ 301 int 302 scanjet_ctl_write(ss, buf, size, flags) 303 struct ss_softc *ss; 304 char *buf; 305 u_int size; 306 int flags; 307 { 308 struct scsi_rw_scanner cmd; 309 310 bzero(&cmd, sizeof(cmd)); 311 cmd.opcode = WRITE; 312 _lto3b(size, cmd.len); 313 return (scsipi_command(ss->sc_link, 314 (struct scsipi_generic *) &cmd, 315 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL, 316 flags | SCSI_DATA_OUT)); 317 } 318 319 320 /* 321 * Do a synchronous read. Used to read responses to control messages. 322 */ 323 int 324 scanjet_ctl_read(ss, buf, size, flags) 325 struct ss_softc *ss; 326 char *buf; 327 u_int size; 328 int flags; 329 { 330 struct scsi_rw_scanner cmd; 331 332 bzero(&cmd, sizeof(cmd)); 333 cmd.opcode = READ; 334 _lto3b(size, cmd.len); 335 return (scsipi_command(ss->sc_link, 336 (struct scsipi_generic *) &cmd, 337 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL, 338 flags | SCSI_DATA_IN)); 339 } 340 341 342 #ifdef SCANJETDEBUG 343 static void show_es(char *es) 344 { 345 char *p = es; 346 while (*p) { 347 if (*p == '\033') 348 printf("[Esc]"); 349 else 350 printf("%c", *p); 351 ++p; 352 } 353 printf("\n"); 354 } 355 #endif 356 357 /* 358 * simulate SCSI_SET_WINDOW for ScanJets 359 */ 360 int 361 scanjet_set_window(ss) 362 struct ss_softc *ss; 363 { 364 char escape_codes[128], *p; 365 366 p = escape_codes; 367 368 p += sprintf(p, "\033*f%ldP", ss->sio.scan_width / 4); 369 p += sprintf(p, "\033*f%ldQ", ss->sio.scan_height / 4); 370 p += sprintf(p, "\033*f%ldX", ss->sio.scan_x_origin / 4); 371 p += sprintf(p, "\033*f%ldY", ss->sio.scan_y_origin / 4); 372 p += sprintf(p, "\033*a%dR", ss->sio.scan_x_resolution); 373 p += sprintf(p, "\033*a%dS", ss->sio.scan_y_resolution); 374 375 switch (ss->sio.scan_image_mode) { 376 case SIM_BINARY_MONOCHROME: 377 ss->sio.scan_bits_per_pixel = 1; 378 /* use "line art" mode */ 379 strcpy(p, "\033*a0T"); 380 p += strlen(p); 381 /* make image data be "min-is-white ala PBM */ 382 strcpy(p, "\033*a0I"); 383 p += strlen(p); 384 break; 385 case SIM_DITHERED_MONOCHROME: 386 ss->sio.scan_bits_per_pixel = 1; 387 /* use dithered mode */ 388 strcpy(p, "\033*a3T"); 389 p += strlen(p); 390 /* make image data be "min-is-white ala PBM */ 391 strcpy(p, "\033*a0I"); 392 p += strlen(p); 393 break; 394 case SIM_GRAYSCALE: 395 ss->sio.scan_bits_per_pixel = 8; 396 /* use grayscale mode */ 397 strcpy(p, "\033*a4T"); 398 p += strlen(p); 399 /* make image data be "min-is-black ala PGM */ 400 strcpy(p, "\033*a1I"); 401 p += strlen(p); 402 break; 403 case SIM_COLOR: 404 ss->sio.scan_bits_per_pixel = 24; 405 /* use RGB color mode */ 406 strcpy(p, "\033*a5T"); 407 p += strlen(p); 408 /* make image data be "min-is-black ala PPM */ 409 strcpy(p, "\033*a1I"); 410 p += strlen(p); 411 /* use pass-through matrix (disable NTSC) */ 412 strcpy(p, "\033*u2T"); 413 p += strlen(p); 414 break; 415 } 416 417 p += sprintf(p, "\033*a%dG", ss->sio.scan_bits_per_pixel); 418 p += sprintf(p, "\033*a%dL", (int)(ss->sio.scan_brightness) - 128); 419 p += sprintf(p, "\033*a%dK", (int)(ss->sio.scan_contrast) - 128); 420 421 return (scanjet_ctl_write(ss, escape_codes, p - escape_codes, 0)); 422 } 423 424 /* atoi() is from /sys/arch/amiga/dev/ite.c 425 and is only used in scanjet_compute_sizes */ 426 427 __inline static int 428 atoi(cp) 429 const char *cp; 430 { 431 int n; 432 433 for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++) 434 n = n * 10 + *cp - '0'; 435 436 return (n); 437 } 438 439 int 440 scanjet_compute_sizes(ss) 441 struct ss_softc *ss; 442 { 443 int error; 444 static char *wfail = "%s: interrogate write failed\n"; 445 static char *rfail = "%s: interrogate read failed\n"; 446 static char *dfail = "%s: bad data returned\n"; 447 char escape_codes[20]; 448 char response[20]; 449 char *p; 450 451 /* 452 * Deal with the fact that the HP ScanJet IIc uses 1/300" not 1/1200" 453 * as its base unit of measurement. PINT uses 1/1200" (yes I know 454 * ScanJet II's use decipoints as well but 1200 % 720 != 0) 455 */ 456 ss->sio.scan_width = (ss->sio.scan_width + 3) & 0xfffffffc; 457 ss->sio.scan_height = (ss->sio.scan_height + 3) & 0xfffffffc; 458 459 switch (ss->sio.scan_image_mode) { 460 case SIM_BINARY_MONOCHROME: 461 case SIM_DITHERED_MONOCHROME: 462 strcpy(escape_codes, "\033*s1025E"); /* bytes wide */ 463 break; 464 case SIM_GRAYSCALE: 465 case SIM_COLOR: 466 strcpy(escape_codes, "\033*s1024E"); /* pixels wide */ 467 break; 468 } 469 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0); 470 if (error) { 471 uprintf(wfail, ss->sc_dev.dv_xname); 472 return (error); 473 } 474 error = scanjet_ctl_read(ss, response, 20, 0); 475 if (error) { 476 uprintf(rfail, ss->sc_dev.dv_xname); 477 return (error); 478 } 479 p = strchr(response, 'd'); 480 if (p == 0) { 481 uprintf(dfail, ss->sc_dev.dv_xname); 482 return (EIO); 483 } 484 ss->sio.scan_pixels_per_line = atoi(p + 1); 485 if (ss->sio.scan_image_mode < SIM_GRAYSCALE) 486 ss->sio.scan_pixels_per_line *= 8; 487 488 strcpy(escape_codes, "\033*s1026E"); /* pixels high */ 489 error = scanjet_ctl_write(ss, escape_codes, strlen(escape_codes), 0); 490 if (error) { 491 uprintf(wfail, ss->sc_dev.dv_xname); 492 return (error); 493 } 494 error = scanjet_ctl_read(ss, response, 20, 0); 495 if (error) { 496 uprintf(rfail, ss->sc_dev.dv_xname); 497 return (error); 498 } 499 p = strchr(response, 'd'); 500 if (p == 0) { 501 uprintf(dfail, ss->sc_dev.dv_xname); 502 return (EIO); 503 } 504 ss->sio.scan_lines = atoi(p + 1); 505 506 ss->sio.scan_window_size = ss->sio.scan_lines * 507 ((ss->sio.scan_pixels_per_line * ss->sio.scan_bits_per_pixel) / 8); 508 509 return (0); 510 } 511