1 /* $NetBSD: ss.c,v 1.85 2014/03/16 05:20:29 dholland 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.85 2014/03/16 05:20:29 dholland 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/bufq.h> 44 #include <sys/proc.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 static int ssmatch(device_t, cfdata_t, void *); 72 static void ssattach(device_t, device_t, void *); 73 static int ssdetach(device_t self, int flags); 74 75 CFATTACH_DECL_NEW( 76 ss, 77 sizeof(struct ss_softc), 78 ssmatch, 79 ssattach, 80 ssdetach, 81 NULL 82 ); 83 84 extern struct cfdriver ss_cd; 85 86 static dev_type_open(ssopen); 87 static dev_type_close(ssclose); 88 static dev_type_read(ssread); 89 static dev_type_ioctl(ssioctl); 90 91 const struct cdevsw ss_cdevsw = { 92 .d_open = ssopen, 93 .d_close = ssclose, 94 .d_read = ssread, 95 .d_write = nowrite, 96 .d_ioctl = ssioctl, 97 .d_stop = nostop, 98 .d_tty = notty, 99 .d_poll = nopoll, 100 .d_mmap = nommap, 101 .d_kqfilter = nokqfilter, 102 .d_flag = D_OTHER 103 }; 104 105 static void ssstrategy(struct buf *); 106 static void ssstart(struct scsipi_periph *); 107 static void ssdone(struct scsipi_xfer *, int); 108 static void ssminphys(struct buf *); 109 110 static const struct scsipi_periphsw ss_switch = { 111 NULL, 112 ssstart, 113 NULL, 114 ssdone, 115 }; 116 117 static const struct scsipi_inquiry_pattern ss_patterns[] = { 118 {T_SCANNER, T_FIXED, 119 "", "", ""}, 120 {T_SCANNER, T_REMOV, 121 "", "", ""}, 122 {T_PROCESSOR, T_FIXED, 123 "HP ", "C1130A ", ""}, 124 {T_PROCESSOR, T_FIXED, 125 "HP ", "C1750A ", ""}, 126 {T_PROCESSOR, T_FIXED, 127 "HP ", "C2500A ", ""}, 128 {T_PROCESSOR, T_FIXED, 129 "HP ", "C2520A ", ""}, 130 {T_PROCESSOR, T_FIXED, 131 "HP ", "C5110A ", ""}, 132 {T_PROCESSOR, T_FIXED, 133 "HP ", "C7670A ", ""}, 134 {T_PROCESSOR, T_FIXED, 135 "HP ", "", ""}, 136 }; 137 138 static int 139 ssmatch(device_t parent, cfdata_t match, void *aux) 140 { 141 struct scsipibus_attach_args *sa = aux; 142 int priority; 143 144 (void)scsipi_inqmatch(&sa->sa_inqbuf, 145 ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]), 146 sizeof(ss_patterns[0]), &priority); 147 return priority; 148 } 149 150 /* 151 * The routine called by the low level scsi routine when it discovers 152 * A device suitable for this driver 153 * If it is a know special, call special attach routine to install 154 * special handlers into the ss_softc structure 155 */ 156 static void 157 ssattach(device_t parent, device_t self, void *aux) 158 { 159 struct ss_softc *ss = device_private(self); 160 struct scsipibus_attach_args *sa = aux; 161 struct scsipi_periph *periph = sa->sa_periph; 162 163 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: ")); 164 ss->sc_dev = self; 165 166 ss->flags |= SSF_AUTOCONF; 167 168 /* Store information needed to contact our base driver */ 169 ss->sc_periph = periph; 170 periph->periph_dev = ss->sc_dev; 171 periph->periph_switch = &ss_switch; 172 173 printf("\n"); 174 175 /* Set up the buf queue for this device */ 176 bufq_alloc(&ss->buf_queue, "fcfs", 0); 177 178 callout_init(&ss->sc_callout, 0); 179 180 /* 181 * look for non-standard scanners with help of the quirk table 182 * and install functions for special handling 183 */ 184 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n")); 185 if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0) 186 mustek_attach(ss, sa); 187 if (memcmp(sa->sa_inqbuf.vendor, "HP ", 8) == 0 && 188 memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0) 189 scanjet_attach(ss, sa); 190 191 if (ss->special == NULL) { 192 /* XXX add code to restart a SCSI2 scanner, if any */ 193 } 194 ss->flags &= ~SSF_AUTOCONF; 195 } 196 197 static int 198 ssdetach(device_t self, int flags) 199 { 200 struct ss_softc *ss = device_private(self); 201 int s, cmaj, mn; 202 203 /* locate the major number */ 204 cmaj = cdevsw_lookup_major(&ss_cdevsw); 205 206 /* kill any pending restart */ 207 callout_stop(&ss->sc_callout); 208 209 s = splbio(); 210 211 /* Kill off any queued buffers. */ 212 bufq_drain(ss->buf_queue); 213 214 bufq_free(ss->buf_queue); 215 216 /* Kill off any pending commands. */ 217 scsipi_kill_pending(ss->sc_periph); 218 219 splx(s); 220 221 /* Nuke the vnodes for any open instances */ 222 mn = SSUNIT(device_unit(self)); 223 vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR); 224 225 return 0; 226 } 227 228 /* open the device. */ 229 static int 230 ssopen(dev_t dev, int flag, int mode, struct lwp *l) 231 { 232 int unit; 233 u_int ssmode; 234 int error; 235 struct ss_softc *ss; 236 struct scsipi_periph *periph; 237 struct scsipi_adapter *adapt; 238 239 unit = SSUNIT(dev); 240 ss = device_lookup_private(&ss_cd, unit); 241 if (ss == NULL) 242 return ENXIO; 243 244 if (!device_is_active(ss->sc_dev)) 245 return ENODEV; 246 247 ssmode = SSMODE(dev); 248 249 periph = ss->sc_periph; 250 adapt = periph->periph_channel->chan_adapter; 251 252 SC_DEBUG(periph, SCSIPI_DB1, 253 ("open: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit, 254 ss_cd.cd_ndevs)); 255 256 if (periph->periph_flags & PERIPH_OPEN) { 257 aprint_error_dev(ss->sc_dev, "already open\n"); 258 return EBUSY; 259 } 260 261 if ((error = scsipi_adapter_addref(adapt)) != 0) 262 return error; 263 264 /* 265 * Catch any unit attention errors. 266 * 267 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners 268 * consider paper to be a changeable media 269 * 270 */ 271 error = scsipi_test_unit_ready(periph, 272 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST | 273 (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0)); 274 if (error) 275 goto bad; 276 277 periph->periph_flags |= PERIPH_OPEN; /* unit attn now errors */ 278 279 /* 280 * If the mode is 3 (e.g. minor = 3,7,11,15) 281 * then the device has been opened to set defaults 282 * This mode does NOT ALLOW I/O, only ioctls 283 */ 284 if (ssmode == MODE_CONTROL) 285 return 0; 286 287 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n")); 288 return 0; 289 290 bad: 291 scsipi_adapter_delref(adapt); 292 periph->periph_flags &= ~PERIPH_OPEN; 293 return error; 294 } 295 296 /* 297 * close the device.. only called if we are the LAST 298 * occurence of an open device 299 */ 300 static int 301 ssclose(dev_t dev, int flag, int mode, struct lwp *l) 302 { 303 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 304 struct scsipi_periph *periph = ss->sc_periph; 305 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 306 int error; 307 308 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n")); 309 310 if (SSMODE(dev) == MODE_REWIND) { 311 if (ss->special && ss->special->rewind_scanner) { 312 /* call special handler to rewind/abort scan */ 313 error = (ss->special->rewind_scanner)(ss); 314 if (error) 315 return error; 316 } else { 317 /* XXX add code to restart a SCSI2 scanner, if any */ 318 } 319 ss->sio.scan_window_size = 0; 320 ss->flags &= ~SSF_TRIGGERED; 321 } 322 323 scsipi_wait_drain(periph); 324 325 scsipi_adapter_delref(adapt); 326 periph->periph_flags &= ~PERIPH_OPEN; 327 328 return 0; 329 } 330 331 /* 332 * trim the size of the transfer if needed, called by physio 333 * basically the smaller of our min and the scsi driver's minphys 334 */ 335 static void 336 ssminphys(struct buf *bp) 337 { 338 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); 339 struct scsipi_periph *periph = ss->sc_periph; 340 341 scsipi_adapter_minphys(periph->periph_channel, bp); 342 343 /* 344 * trim the transfer further for special devices this is 345 * because some scanners only read multiples of a line at a 346 * time, also some cannot disconnect, so the read must be 347 * short enough to happen quickly 348 */ 349 if (ss->special && ss->special->minphys) 350 (ss->special->minphys)(ss, bp); 351 } 352 353 /* 354 * Do a read on a device for a user process. 355 * Prime scanner at start of read, check uio values, call ssstrategy 356 * via physio for the actual transfer. 357 */ 358 static int 359 ssread(dev_t dev, struct uio *uio, int flag) 360 { 361 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 362 int error; 363 364 if (!device_is_active(ss->sc_dev)) 365 return ENODEV; 366 367 /* if the scanner has not yet been started, do it now */ 368 if (!(ss->flags & SSF_TRIGGERED)) { 369 if (ss->special && ss->special->trigger_scanner) { 370 error = (ss->special->trigger_scanner)(ss); 371 if (error) 372 return (error); 373 } 374 ss->flags |= SSF_TRIGGERED; 375 } 376 377 return physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio); 378 } 379 380 /* 381 * Actually translate the requested transfer into one the physical 382 * driver can understand The transfer is described by a buf and will 383 * include only one physical transfer. 384 */ 385 static void 386 ssstrategy(struct buf *bp) 387 { 388 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); 389 struct scsipi_periph *periph = ss->sc_periph; 390 int s; 391 392 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, 393 ("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, 394 bp->b_blkno)); 395 396 /* If the device has been made invalid, error out */ 397 if (!device_is_active(ss->sc_dev)) { 398 if (periph->periph_flags & PERIPH_OPEN) 399 bp->b_error = EIO; 400 else 401 bp->b_error = ENODEV; 402 goto done; 403 } 404 405 /* If negative offset, error */ 406 if (bp->b_blkno < 0) { 407 bp->b_error = EINVAL; 408 goto done; 409 } 410 411 if (bp->b_bcount > ss->sio.scan_window_size) 412 bp->b_bcount = ss->sio.scan_window_size; 413 414 /* If it's a null transfer, return immediatly */ 415 if (bp->b_bcount == 0) 416 goto done; 417 418 s = splbio(); 419 420 /* 421 * Place it in the queue of activities for this scanner 422 * at the end (a bit silly because we only have on user.. 423 * (but it could fork())) 424 */ 425 bufq_put(ss->buf_queue, bp); 426 427 /* 428 * Tell the device to get going on the transfer if it's 429 * not doing anything, otherwise just wait for completion 430 * (All a bit silly if we're only allowing 1 open but..) 431 */ 432 ssstart(ss->sc_periph); 433 434 splx(s); 435 return; 436 done: 437 /* Correctly set the buf to indicate a completed xfer */ 438 bp->b_resid = bp->b_bcount; 439 biodone(bp); 440 } 441 442 /* 443 * ssstart looks to see if there is a buf waiting for the device 444 * and that the device is not already busy. If both are true, 445 * It dequeues the buf and creates a scsi command to perform the 446 * transfer required. The transfer request will call scsipi_done 447 * on completion, which will in turn call this routine again 448 * so that the next queued transfer is performed. 449 * The bufs are queued by the strategy routine (ssstrategy) 450 * 451 * This routine is also called after other non-queued requests 452 * have been made of the scsi driver, to ensure that the queue 453 * continues to be drained. 454 * ssstart() is called at splbio 455 */ 456 static void 457 ssstart(struct scsipi_periph *periph) 458 { 459 struct ss_softc *ss = device_private(periph->periph_dev); 460 struct buf *bp; 461 462 SC_DEBUG(periph, SCSIPI_DB2, ("ssstart ")); 463 464 /* See if there is a buf to do and we are not already doing one */ 465 while (periph->periph_active < periph->periph_openings) { 466 /* if a special awaits, let it proceed first */ 467 if (periph->periph_flags & PERIPH_WAITING) { 468 periph->periph_flags &= ~PERIPH_WAITING; 469 wakeup((void *)periph); 470 return; 471 } 472 473 /* See if there is a buf with work for us to do.. */ 474 if ((bp = bufq_peek(ss->buf_queue)) == NULL) 475 return; 476 477 if (ss->special && ss->special->read) 478 (ss->special->read)(ss, bp); 479 else { 480 /* generic scsi2 scanner read */ 481 /* XXX add code for SCSI2 scanner read */ 482 } 483 } 484 } 485 486 void 487 ssrestart(void *v) 488 { 489 int s = splbio(); 490 ssstart((struct scsipi_periph *)v); 491 splx(s); 492 } 493 494 static void 495 ssdone(struct scsipi_xfer *xs, int error) 496 { 497 struct buf *bp = xs->bp; 498 499 if (bp) { 500 bp->b_error = error; 501 bp->b_resid = xs->resid; 502 biodone(bp); 503 } 504 } 505 506 507 /* 508 * Perform special action on behalf of the user; 509 * knows about the internals of this device 510 */ 511 int 512 ssioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 513 { 514 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 515 int error = 0; 516 struct scan_io *sio; 517 518 if (!device_is_active(ss->sc_dev)) 519 return ENODEV; 520 521 switch (cmd) { 522 case SCIOCGET: 523 if (ss->special && ss->special->get_params) { 524 /* call special handler */ 525 error = (ss->special->get_params)(ss); 526 if (error) 527 return error; 528 } else 529 /* XXX add code for SCSI2 scanner, if any */ 530 return EOPNOTSUPP; 531 memcpy(addr, &ss->sio, sizeof(struct scan_io)); 532 break; 533 case SCIOCSET: 534 sio = (struct scan_io *)addr; 535 536 if (ss->special && ss->special->set_params) { 537 /* call special handler */ 538 error = (ss->special->set_params)(ss, sio); 539 if (error) 540 return error; 541 } else 542 /* XXX add code for SCSI2 scanner, if any */ 543 return EOPNOTSUPP; 544 break; 545 case SCIOCRESTART: 546 if (ss->special && ss->special->rewind_scanner ) { 547 /* call special handler */ 548 error = (ss->special->rewind_scanner)(ss); 549 if (error) 550 return error; 551 } else 552 /* XXX add code for SCSI2 scanner, if any */ 553 return EOPNOTSUPP; 554 ss->flags &= ~SSF_TRIGGERED; 555 break; 556 #ifdef NOTYET 557 case SCAN_USE_ADF: 558 break; 559 #endif 560 default: 561 return scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr, flag, l); 562 } 563 return error; 564 } 565