/*
* Copyright (c) 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nick Hudson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code is based on the ucom driver.
*/
/*
* Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
* RS232 bridges.
*/
/* settings */
uint32_t sc_baud;
uint8_t sc_cfg; /* Data format */
uint8_t sc_mcr; /* Modem control */
uint8_t sc_msr; /* Modem status */
int sc_swflags;
DPRINTF(("ucycom_shutdown\n"));
/*
* Hang up if necessary. Wait a bit, so the other side has time to
* notice even if we immediately open the port again.
*/
if (ISSET(tp->t_cflag, HUPCL)) {
ucycom_dtr(sc, 0);
(void)tsleep(sc, TTIPRI, ttclos, hz);
}
}
#endif
static int
ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
{
struct ucycom_softc *sc =
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
struct tty *tp;
int s, err;
if (sc == NULL)
return ENXIO;
if (sc->sc_dying)
return EIO;
if (sc->sc_init_state != UCYCOM_INIT_INITED)
return ENXIO;
if (!device_is_active(sc->sc_dev))
return ENXIO;
tp = sc->sc_tty;
DPRINTF(("ucycomopen: tp=%p\n", tp));
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
return EBUSY;
err = uhidev_open(sc->sc_hdev, &ucycom_intr, sc);
if (err) {
/* Any cleanup? */
splx(s);
return err;
}
/*
* Initialize the termios status to the defaults. Add in the
* sticky bits from TIOCSFLAGS.
*/
t.c_ispeed = 0;
t.c_ospeed = TTYDEF_SPEED;
t.c_cflag = TTYDEF_CFLAG;
if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
SET(t.c_cflag, CLOCAL);
if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
SET(t.c_cflag, CRTSCTS);
if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
SET(t.c_cflag, MDMBUF);
#if 0
/* XXX Don't do this as for some reason trying to do an
* XXX interrupt out transfer at this point means everything
* XXX gets stuck!?!
*/
/*
* Turn on DTR. We must always do this, even if carrier is not
* present, because otherwise we'd have to use TIOCSDTR
* immediately after setting CLOCAL, which applications do not
* expect. We always assert DTR while the device is open
* unless explicitly requested to deassert it.
*/
ucycom_dtr(sc, 1);
#endif
err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
if (err)
goto bad;
err = (*tp->t_linesw->l_open)(dev, tp);
if (err)
goto bad;
return 0;
bad:
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
/*
* We failed to open the device, and nobody else had it opened.
* Clean up the state as appropriate.
*/
ucycom_cleanup(sc);
}
return err;
}
static int
ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct ucycom_softc *sc =
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
struct tty *tp = sc->sc_tty;
DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
if (!ISSET(tp->t_state, TS_ISOPEN))
return 0;
(*tp->t_linesw->l_close)(tp, flag);
ttyclose(tp);
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
/*
* Although we got a last close, the device may still be in
* use; e.g. if this was the dialout node, and there are still
* processes waiting for carrier on the non-dialout node.
*/
ucycom_cleanup(sc);
}
/*
* The 8 byte output report uses byte 0 for control and byte
* count.
*
* The 32 byte output report uses byte 0 for control. Byte 1
* is used for byte count.
*/
memset(sc->sc_obuf, 0, sc->sc_olen);
len = cnt;
switch (sc->sc_olen) {
case 8:
if (cnt > sc->sc_olen - 1) {
DPRINTF(("ucycomstart(8): big buffer %d chars\n", len));
len = sc->sc_olen - 1;
}
memcpy(sc->sc_obuf + 1, data, len);
sc->sc_obuf[0] = len | sc->sc_mcr;
/*
* For the console, always force CLOCAL and !HUPCL, so that the port
* is always active.
*/
if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
SET(t->c_cflag, CLOCAL);
CLR(t->c_cflag, HUPCL);
}
/*
* If there were no changes, don't do anything. This avoids dropping
* input and improves performance when all we did was frob things like
* VMIN and VTIME.
*/
if (tp->t_ospeed == t->c_ospeed &&
tp->t_cflag == t->c_cflag)
return 0;
/*
* Update the tty layer's idea of the carrier bit, in case we changed
* CLOCAL or MDMBUF. We don't hang up here; we only do that by
* explicit request.
*/
DPRINTF(("ucycomparam: l_modem\n"));
(void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ );
Static int
ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
{
uint8_t report[5];
int err;
switch (baud) {
case 600:
case 1200:
case 2400:
case 4800:
case 9600:
case 19200:
case 38400:
case 57600:
#if 0
/*
* Stock chips only support standard baud rates in the 600 - 57600
* range, but higher rates can be achieved using custom firmware.
*/
case 115200:
case 153600:
case 192000:
#endif
break;
default:
return EINVAL;
}
#ifdef UCYCOM_DEBUG
if (ucycomdebug > 5) {
uint32_t i;
if (n != 0) {
DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
for (i = 0; i < n; i++)
DPRINTF((" %02x", cp[i]));
DPRINTF(("\n"));
}
}
#endif
/* Give characters to tty layer. */
s = spltty();
while (n-- > 0) {
DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
if ((*rint)(*cp++, tp) == -1) {
/* XXX what should we do? */
aprint_error_dev(sc->sc_dev, "lost a character\n");
break;
}
}
splx(s);
chg = st ^ sc->sc_msr;
sc->sc_msr = st;
if (ISSET(chg, UCYCOM_DCD))
(*tp->t_linesw->l_modem)(tp,
ISSET(sc->sc_msr, UCYCOM_DCD));
}
Static void
tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits)
{
u_char combits;
u_char before = sc->sc_mcr;
combits = 0;
if (ISSET(ttybits, TIOCM_DTR))
SET(combits, UCYCOM_DTR);
if (ISSET(ttybits, TIOCM_RTS))
SET(combits, UCYCOM_RTS);
switch (how) {
case TIOCMBIC:
CLR(sc->sc_mcr, combits);
break;
case TIOCMBIS:
SET(sc->sc_mcr, combits);
break;
case TIOCMSET:
CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS);
SET(sc->sc_mcr, combits);
break;
}
if (before ^ sc->sc_mcr) {
DPRINTF(("tiocm_to_ucycom: something has changed\n"));
ucycom_set_status(sc);
}
}
Static int
ucycom_to_tiocm(struct ucycom_softc *sc)
{
u_char combits;
int ttybits = 0;
combits = sc->sc_mcr;
if (ISSET(combits, UCYCOM_DTR))
SET(ttybits, TIOCM_DTR);
if (ISSET(combits, UCYCOM_RTS))
SET(ttybits, TIOCM_RTS);
combits = sc->sc_msr;
if (ISSET(combits, UCYCOM_DCD))
SET(ttybits, TIOCM_CD);
if (ISSET(combits, UCYCOM_CTS))
SET(ttybits, TIOCM_CTS);
if (ISSET(combits, UCYCOM_DSR))
SET(ttybits, TIOCM_DSR);
if (ISSET(combits, UCYCOM_RI))
SET(ttybits, TIOCM_RI);
return ttybits;
}
Static void
ucycom_dtr(struct ucycom_softc *sc, int set)
{
uint8_t old;
old = sc->sc_mcr;
if (set)
SET(sc->sc_mcr, UCYCOM_DTR);
else
CLR(sc->sc_mcr, UCYCOM_DTR);