1 /* $NetBSD: ss.c,v 1.18 1997/11/19 03:03:15 augustss 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/types.h> 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/fcntl.h> 37 #include <sys/errno.h> 38 #include <sys/ioctl.h> 39 #include <sys/malloc.h> 40 #include <sys/buf.h> 41 #include <sys/proc.h> 42 #include <sys/user.h> 43 #include <sys/device.h> 44 #include <sys/conf.h> 45 #include <sys/scanio.h> 46 47 #include <dev/scsipi/scsi_all.h> 48 #include <dev/scsipi/scsipi_all.h> 49 #include <dev/scsipi/scsi_scanner.h> 50 #include <dev/scsipi/scsiconf.h> 51 #include <dev/scsipi/ssvar.h> 52 53 #include <dev/scsipi/ss_mustek.h> 54 55 #define SSMODE(z) ( minor(z) & 0x03) 56 #define SSUNIT(z) ((minor(z) >> 4) ) 57 58 /* 59 * If the mode is 3 (e.g. minor = 3,7,11,15) 60 * then the device has been openned to set defaults 61 * This mode does NOT ALLOW I/O, only ioctls 62 */ 63 #define MODE_REWIND 0 64 #define MODE_NONREWIND 1 65 #define MODE_CONTROL 3 66 67 #ifdef __BROKEN_INDIRECT_CONFIG 68 int ssmatch __P((struct device *, void *, void *)); 69 #else 70 int ssmatch __P((struct device *, struct cfdata *, void *)); 71 #endif 72 void ssattach __P((struct device *, struct device *, void *)); 73 74 struct cfattach ss_ca = { 75 sizeof(struct ss_softc), ssmatch, ssattach 76 }; 77 78 struct cfdriver ss_cd = { 79 NULL, "ss", DV_DULL 80 }; 81 82 void ssstrategy __P((struct buf *)); 83 void ssstart __P((void *)); 84 void ssminphys __P((struct buf *)); 85 86 struct scsipi_device ss_switch = { 87 NULL, 88 ssstart, 89 NULL, 90 NULL, 91 }; 92 93 struct scsipi_inquiry_pattern ss_patterns[] = { 94 {T_SCANNER, T_FIXED, 95 "", "", ""}, 96 {T_SCANNER, T_REMOV, 97 "", "", ""}, 98 {T_PROCESSOR, T_FIXED, 99 "HP ", "C1750A ", ""}, 100 {T_PROCESSOR, T_FIXED, 101 "HP ", "C2500A ", ""}, 102 {T_PROCESSOR, T_FIXED, 103 "HP ", "C1130A ", ""}, 104 {T_PROCESSOR, T_FIXED, 105 "HP ", "C5110A ", ""}, 106 }; 107 108 int 109 ssmatch(parent, match, aux) 110 struct device *parent; 111 #ifdef __BROKEN_INDIRECT_CONFIG 112 void *match; 113 #else 114 struct cfdata *match; 115 #endif 116 void *aux; 117 { 118 struct scsipibus_attach_args *sa = aux; 119 int priority; 120 121 (void)scsipi_inqmatch(&sa->sa_inqbuf, 122 (caddr_t)ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]), 123 sizeof(ss_patterns[0]), &priority); 124 return (priority); 125 } 126 127 /* 128 * The routine called by the low level scsi routine when it discovers 129 * A device suitable for this driver 130 * If it is a know special, call special attach routine to install 131 * special handlers into the ss_softc structure 132 */ 133 void 134 ssattach(parent, self, aux) 135 struct device *parent, *self; 136 void *aux; 137 { 138 struct ss_softc *ss = (void *)self; 139 struct scsipibus_attach_args *sa = aux; 140 struct scsipi_link *sc_link = sa->sa_sc_link; 141 142 SC_DEBUG(sc_link, SDEV_DB2, ("ssattach: ")); 143 144 /* 145 * Store information needed to contact our base driver 146 */ 147 ss->sc_link = sc_link; 148 sc_link->device = &ss_switch; 149 sc_link->device_softc = ss; 150 sc_link->openings = 1; 151 152 /* 153 * look for non-standard scanners with help of the quirk table 154 * and install functions for special handling 155 */ 156 SC_DEBUG(sc_link, SDEV_DB2, ("ssattach:\n")); 157 if (!bcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6)) 158 mustek_attach(ss, sa); 159 if (!bcmp(sa->sa_inqbuf.vendor, "HP ", 8)) 160 scanjet_attach(ss, sa); 161 if (ss->special == NULL) { 162 /* XXX add code to restart a SCSI2 scanner, if any */ 163 } 164 165 /* 166 * Set up the buf queue for this device 167 */ 168 ss->buf_queue.b_active = 0; 169 ss->buf_queue.b_actf = 0; 170 ss->buf_queue.b_actb = &ss->buf_queue.b_actf; 171 } 172 173 /* 174 * open the device. 175 */ 176 int 177 ssopen(dev, flag, mode, p) 178 dev_t dev; 179 int flag; 180 int mode; 181 struct proc *p; 182 { 183 int unit; 184 u_int ssmode; 185 int error = 0; 186 struct ss_softc *ss; 187 struct scsipi_link *sc_link; 188 189 unit = SSUNIT(dev); 190 if (unit >= ss_cd.cd_ndevs) 191 return (ENXIO); 192 ss = ss_cd.cd_devs[unit]; 193 if (!ss) 194 return (ENXIO); 195 196 ssmode = SSMODE(dev); 197 sc_link = ss->sc_link; 198 199 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev, 200 unit, ss_cd.cd_ndevs)); 201 202 if (sc_link->flags & SDEV_OPEN) { 203 printf("%s: already open\n", ss->sc_dev.dv_xname); 204 return (EBUSY); 205 } 206 207 /* 208 * Catch any unit attention errors. 209 * 210 * SCSI_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners 211 * consider paper to be a changeable media 212 * 213 */ 214 error = scsipi_test_unit_ready(sc_link, 215 SCSI_IGNORE_MEDIA_CHANGE | SCSI_IGNORE_ILLEGAL_REQUEST | 216 (ssmode == MODE_CONTROL ? SCSI_IGNORE_NOT_READY : 0)); 217 if (error) 218 goto bad; 219 220 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */ 221 222 /* 223 * If the mode is 3 (e.g. minor = 3,7,11,15) 224 * then the device has been opened to set defaults 225 * This mode does NOT ALLOW I/O, only ioctls 226 */ 227 if (ssmode == MODE_CONTROL) 228 return (0); 229 230 SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n")); 231 return (0); 232 233 bad: 234 sc_link->flags &= ~SDEV_OPEN; 235 return (error); 236 } 237 238 /* 239 * close the device.. only called if we are the LAST 240 * occurence of an open device 241 */ 242 int 243 ssclose(dev, flag, mode, p) 244 dev_t dev; 245 int flag; 246 int mode; 247 struct proc *p; 248 { 249 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 250 int error; 251 252 SC_DEBUG(ss->sc_link, SDEV_DB1, ("closing\n")); 253 254 if (SSMODE(dev) == MODE_REWIND) { 255 if (ss->special->rewind_scanner) { 256 /* call special handler to rewind/abort scan */ 257 error = (ss->special->rewind_scanner)(ss); 258 if (error) 259 return (error); 260 } else { 261 /* XXX add code to restart a SCSI2 scanner, if any */ 262 } 263 ss->sio.scan_window_size = 0; 264 ss->flags &= ~SSF_TRIGGERED; 265 } 266 ss->sc_link->flags &= ~SDEV_OPEN; 267 268 return (0); 269 } 270 271 /* 272 * trim the size of the transfer if needed, 273 * called by physio 274 * basically the smaller of our min and the scsi driver's 275 * minphys 276 */ 277 void 278 ssminphys(bp) 279 struct buf *bp; 280 { 281 register struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)]; 282 283 (ss->sc_link->adapter->scsipi_minphys)(bp); 284 285 /* 286 * trim the transfer further for special devices this is 287 * because some scanners only read multiples of a line at a 288 * time, also some cannot disconnect, so the read must be 289 * short enough to happen quickly 290 */ 291 if (ss->special->minphys) 292 (ss->special->minphys)(ss, bp); 293 } 294 295 /* 296 * Do a read on a device for a user process. 297 * Prime scanner at start of read, check uio values, call ssstrategy 298 * via physio for the actual transfer. 299 */ 300 int 301 ssread(dev, uio, flag) 302 dev_t dev; 303 struct uio *uio; 304 int flag; 305 { 306 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 307 int error; 308 309 /* if the scanner has not yet been started, do it now */ 310 if (!(ss->flags & SSF_TRIGGERED)) { 311 if (ss->special->trigger_scanner) { 312 error = (ss->special->trigger_scanner)(ss); 313 if (error) 314 return (error); 315 } 316 ss->flags |= SSF_TRIGGERED; 317 } 318 319 return (physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio)); 320 } 321 322 /* 323 * Actually translate the requested transfer into one the physical 324 * driver can understand The transfer is described by a buf and will 325 * include only one physical transfer. 326 */ 327 void 328 ssstrategy(bp) 329 struct buf *bp; 330 { 331 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(bp->b_dev)]; 332 struct buf *dp; 333 int s; 334 335 SC_DEBUG(ss->sc_link, SDEV_DB1, 336 ("ssstrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno)); 337 338 if (bp->b_bcount > ss->sio.scan_window_size) 339 bp->b_bcount = ss->sio.scan_window_size; 340 341 /* 342 * If it's a null transfer, return immediatly 343 */ 344 if (bp->b_bcount == 0) 345 goto done; 346 347 s = splbio(); 348 349 /* 350 * Place it in the queue of activities for this scanner 351 * at the end (a bit silly because we only have on user.. 352 * (but it could fork())) 353 */ 354 dp = &ss->buf_queue; 355 bp->b_actf = NULL; 356 bp->b_actb = dp->b_actb; 357 *dp->b_actb = bp; 358 dp->b_actb = &bp->b_actf; 359 360 /* 361 * Tell the device to get going on the transfer if it's 362 * not doing anything, otherwise just wait for completion 363 * (All a bit silly if we're only allowing 1 open but..) 364 */ 365 ssstart(ss); 366 367 splx(s); 368 return; 369 bp->b_flags |= B_ERROR; 370 done: 371 /* 372 * Correctly set the buf to indicate a completed xfer 373 */ 374 bp->b_resid = bp->b_bcount; 375 biodone(bp); 376 } 377 378 /* 379 * ssstart looks to see if there is a buf waiting for the device 380 * and that the device is not already busy. If both are true, 381 * It dequeues the buf and creates a scsi command to perform the 382 * transfer required. The transfer request will call scsipi_done 383 * on completion, which will in turn call this routine again 384 * so that the next queued transfer is performed. 385 * The bufs are queued by the strategy routine (ssstrategy) 386 * 387 * This routine is also called after other non-queued requests 388 * have been made of the scsi driver, to ensure that the queue 389 * continues to be drained. 390 * ssstart() is called at splbio 391 */ 392 void 393 ssstart(v) 394 void *v; 395 { 396 struct ss_softc *ss = v; 397 struct scsipi_link *sc_link = ss->sc_link; 398 register struct buf *bp, *dp; 399 400 SC_DEBUG(sc_link, SDEV_DB2, ("ssstart ")); 401 /* 402 * See if there is a buf to do and we are not already 403 * doing one 404 */ 405 while (sc_link->openings > 0) { 406 /* if a special awaits, let it proceed first */ 407 if (sc_link->flags & SDEV_WAITING) { 408 sc_link->flags &= ~SDEV_WAITING; 409 wakeup((caddr_t)sc_link); 410 return; 411 } 412 413 /* 414 * See if there is a buf with work for us to do.. 415 */ 416 dp = &ss->buf_queue; 417 if ((bp = dp->b_actf) == NULL) 418 return; 419 if ((dp = bp->b_actf) != NULL) 420 dp->b_actb = bp->b_actb; 421 else 422 ss->buf_queue.b_actb = bp->b_actb; 423 *bp->b_actb = dp; 424 425 if (ss->special->read) { 426 (ss->special->read)(ss, bp); 427 } else { 428 /* generic scsi2 scanner read */ 429 /* XXX add code for SCSI2 scanner read */ 430 } 431 } 432 } 433 434 /* 435 * Perform special action on behalf of the user; 436 * knows about the internals of this device 437 */ 438 int 439 ssioctl(dev, cmd, addr, flag, p) 440 dev_t dev; 441 u_long cmd; 442 caddr_t addr; 443 int flag; 444 struct proc *p; 445 { 446 struct ss_softc *ss = ss_cd.cd_devs[SSUNIT(dev)]; 447 int error = 0; 448 struct scan_io *sio; 449 450 switch (cmd) { 451 case SCIOCGET: 452 if (ss->special->get_params) { 453 /* call special handler */ 454 error = (ss->special->get_params)(ss); 455 if (error) 456 return (error); 457 } else { 458 /* XXX add code for SCSI2 scanner, if any */ 459 return (EOPNOTSUPP); 460 } 461 bcopy(&ss->sio, addr, sizeof(struct scan_io)); 462 break; 463 case SCIOCSET: 464 sio = (struct scan_io *)addr; 465 466 if (ss->special->set_params) { 467 /* call special handler */ 468 error = (ss->special->set_params)(ss, sio); 469 if (error) 470 return (error); 471 } else { 472 /* XXX add code for SCSI2 scanner, if any */ 473 return (EOPNOTSUPP); 474 } 475 break; 476 case SCIOCRESTART: 477 if (ss->special->rewind_scanner ) { 478 /* call special handler */ 479 error = (ss->special->rewind_scanner)(ss); 480 if (error) 481 return (error); 482 } else 483 /* XXX add code for SCSI2 scanner, if any */ 484 return (EOPNOTSUPP); 485 ss->flags &= ~SSF_TRIGGERED; 486 break; 487 #ifdef NOTYET 488 case SCAN_USE_ADF: 489 break; 490 #endif 491 default: 492 if (SSMODE(dev) != MODE_CONTROL) 493 return (ENOTTY); 494 return (scsipi_do_ioctl(ss->sc_link, dev, cmd, addr, flag, p)); 495 } 496 return (error); 497 } 498