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