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