2003-01-11 09:12:09 +03:00
|
|
|
/* $NetBSD: bthci.c,v 1.12 2003/01/11 06:12:09 dsainty Exp $ */
|
2002-08-24 21:30:13 +04:00
|
|
|
|
|
|
|
/*
|
2003-01-05 08:12:38 +03:00
|
|
|
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
|
2002-08-24 21:30:13 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2003-01-05 08:12:38 +03:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) and
|
|
|
|
* David Sainty (David.Sainty@dtsp.co.nz).
|
2002-08-24 21:30:13 +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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bthcidrv.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
|
|
|
#include <dev/bluetooth/bluetooth.h>
|
2003-01-11 09:12:09 +03:00
|
|
|
#include <dev/bluetooth/bthci_util.h>
|
2002-08-24 21:30:13 +04:00
|
|
|
#include <dev/bluetooth/bthcivar.h>
|
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
#ifdef BTHCI_DEBUG
|
2002-08-24 21:30:13 +04:00
|
|
|
#define DPRINTF(x) if (bthcidebug) printf x
|
2003-01-05 08:12:38 +03:00
|
|
|
#define DPRINTFN(n,x) if (bthcidebug>(n)) printf x
|
2002-08-24 21:30:13 +04:00
|
|
|
#define Static
|
2003-01-05 08:12:38 +03:00
|
|
|
int bthcidebug = 99;
|
2002-08-24 21:30:13 +04:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
2003-01-05 08:12:38 +03:00
|
|
|
#define DPRINTFN(n,x)
|
2002-08-24 21:30:13 +04:00
|
|
|
#define Static static
|
|
|
|
#endif
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
struct bthci_softc {
|
|
|
|
struct device sc_dev;
|
|
|
|
struct btframe_methods const *sc_methods;
|
|
|
|
void *sc_handle;
|
|
|
|
|
|
|
|
u_int8_t *sc_rd_buf;
|
|
|
|
size_t sc_rd_len;
|
|
|
|
struct selinfo sc_rd_sel;
|
|
|
|
|
|
|
|
int sc_refcnt;
|
|
|
|
int sc_outputready;
|
|
|
|
char sc_open;
|
|
|
|
char sc_dying;
|
|
|
|
};
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
int bthci_match(struct device *parent, struct cfdata *match, void *aux);
|
|
|
|
void bthci_attach(struct device *parent, struct device *self, void *aux);
|
|
|
|
int bthci_activate(struct device *self, enum devact act);
|
|
|
|
int bthci_detach(struct device *self, int flags);
|
|
|
|
|
|
|
|
#if NBTHCIDRV == 0
|
|
|
|
/* In case we just have tty attachment. */
|
|
|
|
struct cfdriver bthci_cd = {
|
|
|
|
NULL, "bthci", DV_DULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-10-01 00:43:55 +04:00
|
|
|
CFATTACH_DECL(bthci, sizeof(struct bthci_softc),
|
2002-10-02 20:33:28 +04:00
|
|
|
bthci_match, bthci_attach, bthci_detach, bthci_activate);
|
2002-08-24 21:30:13 +04:00
|
|
|
|
|
|
|
extern struct cfdriver bthci_cd;
|
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
dev_type_open(bthciopen);
|
|
|
|
dev_type_close(bthciclose);
|
|
|
|
dev_type_poll(bthcipoll);
|
2002-10-23 13:10:23 +04:00
|
|
|
dev_type_kqfilter(bthcikqfilter);
|
2002-09-06 17:18:43 +04:00
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
static int bthciread(dev_t dev, struct uio *uio, int flag);
|
|
|
|
static int bthciwrite(dev_t dev, struct uio *uio, int flag);
|
|
|
|
|
|
|
|
static void bthci_recveventdata(void *h, u_int8_t *data, size_t len);
|
|
|
|
static void bthci_recvacldata(void *h, u_int8_t *data, size_t len);
|
|
|
|
static void bthci_recvscodata(void *h, u_int8_t *data, size_t len);
|
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
const struct cdevsw bthci_cdevsw = {
|
2003-01-05 08:12:38 +03:00
|
|
|
bthciopen, bthciclose, bthciread, bthciwrite, noioctl,
|
2002-10-23 13:10:23 +04:00
|
|
|
nostop, notty, bthcipoll, nommap, bthcikqfilter,
|
2002-09-06 17:18:43 +04:00
|
|
|
};
|
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
#define BTHCIUNIT(dev) (minor(dev))
|
2002-08-24 21:30:13 +04:00
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
struct btframe_callback_methods const bthci_callbacks = {
|
|
|
|
bthci_recveventdata, bthci_recvacldata, bthci_recvscodata
|
|
|
|
};
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
int
|
|
|
|
bthci_match(struct device *parent, struct cfdata *match, void *aux)
|
|
|
|
{
|
|
|
|
/*struct bt_attach_args *bt = aux;*/
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bthci_attach(struct device *parent, struct device *self, void *aux)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc = (struct bthci_softc *)self;
|
|
|
|
struct bt_attach_args *bt = aux;
|
|
|
|
|
|
|
|
sc->sc_methods = bt->bt_methods;
|
|
|
|
sc->sc_handle = bt->bt_handle;
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
*bt->bt_cb = &bthci_callbacks;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (sc->sc_methods->bt_open == NULL ||
|
|
|
|
sc->sc_methods->bt_close == NULL ||
|
2003-01-11 09:12:09 +03:00
|
|
|
|
|
|
|
sc->sc_methods->bt_control.bt_alloc == NULL ||
|
|
|
|
sc->sc_methods->bt_control.bt_send == NULL ||
|
|
|
|
|
|
|
|
sc->sc_methods->bt_acldata.bt_alloc == NULL ||
|
|
|
|
sc->sc_methods->bt_acldata.bt_send == NULL ||
|
|
|
|
|
|
|
|
sc->sc_methods->bt_scodata.bt_alloc == NULL ||
|
|
|
|
sc->sc_methods->bt_scodata.bt_send == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("%s: missing methods", sc->sc_dev.dv_xname);
|
2002-08-24 21:30:13 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bthci_activate(struct device *self, enum devact act)
|
|
|
|
{
|
|
|
|
/*struct bthci_softc *sc = (struct bthci_softc *)self;*/
|
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
static void
|
|
|
|
bthci_abortdealloc(struct bthci_softc *sc)
|
|
|
|
{
|
|
|
|
DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
|
|
|
|
|
|
|
|
if (sc->sc_rd_buf != NULL) {
|
|
|
|
free(sc->sc_rd_buf, M_TEMP);
|
|
|
|
sc->sc_rd_buf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
int
|
|
|
|
bthci_detach(struct device *self, int flags)
|
|
|
|
{
|
2003-01-05 08:12:38 +03:00
|
|
|
struct bthci_softc *sc = (struct bthci_softc *)self;
|
2002-08-24 21:30:13 +04:00
|
|
|
int maj, mn;
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
sc->sc_dying = 1;
|
|
|
|
wakeup(&sc->sc_outputready);
|
|
|
|
|
|
|
|
DPRINTFN(1, ("%s: waiting for refcount (%d)\n",
|
|
|
|
__func__, sc->sc_refcnt));
|
|
|
|
|
|
|
|
if (--sc->sc_refcnt >= 0)
|
|
|
|
tsleep(&sc->sc_refcnt, PZERO, "bthcdt", 0);
|
|
|
|
|
|
|
|
DPRINTFN(1, ("%s: refcount complete\n", __func__));
|
|
|
|
|
|
|
|
bthci_abortdealloc(sc);
|
2002-08-24 21:30:13 +04:00
|
|
|
|
|
|
|
/* locate the major number */
|
2002-09-12 10:42:54 +04:00
|
|
|
maj = cdevsw_lookup_major(&bthci_cdevsw);
|
2002-08-24 21:30:13 +04:00
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances (calls close). */
|
|
|
|
mn = self->dv_unit;
|
|
|
|
vdevgone(maj, mn, mn, VCHR);
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
DPRINTFN(1, ("%s: driver detached\n", __func__));
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bthciopen(dev_t dev, int flag, int mode, struct proc *p)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
|
|
|
int error;
|
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
|
|
|
|
return (EIO);
|
|
|
|
if (sc->sc_open)
|
|
|
|
return (EBUSY);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
sc->sc_rd_buf = malloc(BTHCI_ACL_DATA_MAX_LEN + 1, M_TEMP, M_NOWAIT);
|
|
|
|
if (sc->sc_rd_buf == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
sc->sc_refcnt++;
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc->sc_methods->bt_open != NULL) {
|
|
|
|
error = sc->sc_methods->bt_open(sc->sc_handle, flag, mode, p);
|
|
|
|
if (error)
|
2003-01-05 08:12:38 +03:00
|
|
|
goto bad;
|
2002-08-24 21:30:13 +04:00
|
|
|
}
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
if (--sc->sc_refcnt < 0)
|
|
|
|
wakeup(&sc->sc_refcnt);
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
sc->sc_open = 1;
|
|
|
|
return (0);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
bad:
|
|
|
|
free(sc->sc_rd_buf, M_TEMP);
|
|
|
|
sc->sc_rd_buf = NULL;
|
|
|
|
|
|
|
|
return error;
|
2002-08-24 21:30:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bthciclose(dev_t dev, int flag, int mode, struct proc *p)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
|
|
|
int error;
|
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
if (!sc->sc_open)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
sc->sc_refcnt++;
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
sc->sc_open = 0;
|
2003-01-05 08:12:38 +03:00
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc->sc_methods->bt_close != NULL)
|
|
|
|
error = sc->sc_methods->bt_close(sc->sc_handle, flag, mode, p);
|
|
|
|
else
|
|
|
|
error = 0;
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
bthci_abortdealloc(sc);
|
|
|
|
|
|
|
|
if (--sc->sc_refcnt < 0)
|
|
|
|
wakeup(&sc->sc_refcnt);
|
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
static int
|
2002-08-24 21:30:13 +04:00
|
|
|
bthciread(dev_t dev, struct uio *uio, int flag)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
2003-01-05 08:12:38 +03:00
|
|
|
int error;
|
|
|
|
int s;
|
|
|
|
unsigned int blocked;
|
2002-08-24 21:30:13 +04:00
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
|
|
|
|
!sc->sc_open || sc->sc_dying)
|
2002-08-24 21:30:13 +04:00
|
|
|
return (EIO);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
DPRINTFN(1, ("%s: length=%lu\n", __func__,
|
|
|
|
(unsigned long)uio->uio_resid));
|
|
|
|
|
|
|
|
sc->sc_refcnt++;
|
|
|
|
|
|
|
|
s = sc->sc_methods->bt_splraise();
|
|
|
|
while (!sc->sc_outputready) {
|
|
|
|
#define BTHCI_READ_EVTS (BT_CBBLOCK_ACL_DATA | BT_CBBLOCK_EVENT)
|
|
|
|
blocked = sc->sc_methods->bt_unblockcb(sc->sc_handle,
|
|
|
|
BTHCI_READ_EVTS);
|
|
|
|
if ((blocked & BTHCI_READ_EVTS) != 0) {
|
|
|
|
/* Problem unblocking events */
|
|
|
|
splx(s);
|
|
|
|
DPRINTFN(1,("%s: couldn't unblock events\n",
|
|
|
|
__func__));
|
|
|
|
error = EIO;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTFN(5,("%s: calling tsleep()\n", __func__));
|
|
|
|
error = tsleep(&sc->sc_outputready, PZERO | PCATCH,
|
|
|
|
"bthcrd", 0);
|
|
|
|
if (sc->sc_dying)
|
|
|
|
error = EIO;
|
|
|
|
if (error) {
|
|
|
|
splx(s);
|
|
|
|
DPRINTFN(0, ("%s: tsleep() = %d\n",
|
|
|
|
__func__, error));
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if (uio->uio_resid < sc->sc_rd_len) {
|
2002-08-24 21:30:13 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("bthciread: short read %d < %d\n", uio->uio_resid,
|
2003-01-05 08:12:38 +03:00
|
|
|
sc->sc_rd_len);
|
2002-08-24 21:30:13 +04:00
|
|
|
#endif
|
2003-01-05 08:12:38 +03:00
|
|
|
error = EINVAL;
|
|
|
|
goto ret;
|
2002-08-24 21:30:13 +04:00
|
|
|
}
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
error = uiomove(sc->sc_rd_buf, sc->sc_rd_len, uio);
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
sc->sc_outputready = 0;
|
|
|
|
sc->sc_rd_len = 0;
|
|
|
|
|
|
|
|
/* Unblock events (in case they were blocked) */
|
|
|
|
sc->sc_methods->bt_unblockcb(sc->sc_handle,
|
|
|
|
BT_CBBLOCK_ACL_DATA |
|
|
|
|
BT_CBBLOCK_EVENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret:
|
|
|
|
if (--sc->sc_refcnt < 0)
|
|
|
|
wakeup(&sc->sc_refcnt);
|
|
|
|
|
|
|
|
return error;
|
2002-08-24 21:30:13 +04:00
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
static int
|
|
|
|
bthcichanwrite(struct bthci_softc *sc,
|
|
|
|
struct btframe_channel const *btchannel, struct uio *uio)
|
|
|
|
{
|
|
|
|
struct btframe_buffer *btframebuf;
|
|
|
|
u_int8_t *btbuf;
|
|
|
|
int error;
|
|
|
|
size_t uiolen;
|
|
|
|
|
|
|
|
sc->sc_refcnt++;
|
|
|
|
|
|
|
|
uiolen = uio->uio_resid;
|
|
|
|
|
|
|
|
btbuf = btchannel->bt_alloc(sc->sc_handle, uiolen, &btframebuf);
|
|
|
|
if (btbuf == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = uiomove(btbuf, uiolen, uio);
|
|
|
|
if (error)
|
|
|
|
goto ret;
|
|
|
|
|
|
|
|
error = btchannel->bt_send(sc->sc_handle, btframebuf, uiolen);
|
|
|
|
|
|
|
|
ret:
|
|
|
|
if (--sc->sc_refcnt < 0)
|
|
|
|
wakeup(&sc->sc_refcnt);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
static int
|
2002-08-24 21:30:13 +04:00
|
|
|
bthciwrite(dev_t dev, struct uio *uio, int flag)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
2003-01-11 09:12:09 +03:00
|
|
|
struct btframe_channel const *btchannel;
|
2003-01-05 08:12:38 +03:00
|
|
|
int error;
|
|
|
|
size_t uiolen;
|
2003-01-11 09:12:09 +03:00
|
|
|
u_int8_t bthcitype;
|
2002-08-24 21:30:13 +04:00
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
2002-08-24 21:30:13 +04:00
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
|
|
|
|
return (EIO);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
uiolen = uio->uio_resid;
|
|
|
|
|
|
|
|
DPRINTFN(1, ("%s: length=%lu\n", __func__, (unsigned long)uiolen));
|
|
|
|
|
|
|
|
if (uiolen > BTHCI_ACL_DATA_MAX_LEN + 1) {
|
2002-08-24 21:30:13 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
2003-01-05 08:12:38 +03:00
|
|
|
printf("bthciread: long write %d\n", uio->uio_resid);
|
2002-08-24 21:30:13 +04:00
|
|
|
#endif
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
if (uiolen <= 1) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("bthciread: short write %d\n", uio->uio_resid);
|
2002-08-24 21:30:13 +04:00
|
|
|
#endif
|
2003-01-05 08:12:38 +03:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
error = uiomove(&bthcitype, 1, uio);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2003-01-05 08:12:38 +03:00
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
if (bthcitype == BTHCI_PKTID_COMMAND) {
|
2003-01-05 08:12:38 +03:00
|
|
|
/* Command */
|
2003-01-11 09:12:09 +03:00
|
|
|
btchannel = &sc->sc_methods->bt_control;
|
|
|
|
} else if (bthcitype == BTHCI_PKTID_ACL_DATA) {
|
2003-01-05 08:12:38 +03:00
|
|
|
/* ACL data */
|
2003-01-11 09:12:09 +03:00
|
|
|
btchannel = &sc->sc_methods->bt_acldata;
|
|
|
|
} else if (bthcitype == BTHCI_PKTID_SCO_DATA) {
|
2003-01-05 08:12:38 +03:00
|
|
|
/* SCO data */
|
2003-01-11 09:12:09 +03:00
|
|
|
btchannel = &sc->sc_methods->bt_scodata;
|
2003-01-05 08:12:38 +03:00
|
|
|
} else {
|
|
|
|
/* Bad packet type */
|
2003-01-11 09:12:09 +03:00
|
|
|
return (EINVAL);
|
2003-01-05 08:12:38 +03:00
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
return (bthcichanwrite(sc, btchannel, uio));
|
2003-01-05 08:12:38 +03:00
|
|
|
}
|
2002-08-24 21:30:13 +04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
int
|
|
|
|
bthciioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
|
|
|
void *vaddr = addr;
|
|
|
|
int error;
|
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
|
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FIONBIO:
|
|
|
|
/* All handled in the upper FS layer. */
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
bthcipoll(dev_t dev, int events, struct proc *p)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
2003-01-05 08:12:38 +03:00
|
|
|
int revents;
|
|
|
|
int s;
|
2002-08-24 21:30:13 +04:00
|
|
|
|
2002-09-12 10:42:54 +04:00
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
2002-08-24 21:30:13 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
|
|
|
|
return (EIO);
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
revents = events & (POLLOUT | POLLWRNORM);
|
|
|
|
|
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
s = sc->sc_methods->bt_splraise();
|
|
|
|
if (sc->sc_outputready) {
|
|
|
|
DPRINTFN(2,("%s: have data\n", __func__));
|
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
|
|
|
} else {
|
|
|
|
DPRINTFN(2,("%s: recording read select\n",
|
|
|
|
__func__));
|
|
|
|
selrecord(p, &sc->sc_rd_sel);
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return revents;
|
2002-08-24 21:30:13 +04:00
|
|
|
}
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
static void
|
|
|
|
filt_bthcirdetach(struct knote *kn)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc = kn->kn_hook;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = sc->sc_methods->bt_splraise();
|
|
|
|
SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
filt_bthciread(struct knote *kn, long hint)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc = kn->kn_hook;
|
|
|
|
|
|
|
|
kn->kn_data = sc->sc_rd_len;
|
|
|
|
return (kn->kn_data > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct filterops bthciread_filtops =
|
|
|
|
{ 1, NULL, filt_bthcirdetach, filt_bthciread };
|
|
|
|
|
2002-10-23 13:10:23 +04:00
|
|
|
int
|
|
|
|
bthcikqfilter(dev_t dev, struct knote *kn)
|
|
|
|
{
|
|
|
|
struct bthci_softc *sc;
|
2003-01-05 08:12:38 +03:00
|
|
|
struct klist *klist;
|
|
|
|
int s;
|
2002-10-23 13:10:23 +04:00
|
|
|
|
|
|
|
sc = device_lookup(&bthci_cd, BTHCIUNIT(dev));
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
|
|
|
|
return (EIO);
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
switch (kn->kn_filter) {
|
|
|
|
case EVFILT_READ:
|
|
|
|
klist = &sc->sc_rd_sel.sel_klist;
|
|
|
|
kn->kn_fop = &bthciread_filtops;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
kn->kn_hook = sc;
|
|
|
|
|
|
|
|
s = sc->sc_methods->bt_splraise();
|
|
|
|
SLIST_INSERT_HEAD(klist, kn, kn_selnext);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
static void
|
|
|
|
bthci_recveventdata(void *h, u_int8_t *data, size_t len)
|
2003-01-05 08:12:38 +03:00
|
|
|
{
|
|
|
|
struct bthci_softc *sc = h;
|
|
|
|
size_t pktlength;
|
2003-01-11 09:12:09 +03:00
|
|
|
unsigned int ecode;
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
if (len < BTHCI_EVENT_MIN_LEN || len > BTHCI_EVENT_MAX_LEN) {
|
|
|
|
DPRINTFN(1,("%s: invalid sized event, size=%u\n",
|
|
|
|
__func__, (unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
ecode = data[0];
|
|
|
|
DPRINTFN(1,("%s: received event: %02x (%s)\n", __func__,
|
|
|
|
ecode, bthci_eventstr(ecode)));
|
2003-01-05 08:12:38 +03:00
|
|
|
|
|
|
|
pktlength = data[BTHCI_EVENT_LEN_OFFT] + BTHCI_EVENT_LEN_OFFT +
|
|
|
|
BTHCI_EVENT_LEN_LENGTH;
|
|
|
|
if (pktlength != len) {
|
|
|
|
DPRINTFN(1,("%s: event length didn't match, "
|
|
|
|
"pktlen=%u size=%u\n",
|
|
|
|
__func__, (unsigned int)pktlength,
|
|
|
|
(unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
if (!sc->sc_open)
|
|
|
|
return;
|
|
|
|
|
2003-01-05 08:12:38 +03:00
|
|
|
if (sc->sc_rd_len != 0) {
|
|
|
|
DPRINTFN(1,("%s: dropping an event, size=%u\n",
|
|
|
|
__func__, (unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_rd_buf[0] = BTHCI_PKTID_EVENT;
|
|
|
|
memcpy(&sc->sc_rd_buf[1], data, len);
|
|
|
|
sc->sc_rd_len = len + 1;
|
|
|
|
sc->sc_outputready = 1;
|
|
|
|
|
|
|
|
sc->sc_methods->bt_blockcb(sc->sc_handle,
|
|
|
|
BT_CBBLOCK_ACL_DATA | BT_CBBLOCK_EVENT);
|
|
|
|
|
|
|
|
wakeup(&sc->sc_outputready);
|
|
|
|
selnotify(&sc->sc_rd_sel, 0);
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
static void
|
|
|
|
bthci_recvacldata(void *h, u_int8_t *data, size_t len)
|
2003-01-05 08:12:38 +03:00
|
|
|
{
|
|
|
|
struct bthci_softc *sc = h;
|
|
|
|
size_t pktlength;
|
|
|
|
|
|
|
|
if (!sc->sc_open)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (len < BTHCI_ACL_DATA_MIN_LEN || len > BTHCI_ACL_DATA_MAX_LEN) {
|
|
|
|
DPRINTFN(1,("%s: invalid acl packet, size=%u\n",
|
|
|
|
__func__, (unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pktlength = BTGETW(&data[BTHCI_ACL_DATA_LEN_OFFT]) +
|
|
|
|
BTHCI_ACL_DATA_LEN_OFFT +
|
|
|
|
BTHCI_ACL_DATA_LEN_LENGTH;
|
|
|
|
|
|
|
|
if (pktlength != len) {
|
|
|
|
DPRINTFN(1,("%s: acl packet length didn't match, "
|
|
|
|
"pktlen=%u size=%u\n",
|
|
|
|
__func__, (unsigned int)pktlength,
|
|
|
|
(unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sc_rd_len != 0) {
|
|
|
|
DPRINTFN(1,("%s: dropping an acl packet, size=%u\n",
|
|
|
|
__func__, (unsigned int)len));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_rd_buf[0] = BTHCI_PKTID_ACL_DATA;
|
|
|
|
memcpy(&sc->sc_rd_buf[1], data, len);
|
|
|
|
sc->sc_rd_len = len + 1;
|
|
|
|
sc->sc_outputready = 1;
|
|
|
|
|
|
|
|
sc->sc_methods->bt_blockcb(sc->sc_handle,
|
|
|
|
BT_CBBLOCK_ACL_DATA | BT_CBBLOCK_EVENT);
|
|
|
|
|
|
|
|
wakeup(&sc->sc_outputready);
|
|
|
|
selnotify(&sc->sc_rd_sel, 0);
|
|
|
|
}
|
|
|
|
|
2003-01-11 09:12:09 +03:00
|
|
|
static void
|
|
|
|
bthci_recvscodata(void *h, u_int8_t *data, size_t len)
|
2003-01-05 08:12:38 +03:00
|
|
|
{
|
|
|
|
struct bthci_softc *sc = h;
|
|
|
|
|
|
|
|
if (!sc->sc_open)
|
|
|
|
return;
|
2002-10-23 13:10:23 +04:00
|
|
|
}
|