Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/bluetooth Add support for btuart h5 3-wire protocol.



details:   https://anonhg.NetBSD.org/src/rev/2420233fd3f0
branches:  trunk
changeset: 825996:2420233fd3f0
user:      nat <nat%NetBSD.org@localhost>
date:      Thu Aug 10 13:22:19 2017 +0000

description:
Add support for btuart h5 3-wire protocol.

diffstat:

 sys/dev/bluetooth/Makefile                |     4 +-
 sys/dev/bluetooth/bluetoothdevices.config |     3 +-
 sys/dev/bluetooth/bth5.c                  |  1830 +++++++++++++++++++++++++++++
 sys/dev/bluetooth/bth5.h                  |   128 ++
 sys/dev/bluetooth/files.bluetooth         |     6 +-
 5 files changed, 1967 insertions(+), 4 deletions(-)

diffs (truncated from 2009 to 300 lines):

diff -r 788763ad6083 -r 2420233fd3f0 sys/dev/bluetooth/Makefile
--- a/sys/dev/bluetooth/Makefile        Thu Aug 10 13:13:03 2017 +0000
+++ b/sys/dev/bluetooth/Makefile        Thu Aug 10 13:22:19 2017 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: Makefile,v 1.6 2008/04/15 11:17:48 plunky Exp $
+# $NetBSD: Makefile,v 1.7 2017/08/10 13:22:19 nat Exp $
 
 INCSDIR=       /usr/include/dev/bluetooth
-INCS=          bcsp.h btdev.h bthidev.h btsco.h
+INCS=          bcsp.h btdev.h bth5.h bthidev.h btsco.h
 
 .include <bsd.kinc.mk>
diff -r 788763ad6083 -r 2420233fd3f0 sys/dev/bluetooth/bluetoothdevices.config
--- a/sys/dev/bluetooth/bluetoothdevices.config Thu Aug 10 13:13:03 2017 +0000
+++ b/sys/dev/bluetooth/bluetoothdevices.config Thu Aug 10 13:22:19 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: bluetoothdevices.config,v 1.1 2011/07/20 22:42:59 jakllsch Exp $
+# $NetBSD: bluetoothdevices.config,v 1.2 2017/08/10 13:22:19 nat Exp $
 #
 # Bluetooth devices for config(5) file inclusion.
 
@@ -44,3 +44,4 @@
 # Bluetooth pseudo devices
 pseudo-device  bcsp                    # BlueCore Serial Protocol
 pseudo-device  btuart                  # Bluetooth HCI UART (H4)
