2019-01-24 12:33:03 +03:00
|
|
|
/* $NetBSD: btuart.c,v 1.29 2019/01/24 09:33:03 knakahara Exp $ */
|
2008-04-15 15:17:47 +04:00
|
|
|
|
|
|
|
/*-
|
2007-02-20 19:53:20 +03:00
|
|
|
* Copyright (c) 2006, 2007 KIYOHARA Takashi
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2019-01-24 12:33:03 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: btuart.c,v 1.29 2019/01/24 09:33:03 knakahara Exp $");
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2008-04-15 15:17:47 +04:00
|
|
|
#include <sys/conf.h>
|
2007-02-20 19:53:20 +03:00
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/kauth.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
|
2007-10-19 15:59:34 +04:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/intr.h>
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
#include <netbt/bluetooth.h>
|
|
|
|
#include <netbt/hci.h>
|
|
|
|
|
2011-07-31 17:51:53 +04:00
|
|
|
#include "ioconf.h"
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
struct btuart_softc {
|
2007-11-03 20:41:03 +03:00
|
|
|
device_t sc_dev;
|
2008-04-15 15:17:47 +04:00
|
|
|
struct tty * sc_tp; /* tty pointer */
|
|
|
|
|
|
|
|
bool sc_enabled; /* device is enabled */
|
|
|
|
struct hci_unit *sc_unit; /* Bluetooth HCI handle */
|
|
|
|
struct bt_stats sc_stats;
|
|
|
|
|
|
|
|
int sc_state; /* receive state */
|
|
|
|
int sc_want; /* how much we want */
|
|
|
|
struct mbuf * sc_rxp; /* incoming packet */
|
|
|
|
|
|
|
|
bool sc_xmit; /* transmit is active */
|
|
|
|
struct mbuf * sc_txp; /* outgoing packet */
|
|
|
|
|
|
|
|
/* transmit queues */
|
|
|
|
MBUFQ_HEAD() sc_cmdq;
|
|
|
|
MBUFQ_HEAD() sc_aclq;
|
|
|
|
MBUFQ_HEAD() sc_scoq;
|
2007-02-20 19:53:20 +03:00
|
|
|
};
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
/* sc_state */
|
|
|
|
#define BTUART_RECV_PKT_TYPE 0 /* packet type */
|
|
|
|
#define BTUART_RECV_ACL_HDR 1 /* acl header */
|
|
|
|
#define BTUART_RECV_SCO_HDR 2 /* sco header */
|
|
|
|
#define BTUART_RECV_EVENT_HDR 3 /* event header */
|
|
|
|
#define BTUART_RECV_ACL_DATA 4 /* acl packet data */
|
|
|
|
#define BTUART_RECV_SCO_DATA 5 /* sco packet data */
|
|
|
|
#define BTUART_RECV_EVENT_DATA 6 /* event packet data */
|
2007-11-28 23:16:11 +03:00
|
|
|
|
2009-05-12 16:10:29 +04:00
|
|
|
static int btuart_match(device_t, cfdata_t, void *);
|
2007-11-03 20:41:03 +03:00
|
|
|
static void btuart_attach(device_t, device_t, void *);
|
|
|
|
static int btuart_detach(device_t, int);
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
static int btuartopen(dev_t, struct tty *);
|
|
|
|
static int btuartclose(struct tty *, int);
|
|
|
|
static int btuartioctl(struct tty *, u_long, void *, int, struct lwp *);
|
|
|
|
static int btuartinput(int, struct tty *);
|
|
|
|
static int btuartstart(struct tty *);
|
|
|
|
|
|
|
|
static int btuart_enable(device_t);
|
|
|
|
static void btuart_disable(device_t);
|
|
|
|
static void btuart_output_cmd(device_t, struct mbuf *);
|
|
|
|
static void btuart_output_acl(device_t, struct mbuf *);
|
|
|
|
static void btuart_output_sco(device_t, struct mbuf *);
|
|
|
|
static void btuart_stats(device_t, struct bt_stats *, int);
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It doesn't need to be exported, as only btuartattach() uses it,
|
|
|
|
* but there's no "official" way to make it static.
|
|
|
|
*/
|
2007-11-03 20:41:03 +03:00
|
|
|
CFATTACH_DECL_NEW(btuart, sizeof(struct btuart_softc),
|
2007-02-20 19:53:20 +03:00
|
|
|
btuart_match, btuart_attach, btuart_detach, NULL);
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
static struct linesw btuart_disc = {
|
|
|
|
.l_name = "btuart",
|
|
|
|
.l_open = btuartopen,
|
|
|
|
.l_close = btuartclose,
|
|
|
|
.l_read = ttyerrio,
|
|
|
|
.l_write = ttyerrio,
|
|
|
|
.l_ioctl = btuartioctl,
|
|
|
|
.l_rint = btuartinput,
|
|
|
|
.l_start = btuartstart,
|
|
|
|
.l_modem = ttymodem,
|
|
|
|
.l_poll = ttyerrpoll,
|
2007-02-20 19:53:20 +03:00
|
|
|
};
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
static const struct hci_if btuart_hci = {
|
2008-04-15 15:17:47 +04:00
|
|
|
.enable = btuart_enable,
|
|
|
|
.disable = btuart_disable,
|
|
|
|
.output_cmd = btuart_output_cmd,
|
|
|
|
.output_acl = btuart_output_acl,
|
|
|
|
.output_sco = btuart_output_sco,
|
|
|
|
.get_stats = btuart_stats,
|
|
|
|
.ipl = IPL_TTY,
|
2007-02-20 19:53:20 +03:00
|
|
|
};
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* autoconf(9) functions
|
|
|
|
*/
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
/*
|
|
|
|
* pseudo-device attach routine.
|
|
|
|
*/
|
2007-02-20 19:53:20 +03:00
|
|
|
void
|
|
|
|
btuartattach(int num __unused)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
error = ttyldisc_attach(&btuart_disc);
|
2007-02-20 19:53:20 +03:00
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to register line discipline, "
|
|
|
|
"error = %d\n", btuart_cd.cd_name, error);
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = config_cfattach_attach(btuart_cd.cd_name, &btuart_ca);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to register cfattach, error = %d\n",
|
|
|
|
btuart_cd.cd_name, error);
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
config_cfdriver_detach(&btuart_cd);
|
2008-04-15 15:17:47 +04:00
|
|
|
(void) ttyldisc_detach(&btuart_disc);
|
2007-02-20 19:53:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Autoconf match routine.
|
|
|
|
*/
|
|
|
|
static int
|
2009-05-12 16:10:29 +04:00
|
|
|
btuart_match(device_t self __unused, cfdata_t cfdata __unused,
|
2007-12-02 03:09:49 +03:00
|
|
|
void *arg __unused)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
/* pseudo-device; always present */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-04-15 15:17:47 +04:00
|
|
|
* Autoconf attach routine.
|
|
|
|
* Called by config_attach_pseudo(9) when we open the line discipline.
|
2007-02-20 19:53:20 +03:00
|
|
|
*/
|
|
|
|
static void
|
2007-12-02 03:09:49 +03:00
|
|
|
btuart_attach(device_t parent __unused, device_t self, void *aux __unused)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
|
2007-11-03 20:41:03 +03:00
|
|
|
sc->sc_dev = self;
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
MBUFQ_INIT(&sc->sc_cmdq);
|
|
|
|
MBUFQ_INIT(&sc->sc_aclq);
|
|
|
|
MBUFQ_INIT(&sc->sc_scoq);
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
/* Attach Bluetooth unit */
|
2014-05-20 22:25:54 +04:00
|
|
|
sc->sc_unit = hci_attach_pcb(&btuart_hci, self, 0);
|
2008-04-15 15:17:47 +04:00
|
|
|
if (sc->sc_unit == NULL)
|
|
|
|
aprint_error_dev(self, "HCI attach failed\n");
|
2007-02-20 19:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-04-15 15:17:47 +04:00
|
|
|
* Autoconf detach routine.
|
|
|
|
* Called when we close the line discipline.
|
2007-02-20 19:53:20 +03:00
|
|
|
*/
|
|
|
|
static int
|
2007-11-03 20:41:03 +03:00
|
|
|
btuart_detach(device_t self, int flags __unused)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_disable(self);
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
if (sc->sc_unit) {
|
2014-05-20 22:25:54 +04:00
|
|
|
hci_detach_pcb(sc->sc_unit);
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_unit = NULL;
|
|
|
|
}
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Line discipline functions.
|
2007-02-20 19:53:20 +03:00
|
|
|
*/
|
2007-12-02 03:09:49 +03:00
|
|
|
|
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuartopen(dev_t devno __unused, struct tty *tp)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc;
|
2007-12-02 03:09:49 +03:00
|
|
|
device_t dev;
|
2009-05-12 16:10:29 +04:00
|
|
|
cfdata_t cfdata;
|
2007-02-20 19:53:20 +03:00
|
|
|
struct lwp *l = curlwp; /* XXX */
|
|
|
|
int error, unit, s;
|
|
|
|
|
2009-05-07 22:01:56 +04:00
|
|
|
error = kauth_authorize_device(l->l_cred, KAUTH_DEVICE_BLUETOOTH_BTUART,
|
|
|
|
KAUTH_ARG(KAUTH_REQ_DEVICE_BLUETOOTH_BTUART_ADD), NULL, NULL, NULL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
if (tp->t_linesw == &btuart_disc) {
|
|
|
|
sc = tp->t_sc;
|
2007-02-20 19:53:20 +03:00
|
|
|
if (sc != NULL) {
|
|
|
|
splx(s);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT(tp->t_oproc != NULL);
|
|
|
|
|
|
|
|
cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
|
|
|
|
for (unit = 0; unit < btuart_cd.cd_ndevs; unit++)
|
2008-06-13 01:46:21 +04:00
|
|
|
if (device_lookup(&btuart_cd, unit) == NULL)
|
2007-02-20 19:53:20 +03:00
|
|
|
break;
|
2008-04-15 15:17:47 +04:00
|
|
|
|
|
|
|
cfdata->cf_name = btuart_cd.cd_name;
|
|
|
|
cfdata->cf_atname = btuart_cd.cd_name;
|
2007-02-20 19:53:20 +03:00
|
|
|
cfdata->cf_unit = unit;
|
2007-03-06 22:50:28 +03:00
|
|
|
cfdata->cf_fstate = FSTATE_STAR;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2007-12-02 03:09:49 +03:00
|
|
|
dev = config_attach_pseudo(cfdata);
|
|
|
|
if (dev == NULL) {
|
2008-04-15 15:17:47 +04:00
|
|
|
free(cfdata, M_DEVBUF);
|
2007-02-20 19:53:20 +03:00
|
|
|
splx(s);
|
|
|
|
return EIO;
|
|
|
|
}
|
2007-12-02 03:09:49 +03:00
|
|
|
sc = device_private(dev);
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2009-01-11 05:45:45 +03:00
|
|
|
aprint_normal_dev(dev, "major %llu minor %llu\n",
|
|
|
|
(unsigned long long)major(tp->t_dev),
|
|
|
|
(unsigned long long)minor(tp->t_dev));
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
sc->sc_tp = tp;
|
2008-04-15 15:17:47 +04:00
|
|
|
tp->t_sc = sc;
|
|
|
|
|
|
|
|
mutex_spin_enter(&tty_lock);
|
2007-02-20 19:53:20 +03:00
|
|
|
ttyflush(tp, FREAD | FWRITE);
|
2007-11-10 21:29:36 +03:00
|
|
|
mutex_spin_exit(&tty_lock);
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuartclose(struct tty *tp, int flag __unused)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
2008-04-15 15:17:47 +04:00
|
|
|
struct btuart_softc *sc = tp->t_sc;
|
2009-05-12 16:10:29 +04:00
|
|
|
cfdata_t cfdata;
|
2008-04-15 15:17:47 +04:00
|
|
|
int s;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
s = spltty();
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-11-10 21:29:36 +03:00
|
|
|
mutex_spin_enter(&tty_lock);
|
2007-02-20 19:53:20 +03:00
|
|
|
ttyflush(tp, FREAD | FWRITE);
|
2007-11-10 21:29:36 +03:00
|
|
|
mutex_spin_exit(&tty_lock); /* XXX */
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
ttyldisc_release(tp->t_linesw);
|
|
|
|
tp->t_linesw = ttyldisc_default();
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
if (sc != NULL) {
|
|
|
|
tp->t_sc = NULL;
|
|
|
|
if (sc->sc_tp == tp) {
|
2007-11-03 20:41:03 +03:00
|
|
|
cfdata = device_cfdata(sc->sc_dev);
|
|
|
|
config_detach(sc->sc_dev, 0);
|
2007-02-20 19:53:20 +03:00
|
|
|
free(cfdata, M_DEVBUF);
|
|
|
|
}
|
|
|
|
}
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
splx(s);
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuartioctl(struct tty *tp, u_long cmd, void *data __unused,
|
2007-02-20 19:53:20 +03:00
|
|
|
int flag __unused, struct lwp *l __unused)
|
|
|
|
{
|
2008-04-15 15:17:47 +04:00
|
|
|
struct btuart_softc *sc = tp->t_sc;
|
|
|
|
int error;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2019-01-24 12:33:03 +03:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* This function can be called without KERNEL_LOCK when caller's
|
|
|
|
* struct cdevsw is set D_MPSAFE. Is KERNEL_LOCK required?
|
|
|
|
*/
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
if (sc == NULL || tp != sc->sc_tp)
|
|
|
|
return EPASSTHROUGH;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
switch(cmd) {
|
2007-02-20 19:53:20 +03:00
|
|
|
default:
|
|
|
|
error = EPASSTHROUGH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuartinput(int c, struct tty *tp)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
2008-04-15 15:17:47 +04:00
|
|
|
struct btuart_softc *sc = tp->t_sc;
|
2007-02-20 19:53:20 +03:00
|
|
|
struct mbuf *m = sc->sc_rxp;
|
|
|
|
int space = 0;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_enabled)
|
|
|
|
return 0;
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
c &= TTY_CHARMASK;
|
|
|
|
|
|
|
|
/* If we already started a packet, find the trailing end of it. */
|
|
|
|
if (m) {
|
|
|
|
while (m->m_next)
|
|
|
|
m = m->m_next;
|
|
|
|
|
|
|
|
space = M_TRAILINGSPACE(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space == 0) {
|
|
|
|
if (m == NULL) {
|
|
|
|
/* new packet */
|
|
|
|
MGETHDR(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m == NULL) {
|
2010-02-18 10:24:16 +03:00
|
|
|
aprint_error_dev(sc->sc_dev, "out of memory\n");
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
return 0; /* (lost sync) */
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_rxp = m;
|
|
|
|
m->m_pkthdr.len = m->m_len = 0;
|
|
|
|
space = MHLEN;
|
|
|
|
|
|
|
|
sc->sc_state = BTUART_RECV_PKT_TYPE;
|
|
|
|
sc->sc_want = 1;
|
|
|
|
} else {
|
|
|
|
/* extend mbuf */
|
|
|
|
MGET(m->m_next, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m->m_next == NULL) {
|
2010-02-18 10:24:16 +03:00
|
|
|
aprint_error_dev(sc->sc_dev, "out of memory\n");
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
return 0; /* (lost sync) */
|
|
|
|
}
|
|
|
|
|
|
|
|
m = m->m_next;
|
|
|
|
m->m_len = 0;
|
|
|
|
space = MLEN;
|
|
|
|
|
|
|
|
if (sc->sc_want > MINCLSIZE) {
|
|
|
|
MCLGET(m, M_DONTWAIT);
|
|
|
|
if (m->m_flags & M_EXT)
|
|
|
|
space = MCLBYTES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtod(m, uint8_t *)[m->m_len++] = c;
|
|
|
|
sc->sc_rxp->m_pkthdr.len++;
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.byte_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
sc->sc_want--;
|
|
|
|
if (sc->sc_want > 0)
|
|
|
|
return 0; /* want more */
|
|
|
|
|
|
|
|
switch (sc->sc_state) {
|
|
|
|
case BTUART_RECV_PKT_TYPE: /* Got packet type */
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case HCI_ACL_DATA_PKT:
|
|
|
|
sc->sc_state = BTUART_RECV_ACL_HDR;
|
|
|
|
sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_SCO_DATA_PKT:
|
|
|
|
sc->sc_state = BTUART_RECV_SCO_HDR;
|
|
|
|
sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_PKT:
|
|
|
|
sc->sc_state = BTUART_RECV_EVENT_HDR;
|
|
|
|
sc->sc_want = sizeof(hci_event_hdr_t) - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-11-11 15:59:02 +03:00
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"Unknown packet type=%#x!\n", c);
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
m_freem(sc->sc_rxp);
|
|
|
|
sc->sc_rxp = NULL;
|
|
|
|
return 0; /* (lost sync) */
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we assume (correctly of course :) that the packet headers all fit
|
|
|
|
* into a single pkthdr mbuf
|
|
|
|
*/
|
|
|
|
case BTUART_RECV_ACL_HDR: /* Got ACL Header */
|
|
|
|
sc->sc_state = BTUART_RECV_ACL_DATA;
|
|
|
|
sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
|
|
|
|
sc->sc_want = le16toh(sc->sc_want);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTUART_RECV_SCO_HDR: /* Got SCO Header */
|
|
|
|
sc->sc_state = BTUART_RECV_SCO_DATA;
|
|
|
|
sc->sc_want = mtod(m, hci_scodata_hdr_t *)->length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTUART_RECV_EVENT_HDR: /* Got Event Header */
|
|
|
|
sc->sc_state = BTUART_RECV_EVENT_DATA;
|
|
|
|
sc->sc_want = mtod(m, hci_event_hdr_t *)->length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTUART_RECV_ACL_DATA: /* ACL Packet Complete */
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!hci_input_acl(sc->sc_unit, sc->sc_rxp))
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
|
|
|
|
|
|
|
sc->sc_stats.acl_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
sc->sc_rxp = m = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTUART_RECV_SCO_DATA: /* SCO Packet Complete */
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!hci_input_sco(sc->sc_unit, sc->sc_rxp))
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
|
|
|
|
|
|
|
sc->sc_stats.sco_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
sc->sc_rxp = m = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BTUART_RECV_EVENT_DATA: /* Event Packet Complete */
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!hci_input_event(sc->sc_unit, sc->sc_rxp))
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.err_rx++;
|
|
|
|
|
|
|
|
sc->sc_stats.evt_rx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
sc->sc_rxp = m = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("%s: invalid state %d!\n",
|
2007-11-03 20:41:03 +03:00
|
|
|
device_xname(sc->sc_dev), sc->sc_state);
|
2007-02-20 19:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuartstart(struct tty *tp)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
2008-04-15 15:17:47 +04:00
|
|
|
struct btuart_softc *sc = tp->t_sc;
|
2007-02-20 19:53:20 +03:00
|
|
|
struct mbuf *m;
|
|
|
|
int count, rlen;
|
|
|
|
uint8_t *rptr;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_enabled)
|
|
|
|
return 0;
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
m = sc->sc_txp;
|
|
|
|
if (m == NULL) {
|
2008-04-15 15:17:47 +04:00
|
|
|
if (MBUFQ_FIRST(&sc->sc_cmdq)) {
|
|
|
|
MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
|
|
|
|
sc->sc_stats.cmd_tx++;
|
|
|
|
} else if (MBUFQ_FIRST(&sc->sc_scoq)) {
|
|
|
|
MBUFQ_DEQUEUE(&sc->sc_scoq, m);
|
|
|
|
sc->sc_stats.sco_tx++;
|
|
|
|
} else if (MBUFQ_FIRST(&sc->sc_aclq)) {
|
|
|
|
MBUFQ_DEQUEUE(&sc->sc_aclq, m);
|
|
|
|
sc->sc_stats.acl_tx++;
|
|
|
|
} else {
|
|
|
|
sc->sc_xmit = false;
|
|
|
|
return 0; /* no more to send */
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_txp = m;
|
|
|
|
sc->sc_xmit = true;
|
2007-02-20 19:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
rlen = 0;
|
|
|
|
rptr = mtod(m, uint8_t *);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (rlen >= m->m_len) {
|
|
|
|
m = m->m_next;
|
|
|
|
if (m == NULL) {
|
|
|
|
m = sc->sc_txp;
|
|
|
|
sc->sc_txp = NULL;
|
|
|
|
|
|
|
|
if (M_GETCTX(m, void *) == NULL)
|
|
|
|
m_freem(m);
|
2007-11-28 23:16:11 +03:00
|
|
|
else if (!hci_complete_sco(sc->sc_unit, m))
|
|
|
|
sc->sc_stats.err_tx++;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlen = 0;
|
|
|
|
rptr = mtod(m, uint8_t *);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (putc(*rptr++, &tp->t_outq) < 0) {
|
|
|
|
m_adj(m, rlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rlen++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
sc->sc_stats.byte_tx += count;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
if (tp->t_outq.c_cc != 0)
|
|
|
|
(*tp->t_oproc)(tp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* bluetooth(9) functions
|
2007-02-20 19:53:20 +03:00
|
|
|
*/
|
2008-04-15 15:17:47 +04:00
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
static int
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_enable(device_t self)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
2007-11-11 02:12:22 +03:00
|
|
|
struct btuart_softc *sc = device_private(self);
|
2007-11-28 23:16:11 +03:00
|
|
|
int s;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
if (sc->sc_enabled)
|
2007-02-20 19:53:20 +03:00
|
|
|
return 0;
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
s = spltty();
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
sc->sc_enabled = true;
|
|
|
|
sc->sc_xmit = false;
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
splx(s);
|
2007-02-20 19:53:20 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_disable(device_t self)
|
2007-02-20 19:53:20 +03:00
|
|
|
{
|
2007-11-11 02:12:22 +03:00
|
|
|
struct btuart_softc *sc = device_private(self);
|
2007-11-28 23:16:11 +03:00
|
|
|
int s;
|
2007-02-20 19:53:20 +03:00
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_enabled)
|
2007-02-20 19:53:20 +03:00
|
|
|
return;
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
s = spltty();
|
|
|
|
|
2007-02-20 19:53:20 +03:00
|
|
|
if (sc->sc_rxp) {
|
|
|
|
m_freem(sc->sc_rxp);
|
|
|
|
sc->sc_rxp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sc_txp) {
|
|
|
|
m_freem(sc->sc_txp);
|
|
|
|
sc->sc_txp = NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-28 23:16:11 +03:00
|
|
|
MBUFQ_DRAIN(&sc->sc_cmdq);
|
|
|
|
MBUFQ_DRAIN(&sc->sc_aclq);
|
|
|
|
MBUFQ_DRAIN(&sc->sc_scoq);
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
sc->sc_enabled = false;
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
splx(s);
|
2007-02-20 19:53:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_output_cmd(device_t self, struct mbuf *m)
|
2007-11-28 23:16:11 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
int s;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
KASSERT(sc->sc_enabled);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
M_SETCTX(m, NULL);
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
MBUFQ_ENQUEUE(&sc->sc_cmdq, m);
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_xmit)
|
|
|
|
btuartstart(sc->sc_tp);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_output_acl(device_t self, struct mbuf *m)
|
2007-11-28 23:16:11 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
int s;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
KASSERT(sc->sc_enabled);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
M_SETCTX(m, NULL);
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
MBUFQ_ENQUEUE(&sc->sc_aclq, m);
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_xmit)
|
|
|
|
btuartstart(sc->sc_tp);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_output_sco(device_t self, struct mbuf *m)
|
2007-11-28 23:16:11 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
int s;
|
|
|
|
|
2008-04-15 15:17:47 +04:00
|
|
|
KASSERT(sc->sc_enabled);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
MBUFQ_ENQUEUE(&sc->sc_scoq, m);
|
2008-04-15 15:17:47 +04:00
|
|
|
if (!sc->sc_xmit)
|
|
|
|
btuartstart(sc->sc_tp);
|
2007-11-28 23:16:11 +03:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-15 15:17:47 +04:00
|
|
|
btuart_stats(device_t self, struct bt_stats *dest, int flush)
|
2007-11-28 23:16:11 +03:00
|
|
|
{
|
|
|
|
struct btuart_softc *sc = device_private(self);
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
|
|
|
|
|
|
|
|
if (flush)
|
|
|
|
memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|