1 /* $NetBSD: ss.c,v 1.41 2002/09/14 21:41:24 chs Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Kenneth Stailey. All rights reserved. 5 * modified for configurable scanner support by Joachim Koenig 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Kenneth Stailey. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ss.c,v 1.41 2002/09/14 21:41:24 chs Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/fcntl.h> 39 #include <sys/errno.h> 40 #include <sys/ioctl.h> 41 #include <sys/malloc.h> 42 #include <sys/buf.h> 43 #include <sys/proc.h> 44 #include <sys/user.h> 45 #include <sys/device.h> 46 #include <sys/conf.h> 47 #include <sys/vnode.h> 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 #include <dev/scsipi/ss_mustek.h> 57 58 #define SSMODE(z) ( minor(z) & 0x03) 59 #define SSUNIT(z) ((minor(z) >> 4) ) 60 #define SSNMINOR 16 61 62 /* 63 * If the mode is 3 (e.g. minor = 3,7,11,15) 64 * then the device has been openned to set defaults 65 * This mode does NOT ALLOW I/O, only ioctls 66 */ 67 #define MODE_REWIND 0 68 #define MODE_NONREWIND 1 69 #define MODE_CONTROL 3 70 71 int ssmatch(struct device *, struct cfdata *, void *); 72 void ssattach(struct device *, struct device *, void *); 73 int ssdetach(struct device *self, int flags); 74 int ssactivate(struct device *self, enum devact act); 75 76 struct cfattach ss_ca = { 77 sizeof(struct ss_softc), ssmatch, ssattach, ssdetach, ssactivate 78 }; 79 80 extern struct cfdriver ss_cd; 81 82 dev_type_open(ssopen); 83 dev_type_close(ssclose); 84 dev_type_read(ssread); 85 dev_type_ioctl(ssioctl); 86 87 const struct cdevsw ss_cdevsw = { 88 ssopen, ssclose, ssread, nowrite, ssioctl, 89 nostop, notty, nopoll, nommap, 90 }; 91 92 void ssstrategy __P((struct buf *)); 93 void ssstart __P((struct scsipi_periph *)); 94 void ssminphys __P((struct buf *)); 95 96 const struct scsipi_periphsw ss_switch = { 97 NULL, 98 ssstart, 99 NULL, 100 NULL, 101 }; 102 103 struct scsipi_inquiry_pattern ss_patterns[] = { 104 {T_SCANNER, T_FIXED, 105 "", "", ""}, 106 {T_SCANNER, T_REMOV, 107 "", "", ""}, 108 {T_PROCESSOR, T_FIXED, 109 "HP ", "C1130A ", ""}, 110 {T_PROCESSOR, T_FIXED, 111 "HP ", "C1750A ", ""}, 112 {T_PROCESSOR, T_FIXED, 113 "HP ", "C2500A ", ""}, 114 {T_PROCESSOR, T_FIXED, 115 "HP ", "C2520A ", ""}, 116 {T_PROCESSOR, T_FIXED, 117 "HP ", "C5110A ", ""}, 118 {T_PROCESSOR, T_FIXED, 119 "HP ", "C7670A ", ""}, 120 {T_PROCESSOR, T_FIXED, 121 "HP ", "", ""}, 122 }; 123 124 int 125 ssmatch(struct device *parent, struct cfdata *match, void *aux) 126 { 127 struct scsipibus_attach_args *sa = aux; 128 int priority; 129 130 (void)scsipi_inqmatch(&sa->sa_inqbuf, 131 (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]), 132 sizeof(ss_patterns[0]), &priority); 133 return (priority); 134 } 135 136 /* 137 * The routine called by the low level scsi routine when it discovers 138 * A device suitable for this driver 139 * If it is a know special, call special attach routine to install 140 * special handlers into the ss_softc structure 141 */ 142 void 143 ssattach(struct device *parent, struct device *self, void *aux) 144 { 145 struct ss_softc *ss = (void *)self; 146 struct scsipibus_attach_args *sa = aux; 147 struct scsipi_periph *periph = sa->sa_periph; 148 149 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: ")); 150 151 ss->flags |= SSF_AUTOCONF; 152 153 /* 154 * Store information needed to contact our base driver 155 */ 156 ss->sc_periph = periph; 157 periph->periph_dev = &ss->sc_dev; 158 periph->periph_switch = &ss_switch; 159 160 printf("\n"); 161 162 /* 163 * Set up the buf queue for this device 164 */ 165 bufq_alloc(&ss->buf_queue, BUFQ_FCFS); 166 167 /* 168 * look for non-standard scanners with help of the quirk table 169 * and install functions for special handling 170 */ 171 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n")); 172 if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0) 173 mustek_attach(ss, sa); 174 if (memcmp(sa->sa_inqbuf.vendor, "HP ", 8) == 0 && 175 memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0) 176 scanjet_attach(ss, sa); 177 if (ss->special == NULL) { 178 /* XXX add code to restart a SCSI2 scanner, if any */ 179 } 180 181 ss->flags &= ~SSF_AUTOCONF; 182 } 183 184 int 185 ssdetach(struct device *self, int flags) 186 { 187 struct ss_softc *ss = (struct ss_softc *) self; 188 struct buf *bp; 189 int s, cmaj, mn; 190 191 /* locate the major number */ 192 cmaj = cdevsw_lookup_major(&ss_cdevsw); 193 194 s = splbio(); 195 196 /* Kill off any queued buffers. */ 197 while ((bp = BUFQ_GET(&ss->buf_queue)) != NULL) { 198 bp->b_error = EIO; 199 bp->b_flags |= B_ERROR; 200 bp->b_resid = bp->b_bcount; 201 biodone(bp); 202 } 203 204 bufq_free(&ss->buf_queue); 205 206 /* Kill off any pending commands. */ 207 scsipi_kill_pending(ss->sc_periph); 208 209 splx(s); 210 211 /* Nuke the vnodes for any open instances */ 212 mn = SSUNIT(self->dv_unit); 213 vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR); 214 215 return (0); 216 } 217 218 int 219 ssactivate(struct device *self, enum devact act) 220 { 221 int rv = 0; 222 223 switch (act) { 224 case DVACT_ACTIVATE: 225 rv = EOPNOTSUPP; 226 break; 227 228 case DVACT_DEACTIVATE: 229 /* 230 * Nothing to do; we key off the device's DVF_ACTIVE. 231 */ 232 break; 233 } 234 return (rv); 235 } 236 237 /* 238 * open the device. 239 */ 240 int 241 ssopen(dev_t dev, int flag, int mode, struct proc *p) 242 { 243 int unit; 244 u_int ssmode; 245 int error; 246 struct ss_softc *ss; 247 struct scsipi_periph *periph; 248 struct scsipi_adapter *adapt; 249 250 unit = SSUNIT(dev); 251 if (unit >= ss_cd.cd_ndevs) 252 return (ENXIO); 253 ss = ss_cd.cd_devs[unit]; 254 if (!ss) 255 return (ENXIO); 256 257 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) 258 return (ENODEV); 259 260 ssmode = SSMODE(dev); 261 262 periph = ss->sc_periph; 263 adapt = periph->periph_channel->chan_adapter; 264 265 SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 266 unit, ss_cd.cd_ndevs)); 267 268 if (periph->periph_flags & PERIPH_OPEN) { 269 printf("%s: already open\n", ss->sc_dev.dv_xname); 270 return (EBUSY); 271 } 272 273 if ((error = scsipi_adapter_addref(adapt)) != 0) 274 return (error); 275 276 /* 277 * Catch any unit attention errors. 278 * 279 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners 280 * consider paper to be a changeable media 281 * 282 */ 283 error = scsipi_test_unit_ready(periph, 284 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST | 285 (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0)); 286 if (error) 287 goto bad; 288 289 periph->periph_flags |= PERIPH_OPEN; /* unit attn now errors */ 290 291 /* 292 * If the mode is 3 (e.g. minor = 3,7,11,15) 293 * then the device has been opened to set defaults 294 * This mode does NOT ALLOW I/O, only ioctls 295 */ 296 if (ssmode == MODE_CONTROL) 297 return (0); 298 299 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n")); 300 return (0); 301 302 bad: 303 scsipi_adapter_delref(adapt); 304 periph->periph_flags &= ~PERIPH_OPEN; 305 return (error); 306 } 307 308 /* 309 * close the device.. only called if we are the LAST 310 * occurence of an open device 311 */ 312 int 313 ssclose(dev_t dev, int flag, int mode, struct proc *p) 314 { 315 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 316 struct scsipi_periph *periph = ss->sc_periph; 317 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 318 int error; 319 320 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n")); 321 322 if (SSMODE(dev) == MODE_REWIND) { 323 if (ss->special && ss->special->rewind_scanner) { 324 /* call special handler to rewind/abort scan */ 325 error = (ss->special->rewind_scanner)(ss); 326 if (error) 327 return (error); 328 } else { 329 /* XXX add code to restart a SCSI2 scanner, if any */ 330 } 331 ss->sio.scan_window_size = 0; 332 ss->flags &= ~SSF_TRIGGERED; 333 } 334 335 scsipi_wait_drain(periph); 336 337 scsipi_adapter_delref(adapt); 338 periph->periph_flags &= ~PERIPH_OPEN; 339 340 return (0); 341 } 342 343 /* 344 * trim the size of the transfer if needed, 345 * called by physio 346 * basically the smaller of our min and the scsi driver's 347 * minphys 348 */ 349 void 350 ssminphys(struct buf *bp) 351 { 352 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)]; 353 struct scsipi_periph *periph = ss->sc_periph; 354 355 (*periph->periph_channel->chan_adapter->adapt_minphys)(bp); 356 357 /* 358 * trim the transfer further for special devices this is 359 * because some scanners only read multiples of a line at a 360 * time, also some cannot disconnect, so the read must be 361 * short enough to happen quickly 362 */ 363 if (ss->special && ss->special->minphys) 364 (ss->special->minphys)(ss, bp); 365 } 366 367 /* 368 * Do a read on a device for a user process. 369 * Prime scanner at start of read, check uio values, call ssstrategy 370 * via physio for the actual transfer. 371 */ 372 int 373 ssread(dev, uio, flag) 374 dev_t dev; 375 struct uio *uio; 376 int flag; 377 { 378 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 379 int error; 380 381 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) 382 return (ENODEV); 383 384 /* if the scanner has not yet been started, do it now */ 385 if (!(ss->flags & SSF_TRIGGERED)) { 386 if (ss->special && ss->special->trigger_scanner) { 387 error = (ss->special->trigger_scanner)(ss); 388 if (error) 389 return (error); 390 } 391 ss->flags |= SSF_TRIGGERED; 392 } 393 394 return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio)); 395 } 396 397 /* 398 * Actually translate the requested transfer into one the physical 399 * driver can understand The transfer is described by a buf and will 400 * include only one physical transfer. 401 */ 402 void 403 ssstrategy(bp) 404 struct buf *bp; 405 { 406 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)]; 407 struct scsipi_periph *periph = ss->sc_periph; 408 int s; 409 410 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, 411 ("ssstrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 412 413 /* 414 * If the device has been made invalid, error out 415 */ 416 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) { 417 bp->b_flags |= B_ERROR; 418 if (periph->periph_flags & PERIPH_OPEN) 419 bp->b_error = EIO; 420 else 421 bp->b_error = ENODEV; 422 goto done; 423 } 424 425 /* If negative offset, error */ 426 if (bp->b_blkno < 0) { 427 bp->b_flags |= B_ERROR; 428 bp->b_error = EINVAL; 429 goto done; 430 } 431 432 if (bp->b_bcount > ss->sio.scan_window_size) 433 bp->b_bcount = ss->sio.scan_window_size; 434 435 /* 436 * If it's a null transfer, return immediatly 437 */ 438 if (bp->b_bcount == 0) 439 goto done; 440 441 s = splbio(); 442 443 /* 444 * Place it in the queue of activities for this scanner 445 * at the end (a bit silly because we only have on user.. 446 * (but it could fork())) 447 */ 448 BUFQ_PUT(&ss->buf_queue, bp); 449 450 /* 451 * Tell the device to get going on the transfer if it's 452 * not doing anything, otherwise just wait for completion 453 * (All a bit silly if we're only allowing 1 open but..) 454 */ 455 ssstart(ss->sc_periph); 456 457 splx(s); 458 return; 459 bp->b_flags |= B_ERROR; 460 done: 461 /* 462 * Correctly set the buf to indicate a completed xfer 463 */ 464 bp->b_resid = bp->b_bcount; 465 biodone(bp); 466 } 467 468 /* 469 * ssstart looks to see if there is a buf waiting for the device 470 * and that the device is not already busy. If both are true, 471 * It dequeues the buf and creates a scsi command to perform the 472 * transfer required. The transfer request will call scsipi_done 473 * on completion, which will in turn call this routine again 474 * so that the next queued transfer is performed. 475 * The bufs are queued by the strategy routine (ssstrategy) 476 * 477 * This routine is also called after other non-queued requests 478 * have been made of the scsi driver, to ensure that the queue 479 * continues to be drained. 480 * ssstart() is called at splbio 481 */ 482 void 483 ssstart(periph) 484 struct scsipi_periph *periph; 485 { 486 struct ss_softc *ss = (void *)periph->periph_dev; 487 struct buf *bp; 488 489 SC_DEBUG(periph, SCSIPI_DB2, ("ssstart ")); 490 /* 491 * See if there is a buf to do and we are not already 492 * doing one 493 */ 494 while (periph->periph_active < periph->periph_openings) { 495 /* if a special awaits, let it proceed first */ 496 if (periph->periph_flags & PERIPH_WAITING) { 497 periph->periph_flags &= ~PERIPH_WAITING; 498 wakeup((caddr_t)periph); 499 return; 500 } 501 502 /* 503 * See if there is a buf with work for us to do.. 504 */ 505 if ((bp = BUFQ_GET(&ss->buf_queue)) == NULL) 506 return; 507 508 if (ss->special && ss->special->read) { 509 (ss->special->read)(ss, bp); 510 } else { 511 /* generic scsi2 scanner read */ 512 /* XXX add code for SCSI2 scanner read */ 513 } 514 } 515 } 516 517 /* 518 * Perform special action on behalf of the user; 519 * knows about the internals of this device 520 */ 521 int 522 ssioctl(dev, cmd, addr, flag, p) 523 dev_t dev; 524 u_long cmd; 525 caddr_t addr; 526 int flag; 527 struct proc *p; 528 { 529 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 530 int error = 0; 531 struct scan_io *sio; 532 533 if ((ss->sc_dev.dv_flags & DVF_ACTIVE) == 0) 534 return (ENODEV); 535 536 switch (cmd) { 537 case SCIOCGET: 538 if (ss->special && ss->special->get_params) { 539 /* call special handler */ 540 error = (ss->special->get_params)(ss); 541 if (error) 542 return (error); 543 } else { 544 /* XXX add code for SCSI2 scanner, if any */ 545 return (EOPNOTSUPP); 546 } 547 memcpy(addr, &ss->sio, sizeof(struct scan_io)); 548 break; 549 case SCIOCSET: 550 sio = (struct scan_io *)addr; 551 552 if (ss->special && ss->special->set_params) { 553 /* call special handler */ 554 error = (ss->special->set_params)(ss, sio); 555 if (error) 556 return (error); 557 } else { 558 /* XXX add code for SCSI2 scanner, if any */ 559 return (EOPNOTSUPP); 560 } 561 break; 562 case SCIOCRESTART: 563 if (ss->special && ss->special->rewind_scanner ) { 564 /* call special handler */ 565 error = (ss->special->rewind_scanner)(ss); 566 if (error) 567 return (error); 568 } else 569 /* XXX add code for SCSI2 scanner, if any */ 570 return (EOPNOTSUPP); 571 ss->flags &= ~SSF_TRIGGERED; 572 break; 573 #ifdef NOTYET 574 case SCAN_USE_ADF: 575 break; 576 #endif 577 default: 578 return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr, 579 flag, p)); 580 } 581 return (error); 582 } 583