+pseudo-device  bthfive                 # Bluetooth HCI UART (H5)
diff -r 788763ad6083 -r 2420233fd3f0 sys/dev/bluetooth/bth5.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/bluetooth/bth5.c  Thu Aug 10 13:22:19 2017 +0000
@@ -0,0 +1,1830 @@
+/*     $NetBSD: bth5.c,v 1.1 2017/08/10 13:22:19 nat Exp $     */
+/*
+ * Copyright (c) 2017 Nathanial Sloss <nathanialsloss%yahoo.com.au@localhost>
+ * All rights reserved.
+ *
+ * Copyright (c) 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>
+__KERNEL_RCSID(0, "$NetBSD: bth5.c,v 1.1 2017/08/10 13:22:19 nat Exp $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/callout.h>
+#include <sys/conf.h>
+#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/sysctl.h>
+#include <sys/syslimits.h>
+#include <sys/systm.h>
+#include <sys/tty.h>
+
+#include <netbt/bluetooth.h>
+#include <netbt/hci.h>
+
+#include <dev/bluetooth/bth5.h>
+
+#include "ioconf.h"
+
+#ifdef BTH5_DEBUG
+#ifdef DPRINTF
+#undef DPRINTF
+#endif
+#ifdef DPRINTFN
+#undef DPRINTFN
+#endif
+
+#define DPRINTF(x)     printf x
+#define DPRINTFN(n, x) do { if (bth5_debug > (n)) printf x; } while (0)
+int bth5_debug = 3;
+#else
+#undef DPRINTF
+#undef DPRINTFN
+
+#define DPRINTF(x)
+#define DPRINTFN(n, x)
+#endif
+
+struct bth5_softc {
+       device_t sc_dev;
+
+       struct tty *sc_tp;
+       struct hci_unit *sc_unit;               /* Bluetooth HCI Unit */
+       struct bt_stats sc_stats;
+
+       int sc_flags;
+
+       /* output queues */
+       MBUFQ_HEAD()    sc_cmdq;
+       MBUFQ_HEAD()    sc_aclq;
+       MBUFQ_HEAD()    sc_scoq;
+
+       int sc_baud;
+       int sc_init_baud;
+
+       /* variables of SLIP Layer */
+       struct mbuf *sc_txp;                    /* outgoing packet */
+       struct mbuf *sc_rxp;                    /* incoming packet */
+       int sc_slip_txrsv;                      /* reserved byte data */
+       int sc_slip_rxexp;                      /* expected byte data */
+       void (*sc_transmit_callback)(struct bth5_softc *, struct mbuf *);
+
+       /* variables of Packet Integrity Layer */
+       int sc_pi_txcrc;                        /* use CRC, if true */
+
+       /* variables of MUX Layer */
+       bool sc_mux_send_ack;                   /* flag for send_ack */
+       bool sc_mux_choke;                      /* Choke signal */
+       struct timeval sc_mux_lastrx;           /* Last Rx Pkt Time */
+
+       /* variables of Sequencing Layer */
+       MBUFQ_HEAD() sc_seqq;                   /* Sequencing Layer queue */
+       MBUFQ_HEAD() sc_seq_retryq;             /* retry queue */
+       uint32_t sc_seq_txseq;
+       uint32_t sc_seq_txack;
+       uint32_t sc_seq_expected_rxseq;
+       uint32_t sc_seq_winspace;
+       uint32_t sc_seq_retries;
+       callout_t sc_seq_timer;
+       uint32_t sc_seq_timeout;
+       uint32_t sc_seq_winsize;
+       uint32_t sc_seq_retry_limit;
+
+       /* variables of Datagram Queue Layer */
+       MBUFQ_HEAD() sc_dgq;                    /* Datagram Queue Layer queue */
+
+       /* variables of BTH5 Link Establishment Protocol */
+       bool sc_le_muzzled;
+       bth5_le_state_t sc_le_state;
+       callout_t sc_le_timer;
+
+       struct sysctllog *sc_log;               /* sysctl log */
+};
+
+/* sc_flags */
+#define        BTH5_XMIT       (1 << 0)        /* transmit active */
+#define        BTH5_ENABLED    (1 << 1)        /* is enabled */
+
+static int bthfive_match(device_t, cfdata_t, void *);
+static void bthfive_attach(device_t, device_t, void *);
+static int bthfive_detach(device_t, int);
+
+/* tty functions */
+static int bth5open(dev_t, struct tty *);
+static int bth5close(struct tty *, int);
+static int bth5ioctl(struct tty *, u_long, void *, int, struct lwp *);
+
+static int bth5_slip_transmit(struct tty *);
+static int bth5_slip_receive(int, struct tty *);
+
+static void bth5_pktintegrity_transmit(struct bth5_softc *);
+static void bth5_pktintegrity_receive(struct bth5_softc *, struct mbuf *);
+static void bth5_crc_update(uint16_t *, uint8_t);
+static uint16_t bth5_crc_reverse(uint16_t);
+
+static void bth5_mux_transmit(struct bth5_softc *sc);
+static void bth5_mux_receive(struct bth5_softc *sc, struct mbuf *m);
+static __inline void bth5_send_ack_command(struct bth5_softc *sc);
+static __inline struct mbuf *bth5_create_ackpkt(void);
+static __inline void bth5_set_choke(struct bth5_softc *, bool);
+
+static void bth5_sequencing_receive(struct bth5_softc *, struct mbuf *);
+static bool bth5_tx_reliable_pkt(struct bth5_softc *, struct mbuf *, u_int);
+static __inline u_int bth5_get_txack(struct bth5_softc *);
+static void bth5_signal_rxack(struct bth5_softc *, uint32_t);
+static void bth5_reliabletx_callback(struct bth5_softc *, struct mbuf *);
+static void bth5_timer_timeout(void *);
+static void bth5_sequencing_reset(struct bth5_softc *);
+
+static void bth5_datagramq_receive(struct bth5_softc *, struct mbuf *);
+static bool bth5_tx_unreliable_pkt(struct bth5_softc *, struct mbuf *, u_int);
+static void bth5_unreliabletx_callback(struct bth5_softc *, struct mbuf *);
+
+static int bth5_start_le(struct bth5_softc *);
+static void bth5_terminate_le(struct bth5_softc *);
+static void bth5_input_le(struct bth5_softc *, struct mbuf *);
+static void bth5_le_timeout(void *);
+
+static void bth5_start(struct bth5_softc *);
+
+/* bluetooth hci functions */
+static int bth5_enable(device_t);
+static void bth5_disable(device_t);
+static void bth5_output_cmd(device_t, struct mbuf *);
+static void bth5_output_acl(device_t, struct mbuf *);
+static void bth5_output_sco(device_t, struct mbuf *);
+static void bth5_stats(device_t, struct bt_stats *, int);
+
+#ifdef BTH5_DEBUG
+static void bth5_packet_print(struct mbuf *m);
+#endif
+
+
+/*
+ * It doesn't need to be exported, as only bth5attach() uses it,
+ * but there's no "official" way to make it static.
+ */
+CFATTACH_DECL_NEW(bthfive, sizeof(struct bth5_softc),
+    bthfive_match, bthfive_attach, bthfive_detach, NULL);
+
+static struct linesw bth5_disc = {
+       .l_name = "bth5",
+       .l_open = bth5open,
+       .l_close = bth5close,
+       .l_read = ttyerrio,
+       .l_write = ttyerrio,
+       .l_ioctl = bth5ioctl,
+       .l_rint = bth5_slip_receive,
+       .l_start = bth5_slip_transmit,
+       .l_modem = ttymodem,
+       .l_poll = ttyerrpoll
+};
+
+static const struct hci_if bth5_hci = {
+       .enable = bth5_enable,
+       .disable = bth5_disable,
+       .output_cmd = bth5_output_cmd,
+       .output_acl = bth5_output_acl,
+       .output_sco = bth5_output_sco,
+       .get_stats = bth5_stats,
+       .ipl = IPL_TTY,
+};
+
+/* ARGSUSED */
+void
+bthfiveattach(int num __unused)
+{
+       int error;
+
+       error = ttyldisc_attach(&bth5_disc);
+       if (error) {
+               aprint_error("%s: unable to register line discipline, "
+                   "error = %d\n", bthfive_cd.cd_name, error);
+               return;
+       }
+
+       error = config_cfattach_attach(bthfive_cd.cd_name, &bthfive_ca);
+       if (error) {
+               aprint_error("%s: unable to register cfattach, error = %d\n",
+                   bthfive_cd.cd_name, error);
+               config_cfdriver_detach(&bthfive_cd);
+               (void) ttyldisc_detach(&bth5_disc);
+       }
+}
+
+/*
+ * Autoconf match routine.
+ *
+ * XXX: unused: config_attach_pseudo(9) does not call ca_match.
+ */
+/* ARGSUSED */
+static int
+bthfive_match(device_t self __unused, cfdata_t cfdata __unused,
+          void *arg __unused)
+{
+
+       /* pseudo-device; always present */
+       return 1;
+}
+
+/*
+ * Autoconf attach routine.  Called by config_attach_pseudo(9) when we
+ * open the line discipline.
+ */
+/* ARGSUSED */
+static void
+bthfive_attach(device_t parent __unused, device_t self, void *aux __unused)
+{
+       struct bth5_softc *sc = device_private(self);
+       const struct sysctlnode *node;
+       int rc, bth5_node_num;
+



Home | Main Index | Thread Index | Old Index