/* * close the device.. only called if we are the LAST * occurence of an open device */ static int ssclose(dev_t dev, int flag, int mode, struct lwp *l) { struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); struct scsipi_periph *periph = ss->sc_periph; struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; int error; SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n")); if (SSMODE(dev) == MODE_REWIND) { if (ss->special && ss->special->rewind_scanner) { /* call special handler to rewind/abort scan */ error = (ss->special->rewind_scanner)(ss); if (error) return error; } else { /* XXX add code to restart a SCSI2 scanner, if any */ } ss->sio.scan_window_size = 0; ss->flags &= ~SSF_TRIGGERED; } scsipi_wait_drain(periph); scsipi_adapter_delref(adapt); periph->periph_flags &= ~PERIPH_OPEN; return 0; }
static int ssdetach(device_t self, int flags) { struct ss_softc *ss = device_private(self); int s, cmaj, mn; /* locate the major number */ cmaj = cdevsw_lookup_major(&ss_cdevsw); /* kill any pending restart */ callout_stop(&ss->sc_callout); s = splbio(); /* Kill off any queued buffers. */ bufq_drain(ss->buf_queue); bufq_free(ss->buf_queue); /* Kill off any pending commands. */ scsipi_kill_pending(ss->sc_periph); splx(s); /* Nuke the vnodes for any open instances */ mn = SSUNIT(device_unit(self)); vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR); return 0; }
/* * Perform special action on behalf of the user; * knows about the internals of this device */ int ssioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) { struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); int error = 0; struct scan_io *sio; if (!device_is_active(&ss->sc_dev)) return (ENODEV); switch (cmd) { case SCIOCGET: if (ss->special && ss->special->get_params) { /* call special handler */ error = (ss->special->get_params)(ss); if (error) return (error); } else { /* XXX add code for SCSI2 scanner, if any */ return (EOPNOTSUPP); } memcpy(addr, &ss->sio, sizeof(struct scan_io)); break; case SCIOCSET: sio = (struct scan_io *)addr; if (ss->special && ss->special->set_params) { /* call special handler */ error = (ss->special->set_params)(ss, sio); if (error) return (error); } else { /* XXX add code for SCSI2 scanner, if any */ return (EOPNOTSUPP); } break; case SCIOCRESTART: if (ss->special && ss->special->rewind_scanner ) { /* call special handler */ error = (ss->special->rewind_scanner)(ss); if (error) return (error); } else /* XXX add code for SCSI2 scanner, if any */ return (EOPNOTSUPP); ss->flags &= ~SSF_TRIGGERED; break; #ifdef NOTYET case SCAN_USE_ADF: break; #endif default: return (scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr, flag, l)); } return (error); }
/* * Actually translate the requested transfer into one the physical * driver can understand The transfer is described by a buf and will * include only one physical transfer. */ static void ssstrategy(struct buf *bp) { struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); struct scsipi_periph *periph = ss->sc_periph; int s; SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno)); /* If the device has been made invalid, error out */ if (!device_is_active(ss->sc_dev)) { if (periph->periph_flags & PERIPH_OPEN) bp->b_error = EIO; else bp->b_error = ENODEV; goto done; } /* If negative offset, error */ if (bp->b_blkno < 0) { bp->b_error = EINVAL; goto done; } if (bp->b_bcount > ss->sio.scan_window_size) bp->b_bcount = ss->sio.scan_window_size; /* If it's a null transfer, return immediatly */ if (bp->b_bcount == 0) goto done; s = splbio(); /* * Place it in the queue of activities for this scanner * at the end (a bit silly because we only have on user.. * (but it could fork())) */ bufq_put(ss->buf_queue, bp); /* * Tell the device to get going on the transfer if it's * not doing anything, otherwise just wait for completion * (All a bit silly if we're only allowing 1 open but..) */ ssstart(ss->sc_periph); splx(s); return; done: /* Correctly set the buf to indicate a completed xfer */ bp->b_resid = bp->b_bcount; biodone(bp); }
/* * trim the size of the transfer if needed, called by physio * basically the smaller of our min and the scsi driver's minphys */ static void ssminphys(struct buf *bp) { struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); struct scsipi_periph *periph = ss->sc_periph; scsipi_adapter_minphys(periph->periph_channel, bp); /* * trim the transfer further for special devices this is * because some scanners only read multiples of a line at a * time, also some cannot disconnect, so the read must be * short enough to happen quickly */ if (ss->special && ss->special->minphys) (ss->special->minphys)(ss, bp); }
/* * Do a read on a device for a user process. * Prime scanner at start of read, check uio values, call ssstrategy * via physio for the actual transfer. */ static int ssread(dev_t dev, struct uio *uio, int flag) { struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); int error; if (!device_is_active(ss->sc_dev)) return ENODEV; /* if the scanner has not yet been started, do it now */ if (!(ss->flags & SSF_TRIGGERED)) { if (ss->special && ss->special->trigger_scanner) { error = (ss->special->trigger_scanner)(ss); if (error) return (error); } ss->flags |= SSF_TRIGGERED; } return physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio); }
/* open the device. */ static int ssopen(dev_t dev, int flag, int mode, struct lwp *l) { int unit; u_int ssmode; int error; struct ss_softc *ss; struct scsipi_periph *periph; struct scsipi_adapter *adapt; unit = SSUNIT(dev); ss = device_lookup_private(&ss_cd, unit); if (ss == NULL) return ENXIO; if (!device_is_active(ss->sc_dev)) return ENODEV; ssmode = SSMODE(dev); periph = ss->sc_periph; adapt = periph->periph_channel->chan_adapter; SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit, ss_cd.cd_ndevs)); if (periph->periph_flags & PERIPH_OPEN) { aprint_error_dev(ss->sc_dev, "already open\n"); return EBUSY; } if ((error = scsipi_adapter_addref(adapt)) != 0) return error; /* * Catch any unit attention errors. * * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners * consider paper to be a changeable media * */ error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST | (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0)); if (error) goto bad; periph->periph_flags |= PERIPH_OPEN; /* unit attn now errors */ /* * If the mode is 3 (e.g. minor = 3,7,11,15) * then the device has been opened to set defaults * This mode does NOT ALLOW I/O, only ioctls */ if (ssmode == MODE_CONTROL) return 0; SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n")); return 0; bad: scsipi_adapter_delref(adapt); periph->periph_flags &= ~PERIPH_OPEN; return error; }