2024-03-17 23:10:52 +03:00
|
|
|
/* $NetBSD: ucycom.c,v 1.57 2024/03/17 20:10:52 jakllsch Exp $ */
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2009-07-24 10:50:40 +04:00
|
|
|
* by Nick Hudson
|
2005-07-30 10:14:49 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2024-03-17 23:10:52 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ucycom.c,v 1.57 2024/03/17 20:10:52 jakllsch Exp $");
|
2016-11-25 15:56:29 +03:00
|
|
|
|
|
|
|
#ifdef _KERNEL_OPT
|
|
|
|
#include "opt_usb.h"
|
|
|
|
#endif
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
2016-04-23 13:15:27 +03:00
|
|
|
#include <sys/kmem.h>
|
2005-08-05 11:27:47 +04:00
|
|
|
#include <sys/device.h>
|
2005-07-30 10:14:49 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/vnode.h>
|
2006-05-15 01:42:26 +04:00
|
|
|
#include <sys/kauth.h>
|
2010-11-15 09:01:07 +03:00
|
|
|
#include <sys/lwp.h>
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbhid.h>
|
|
|
|
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
#include <dev/usb/usbdevs.h>
|
|
|
|
#include <dev/usb/uhidev.h>
|
2017-12-10 20:03:07 +03:00
|
|
|
#include <dev/hid/hid.h>
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#include "ioconf.h"
|
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
2010-11-04 01:34:23 +03:00
|
|
|
#define DPRINTF(x) if (ucycomdebug) printf x
|
|
|
|
#define DPRINTFN(n, x) if (ucycomdebug > (n)) printf x
|
2009-08-06 11:07:30 +04:00
|
|
|
int ucycomdebug = 20;
|
2005-07-30 10:14:49 +04:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-11-15 22:26:37 +03:00
|
|
|
#define UCYCOMCALLUNIT_MASK TTCALLUNIT_MASK
|
|
|
|
#define UCYCOMUNIT_MASK TTUNIT_MASK
|
|
|
|
#define UCYCOMDIALOUT_MASK TTDIALOUT_MASK
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2014-11-15 22:26:37 +03:00
|
|
|
#define UCYCOMCALLUNIT(x) TTCALLUNIT(x)
|
|
|
|
#define UCYCOMUNIT(x) TTUNIT(x)
|
|
|
|
#define UCYCOMDIALOUT(x) TTDIALOUT(x)
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/* Configuration Byte */
|
|
|
|
#define UCYCOM_RESET 0x80
|
|
|
|
#define UCYCOM_PARITY_TYPE_MASK 0x20
|
|
|
|
#define UCYCOM_PARITY_ODD 0x20
|
|
|
|
#define UCYCOM_PARITY_EVEN 0x00
|
|
|
|
#define UCYCOM_PARITY_MASK 0x10
|
|
|
|
#define UCYCOM_PARITY_ON 0x10
|
|
|
|
#define UCYCOM_PARITY_OFF 0x00
|
|
|
|
#define UCYCOM_STOP_MASK 0x08
|
|
|
|
#define UCYCOM_STOP_BITS_2 0x08
|
|
|
|
#define UCYCOM_STOP_BITS_1 0x00
|
|
|
|
#define UCYCOM_DATA_MASK 0x03
|
|
|
|
#define UCYCOM_DATA_BITS_8 0x03
|
|
|
|
#define UCYCOM_DATA_BITS_7 0x02
|
|
|
|
#define UCYCOM_DATA_BITS_6 0x01
|
|
|
|
#define UCYCOM_DATA_BITS_5 0x00
|
|
|
|
|
|
|
|
/* Modem (Input) status byte */
|
|
|
|
#define UCYCOM_RI 0x80
|
|
|
|
#define UCYCOM_DCD 0x40
|
|
|
|
#define UCYCOM_DSR 0x20
|
|
|
|
#define UCYCOM_CTS 0x10
|
|
|
|
#define UCYCOM_ERROR 0x08
|
|
|
|
#define UCYCOM_LMASK 0x07
|
|
|
|
|
|
|
|
/* Modem (Output) control byte */
|
|
|
|
#define UCYCOM_DTR 0x20
|
|
|
|
#define UCYCOM_RTS 0x10
|
|
|
|
#define UCYCOM_ORESET 0x08
|
|
|
|
|
|
|
|
struct ucycom_softc {
|
2022-03-28 15:44:17 +03:00
|
|
|
device_t sc_dev;
|
|
|
|
struct uhidev *sc_hdev;
|
2022-03-28 15:43:30 +03:00
|
|
|
struct usbd_device *sc_udev;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2022-03-28 15:43:30 +03:00
|
|
|
struct usb_task sc_task;
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *sc_tty;
|
|
|
|
|
2020-01-01 12:08:28 +03:00
|
|
|
enum {
|
|
|
|
UCYCOM_INIT_NONE,
|
|
|
|
UCYCOM_INIT_INITED
|
|
|
|
} sc_init_state;
|
|
|
|
|
2013-01-10 03:02:59 +04:00
|
|
|
kmutex_t sc_lock; /* protects refcnt, others */
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
/* uhidev parameters */
|
|
|
|
size_t sc_flen; /* feature report length */
|
|
|
|
size_t sc_olen; /* output report length */
|
|
|
|
|
|
|
|
uint8_t *sc_obuf;
|
2009-08-06 11:07:30 +04:00
|
|
|
int sc_wlen;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
char sc_dying;
|
|
|
|
};
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static dev_type_open(ucycomopen);
|
|
|
|
static dev_type_close(ucycomclose);
|
|
|
|
static dev_type_read(ucycomread);
|
|
|
|
static dev_type_write(ucycomwrite);
|
|
|
|
static dev_type_ioctl(ucycomioctl);
|
|
|
|
static dev_type_stop(ucycomstop);
|
|
|
|
static dev_type_tty(ucycomtty);
|
|
|
|
static dev_type_poll(ucycompoll);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
const struct cdevsw ucycom_cdevsw = {
|
2014-03-16 09:20:22 +04:00
|
|
|
.d_open = ucycomopen,
|
|
|
|
.d_close = ucycomclose,
|
|
|
|
.d_read = ucycomread,
|
|
|
|
.d_write = ucycomwrite,
|
|
|
|
.d_ioctl = ucycomioctl,
|
|
|
|
.d_stop = ucycomstop,
|
|
|
|
.d_tty = ucycomtty,
|
|
|
|
.d_poll = ucycompoll,
|
|
|
|
.d_mmap = nommap,
|
|
|
|
.d_kqfilter = ttykqfilter,
|
2014-07-25 12:10:31 +04:00
|
|
|
.d_discard = nodiscard,
|
2014-03-16 09:20:22 +04:00
|
|
|
.d_flag = D_TTY
|
2005-07-30 10:14:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Static int ucycomparam(struct tty *, struct termios *);
|
|
|
|
Static void ucycomstart(struct tty *);
|
2022-03-28 15:43:30 +03:00
|
|
|
Static void ucycomstarttask(void *);
|
2016-04-23 13:15:27 +03:00
|
|
|
Static void ucycomwritecb(struct usbd_xfer *, void *, usbd_status);
|
2022-03-28 15:44:17 +03:00
|
|
|
Static void ucycom_intr(void *, void *, u_int);
|
2005-07-30 10:14:49 +04:00
|
|
|
Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
|
|
|
|
Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int);
|
|
|
|
Static int ucycom_to_tiocm(struct ucycom_softc *);
|
|
|
|
Static void ucycom_set_status(struct ucycom_softc *);
|
|
|
|
Static void ucycom_dtr(struct ucycom_softc *, int);
|
|
|
|
#if 0
|
|
|
|
Static void ucycom_rts(struct ucycom_softc *, int);
|
|
|
|
#endif
|
2016-04-23 13:15:27 +03:00
|
|
|
Static void ucycom_cleanup(struct ucycom_softc *);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
|
|
|
Static void ucycom_get_cfg(struct ucycom_softc *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Static const struct usb_devno ucycom_devs[] = {
|
|
|
|
{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
|
|
|
|
{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE },
|
|
|
|
};
|
|
|
|
#define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p)
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int ucycom_match(device_t, cfdata_t, void *);
|
|
|
|
static void ucycom_attach(device_t, device_t, void *);
|
|
|
|
static int ucycom_detach(device_t, int);
|
|
|
|
static int ucycom_activate(device_t, enum devact);
|
2019-05-05 06:17:54 +03:00
|
|
|
|
2016-07-07 09:55:38 +03:00
|
|
|
CFATTACH_DECL_NEW(ucycom, sizeof(struct ucycom_softc), ucycom_match,
|
|
|
|
ucycom_attach, ucycom_detach, ucycom_activate);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2008-05-24 20:40:58 +04:00
|
|
|
ucycom_match(device_t parent, cfdata_t match, void *aux)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
|
|
|
struct uhidev_attach_arg *uha = aux;
|
|
|
|
|
2016-07-07 09:55:38 +03:00
|
|
|
return ucycom_lookup(uha->uiaa->uiaa_vendor, uha->uiaa->uiaa_product)
|
|
|
|
!= NULL ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static void
|
2008-05-24 20:40:58 +04:00
|
|
|
ucycom_attach(device_t parent, device_t self, void *aux)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc = device_private(self);
|
2005-07-30 10:14:49 +04:00
|
|
|
struct uhidev_attach_arg *uha = aux;
|
|
|
|
int size, repid;
|
|
|
|
void *desc;
|
|
|
|
|
2022-03-28 15:44:17 +03:00
|
|
|
sc->sc_dev = self;
|
|
|
|
sc->sc_hdev = uha->parent;
|
2022-03-28 15:43:30 +03:00
|
|
|
sc->sc_udev = uha->uiaa->uiaa_device;
|
2020-01-01 12:08:28 +03:00
|
|
|
sc->sc_init_state = UCYCOM_INIT_NONE;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
uhidev_get_report_desc(uha->parent, &desc, &size);
|
|
|
|
repid = uha->reportid;
|
|
|
|
sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
|
|
|
|
sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
|
|
|
|
|
2020-01-01 12:08:28 +03:00
|
|
|
if (sc->sc_olen != 8 && sc->sc_olen != 32)
|
|
|
|
return;
|
|
|
|
if (sc->sc_flen != 5)
|
|
|
|
return;
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
sc->sc_msr = sc->sc_mcr = 0;
|
|
|
|
|
2022-03-28 15:43:30 +03:00
|
|
|
/* not MP-safe */
|
|
|
|
usb_init_task(&sc->sc_task, ucycomstarttask, sc, 0);
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
/* set up tty */
|
2011-04-24 20:26:51 +04:00
|
|
|
sc->sc_tty = tty_alloc();
|
2005-07-30 10:14:49 +04:00
|
|
|
sc->sc_tty->t_sc = sc;
|
|
|
|
sc->sc_tty->t_oproc = ucycomstart;
|
|
|
|
sc->sc_tty->t_param = ucycomparam;
|
|
|
|
|
|
|
|
tty_attach(sc->sc_tty);
|
|
|
|
|
2005-09-01 22:10:20 +04:00
|
|
|
/* Nothing interesting to report */
|
2008-05-24 20:40:58 +04:00
|
|
|
aprint_normal("\n");
|
2020-01-01 12:08:28 +03:00
|
|
|
|
|
|
|
sc->sc_init_state = UCYCOM_INIT_INITED;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2008-05-24 20:40:58 +04:00
|
|
|
ucycom_detach(device_t self, int flags)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc = device_private(self);
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int maj, mn;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if (tp != NULL) {
|
2022-10-27 02:50:28 +03:00
|
|
|
ttylock(tp);
|
2005-07-30 10:14:49 +04:00
|
|
|
CLR(tp->t_state, TS_CARR_ON);
|
|
|
|
CLR(tp->t_cflag, CLOCAL | MDMBUF);
|
|
|
|
ttyflush(tp, FREAD|FWRITE);
|
2022-10-27 02:50:28 +03:00
|
|
|
ttyunlock(tp);
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
/* Wait for processes to go away. */
|
2022-03-28 15:44:17 +03:00
|
|
|
usb_detach_waitold(sc->sc_dev);
|
2005-07-30 10:14:49 +04:00
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* locate the major number */
|
|
|
|
maj = cdevsw_lookup_major(&ucycom_cdevsw);
|
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances. */
|
2006-03-28 21:38:24 +04:00
|
|
|
mn = device_unit(self);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn));
|
|
|
|
vdevgone(maj, mn, mn, VCHR);
|
|
|
|
vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR);
|
|
|
|
vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR);
|
|
|
|
|
2022-03-28 15:43:30 +03:00
|
|
|
usb_rem_task_wait(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL);
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
/* Detach and free the tty. */
|
|
|
|
if (tp != NULL) {
|
|
|
|
DPRINTF(("ucycom_detach: tty_detach %p\n", tp));
|
|
|
|
tty_detach(tp);
|
2011-04-24 20:26:51 +04:00
|
|
|
tty_free(tp);
|
2005-07-30 10:14:49 +04:00
|
|
|
sc->sc_tty = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2010-11-04 01:34:23 +03:00
|
|
|
ucycom_activate(device_t self, enum devact act)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc = device_private(self);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
DPRINTFN(5,("ucycom_activate: %d\n", act));
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
sc->sc_dying = 1;
|
2009-12-07 00:40:31 +03:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return EOPNOTSUPP;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void
|
|
|
|
ucycom_shutdown(struct ucycom_softc *sc)
|
|
|
|
{
|
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp;
|
|
|
|
int s, err;
|
|
|
|
|
2009-01-20 21:20:47 +03:00
|
|
|
DPRINTF(("ucycomopen: unit=%d\n", UCYCOMUNIT(dev)));
|
2005-07-30 10:14:49 +04:00
|
|
|
DPRINTF(("ucycomopen: sc=%p\n", sc));
|
2009-07-24 10:50:40 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
if (sc == NULL)
|
2016-04-23 13:15:27 +03:00
|
|
|
return ENXIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
if (sc->sc_dying)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EIO;
|
2020-01-01 12:08:28 +03:00
|
|
|
if (sc->sc_init_state != UCYCOM_INIT_INITED)
|
|
|
|
return ENXIO;
|
2022-03-28 15:44:17 +03:00
|
|
|
if (!device_is_active(sc->sc_dev))
|
2016-04-23 13:15:27 +03:00
|
|
|
return ENXIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
tp = sc->sc_tty;
|
|
|
|
|
|
|
|
DPRINTF(("ucycomopen: tp=%p\n", tp));
|
|
|
|
|
2006-10-01 23:28:43 +04:00
|
|
|
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
|
2016-04-23 13:15:27 +03:00
|
|
|
return EBUSY;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
|
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
tp->t_dev = dev;
|
|
|
|
|
2022-03-28 15:44:17 +03:00
|
|
|
err = uhidev_open(sc->sc_hdev, &ucycom_intr, sc);
|
2005-07-30 10:14:49 +04:00
|
|
|
if (err) {
|
|
|
|
/* Any cleanup? */
|
|
|
|
splx(s);
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
tp->t_ospeed = 0;
|
|
|
|
(void) ucycomparam(tp, &t);
|
|
|
|
tp->t_iflag = TTYDEF_IFLAG;
|
|
|
|
tp->t_oflag = TTYDEF_OFLAG;
|
|
|
|
tp->t_lflag = TTYDEF_LFLAG;
|
|
|
|
ttychars(tp);
|
|
|
|
ttsetwater(tp);
|
|
|
|
|
|
|
|
/* Allocate an output report buffer */
|
2016-04-23 13:15:27 +03:00
|
|
|
sc->sc_obuf = kmem_alloc(sc->sc_olen, KM_SLEEP);
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf));
|
|
|
|
|
|
|
|
#if 0
|
2009-07-24 10:50:40 +04:00
|
|
|
/* 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!?!
|
2005-07-30 10:14:49 +04:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
|
|
|
|
ucycom_hwiflow(sc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return 0;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
|
2009-01-20 21:20:47 +03:00
|
|
|
DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
|
2005-07-30 10:14:49 +04:00
|
|
|
if (!ISSET(tp->t_state, TS_ISOPEN))
|
2016-04-23 13:15:27 +03:00
|
|
|
return 0;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
(*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);
|
|
|
|
}
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return 0;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Static void
|
|
|
|
ucycomstart(struct tty *tp)
|
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(tp->t_dev));
|
2013-09-15 19:06:04 +04:00
|
|
|
usbd_status err __unused;
|
2005-07-30 10:14:49 +04:00
|
|
|
u_char *data;
|
2009-08-06 11:07:30 +04:00
|
|
|
int cnt, len, s;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2022-10-27 02:50:28 +03:00
|
|
|
KASSERT(ttylocked(tp));
|
2022-03-28 15:43:30 +03:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
if (sc->sc_dying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
|
2020-03-13 21:17:40 +03:00
|
|
|
DPRINTFN(4,("ucycomstart: no go, state=%#x\n", tp->t_state));
|
2005-07-30 10:14:49 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* HW FLOW CTL */
|
|
|
|
if (sc->sc_tx_stopped)
|
|
|
|
goto out;
|
|
|
|
#endif
|
|
|
|
|
2007-11-19 21:51:36 +03:00
|
|
|
if (ttypull(tp) == 0)
|
|
|
|
goto out;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/* Grab the first contiguous region of buffer space. */
|
|
|
|
data = tp->t_outq.c_cf;
|
|
|
|
cnt = ndqb(&tp->t_outq, 0);
|
|
|
|
|
|
|
|
if (cnt == 0) {
|
|
|
|
DPRINTF(("ucycomstart: cnt == 0\n"));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
SET(tp->t_state, TS_BUSY);
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-07-24 10:54:10 +04:00
|
|
|
memset(sc->sc_obuf, 0, sc->sc_olen);
|
2005-07-30 10:14:49 +04:00
|
|
|
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;
|
|
|
|
}
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
memcpy(sc->sc_obuf + 1, data, len);
|
|
|
|
sc->sc_obuf[0] = len | sc->sc_mcr;
|
|
|
|
|
2016-07-07 09:55:38 +03:00
|
|
|
DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n",
|
|
|
|
len, sc->sc_mcr, sc->sc_obuf[0]));
|
2005-08-01 19:00:24 +04:00
|
|
|
#ifdef UCYCOM_DEBUG
|
2005-07-30 10:14:49 +04:00
|
|
|
if (ucycomdebug > 10) {
|
2016-04-23 13:15:27 +03:00
|
|
|
uint32_t i;
|
|
|
|
uint8_t *d = data;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
DPRINTF(("ucycomstart(8): data ="));
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
DPRINTF((" %02x", d[i]));
|
|
|
|
DPRINTF(("\n"));
|
|
|
|
}
|
2005-08-01 19:00:24 +04:00
|
|
|
#endif
|
2005-07-30 10:14:49 +04:00
|
|
|
break;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
case 32:
|
|
|
|
if (cnt > sc->sc_olen - 2) {
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycomstart(32): big buffer %d chars\n",
|
|
|
|
len));
|
2005-07-30 10:14:49 +04:00
|
|
|
len = sc->sc_olen - 2;
|
|
|
|
}
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
memcpy(sc->sc_obuf + 2, data, len);
|
|
|
|
sc->sc_obuf[0] = sc->sc_mcr;
|
|
|
|
sc->sc_obuf[1] = len;
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\n"
|
|
|
|
"sc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1]));
|
2005-08-01 19:00:24 +04:00
|
|
|
#ifdef UCYCOM_DEBUG
|
2005-07-30 10:14:49 +04:00
|
|
|
if (ucycomdebug > 10) {
|
2016-04-23 13:15:27 +03:00
|
|
|
uint32_t i;
|
|
|
|
uint8_t *d = data;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
DPRINTF(("ucycomstart(32): data ="));
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
DPRINTF((" %02x", d[i]));
|
|
|
|
DPRINTF(("\n"));
|
|
|
|
}
|
2005-08-01 19:00:24 +04:00
|
|
|
#endif
|
2005-07-30 10:14:49 +04:00
|
|
|
break;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
default:
|
2009-07-24 10:58:24 +04:00
|
|
|
DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n",
|
2005-07-30 10:14:49 +04:00
|
|
|
sc->sc_olen));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
splx(s);
|
2009-08-06 11:07:30 +04:00
|
|
|
sc->sc_wlen = len;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
|
|
|
if (ucycomdebug > 5) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (len != 0) {
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =",
|
|
|
|
sc->sc_olen));
|
2005-07-30 10:14:49 +04:00
|
|
|
for (i = 0; i < sc->sc_olen; i++)
|
|
|
|
DPRINTF((" %02x", sc->sc_obuf[i]));
|
|
|
|
DPRINTF(("\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-08-06 11:07:30 +04:00
|
|
|
DPRINTFN(4,("ucycomstart: %d chars\n", len));
|
2022-03-28 15:43:30 +03:00
|
|
|
usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Static void
|
|
|
|
ucycomstarttask(void *cookie)
|
|
|
|
{
|
|
|
|
struct ucycom_softc *sc = cookie;
|
|
|
|
usbd_status err;
|
|
|
|
|
2009-08-06 11:07:30 +04:00
|
|
|
/* What can we do on error? */
|
2022-03-28 15:44:17 +03:00
|
|
|
err = uhidev_write_async(sc->sc_hdev, sc->sc_obuf, sc->sc_olen, 0,
|
2022-03-28 15:43:03 +03:00
|
|
|
USBD_NO_TIMEOUT, ucycomwritecb, sc);
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
2009-08-06 11:07:30 +04:00
|
|
|
if (err != USBD_IN_PROGRESS)
|
|
|
|
DPRINTF(("ucycomstart: err=%s\n", usbd_errstr(err)));
|
2022-03-28 15:43:30 +03:00
|
|
|
#else
|
|
|
|
__USE(err);
|
2005-07-30 10:14:49 +04:00
|
|
|
#endif
|
2009-08-06 11:07:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Static void
|
2016-04-23 13:15:27 +03:00
|
|
|
ucycomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
|
2009-08-06 11:07:30 +04:00
|
|
|
{
|
|
|
|
struct ucycom_softc *sc = (struct ucycom_softc *)p;
|
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
usbd_status stat;
|
|
|
|
int len, s;
|
|
|
|
|
|
|
|
if (status == USBD_CANCELLED || sc->sc_dying)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
DPRINTF(("ucycomwritecb: status=%d\n", status));
|
|
|
|
/* XXX we should restart after some delay. */
|
|
|
|
goto error;
|
|
|
|
}
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2009-08-06 11:07:30 +04:00
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &len, &stat);
|
|
|
|
|
|
|
|
if (status != USBD_NORMAL_COMPLETION) {
|
|
|
|
DPRINTFN(4,("ucycomwritecb: status = %d\n", status));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTFN(4,("ucycomwritecb: did %d/%d chars\n", sc->sc_wlen, len));
|
|
|
|
|
|
|
|
s = spltty();
|
2005-07-30 10:14:49 +04:00
|
|
|
CLR(tp->t_state, TS_BUSY);
|
|
|
|
if (ISSET(tp->t_state, TS_FLUSH))
|
|
|
|
CLR(tp->t_state, TS_FLUSH);
|
|
|
|
else
|
2009-08-06 11:07:30 +04:00
|
|
|
ndflush(&tp->t_outq, sc->sc_wlen);
|
2005-07-30 10:14:49 +04:00
|
|
|
(*tp->t_linesw->l_start)(tp);
|
2009-08-06 11:07:30 +04:00
|
|
|
splx(s);
|
|
|
|
return;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2009-08-06 11:07:30 +04:00
|
|
|
error:
|
|
|
|
s = spltty();
|
|
|
|
CLR(tp->t_state, TS_BUSY);
|
2005-07-30 10:14:49 +04:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Static int
|
|
|
|
ucycomparam(struct tty *tp, struct termios *t)
|
|
|
|
{
|
|
|
|
struct ucycom_softc *sc = tp->t_sc;
|
|
|
|
uint32_t baud;
|
|
|
|
uint8_t cfg;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (t->c_ospeed < 0) {
|
|
|
|
DPRINTF(("ucycomparam: c_ospeed < 0\n"));
|
2016-04-23 13:15:27 +03:00
|
|
|
return EINVAL;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check requested parameters. */
|
|
|
|
if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EINVAL;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2016-04-23 13:15:27 +03:00
|
|
|
return 0;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
|
|
|
|
|
|
|
|
/* And copy to tty. */
|
|
|
|
tp->t_ispeed = 0;
|
|
|
|
tp->t_ospeed = t->c_ospeed;
|
|
|
|
tp->t_cflag = t->c_cflag;
|
|
|
|
|
|
|
|
baud = t->c_ispeed;
|
|
|
|
DPRINTF(("ucycomparam: baud=%d\n", baud));
|
|
|
|
|
|
|
|
if (t->c_cflag & CIGNORE) {
|
|
|
|
cfg = sc->sc_cfg;
|
|
|
|
} else {
|
|
|
|
cfg = 0;
|
|
|
|
switch (t->c_cflag & CSIZE) {
|
|
|
|
case CS8:
|
|
|
|
cfg |= UCYCOM_DATA_BITS_8;
|
|
|
|
break;
|
|
|
|
case CS7:
|
|
|
|
cfg |= UCYCOM_DATA_BITS_7;
|
|
|
|
break;
|
|
|
|
case CS6:
|
|
|
|
cfg |= UCYCOM_DATA_BITS_6;
|
|
|
|
break;
|
|
|
|
case CS5:
|
|
|
|
cfg |= UCYCOM_DATA_BITS_5;
|
|
|
|
break;
|
|
|
|
default:
|
2016-04-23 13:15:27 +03:00
|
|
|
return EINVAL;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
cfg |= ISSET(t->c_cflag, CSTOPB) ?
|
|
|
|
UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
|
|
|
|
cfg |= ISSET(t->c_cflag, PARENB) ?
|
|
|
|
UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
|
|
|
|
cfg |= ISSET(t->c_cflag, PARODD) ?
|
|
|
|
UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 */ );
|
|
|
|
|
|
|
|
err = ucycom_configure(sc, baud, cfg);
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static void
|
2006-11-16 04:32:37 +03:00
|
|
|
ucycomstop(struct tty *tp, int flag)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
|
|
|
DPRINTF(("ucycomstop: flag=%d\n", flag));
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2005-07-30 10:14:49 +04:00
|
|
|
ucycomread(dev_t dev, struct uio *uio, int flag)
|
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int err;
|
|
|
|
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
|
|
|
|
flag));
|
2005-07-30 10:14:49 +04:00
|
|
|
if (sc->sc_dying)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
err = ((*tp->t_linesw->l_read)(tp, uio, flag));
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2005-07-30 10:14:49 +04:00
|
|
|
ucycomwrite(dev_t dev, struct uio *uio, int flag)
|
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int err;
|
|
|
|
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
|
|
|
|
flag));
|
2005-07-30 10:14:49 +04:00
|
|
|
if (sc->sc_dying)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
err = ((*tp->t_linesw->l_write)(tp, uio, flag));
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static struct tty *
|
2005-07-30 10:14:49 +04:00
|
|
|
ucycomtty(dev_t dev)
|
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
|
|
|
|
DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp));
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return tp;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2007-03-04 08:59:00 +03:00
|
|
|
ucycomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int err;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (sc->sc_dying)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data));
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
|
2005-07-30 10:14:49 +04:00
|
|
|
if (err != EPASSTHROUGH)
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
err = ttioctl(tp, cmd, data, flag, l);
|
2005-07-30 10:14:49 +04:00
|
|
|
if (err != EPASSTHROUGH)
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
2020-03-14 05:35:33 +03:00
|
|
|
DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd));
|
2005-07-30 10:14:49 +04:00
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
/* case TIOCSBRK:
|
|
|
|
ucycom_break(sc, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCCBRK:
|
|
|
|
ucycom_break(sc, 0);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
case TIOCSDTR:
|
|
|
|
ucycom_dtr(sc, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCCDTR:
|
|
|
|
ucycom_dtr(sc, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCGFLAGS:
|
|
|
|
*(int *)data = sc->sc_swflags;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCSFLAGS:
|
2006-10-02 00:31:49 +04:00
|
|
|
err = kauth_authorize_device_tty(l->l_cred,
|
|
|
|
KAUTH_DEVICE_TTY_PRIVSET, tp);
|
2005-07-30 10:14:49 +04:00
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
sc->sc_swflags = *(int *)data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCMSET:
|
|
|
|
case TIOCMBIS:
|
|
|
|
case TIOCMBIC:
|
|
|
|
tiocm_to_ucycom(sc, cmd, *(int *)data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCMGET:
|
|
|
|
*(int *)data = ucycom_to_tiocm(sc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = EPASSTHROUGH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:23:09 +03:00
|
|
|
static int
|
2005-12-11 15:16:03 +03:00
|
|
|
ucycompoll(dev_t dev, int events, struct lwp *l)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2008-05-24 20:40:58 +04:00
|
|
|
struct ucycom_softc *sc =
|
|
|
|
device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int err;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp,
|
|
|
|
events, l));
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
if (sc->sc_dying)
|
2016-04-23 13:15:27 +03:00
|
|
|
return EIO;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
err = ((*tp->t_linesw->l_poll)(tp, events, l));
|
2016-04-23 13:15:27 +03:00
|
|
|
return err;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-04-23 13:15:27 +03:00
|
|
|
return EINVAL;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud,
|
|
|
|
5 + (cfg & UCYCOM_DATA_MASK),
|
|
|
|
(cfg & UCYCOM_PARITY_MASK) ?
|
|
|
|
((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
|
|
|
|
(cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
|
|
|
|
|
|
|
|
report[0] = baud & 0xff;
|
|
|
|
report[1] = (baud >> 8) & 0xff;
|
|
|
|
report[2] = (baud >> 16) & 0xff;
|
|
|
|
report[3] = (baud >> 24) & 0xff;
|
|
|
|
report[4] = cfg;
|
2022-03-28 15:44:17 +03:00
|
|
|
err = uhidev_set_report(sc->sc_hdev, UHID_FEATURE_REPORT,
|
2005-07-30 10:14:49 +04:00
|
|
|
report, sc->sc_flen);
|
|
|
|
if (err != 0) {
|
|
|
|
DPRINTF(("%s\n", usbd_errstr(err)));
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
sc->sc_baud = baud;
|
|
|
|
sc->sc_cfg = cfg;
|
|
|
|
|
2009-08-06 11:07:30 +04:00
|
|
|
#ifdef UCYCOM_DEBUG
|
|
|
|
ucycom_get_cfg(sc);
|
|
|
|
#endif
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Static void
|
2022-03-28 15:44:17 +03:00
|
|
|
ucycom_intr(void *cookie, void *ibuf, u_int len)
|
2005-07-30 10:14:49 +04:00
|
|
|
{
|
2022-03-28 15:44:17 +03:00
|
|
|
struct ucycom_softc *sc = cookie;
|
2005-07-30 10:14:49 +04:00
|
|
|
struct tty *tp = sc->sc_tty;
|
|
|
|
int (*rint)(int , struct tty *) = tp->t_linesw->l_rint;
|
|
|
|
uint8_t *cp = ibuf;
|
|
|
|
int s, n, st, chg;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
/* We understand 8 byte and 32 byte input records */
|
|
|
|
switch (len) {
|
|
|
|
case 8:
|
|
|
|
n = cp[0] & UCYCOM_LMASK;
|
|
|
|
st = cp[0] & ~UCYCOM_LMASK;
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
st = cp[0];
|
|
|
|
n = cp[1];
|
|
|
|
cp += 2;
|
2024-03-17 23:10:52 +03:00
|
|
|
n = uimin(n, 30);
|
2005-07-30 10:14:49 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
|
|
|
if (ucycomdebug > 5) {
|
2016-04-23 13:15:27 +03:00
|
|
|
uint32_t i;
|
2005-07-30 10:14:49 +04:00
|
|
|
|
|
|
|
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. */
|
2009-08-06 11:07:30 +04:00
|
|
|
s = spltty();
|
2005-07-30 10:14:49 +04:00
|
|
|
while (n-- > 0) {
|
2020-03-14 05:35:33 +03:00
|
|
|
DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
|
2005-07-30 10:14:49 +04:00
|
|
|
if ((*rint)(*cp++, tp) == -1) {
|
|
|
|
/* XXX what should we do? */
|
2022-03-28 15:44:17 +03:00
|
|
|
aprint_error_dev(sc->sc_dev, "lost a character\n");
|
2005-07-30 10:14:49 +04:00
|
|
|
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);
|
|
|
|
|
2016-04-23 13:15:27 +03:00
|
|
|
return ttybits;
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Static void
|
|
|
|
ucycom_dtr(struct ucycom_softc *sc, int set)
|
|
|
|
{
|
|
|
|
uint8_t old;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
old = sc->sc_mcr;
|
|
|
|
if (set)
|
|
|
|
SET(sc->sc_mcr, UCYCOM_DTR);
|
|
|
|
else
|
|
|
|
CLR(sc->sc_mcr, UCYCOM_DTR);
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
if (old ^ sc->sc_mcr)
|
|
|
|
ucycom_set_status(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
Static void
|
|
|
|
ucycom_rts(struct ucycom_softc *sc, int set)
|
|
|
|
{
|
|
|
|
uint8_t old;
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
old = sc->sc_msr;
|
|
|
|
if (set)
|
|
|
|
SET(sc->sc_mcr, UCYCOM_RTS);
|
|
|
|
else
|
|
|
|
CLR(sc->sc_mcr, UCYCOM_RTS);
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
if (old ^ sc->sc_mcr)
|
|
|
|
ucycom_set_status(sc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Static void
|
|
|
|
ucycom_set_status(struct ucycom_softc *sc)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (sc->sc_olen != 8 && sc->sc_olen != 32) {
|
2009-07-24 10:50:40 +04:00
|
|
|
DPRINTFN(2,("ucycom_set_status: unknown output report "
|
|
|
|
"size (%zd)\n", sc->sc_olen));
|
2005-07-30 10:14:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr));
|
|
|
|
|
|
|
|
memset(sc->sc_obuf, 0, sc->sc_olen);
|
|
|
|
sc->sc_obuf[0] = sc->sc_mcr;
|
|
|
|
|
2022-03-28 15:44:17 +03:00
|
|
|
err = uhidev_write(sc->sc_hdev, sc->sc_obuf, sc->sc_olen);
|
2006-10-04 19:53:24 +04:00
|
|
|
if (err) {
|
2005-07-30 10:14:49 +04:00
|
|
|
DPRINTF(("ucycom_set_status: err=%d\n", err));
|
2006-10-04 19:53:24 +04:00
|
|
|
}
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UCYCOM_DEBUG
|
|
|
|
Static void
|
|
|
|
ucycom_get_cfg(struct ucycom_softc *sc)
|
|
|
|
{
|
|
|
|
int err, cfg, baud;
|
|
|
|
uint8_t report[5];
|
2009-07-24 10:54:10 +04:00
|
|
|
|
2022-03-28 15:44:17 +03:00
|
|
|
err = uhidev_get_report(sc->sc_hdev, UHID_FEATURE_REPORT,
|
2005-07-30 10:14:49 +04:00
|
|
|
report, sc->sc_flen);
|
2013-10-14 22:15:12 +04:00
|
|
|
if (err) {
|
|
|
|
DPRINTF(("%s: failed\n", __func__));
|
|
|
|
return;
|
|
|
|
}
|
2005-07-30 10:14:49 +04:00
|
|
|
cfg = report[4];
|
2009-07-24 10:50:40 +04:00
|
|
|
baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) +
|
|
|
|
report[0];
|
2009-08-06 11:07:30 +04:00
|
|
|
DPRINTF(("ucycom_get_cfg: device reports %d baud, %d-%c-%d (%d)\n",
|
2009-07-24 10:50:40 +04:00
|
|
|
baud, 5 + (cfg & UCYCOM_DATA_MASK),
|
2005-07-30 10:14:49 +04:00
|
|
|
(cfg & UCYCOM_PARITY_MASK) ?
|
|
|
|
((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
|
|
|
|
(cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Static void
|
|
|
|
ucycom_cleanup(struct ucycom_softc *sc)
|
|
|
|
{
|
2015-03-07 23:20:55 +03:00
|
|
|
uint8_t *obuf;
|
|
|
|
|
2005-07-30 10:14:49 +04:00
|
|
|
DPRINTF(("ucycom_cleanup: closing uhidev\n"));
|
|
|
|
|
2015-03-07 23:20:55 +03:00
|
|
|
obuf = sc->sc_obuf;
|
|
|
|
sc->sc_obuf = NULL;
|
2022-03-28 15:44:17 +03:00
|
|
|
uhidev_close(sc->sc_hdev);
|
2015-03-07 23:20:55 +03:00
|
|
|
|
|
|
|
if (obuf != NULL)
|
2016-04-23 13:15:27 +03:00
|
|
|
kmem_free(obuf, sc->sc_olen);
|
2005-07-30 10:14:49 +04:00
|
|
|
}
|