#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ss.c,v 1.91 2022/02/23 21:54:41 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/vnode.h>
#include <sys/scanio.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsi_scanner.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/ssvar.h>
#include <dev/scsipi/ss_mustek.h>
#define SSMODE(z) ( minor(z) & 0x03)
#define SSUNIT(z) ((minor(z) >> 4) )
#define SSNMINOR 16
#define MODE_REWIND 0
#define MODE_NONREWIND 1
#define MODE_CONTROL 3
static int ssmatch(device_t, cfdata_t, void *);
static void ssattach(device_t, device_t, void *);
static int ssdetach(device_t self, int flags);
CFATTACH_DECL_NEW(
ss,
sizeof(struct ss_softc),
ssmatch,
ssattach,
ssdetach,
NULL
);
extern struct cfdriver ss_cd;
static dev_type_open(ssopen);
static dev_type_close(ssclose);
static dev_type_read(ssread);
static dev_type_ioctl(ssioctl);
const struct cdevsw ss_cdevsw = {
.d_open = ssopen,
.d_close = ssclose,
.d_read = ssread,
.d_write = nowrite,
.d_ioctl = ssioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER | D_MPSAFE
};
static void ssstrategy(struct buf *);
static void ssstart(struct scsipi_periph *);
static void ssdone(struct scsipi_xfer *, int);
static void ssminphys(struct buf *);
static const struct scsipi_periphsw ss_switch = {
NULL,
ssstart,
NULL,
ssdone,
};
static const struct scsipi_inquiry_pattern ss_patterns[] = {
{T_SCANNER, T_FIXED,
"", "", ""},
{T_SCANNER, T_REMOV,
"", "", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C1130A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C1750A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C2500A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C2520A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C5110A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "C7670A ", ""},
{T_PROCESSOR, T_FIXED,
"HP ", "", ""},
};
static int
ssmatch(device_t parent, cfdata_t match, void *aux)
{
struct scsipibus_attach_args *sa = aux;
int priority;
(void)scsipi_inqmatch(&sa->sa_inqbuf,
ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]),
sizeof(ss_patterns[0]), &priority);
return priority;
}
static void
ssattach(device_t parent, device_t self, void *aux)
{
struct ss_softc *ss = device_private(self);
struct scsipibus_attach_args *sa = aux;
struct scsipi_periph *periph = sa->sa_periph;
SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: "));
ss->sc_dev = self;
ss->flags |= SSF_AUTOCONF;
ss->sc_periph = periph;
periph->periph_dev = ss->sc_dev;
periph->periph_switch = &ss_switch;
printf("\n");
bufq_alloc(&ss->buf_queue, "fcfs", 0);
callout_init(&ss->sc_callout, 0);
SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n"));
if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0)
mustek_attach(ss, sa);
if (memcmp(sa->sa_inqbuf.vendor, "HP ", 8) == 0 &&
memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0)
scanjet_attach(ss, sa);
if (ss->special == NULL) {
}
ss->flags &= ~SSF_AUTOCONF;
}
static int
ssdetach(device_t self, int flags)
{
struct ss_softc *ss = device_private(self);
struct scsipi_periph *periph = ss->sc_periph;
struct scsipi_channel *chan = periph->periph_channel;
int cmaj, mn;
cmaj = cdevsw_lookup_major(&ss_cdevsw);
callout_halt(&ss->sc_callout, NULL);
mutex_enter(chan_mtx(chan));
bufq_drain(ss->buf_queue);
scsipi_kill_pending(periph);
mutex_exit(chan_mtx(chan));
bufq_free(ss->buf_queue);
mn = SSUNIT(device_unit(self));
vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR);
return 0;
}
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;
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;
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;
}
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) {
error = (ss->special->rewind_scanner)(ss);
if (error)
return error;
} else {
}
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 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);
if (ss->special && ss->special->minphys)
(ss->special->minphys)(ss, bp);
}
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 (!(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);
}
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;
struct scsipi_channel *chan = periph->periph_channel;
SC_DEBUG(ss->sc_periph, SCSIPI_DB1,
("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount,
bp->b_blkno));
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 (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 (bp->b_bcount == 0)
goto done;
mutex_enter(chan_mtx(chan));
bufq_put(ss->buf_queue, bp);
ssstart(periph);
mutex_exit(chan_mtx(chan));
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
}
static void
ssstart(struct scsipi_periph *periph)
{
struct ss_softc *ss = device_private(periph->periph_dev);
struct buf *bp;
SC_DEBUG(periph, SCSIPI_DB2, ("ssstart "));
while (periph->periph_active < periph->periph_openings) {
if (periph->periph_flags & PERIPH_WAITING) {
periph->periph_flags &= ~PERIPH_WAITING;
cv_broadcast(periph_cv_periph(periph));
return;
}
if ((bp = bufq_peek(ss->buf_queue)) == NULL)
return;
if (ss->special && ss->special->read)
(ss->special->read)(ss, bp);
else {
}
}
}
void
ssrestart(void *v)
{
struct scsipi_periph *periph = v;
struct scsipi_channel *chan = periph->periph_channel;
mutex_enter(chan_mtx(chan));
ssstart(periph);
mutex_exit(chan_mtx(chan));
}
static void
ssdone(struct scsipi_xfer *xs, int error)
{
struct buf *bp = xs->bp;
if (bp) {
bp->b_error = error;
bp->b_resid = xs->resid;
biodone(bp);
}
}
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) {
error = (ss->special->get_params)(ss);
if (error)
return error;
} else
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) {
error = (ss->special->set_params)(ss, sio);
if (error)
return error;
} else
return EOPNOTSUPP;
break;
case SCIOCRESTART:
if (ss->special && ss->special->rewind_scanner ) {
error = (ss->special->rewind_scanner)(ss);
if (error)
return error;
} else
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;
}