Home | History | Annotate | Line # | Download | only in bluetooth
btuart.c revision 1.2
      1  1.2  christos /*	$NetBSD: btuart.c,v 1.2 2007/03/04 06:01:45 christos Exp $	*/
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (c) 2006, 2007 KIYOHARA Takashi
      4  1.1  kiyohara  * All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kiyohara  */
     27  1.1  kiyohara 
     28  1.1  kiyohara #include <sys/cdefs.h>
     29  1.2  christos __KERNEL_RCSID(0, "$NetBSD: btuart.c,v 1.2 2007/03/04 06:01:45 christos Exp $");
     30  1.1  kiyohara 
     31  1.1  kiyohara #include <sys/types.h>
     32  1.1  kiyohara #include <sys/param.h>
     33  1.1  kiyohara #include <sys/device.h>
     34  1.1  kiyohara #include <sys/errno.h>
     35  1.1  kiyohara 
     36  1.1  kiyohara #include <sys/conf.h>
     37  1.1  kiyohara #include <sys/fcntl.h>
     38  1.1  kiyohara #include <sys/kauth.h>
     39  1.1  kiyohara #include <sys/kernel.h>
     40  1.1  kiyohara #include <sys/malloc.h>
     41  1.1  kiyohara #include <sys/mbuf.h>
     42  1.1  kiyohara #include <sys/proc.h>
     43  1.1  kiyohara #include <sys/syslimits.h>
     44  1.1  kiyohara #include <sys/systm.h>
     45  1.1  kiyohara #include <sys/tty.h>
     46  1.1  kiyohara 
     47  1.1  kiyohara #include <machine/bus.h>
     48  1.1  kiyohara #include <machine/intr.h>
     49  1.1  kiyohara 
     50  1.1  kiyohara #include <netbt/bluetooth.h>
     51  1.1  kiyohara #include <netbt/hci.h>
     52  1.1  kiyohara 
     53  1.1  kiyohara #include <dev/bluetooth/btuart.h>
     54  1.1  kiyohara #include <dev/firmload.h>
     55  1.1  kiyohara 
     56  1.1  kiyohara #include "ioconf.h"
     57  1.1  kiyohara 
     58  1.1  kiyohara #define BTUART_DEBUG
     59  1.1  kiyohara #ifdef BTUART_DEBUG
     60  1.1  kiyohara int btuart_debug = 1;
     61  1.1  kiyohara #endif
     62  1.1  kiyohara 
     63  1.1  kiyohara struct btuart_softc;
     64  1.1  kiyohara struct bth4hci {
     65  1.1  kiyohara 	int type;
     66  1.1  kiyohara 	int init_baud;
     67  1.1  kiyohara #define FLOW_CTL	1
     68  1.1  kiyohara 	int flags;
     69  1.1  kiyohara 	int (*init)(struct btuart_softc *);
     70  1.1  kiyohara };
     71  1.1  kiyohara 
     72  1.1  kiyohara struct btuart_softc {
     73  1.1  kiyohara 	struct device sc_dev;
     74  1.1  kiyohara 
     75  1.1  kiyohara 	struct tty *sc_tp;
     76  1.1  kiyohara 	struct hci_unit sc_unit;		/* Bluetooth HCI Unit */
     77  1.1  kiyohara 
     78  1.1  kiyohara 	struct bth4hci sc_bth4hci;
     79  1.1  kiyohara 	int sc_baud;
     80  1.1  kiyohara 
     81  1.1  kiyohara 	int sc_state;				/* receive state */
     82  1.1  kiyohara #define BTUART_RECV_PKT_TYPE	0			/* packet type */
     83  1.1  kiyohara #define BTUART_RECV_ACL_HDR	1			/* acl header */
     84  1.1  kiyohara #define BTUART_RECV_SCO_HDR	2			/* sco header */
     85  1.1  kiyohara #define BTUART_RECV_EVENT_HDR	3			/* event header */
     86  1.1  kiyohara #define BTUART_RECV_ACL_DATA	4			/* acl packet data */
     87  1.1  kiyohara #define BTUART_RECV_SCO_DATA	5			/* sco packet data */
     88  1.1  kiyohara #define BTUART_RECV_EVENT_DATA	6			/* event packet data */
     89  1.1  kiyohara 	int sc_want;				/* how much we want */
     90  1.1  kiyohara 	struct mbuf *sc_rxp;			/* incoming packet */
     91  1.1  kiyohara 	struct mbuf *sc_txp;			/* outgoing packet */
     92  1.1  kiyohara 
     93  1.1  kiyohara 	void (*sc_input_acl)(struct hci_unit *, struct mbuf *);
     94  1.1  kiyohara 	void (*sc_input_sco)(struct hci_unit *, struct mbuf *);
     95  1.1  kiyohara 	void (*sc_input_event)(struct hci_unit *, struct mbuf *);
     96  1.1  kiyohara };
     97  1.1  kiyohara 
     98  1.1  kiyohara void btuartattach(int);
     99  1.1  kiyohara static int btuart_match(struct device *, struct cfdata *, void *);
    100  1.1  kiyohara static void btuart_attach(struct device *, struct device *, void *);
    101  1.1  kiyohara static int btuart_detach(struct device *, int);
    102  1.1  kiyohara 
    103  1.1  kiyohara static int bth4_waitresp(struct btuart_softc *, struct mbuf **, uint16_t);
    104  1.1  kiyohara static int bth4_firmload(struct btuart_softc *, char *,
    105  1.1  kiyohara 			 int (*)(struct btuart_softc *, int, char *));
    106  1.1  kiyohara static int init_ericsson(struct btuart_softc *);
    107  1.1  kiyohara static int init_digi(struct btuart_softc *);
    108  1.1  kiyohara static int init_texas(struct btuart_softc *);
    109  1.1  kiyohara static int init_csr(struct btuart_softc *);
    110  1.1  kiyohara static int init_swave(struct btuart_softc *);
    111  1.1  kiyohara static int init_st(struct btuart_softc *);
    112  1.1  kiyohara static int firmload_stlc2500(struct btuart_softc *, int, char *);
    113  1.1  kiyohara static int init_stlc2500(struct btuart_softc *);
    114  1.1  kiyohara static int init_bcm2035(struct btuart_softc *);
    115  1.1  kiyohara static int bth4init(struct btuart_softc *);
    116  1.1  kiyohara static void bth4init_input(struct hci_unit *, struct mbuf *);
    117  1.1  kiyohara 
    118  1.1  kiyohara static int bth4open(dev_t, struct tty *);
    119  1.1  kiyohara static int bth4close(struct tty *, int);
    120  1.2  christos static int bth4ioctl(struct tty *, u_long, void *, int, struct lwp *);
    121  1.1  kiyohara static int bth4input(int, struct tty *);
    122  1.1  kiyohara static int bth4start(struct tty *);
    123  1.1  kiyohara 
    124  1.1  kiyohara static int bth4_enable(struct hci_unit *);
    125  1.1  kiyohara static void bth4_disable(struct hci_unit *);
    126  1.1  kiyohara static void bth4_start(struct hci_unit *);
    127  1.1  kiyohara 
    128  1.1  kiyohara /*
    129  1.1  kiyohara  * It doesn't need to be exported, as only btuartattach() uses it,
    130  1.1  kiyohara  * but there's no "official" way to make it static.
    131  1.1  kiyohara  */
    132  1.1  kiyohara CFATTACH_DECL(btuart, sizeof(struct btuart_softc),
    133  1.1  kiyohara     btuart_match, btuart_attach, btuart_detach, NULL);
    134  1.1  kiyohara 
    135  1.1  kiyohara static struct linesw bth4_disc = {
    136  1.1  kiyohara 	.l_name = "btuart",
    137  1.1  kiyohara 	.l_open = bth4open,
    138  1.1  kiyohara 	.l_close = bth4close,
    139  1.1  kiyohara 	.l_read = ttyerrio,
    140  1.1  kiyohara 	.l_write = ttyerrio,
    141  1.1  kiyohara 	.l_ioctl = bth4ioctl,
    142  1.1  kiyohara 	.l_rint = bth4input,
    143  1.1  kiyohara 	.l_start = bth4start,
    144  1.1  kiyohara 	.l_modem = ttymodem,
    145  1.1  kiyohara 	.l_poll = ttyerrpoll
    146  1.1  kiyohara };
    147  1.1  kiyohara 
    148  1.1  kiyohara static struct bth4hci bth4hci[] = {
    149  1.1  kiyohara 	{ BTUART_HCITYPE_ANY,		     B0, FLOW_CTL, NULL },
    150  1.1  kiyohara 	{ BTUART_HCITYPE_ERICSSON,	 B57600, FLOW_CTL, init_ericsson },
    151  1.1  kiyohara 	{ BTUART_HCITYPE_DIGI,		  B9600, FLOW_CTL, init_digi },
    152  1.1  kiyohara 	{ BTUART_HCITYPE_TEXAS,		B115200, FLOW_CTL, init_texas },
    153  1.1  kiyohara 	/* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
    154  1.1  kiyohara 	{ BTUART_HCITYPE_CSR,		B115200, FLOW_CTL, init_csr },
    155  1.1  kiyohara 	/* Silicon Wave kits */
    156  1.1  kiyohara 	{ BTUART_HCITYPE_SWAVE,		B115200, FLOW_CTL, init_swave },
    157  1.1  kiyohara 	/* ST Microelectronics minikits based on STLC2410/STLC2415 */
    158  1.1  kiyohara 	{ BTUART_HCITYPE_ST,		 B57600, FLOW_CTL, init_st },
    159  1.1  kiyohara 	/* ST Microelectronics minikits based on STLC2500 */
    160  1.1  kiyohara 	{ BTUART_HCITYPE_STLC2500,	B115200, FLOW_CTL, init_stlc2500 },
    161  1.1  kiyohara 	/* AmbiCom BT2000C Bluetooth PC/CF Card */
    162  1.1  kiyohara 	{ BTUART_HCITYPE_BT2000C,	 B57600, FLOW_CTL, init_csr },
    163  1.1  kiyohara 	/* Broadcom BCM2035 */
    164  1.1  kiyohara 	{ BTUART_HCITYPE_BCM2035,	B115200,        0, init_bcm2035 },
    165  1.1  kiyohara 
    166  1.1  kiyohara 	{ -1,				     B0,        0, NULL }
    167  1.1  kiyohara };
    168  1.1  kiyohara 
    169  1.1  kiyohara 
    170  1.1  kiyohara /* ARGSUSED */
    171  1.1  kiyohara void
    172  1.1  kiyohara btuartattach(int num __unused)
    173  1.1  kiyohara {
    174  1.1  kiyohara 	int error;
    175  1.1  kiyohara 
    176  1.1  kiyohara 	error = ttyldisc_attach(&bth4_disc);
    177  1.1  kiyohara 	if (error) {
    178  1.1  kiyohara 		aprint_error("%s: unable to register line discipline, "
    179  1.1  kiyohara 		    "error = %d\n", btuart_cd.cd_name, error);
    180  1.1  kiyohara 		return;
    181  1.1  kiyohara 	}
    182  1.1  kiyohara 
    183  1.1  kiyohara 	error = config_cfattach_attach(btuart_cd.cd_name, &btuart_ca);
    184  1.1  kiyohara 	if (error) {
    185  1.1  kiyohara 		aprint_error("%s: unable to register cfattach, error = %d\n",
    186  1.1  kiyohara 		    btuart_cd.cd_name, error);
    187  1.1  kiyohara 		config_cfdriver_detach(&btuart_cd);
    188  1.1  kiyohara 		(void) ttyldisc_detach(&bth4_disc);
    189  1.1  kiyohara 	}
    190  1.1  kiyohara }
    191  1.1  kiyohara 
    192  1.1  kiyohara /*
    193  1.1  kiyohara  * Autoconf match routine.
    194  1.1  kiyohara  *
    195  1.1  kiyohara  * XXX: unused: config_attach_pseudo(9) does not call ca_match.
    196  1.1  kiyohara  */
    197  1.1  kiyohara /* ARGSUSED */
    198  1.1  kiyohara static int
    199  1.1  kiyohara btuart_match(struct device *self __unused,
    200  1.1  kiyohara     struct cfdata *cfdata __unused, void *arg __unused)
    201  1.1  kiyohara {
    202  1.1  kiyohara 
    203  1.1  kiyohara 	/* pseudo-device; always present */
    204  1.1  kiyohara 	return 1;
    205  1.1  kiyohara }
    206  1.1  kiyohara 
    207  1.1  kiyohara /*
    208  1.1  kiyohara  * Autoconf attach routine.  Called by config_attach_pseudo(9) when we
    209  1.1  kiyohara  * open the line discipline.
    210  1.1  kiyohara  */
    211  1.1  kiyohara /* ARGSUSED */
    212  1.1  kiyohara static void
    213  1.1  kiyohara btuart_attach(struct device *parent __unused,
    214  1.1  kiyohara     struct device *self, void *aux __unused)
    215  1.1  kiyohara {
    216  1.1  kiyohara 	struct btuart_softc *sc = device_private(self);
    217  1.1  kiyohara 	int i;
    218  1.1  kiyohara 
    219  1.1  kiyohara 	aprint_normal("\n");
    220  1.1  kiyohara 	aprint_naive("\n");
    221  1.1  kiyohara 
    222  1.1  kiyohara 	sc->sc_input_acl = bth4init_input;
    223  1.1  kiyohara 	sc->sc_input_sco = bth4init_input;
    224  1.1  kiyohara 	sc->sc_input_event = bth4init_input;
    225  1.1  kiyohara 
    226  1.1  kiyohara 	/* Copy default type */
    227  1.1  kiyohara 	for (i = 0; bth4hci[i].type != BTUART_HCITYPE_ANY; i++);
    228  1.1  kiyohara 	memcpy(&sc->sc_bth4hci, &bth4hci[i], sizeof(struct bth4hci));
    229  1.1  kiyohara 
    230  1.1  kiyohara 	/* Attach Bluetooth unit */
    231  1.1  kiyohara 	sc->sc_unit.hci_softc = sc;
    232  1.1  kiyohara 	sc->sc_unit.hci_devname = sc->sc_dev.dv_xname;
    233  1.1  kiyohara 	sc->sc_unit.hci_enable = bth4_enable;
    234  1.1  kiyohara 	sc->sc_unit.hci_disable = bth4_disable;
    235  1.1  kiyohara 	sc->sc_unit.hci_start_cmd = bth4_start;
    236  1.1  kiyohara 	sc->sc_unit.hci_start_acl = bth4_start;
    237  1.1  kiyohara 	sc->sc_unit.hci_start_sco = bth4_start;
    238  1.1  kiyohara 	sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
    239  1.1  kiyohara 	hci_attach(&sc->sc_unit);
    240  1.1  kiyohara }
    241  1.1  kiyohara 
    242  1.1  kiyohara /*
    243  1.1  kiyohara  * Autoconf detach routine.  Called when we close the line discipline.
    244  1.1  kiyohara  */
    245  1.1  kiyohara static int
    246  1.1  kiyohara btuart_detach(struct device *self, int flags __unused)
    247  1.1  kiyohara {
    248  1.1  kiyohara 	struct btuart_softc *sc = device_private(self);
    249  1.1  kiyohara 
    250  1.1  kiyohara 	hci_detach(&sc->sc_unit);
    251  1.1  kiyohara 
    252  1.1  kiyohara 	return 0;
    253  1.1  kiyohara }
    254  1.1  kiyohara 
    255  1.1  kiyohara 
    256  1.1  kiyohara static int
    257  1.1  kiyohara bth4_waitresp(struct btuart_softc *sc, struct mbuf **mp, uint16_t opcode)
    258  1.1  kiyohara {
    259  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    260  1.1  kiyohara 	hci_event_hdr_t *e;
    261  1.1  kiyohara 	int status = 0, rv;
    262  1.1  kiyohara 
    263  1.1  kiyohara 	*mp = NULL;
    264  1.1  kiyohara 	while (1 /* CONSTCOND */) {
    265  1.1  kiyohara 		if ((rv =
    266  1.1  kiyohara 		    tsleep(&unit->hci_eventq, PCATCH, "bth4init", 0)) != 0)
    267  1.1  kiyohara 			return rv;
    268  1.1  kiyohara 
    269  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_eventq, *mp);
    270  1.1  kiyohara 		unit->hci_eventqlen--;
    271  1.1  kiyohara 		KASSERT(*mp != NULL);
    272  1.1  kiyohara 
    273  1.1  kiyohara 		e = mtod(*mp, hci_event_hdr_t *);
    274  1.1  kiyohara 		if (e->event == HCI_EVENT_COMMAND_COMPL) {
    275  1.1  kiyohara 			hci_command_compl_ep *ep;
    276  1.1  kiyohara 
    277  1.1  kiyohara 			ep = (hci_command_compl_ep *)(e + 1);
    278  1.1  kiyohara 			if (ep->opcode == opcode) {
    279  1.1  kiyohara 				status = *(char *)(ep + 1);
    280  1.1  kiyohara 				break;
    281  1.1  kiyohara 			}
    282  1.1  kiyohara 		} else if (e->event == HCI_EVENT_COMMAND_STATUS) {
    283  1.1  kiyohara 			hci_command_status_ep *ep;
    284  1.1  kiyohara 
    285  1.1  kiyohara 			ep = (hci_command_status_ep *)(e + 1);
    286  1.1  kiyohara 			if (ep->opcode == opcode) {
    287  1.1  kiyohara 				status = ep->status;
    288  1.1  kiyohara 				break;
    289  1.1  kiyohara 			}
    290  1.1  kiyohara 		} else if (e->event == HCI_EVENT_VENDOR)
    291  1.1  kiyohara 				break;
    292  1.1  kiyohara 	}
    293  1.1  kiyohara 
    294  1.1  kiyohara 	return status;
    295  1.1  kiyohara }
    296  1.1  kiyohara 
    297  1.1  kiyohara static int
    298  1.1  kiyohara bth4_firmload(struct btuart_softc *sc, char *filename,
    299  1.1  kiyohara 	      int (*func_firmload)(struct btuart_softc *, int, char *))
    300  1.1  kiyohara {
    301  1.1  kiyohara 	const cfdriver_t cd = device_cfdriver(&sc->sc_dev);
    302  1.1  kiyohara 	firmware_handle_t fh = NULL;
    303  1.1  kiyohara 	int error, size;
    304  1.1  kiyohara 	char *buf;
    305  1.1  kiyohara 
    306  1.1  kiyohara 	if ((error = firmware_open(cd->cd_name, filename, &fh)) != 0) {
    307  1.1  kiyohara 		printf("firmware_open failed\n");
    308  1.1  kiyohara 		return error;
    309  1.1  kiyohara 	}
    310  1.1  kiyohara 	size = firmware_get_size(fh);
    311  1.1  kiyohara 	if ((buf = firmware_malloc(size)) != NULL) {
    312  1.1  kiyohara 		printf("firmware_malloc failed\n");
    313  1.1  kiyohara 		firmware_close(fh);
    314  1.1  kiyohara 		return ENOMEM;
    315  1.1  kiyohara 	}
    316  1.1  kiyohara 
    317  1.1  kiyohara 	if ((error = firmware_read(fh, 0, buf, size)) != 0)
    318  1.1  kiyohara 		printf("firmware_read failed\n");
    319  1.1  kiyohara 	if (error == 0)
    320  1.1  kiyohara 		error = (*func_firmload)(sc, size, buf);
    321  1.1  kiyohara 
    322  1.1  kiyohara 	firmware_close(fh);
    323  1.1  kiyohara 	firmware_free(buf, size);
    324  1.1  kiyohara 
    325  1.1  kiyohara 	return error;
    326  1.1  kiyohara }
    327  1.1  kiyohara 
    328  1.1  kiyohara /*
    329  1.1  kiyohara  * LSI initialize functions.
    330  1.1  kiyohara  */
    331  1.1  kiyohara static int
    332  1.1  kiyohara init_ericsson(struct btuart_softc *sc)
    333  1.1  kiyohara {
    334  1.1  kiyohara 	struct mbuf *m;
    335  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    336  1.1  kiyohara 	hci_cmd_hdr_t *p;
    337  1.1  kiyohara 	int i, error = 0;
    338  1.1  kiyohara 	const uint16_t opcode = htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
    339  1.1  kiyohara 	static struct {
    340  1.1  kiyohara 		int baud;
    341  1.1  kiyohara 		uint8_t param;
    342  1.1  kiyohara 	} ericsson_baudtbl[] = {
    343  1.1  kiyohara 		{ B460800, 0x00 },
    344  1.1  kiyohara 		{ B230400, 0x01 },
    345  1.1  kiyohara 		{ B115200, 0x02 },
    346  1.1  kiyohara 		{  B57600, 0x03 },
    347  1.1  kiyohara 		{  B28800, 0x04 },
    348  1.1  kiyohara 		{  B14400, 0x05 },
    349  1.1  kiyohara 		{   B7200, 0x06 },
    350  1.1  kiyohara #if defined(B3600)
    351  1.1  kiyohara 		{   B3600, 0x07 },
    352  1.1  kiyohara #endif
    353  1.1  kiyohara 		{   B1800, 0x08 },
    354  1.1  kiyohara #if defined(B900)
    355  1.1  kiyohara 		{    B900, 0x09 },
    356  1.1  kiyohara #endif
    357  1.1  kiyohara #if defined(B153600)
    358  1.1  kiyohara 		{ B153600, 0x10 },
    359  1.1  kiyohara #endif
    360  1.1  kiyohara 		{  B76800, 0x11 },
    361  1.1  kiyohara 		{  B38400, 0x12 },
    362  1.1  kiyohara 		{  B19200, 0x13 },
    363  1.1  kiyohara 		{   B9600, 0x14 },
    364  1.1  kiyohara 		{   B4800, 0x15 },
    365  1.1  kiyohara 		{   B2400, 0x16 },
    366  1.1  kiyohara 		{   B1200, 0x17 },
    367  1.1  kiyohara 		{    B600, 0x18 },
    368  1.1  kiyohara 		{    B300, 0x19 },
    369  1.1  kiyohara 		{ B921600, 0x20 },
    370  1.1  kiyohara 		{      B0, 0xff }
    371  1.1  kiyohara 	};
    372  1.1  kiyohara 
    373  1.1  kiyohara printf("sc_baud=%d, init_speed=%d\n", sc->sc_baud, sc->sc_bth4hci.init_baud);
    374  1.1  kiyohara 	for (i = 0; ericsson_baudtbl[i].baud != sc->sc_baud; i++)
    375  1.1  kiyohara 		if (ericsson_baudtbl[i].baud == B0)
    376  1.1  kiyohara 			return EINVAL;
    377  1.1  kiyohara 
    378  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    379  1.1  kiyohara 	if (m == NULL)
    380  1.1  kiyohara 		return ENOMEM;
    381  1.1  kiyohara 
    382  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    383  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    384  1.1  kiyohara 	p->opcode = opcode;
    385  1.1  kiyohara 	p->length = sizeof(ericsson_baudtbl[0].param);
    386  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    387  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length,
    388  1.1  kiyohara 	    &ericsson_baudtbl[i].param);
    389  1.1  kiyohara 
    390  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    391  1.1  kiyohara 	bth4_start(unit);
    392  1.1  kiyohara 
    393  1.1  kiyohara #if 0
    394  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    395  1.1  kiyohara 	if (m != NULL) {
    396  1.1  kiyohara 		if (error != 0) {
    397  1.1  kiyohara 			printf("%s: Ericsson_Set_UART_Baud_Rate failed:"
    398  1.1  kiyohara 			    " Status 0x%02x\n", sc->sc_dev.dv_xname, error);
    399  1.1  kiyohara 			error = EFAULT;
    400  1.1  kiyohara 		}
    401  1.1  kiyohara 		m_freem(m);
    402  1.1  kiyohara 	}
    403  1.1  kiyohara #else
    404  1.1  kiyohara 	/*
    405  1.1  kiyohara 	 * XXXX: We cannot correctly receive this response perhaps.  Wait
    406  1.1  kiyohara 	 * until the transmission of the data of 5 bytes * 10 bit is completed.
    407  1.1  kiyohara 	 *   1000000usec * 10bit * 5byte / baud
    408  1.1  kiyohara 	 */
    409  1.1  kiyohara 	delay(50000000 / sc->sc_bth4hci.init_baud);
    410  1.1  kiyohara #endif
    411  1.1  kiyohara 	return error;
    412  1.1  kiyohara }
    413  1.1  kiyohara 
    414  1.1  kiyohara static int
    415  1.1  kiyohara init_digi(struct btuart_softc *sc)
    416  1.1  kiyohara {
    417  1.1  kiyohara 	struct mbuf *m;
    418  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    419  1.1  kiyohara 	hci_cmd_hdr_t *p;
    420  1.1  kiyohara 	uint8_t param;
    421  1.1  kiyohara 
    422  1.1  kiyohara 	/* XXXX */
    423  1.1  kiyohara 	switch (sc->sc_baud) {
    424  1.1  kiyohara 	case B57600:
    425  1.1  kiyohara 		param = 0x08;
    426  1.1  kiyohara 		break;
    427  1.1  kiyohara 
    428  1.1  kiyohara 	case B115200:
    429  1.1  kiyohara 		param = 0x09;
    430  1.1  kiyohara 		break;
    431  1.1  kiyohara 
    432  1.1  kiyohara 	default:
    433  1.1  kiyohara 		return EINVAL;
    434  1.1  kiyohara 	}
    435  1.1  kiyohara 
    436  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    437  1.1  kiyohara 	if (m == NULL)
    438  1.1  kiyohara 		return ENOMEM;
    439  1.1  kiyohara 
    440  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    441  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    442  1.1  kiyohara #define HCI_CMD_DIGIANSWER_SET_UART_BAUD_RATE	0xfc07		/* XXXX */
    443  1.1  kiyohara 	p->opcode = htole16(HCI_CMD_DIGIANSWER_SET_UART_BAUD_RATE);
    444  1.1  kiyohara 	p->length = sizeof(param);
    445  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    446  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &param);
    447  1.1  kiyohara 
    448  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    449  1.1  kiyohara 	bth4_start(unit);
    450  1.1  kiyohara 
    451  1.1  kiyohara 	/*
    452  1.1  kiyohara 	 * XXXX
    453  1.1  kiyohara 	 * Wait until the transmission of the data of 5 bytes * 10 bit is
    454  1.1  kiyohara 	 * completed.
    455  1.1  kiyohara 	 *     1000000usec * 10bit * 5byte / baud
    456  1.1  kiyohara 	 */
    457  1.1  kiyohara 	delay(50000000 / sc->sc_bth4hci.init_baud);
    458  1.1  kiyohara 	return 0;
    459  1.1  kiyohara }
    460  1.1  kiyohara 
    461  1.1  kiyohara static int
    462  1.1  kiyohara init_texas(struct btuart_softc *sc)
    463  1.1  kiyohara {
    464  1.1  kiyohara 
    465  1.1  kiyohara 	/* XXXX: Should we obtain the version of LMP? */
    466  1.1  kiyohara 	return 0;
    467  1.1  kiyohara }
    468  1.1  kiyohara 
    469  1.1  kiyohara static int
    470  1.1  kiyohara init_csr(struct btuart_softc *sc)
    471  1.1  kiyohara {
    472  1.1  kiyohara 	struct mbuf *m;
    473  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    474  1.1  kiyohara 	hci_cmd_hdr_t *p;
    475  1.1  kiyohara 	int error;
    476  1.1  kiyohara 	const uint16_t opcode = htole16(HCI_CMD_CSR_EXTN);
    477  1.1  kiyohara 	struct {
    478  1.1  kiyohara 		uint8_t last :1;
    479  1.1  kiyohara 		uint8_t first :1;
    480  1.1  kiyohara #define CSR_BCCMD_CHANID_BCCMD		2
    481  1.1  kiyohara #define CSR_BCCMD_CHANID_HQ		3
    482  1.1  kiyohara #define CSR_BCCMD_CHANID_DEVMGRLIB	4
    483  1.1  kiyohara #define CSR_BCCMD_CHANID_L2CAPLIB	8
    484  1.1  kiyohara #define CSR_BCCMD_CHANID_RFCOMMLIB	9
    485  1.1  kiyohara #define CSR_BCCMD_CHANID_SDPLIB		10
    486  1.1  kiyohara #define CSR_BCCMD_CHANID_DFU		12
    487  1.1  kiyohara #define CSR_BCCMD_CHANID_VM		13
    488  1.1  kiyohara #define CSR_BCCMD_CHANID_LMDEBUG	20
    489  1.1  kiyohara 		uint8_t chanid :6;
    490  1.1  kiyohara 
    491  1.1  kiyohara 		struct {
    492  1.1  kiyohara #define CSR_BCCMD_MESSAGE_TYPE_GETREQ	0x0000
    493  1.1  kiyohara #define CSR_BCCMD_MESSAGE_TYPE_GETRESP	0x0001
    494  1.1  kiyohara #define CSR_BCCMD_MESSAGE_TYPE_SETREQ	0x0002
    495  1.1  kiyohara 			uint16_t type;
    496  1.1  kiyohara 			uint16_t length;
    497  1.1  kiyohara 			uint16_t seqno;
    498  1.1  kiyohara #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART		0x6802
    499  1.1  kiyohara #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB		0x2000
    500  1.1  kiyohara #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB		0x4000
    501  1.1  kiyohara #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD		0x8000
    502  1.1  kiyohara 			uint16_t varid;
    503  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_OK			0x0000
    504  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID		0x0001
    505  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG		0x0002
    506  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE		0x0003
    507  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ		0x0004
    508  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS		0x0005
    509  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY		0x0006
    510  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY		0x0007
    511  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_ERROR			0x0008
    512  1.1  kiyohara #define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED	0x0009
    513  1.1  kiyohara 			uint16_t status;
    514  1.1  kiyohara 			uint16_t payload[4];
    515  1.1  kiyohara 		} message;
    516  1.1  kiyohara 	} bccmd;
    517  1.1  kiyohara 
    518  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    519  1.1  kiyohara 	if (m == NULL)
    520  1.1  kiyohara 		return ENOMEM;
    521  1.1  kiyohara 
    522  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    523  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    524  1.1  kiyohara 	p->opcode = opcode;
    525  1.1  kiyohara 	p->length = sizeof(bccmd);
    526  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    527  1.1  kiyohara 
    528  1.1  kiyohara 	/* setup BCSP command packet */
    529  1.1  kiyohara 	bccmd.last = 1;
    530  1.1  kiyohara 	bccmd.first = 1;
    531  1.1  kiyohara 	bccmd.chanid = CSR_BCCMD_CHANID_BCCMD;
    532  1.1  kiyohara 	bccmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ);
    533  1.1  kiyohara 	bccmd.message.length = htole16(sizeof(bccmd.message) >> 1);
    534  1.1  kiyohara 	bccmd.message.seqno = htole16(0);
    535  1.1  kiyohara 	bccmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART);
    536  1.1  kiyohara 	bccmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK);
    537  1.1  kiyohara 	memset(bccmd.message.payload, 0, sizeof(bccmd.message.payload));
    538  1.1  kiyohara 
    539  1.1  kiyohara 	/* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */
    540  1.1  kiyohara 	bccmd.message.payload[0] = htole16((sc->sc_baud * 64 + 7812) / 15625);
    541  1.1  kiyohara 
    542  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &bccmd);
    543  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    544  1.1  kiyohara 	bth4_start(unit);
    545  1.1  kiyohara 
    546  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    547  1.1  kiyohara 	if (m != NULL) {
    548  1.1  kiyohara 		/*
    549  1.1  kiyohara 		 * XXXX:
    550  1.1  kiyohara 		 * We will have to check the HCI_EVENT_VENDOR packet. For
    551  1.1  kiyohara 		 * instance, it might be a different HCI_EVENT_VENDOR packet.
    552  1.1  kiyohara 		 */
    553  1.1  kiyohara 		if (error != 0) {
    554  1.1  kiyohara 			printf("%s: CSR set UART speed failed: Status 0x%02x\n",
    555  1.1  kiyohara 			    sc->sc_dev.dv_xname, error);
    556  1.1  kiyohara 			error = EFAULT;
    557  1.1  kiyohara 		}
    558  1.1  kiyohara 		m_freem(m);
    559  1.1  kiyohara 	}
    560  1.1  kiyohara 
    561  1.1  kiyohara 	return error;
    562  1.1  kiyohara }
    563  1.1  kiyohara 
    564  1.1  kiyohara static int
    565  1.1  kiyohara init_swave(struct btuart_softc *sc)
    566  1.1  kiyohara {
    567  1.1  kiyohara 	struct mbuf *m;
    568  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    569  1.1  kiyohara 	hci_cmd_hdr_t *p;
    570  1.1  kiyohara 	hci_event_hdr_t *e;
    571  1.1  kiyohara 	int i, error;
    572  1.1  kiyohara #define HCI_CMD_SWAVE_SET_UART_BAUD_RATE	0xfc0b		/* XXXX */
    573  1.1  kiyohara 	const uint16_t opcode = htole16(HCI_CMD_SWAVE_SET_UART_BAUD_RATE);
    574  1.1  kiyohara 	char param[6], *resp;
    575  1.1  kiyohara 	static struct {			/* XXXX */
    576  1.1  kiyohara 		int baud;
    577  1.1  kiyohara 		uint8_t param;
    578  1.1  kiyohara 	} swave_baudtbl[] = {
    579  1.1  kiyohara 		{  B19200, 0x03 },
    580  1.1  kiyohara 		{  B38400, 0x02 },
    581  1.1  kiyohara 		{  B57600, 0x01 },
    582  1.1  kiyohara 		{ B115200, 0x00 },
    583  1.1  kiyohara 		{      B0, 0xff }
    584  1.1  kiyohara 	};
    585  1.1  kiyohara 
    586  1.1  kiyohara 	for (i = 0; swave_baudtbl[i].baud != sc->sc_baud; i++)
    587  1.1  kiyohara 		if (swave_baudtbl[i].baud == B0)
    588  1.1  kiyohara 			return EINVAL;
    589  1.1  kiyohara 
    590  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    591  1.1  kiyohara 	if (m == NULL)
    592  1.1  kiyohara 		return ENOMEM;
    593  1.1  kiyohara 
    594  1.1  kiyohara 	/* first send 'param access set' command. */
    595  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    596  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    597  1.1  kiyohara 	p->opcode = opcode;
    598  1.1  kiyohara 	p->length = sizeof(param);
    599  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    600  1.1  kiyohara 
    601  1.1  kiyohara 	/* XXXX */
    602  1.1  kiyohara 	param[0] = 0x01;		/* param sub command */
    603  1.1  kiyohara 	param[1] = 0x11;		/* HCI Tranport Params */
    604  1.1  kiyohara 	param[2] = 0x03;		/* length of the parameter following */
    605  1.1  kiyohara 	param[3] = 0x01;		/* HCI Transport flow control enable */
    606  1.1  kiyohara 	param[4] = 0x01;		/* HCI Transport Type = UART */
    607  1.1  kiyohara 	param[5] = swave_baudtbl[i].param;
    608  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &param);
    609  1.1  kiyohara 
    610  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    611  1.1  kiyohara 	bth4_start(unit);
    612  1.1  kiyohara 
    613  1.1  kiyohara 	while(1 /* CONSTCOND */) {
    614  1.1  kiyohara 		error = bth4_waitresp(sc, &m, opcode);
    615  1.1  kiyohara 		if (error != 0) {
    616  1.1  kiyohara 			if (m != NULL)
    617  1.1  kiyohara 				m_freem(m);
    618  1.1  kiyohara 			printf("%s: swave set baud rate command failed:"
    619  1.1  kiyohara 			    " error 0x%02x\n", sc->sc_dev.dv_xname, error);
    620  1.1  kiyohara 			return error;
    621  1.1  kiyohara 		}
    622  1.1  kiyohara 		if (m != NULL) {
    623  1.1  kiyohara 			e = mtod(m, hci_event_hdr_t *);
    624  1.1  kiyohara 			resp = (char *)(e + 1);
    625  1.1  kiyohara 			if (e->length == 7 && *resp == 0xb &&
    626  1.1  kiyohara 			    memcmp(resp + 1, param, sizeof(param)) == 0)
    627  1.1  kiyohara 				break;
    628  1.1  kiyohara 			m_freem(m);
    629  1.1  kiyohara 		}
    630  1.1  kiyohara 	}
    631  1.1  kiyohara 
    632  1.1  kiyohara 	/* send 'reset' command consecutively. */
    633  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    634  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    635  1.1  kiyohara 	p->opcode = htole16(HCI_CMD_RESET);
    636  1.1  kiyohara 	p->length = 0;
    637  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    638  1.1  kiyohara 
    639  1.1  kiyohara 	/*
    640  1.1  kiyohara 	 * XXXX
    641  1.1  kiyohara 	 * Wait until the transmission of the data of 4 bytes * 10 bit is
    642  1.1  kiyohara 	 * completed.
    643  1.1  kiyohara 	 *     1000000usec * 10bit * 4byte / baud
    644  1.1  kiyohara 	 */
    645  1.1  kiyohara 	delay(40000000 / sc->sc_bth4hci.init_baud);
    646  1.1  kiyohara 	return 0;
    647  1.1  kiyohara }
    648  1.1  kiyohara 
    649  1.1  kiyohara static int
    650  1.1  kiyohara init_st(struct btuart_softc *sc)
    651  1.1  kiyohara {
    652  1.1  kiyohara 	struct mbuf *m;
    653  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    654  1.1  kiyohara 	hci_cmd_hdr_t *p;
    655  1.1  kiyohara 	int i;
    656  1.1  kiyohara 	static struct {			/* XXXX */
    657  1.1  kiyohara 		int baud;
    658  1.1  kiyohara 		uint8_t param;
    659  1.1  kiyohara 	} st_baudtbl[] = {
    660  1.1  kiyohara 		{   B9600, 0x09 },
    661  1.1  kiyohara 		{  B19200, 0x0b },
    662  1.1  kiyohara 		{  B38400, 0x0d },
    663  1.1  kiyohara 		{  B57600, 0x0e },
    664  1.1  kiyohara 		{ B115200, 0x10 },
    665  1.1  kiyohara 		{ B230400, 0x12 },
    666  1.1  kiyohara 		{ B460800, 0x13 },
    667  1.1  kiyohara 		{ B921600, 0x14 },
    668  1.1  kiyohara 		{      B0, 0xff }
    669  1.1  kiyohara 	};
    670  1.1  kiyohara 
    671  1.1  kiyohara 	for (i = 0; st_baudtbl[i].baud != sc->sc_baud; i++)
    672  1.1  kiyohara 		if (st_baudtbl[i].baud == B0)
    673  1.1  kiyohara 			return EINVAL;
    674  1.1  kiyohara 
    675  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    676  1.1  kiyohara 	if (m == NULL)
    677  1.1  kiyohara 		return ENOMEM;
    678  1.1  kiyohara 
    679  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    680  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    681  1.1  kiyohara #define HCI_CMD_ST_SET_UART_BAUD_RATE	0xfc46	/* XXXX */
    682  1.1  kiyohara 	p->opcode = htole16(HCI_CMD_ST_SET_UART_BAUD_RATE);
    683  1.1  kiyohara 	p->length = sizeof(st_baudtbl[0].param);
    684  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    685  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &st_baudtbl[i].param);
    686  1.1  kiyohara 
    687  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    688  1.1  kiyohara 	bth4_start(unit);
    689  1.1  kiyohara 
    690  1.1  kiyohara 	/*
    691  1.1  kiyohara 	 * XXXX
    692  1.1  kiyohara 	 * Wait until the transmission of the data of 5 bytes * 10 bit is
    693  1.1  kiyohara 	 * completed.
    694  1.1  kiyohara 	 *     1000000usec * 10bit * 5byte / baud
    695  1.1  kiyohara 	 */
    696  1.1  kiyohara 	delay(50000000 / sc->sc_bth4hci.init_baud);
    697  1.1  kiyohara 	return 0;
    698  1.1  kiyohara }
    699  1.1  kiyohara 
    700  1.1  kiyohara static int
    701  1.1  kiyohara firmload_stlc2500(struct btuart_softc *sc, int size, char *buf)
    702  1.1  kiyohara {
    703  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    704  1.1  kiyohara 	struct mbuf *m;
    705  1.1  kiyohara 	hci_cmd_hdr_t *p;
    706  1.1  kiyohara 	int error, offset, n;
    707  1.1  kiyohara 	uint16_t opcode = htole16(0xfc2e);	/* XXXX */
    708  1.1  kiyohara 	uint8_t seq;
    709  1.1  kiyohara 
    710  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    711  1.1  kiyohara 	if (m == NULL)
    712  1.1  kiyohara 		return ENOMEM;
    713  1.1  kiyohara 	seq = 0;
    714  1.1  kiyohara 	offset = 0;
    715  1.1  kiyohara 	error = 0;
    716  1.1  kiyohara 	while (offset < size) {
    717  1.1  kiyohara 		n = size - offset < 254 ? size - offset : 254;
    718  1.1  kiyohara 
    719  1.1  kiyohara 		p = mtod(m, hci_cmd_hdr_t *);
    720  1.1  kiyohara 		p->type = HCI_CMD_PKT;
    721  1.1  kiyohara 		p->opcode = opcode;
    722  1.1  kiyohara 		p->length = n;
    723  1.1  kiyohara 		m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    724  1.1  kiyohara 		*(char *)(p + 1) = seq;
    725  1.1  kiyohara 		m_copyback(m,
    726  1.1  kiyohara 		    sizeof(hci_cmd_hdr_t) + 1, p->length, buf + offset);
    727  1.1  kiyohara 
    728  1.1  kiyohara 		MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    729  1.1  kiyohara 		bth4_start(unit);
    730  1.1  kiyohara 
    731  1.1  kiyohara 		error = bth4_waitresp(sc, &m, opcode);
    732  1.1  kiyohara 		if (m != NULL) {
    733  1.1  kiyohara 			if (error != 0) {
    734  1.1  kiyohara 				printf("%s: stlc2500 firmware load failed:"
    735  1.1  kiyohara 				    " Status 0x%02x\n",
    736  1.1  kiyohara 				    sc->sc_dev.dv_xname, error);
    737  1.1  kiyohara 				error = EFAULT;
    738  1.1  kiyohara 				break;
    739  1.1  kiyohara 			}
    740  1.1  kiyohara 		}
    741  1.1  kiyohara 
    742  1.1  kiyohara 		seq++;
    743  1.1  kiyohara 		offset += n;
    744  1.1  kiyohara 	}
    745  1.1  kiyohara 	m_freem(m);
    746  1.1  kiyohara 
    747  1.1  kiyohara 	return error;
    748  1.1  kiyohara }
    749  1.1  kiyohara 
    750  1.1  kiyohara static int
    751  1.1  kiyohara init_stlc2500(struct btuart_softc *sc)
    752  1.1  kiyohara {
    753  1.1  kiyohara 	struct mbuf *m;
    754  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    755  1.1  kiyohara 	hci_cmd_hdr_t *p;
    756  1.1  kiyohara 	hci_event_hdr_t *e;
    757  1.1  kiyohara 	hci_read_local_ver_rp *lv;
    758  1.1  kiyohara 	int error, revision, i;
    759  1.1  kiyohara 	uint16_t opcode;
    760  1.1  kiyohara 	char filename[NAME_MAX], param[8];
    761  1.1  kiyohara 	static const char filenametmpl[] = "STLC2500_R%d_%02d%s";
    762  1.1  kiyohara 	const char *suffix[] = { ".ptc", ".ssf", NULL };
    763  1.1  kiyohara 
    764  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    765  1.1  kiyohara 	if (m == NULL)
    766  1.1  kiyohara 		return ENOMEM;
    767  1.1  kiyohara 
    768  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    769  1.1  kiyohara 	opcode = htole16(HCI_CMD_READ_LOCAL_VER);
    770  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    771  1.1  kiyohara 	p->opcode = opcode;
    772  1.1  kiyohara 	p->length = 0;
    773  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    774  1.1  kiyohara 
    775  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    776  1.1  kiyohara 	bth4_start(unit);
    777  1.1  kiyohara 
    778  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    779  1.1  kiyohara 	if (m != NULL) {
    780  1.1  kiyohara 		if (error != 0) {
    781  1.1  kiyohara 			printf("%s: HCI_Read_Local_Version_Information failed:"
    782  1.1  kiyohara 			    " Status 0x%02x\n", sc->sc_dev.dv_xname, error);
    783  1.1  kiyohara 			error = EFAULT;
    784  1.1  kiyohara 			m_freem(m);
    785  1.1  kiyohara 		}
    786  1.1  kiyohara 	}
    787  1.1  kiyohara 	if (error != 0)
    788  1.1  kiyohara 		return error;
    789  1.1  kiyohara 
    790  1.1  kiyohara 	e = mtod(m, hci_event_hdr_t *);
    791  1.1  kiyohara 	lv = (hci_read_local_ver_rp *)(e + 1);
    792  1.1  kiyohara 	revision = le16toh(lv->hci_revision);
    793  1.1  kiyohara 	opcode = htole16(HCI_CMD_RESET);
    794  1.1  kiyohara 	for (i = 0; suffix[i] != NULL; i++) {
    795  1.1  kiyohara 		/* send firmware */
    796  1.1  kiyohara 		snprintf(filename, sizeof(filename), filenametmpl,
    797  1.1  kiyohara 		    (uint8_t)(revision >> 8), (uint8_t)revision, suffix[i]);
    798  1.1  kiyohara 		bth4_firmload(sc, filename, firmload_stlc2500);
    799  1.1  kiyohara 
    800  1.1  kiyohara 		p = mtod(m, hci_cmd_hdr_t *);
    801  1.1  kiyohara 		p->type = HCI_CMD_PKT;
    802  1.1  kiyohara 		p->opcode = opcode;
    803  1.1  kiyohara 		p->length = 0;
    804  1.1  kiyohara 		m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    805  1.1  kiyohara 
    806  1.1  kiyohara 		MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    807  1.1  kiyohara 		bth4_start(unit);
    808  1.1  kiyohara 
    809  1.1  kiyohara 		error = bth4_waitresp(sc, &m, opcode);
    810  1.1  kiyohara 		if (m != NULL) {
    811  1.1  kiyohara 			if (error != 0) {
    812  1.1  kiyohara 				printf("%s: HCI_Reset (%d) failed:"
    813  1.1  kiyohara 				    " Status 0x%02x\n",
    814  1.1  kiyohara 				    sc->sc_dev.dv_xname, i, error);
    815  1.1  kiyohara 				error = EFAULT;
    816  1.1  kiyohara 				m_freem(m);
    817  1.1  kiyohara 			}
    818  1.1  kiyohara 		}
    819  1.1  kiyohara 		if (error != 0)
    820  1.1  kiyohara 			return error;
    821  1.1  kiyohara 	}
    822  1.1  kiyohara 
    823  1.1  kiyohara 	/* XXXX: We will obtain the character string.  But I don't know... */
    824  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    825  1.1  kiyohara 	opcode = htole16(0xfc0f);		/* XXXXX ?? */
    826  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    827  1.1  kiyohara 	p->opcode = opcode;
    828  1.1  kiyohara 	p->length = 0;
    829  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    830  1.1  kiyohara 
    831  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    832  1.1  kiyohara 	bth4_start(unit);
    833  1.1  kiyohara 
    834  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    835  1.1  kiyohara 	if (m != NULL) {
    836  1.1  kiyohara 		if (error != 0) {
    837  1.1  kiyohara 			printf("%s: failed: opcode 0xfc0f Status 0x%02x\n",
    838  1.1  kiyohara 			    sc->sc_dev.dv_xname, error);
    839  1.1  kiyohara 			error = EFAULT;
    840  1.1  kiyohara 			m_freem(m);
    841  1.1  kiyohara 		}
    842  1.1  kiyohara 	}
    843  1.1  kiyohara 	if (error != 0)
    844  1.1  kiyohara 		return error;
    845  1.1  kiyohara 	/*
    846  1.1  kiyohara 	 * XXXX:
    847  1.1  kiyohara 	 * We do not know the beginning point of this character string.
    848  1.1  kiyohara 	 * Because it doesn't know the event of this packet.
    849  1.1  kiyohara 	 *
    850  1.1  kiyohara 	 * printf("%s: %s\n", sc->sc_dev.dv_xname, ???);
    851  1.1  kiyohara 	 */
    852  1.1  kiyohara 
    853  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    854  1.1  kiyohara 	opcode = htole16(0xfc22);		/* XXXXX ?? */
    855  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    856  1.1  kiyohara 	p->opcode = opcode;
    857  1.1  kiyohara 	p->length = sizeof(param);
    858  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    859  1.1  kiyohara 
    860  1.1  kiyohara 	/* XXXX */
    861  1.1  kiyohara 	param[0] = 0xfe;
    862  1.1  kiyohara 	param[1] = 0x06;
    863  1.1  kiyohara 	param[2] = 0xba;
    864  1.1  kiyohara 	param[3] = 0xab;
    865  1.1  kiyohara 	param[4] = 0x00;
    866  1.1  kiyohara 	param[5] = 0xe1;
    867  1.1  kiyohara 	param[6] = 0x80;
    868  1.1  kiyohara 	param[7] = 0x00;
    869  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, param);
    870  1.1  kiyohara 
    871  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    872  1.1  kiyohara 	bth4_start(unit);
    873  1.1  kiyohara 
    874  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    875  1.1  kiyohara 	if (m != NULL) {
    876  1.1  kiyohara 		if (error != 0) {
    877  1.1  kiyohara 			printf("%s: failed: opcode 0xfc0f Status 0x%02x\n",
    878  1.1  kiyohara 			    sc->sc_dev.dv_xname, error);
    879  1.1  kiyohara 			error = EFAULT;
    880  1.1  kiyohara 			m_freem(m);
    881  1.1  kiyohara 		}
    882  1.1  kiyohara 	}
    883  1.1  kiyohara 	if (error != 0)
    884  1.1  kiyohara 		return error;
    885  1.1  kiyohara 
    886  1.1  kiyohara 	opcode = htole16(HCI_CMD_RESET);
    887  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    888  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    889  1.1  kiyohara 	p->opcode = opcode;
    890  1.1  kiyohara 	p->length = 0;
    891  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    892  1.1  kiyohara 
    893  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    894  1.1  kiyohara 	bth4_start(unit);
    895  1.1  kiyohara 
    896  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    897  1.1  kiyohara 	if (m != NULL) {
    898  1.1  kiyohara 		if (error != 0) {
    899  1.1  kiyohara 			printf("%s: HCI_Reset failed: Status 0x%02x\n",
    900  1.1  kiyohara 			    sc->sc_dev.dv_xname, error);
    901  1.1  kiyohara 			error = EFAULT;
    902  1.1  kiyohara 			m_freem(m);
    903  1.1  kiyohara 		}
    904  1.1  kiyohara 	}
    905  1.1  kiyohara 
    906  1.1  kiyohara 	return error;
    907  1.1  kiyohara }
    908  1.1  kiyohara 
    909  1.1  kiyohara static int
    910  1.1  kiyohara init_bcm2035(struct btuart_softc *sc)
    911  1.1  kiyohara {
    912  1.1  kiyohara 	struct mbuf *m;
    913  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    914  1.1  kiyohara 	hci_cmd_hdr_t *p;
    915  1.1  kiyohara 	int i, error;
    916  1.1  kiyohara #define HCI_CMD_BCM2035_SET_UART_BAUD_RATE	0xfc18	/* XXXX */
    917  1.1  kiyohara 	const uint16_t opcode = htole16(HCI_CMD_BCM2035_SET_UART_BAUD_RATE);
    918  1.1  kiyohara 	static struct {			/* XXXX */
    919  1.1  kiyohara 		int baud;
    920  1.1  kiyohara 		uint16_t param;
    921  1.1  kiyohara 	} bcm2035_baudtbl[] = {
    922  1.1  kiyohara 		{  B57600, 0xe600 },
    923  1.1  kiyohara 		{ B230400, 0xfa22 },
    924  1.1  kiyohara 		{ B460800, 0xfd11 },
    925  1.1  kiyohara 		{ B921600, 0xff65 },
    926  1.1  kiyohara 		{      B0, 0xffff }
    927  1.1  kiyohara 	};
    928  1.1  kiyohara 
    929  1.1  kiyohara 	for (i = 0; bcm2035_baudtbl[i].baud != sc->sc_baud; i++)
    930  1.1  kiyohara 		if (bcm2035_baudtbl[i].baud == -1)
    931  1.1  kiyohara 			return EINVAL;
    932  1.1  kiyohara 
    933  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    934  1.1  kiyohara 	if (m == NULL)
    935  1.1  kiyohara 		return ENOMEM;
    936  1.1  kiyohara 
    937  1.1  kiyohara 	/*
    938  1.1  kiyohara 	 * XXXX: Should we send some commands?
    939  1.1  kiyohara 	 *         HCI_CMD_RESET and HCI_CMD_READ_LOCAL_VER and
    940  1.1  kiyohara 	 *         HCI_CMD_READ_LOCAL_COMMANDS
    941  1.1  kiyohara 	 */
    942  1.1  kiyohara 
    943  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    944  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    945  1.1  kiyohara 	p->opcode = opcode;
    946  1.1  kiyohara 	p->length = sizeof(bcm2035_baudtbl[0].param);
    947  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    948  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length,
    949  1.1  kiyohara 	    &bcm2035_baudtbl[i].param);
    950  1.1  kiyohara 
    951  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    952  1.1  kiyohara 	bth4_start(unit);
    953  1.1  kiyohara 
    954  1.1  kiyohara 	error = bth4_waitresp(sc, &m, opcode);
    955  1.1  kiyohara 	if (m != NULL) {
    956  1.1  kiyohara 		if (error != 0) {
    957  1.1  kiyohara 			printf("%s: bcm2035 set baud rate failed:"
    958  1.1  kiyohara 			    " Status 0x%02x\n", sc->sc_dev.dv_xname, error);
    959  1.1  kiyohara 			error = EFAULT;
    960  1.1  kiyohara 		}
    961  1.1  kiyohara 		m_freem(m);
    962  1.1  kiyohara 	}
    963  1.1  kiyohara 
    964  1.1  kiyohara 	return error;
    965  1.1  kiyohara }
    966  1.1  kiyohara 
    967  1.1  kiyohara static int
    968  1.1  kiyohara bth4init(struct btuart_softc *sc)
    969  1.1  kiyohara {
    970  1.1  kiyohara 	struct tty *tp = sc->sc_tp;
    971  1.1  kiyohara 	struct termios t;
    972  1.1  kiyohara 	int error = 0, s;
    973  1.1  kiyohara 
    974  1.1  kiyohara 	sc->sc_baud = tp->t_ospeed;
    975  1.1  kiyohara 	t.c_cflag = tp->t_cflag;
    976  1.1  kiyohara 	t.c_ispeed = 0;
    977  1.1  kiyohara 	t.c_ospeed = tp->t_ospeed;
    978  1.1  kiyohara 	if ((tp->t_cflag & CRTSCTS) && !(sc->sc_bth4hci.flags & FLOW_CTL))
    979  1.1  kiyohara 		t.c_cflag &= ~CRTSCTS;
    980  1.1  kiyohara 	if (sc->sc_bth4hci.init_baud != 0 &&
    981  1.1  kiyohara 	    tp->t_ospeed != sc->sc_bth4hci.init_baud)
    982  1.1  kiyohara 		t.c_ospeed = sc->sc_bth4hci.init_baud;
    983  1.1  kiyohara 	if (t.c_ospeed != tp->t_ospeed || t.c_cflag != tp->t_cflag)
    984  1.1  kiyohara 		error = (*tp->t_param)(tp, &t);
    985  1.1  kiyohara 
    986  1.1  kiyohara 	if (error == 0 && sc->sc_bth4hci.init != NULL)
    987  1.1  kiyohara 		error = (*sc->sc_bth4hci.init)(sc);
    988  1.1  kiyohara 
    989  1.1  kiyohara 	s = splserial();
    990  1.1  kiyohara 	sc->sc_input_acl = hci_input_acl;
    991  1.1  kiyohara 	sc->sc_input_sco = hci_input_sco;
    992  1.1  kiyohara 	sc->sc_input_event = hci_input_event;
    993  1.1  kiyohara 	splx(s);
    994  1.1  kiyohara 
    995  1.1  kiyohara 	if (sc->sc_bth4hci.init_baud != 0 &&
    996  1.1  kiyohara 	    sc->sc_bth4hci.init_baud != sc->sc_baud) {
    997  1.1  kiyohara 		t.c_ospeed = sc->sc_baud;
    998  1.1  kiyohara 		t.c_cflag = tp->t_cflag;
    999  1.1  kiyohara 		error = (*tp->t_param)(tp, &t);
   1000  1.1  kiyohara 	}
   1001  1.1  kiyohara 
   1002  1.1  kiyohara 	return error;
   1003  1.1  kiyohara }
   1004  1.1  kiyohara 
   1005  1.1  kiyohara static void
   1006  1.1  kiyohara bth4init_input(struct hci_unit *unit, struct mbuf *m)
   1007  1.1  kiyohara {
   1008  1.1  kiyohara 	int i;
   1009  1.1  kiyohara 	uint8_t *rptr =  mtod(m, uint8_t *);
   1010  1.1  kiyohara 	const char *pktstr = NULL;
   1011  1.1  kiyohara 
   1012  1.1  kiyohara 	switch (*rptr) {
   1013  1.1  kiyohara 	case HCI_ACL_DATA_PKT:
   1014  1.1  kiyohara 		pktstr = "acl data";
   1015  1.1  kiyohara 		break;
   1016  1.1  kiyohara 
   1017  1.1  kiyohara 	case HCI_SCO_DATA_PKT:
   1018  1.1  kiyohara 		pktstr = "sco data";
   1019  1.1  kiyohara 		break;
   1020  1.1  kiyohara 
   1021  1.1  kiyohara 	case HCI_EVENT_PKT:
   1022  1.1  kiyohara 		break;
   1023  1.1  kiyohara 
   1024  1.1  kiyohara 	default:
   1025  1.1  kiyohara 		pktstr = "unknown";
   1026  1.1  kiyohara 		break;
   1027  1.1  kiyohara 	}
   1028  1.1  kiyohara 	if (pktstr != NULL)
   1029  1.1  kiyohara 		printf("%s: %s packet was received in initialization phase\n",
   1030  1.1  kiyohara 		    unit->hci_devname, pktstr);
   1031  1.1  kiyohara 	if (
   1032  1.1  kiyohara #ifdef BTUART_DEBUG
   1033  1.1  kiyohara 	    btuart_debug ||
   1034  1.1  kiyohara #endif
   1035  1.1  kiyohara 	    pktstr != NULL) {
   1036  1.1  kiyohara 		printf("%s: %s:", __FUNCTION__, unit->hci_devname);
   1037  1.1  kiyohara 		for (i = 0; i < m->m_len; i++)
   1038  1.1  kiyohara 			printf(" %02x", *(rptr + i));
   1039  1.1  kiyohara 		printf("\n");
   1040  1.1  kiyohara 	}
   1041  1.1  kiyohara 
   1042  1.1  kiyohara 	if (*rptr == HCI_EVENT_PKT)
   1043  1.1  kiyohara 		if (unit->hci_eventqlen <= hci_eventq_max) {
   1044  1.1  kiyohara 			unit->hci_eventqlen++;
   1045  1.1  kiyohara 			MBUFQ_ENQUEUE(&unit->hci_eventq, m);
   1046  1.1  kiyohara 			m = NULL;
   1047  1.1  kiyohara 			wakeup(&unit->hci_eventq);
   1048  1.1  kiyohara 		}
   1049  1.1  kiyohara 
   1050  1.1  kiyohara 	if (m != NULL)
   1051  1.1  kiyohara 		m_freem(m);
   1052  1.1  kiyohara }
   1053  1.1  kiyohara 
   1054  1.1  kiyohara 
   1055  1.1  kiyohara /*
   1056  1.1  kiyohara  * Line discipline functions.
   1057  1.1  kiyohara  */
   1058  1.1  kiyohara /* ARGSUSED */
   1059  1.1  kiyohara static int
   1060  1.1  kiyohara bth4open(dev_t device __unused, struct tty *tp)
   1061  1.1  kiyohara {
   1062  1.1  kiyohara 	struct btuart_softc *sc;
   1063  1.1  kiyohara 	struct cfdata *cfdata;
   1064  1.1  kiyohara 	struct lwp *l = curlwp;		/* XXX */
   1065  1.1  kiyohara 	int error, unit, s;
   1066  1.1  kiyohara 	static char name[] = "btuart";
   1067  1.1  kiyohara 
   1068  1.1  kiyohara 	if ((error = kauth_authorize_device_tty(l->l_cred,
   1069  1.1  kiyohara 	    KAUTH_DEVICE_TTY_OPEN, tp)) != 0)
   1070  1.1  kiyohara 		return error;
   1071  1.1  kiyohara 
   1072  1.1  kiyohara 	s = spltty();
   1073  1.1  kiyohara 
   1074  1.1  kiyohara 	if (tp->t_linesw == &bth4_disc) {
   1075  1.1  kiyohara 		sc = (struct btuart_softc *)tp->t_sc;
   1076  1.1  kiyohara 		if (sc != NULL) {
   1077  1.1  kiyohara 			splx(s);
   1078  1.1  kiyohara 			return EBUSY;
   1079  1.1  kiyohara 		}
   1080  1.1  kiyohara 	}
   1081  1.1  kiyohara 
   1082  1.1  kiyohara 	KASSERT(tp->t_oproc != NULL);
   1083  1.1  kiyohara 
   1084  1.1  kiyohara 	cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
   1085  1.1  kiyohara 	for (unit = 0; unit < btuart_cd.cd_ndevs; unit++)
   1086  1.1  kiyohara 		if (btuart_cd.cd_devs[unit] == NULL)
   1087  1.1  kiyohara 			break;
   1088  1.1  kiyohara 	cfdata->cf_name = name;
   1089  1.1  kiyohara 	cfdata->cf_atname = name;
   1090  1.1  kiyohara 	cfdata->cf_unit = unit;
   1091  1.1  kiyohara 	cfdata->cf_fstate = FSTATE_STAR,
   1092  1.1  kiyohara 
   1093  1.1  kiyohara 	printf("%s%d at tty major %d minor %d",
   1094  1.1  kiyohara 	    name, unit, major(tp->t_dev), minor(tp->t_dev));
   1095  1.1  kiyohara 	sc = (struct btuart_softc *)config_attach_pseudo(cfdata);
   1096  1.1  kiyohara 	if (sc == NULL) {
   1097  1.1  kiyohara 		splx(s);
   1098  1.1  kiyohara 		return EIO;
   1099  1.1  kiyohara 	}
   1100  1.1  kiyohara 	tp->t_sc = sc;
   1101  1.1  kiyohara 	sc->sc_tp = tp;
   1102  1.1  kiyohara 
   1103  1.1  kiyohara 	ttyflush(tp, FREAD | FWRITE);
   1104  1.1  kiyohara 
   1105  1.1  kiyohara 	splx(s);
   1106  1.1  kiyohara 
   1107  1.1  kiyohara 	return 0;
   1108  1.1  kiyohara }
   1109  1.1  kiyohara 
   1110  1.1  kiyohara /* ARGSUSED */
   1111  1.1  kiyohara static int
   1112  1.1  kiyohara bth4close(struct tty *tp, int flag __unused)
   1113  1.1  kiyohara {
   1114  1.1  kiyohara 	struct btuart_softc *sc;
   1115  1.1  kiyohara 	struct cfdata *cfdata;
   1116  1.1  kiyohara 	int s, baud;
   1117  1.1  kiyohara 
   1118  1.1  kiyohara 	sc = tp->t_sc;
   1119  1.1  kiyohara 
   1120  1.1  kiyohara 	/* reset to initial speed */
   1121  1.1  kiyohara 	if (sc->sc_bth4hci.init != NULL) {
   1122  1.1  kiyohara 		baud = sc->sc_baud;
   1123  1.1  kiyohara 		sc->sc_baud = sc->sc_bth4hci.init_baud;
   1124  1.1  kiyohara 		sc->sc_bth4hci.init_baud = baud;
   1125  1.1  kiyohara 		s = splserial();
   1126  1.1  kiyohara 		sc->sc_input_acl = bth4init_input;
   1127  1.1  kiyohara 		sc->sc_input_sco = bth4init_input;
   1128  1.1  kiyohara 		sc->sc_input_event = bth4init_input;
   1129  1.1  kiyohara 		splx(s);
   1130  1.1  kiyohara 		if ((*sc->sc_bth4hci.init)(sc) != 0)
   1131  1.1  kiyohara 			printf("%s: reset speed fail\n", sc->sc_dev.dv_xname);
   1132  1.1  kiyohara 	}
   1133  1.1  kiyohara 
   1134  1.1  kiyohara 	s = spltty();
   1135  1.1  kiyohara 	ttyflush(tp, FREAD | FWRITE);
   1136  1.1  kiyohara 	ttyldisc_release(tp->t_linesw);
   1137  1.1  kiyohara 	tp->t_linesw = ttyldisc_default();
   1138  1.1  kiyohara 	if (sc != NULL) {
   1139  1.1  kiyohara 		tp->t_sc = NULL;
   1140  1.1  kiyohara 		if (sc->sc_tp == tp) {
   1141  1.1  kiyohara 			cfdata = sc->sc_dev.dv_cfdata;
   1142  1.1  kiyohara 			config_detach(&sc->sc_dev, 0);
   1143  1.1  kiyohara 			free(cfdata, M_DEVBUF);
   1144  1.1  kiyohara 		}
   1145  1.1  kiyohara 
   1146  1.1  kiyohara 	}
   1147  1.1  kiyohara 	splx(s);
   1148  1.1  kiyohara 	return 0;
   1149  1.1  kiyohara }
   1150  1.1  kiyohara 
   1151  1.1  kiyohara /* ARGSUSED */
   1152  1.1  kiyohara static int
   1153  1.2  christos bth4ioctl(struct tty *tp, u_long cmd, void *data,
   1154  1.1  kiyohara     int flag __unused, struct lwp *l __unused)
   1155  1.1  kiyohara {
   1156  1.1  kiyohara 	struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
   1157  1.1  kiyohara 	int error, i;
   1158  1.1  kiyohara 
   1159  1.1  kiyohara 	if (sc == NULL || tp != sc->sc_tp)
   1160  1.1  kiyohara 		return EPASSTHROUGH;
   1161  1.1  kiyohara 
   1162  1.1  kiyohara 	error = 0;
   1163  1.1  kiyohara 	switch (cmd) {
   1164  1.1  kiyohara 	case BTUART_HCITYPE:
   1165  1.1  kiyohara 		for (i = 0; bth4hci[i].type != -1; i++)
   1166  1.1  kiyohara 			if (bth4hci[i].type == *(uint32_t *)data)
   1167  1.1  kiyohara 				break;
   1168  1.1  kiyohara 		if (bth4hci[i].type != -1)
   1169  1.1  kiyohara 			memcpy(&sc->sc_bth4hci, &bth4hci[i],
   1170  1.1  kiyohara 			    sizeof(struct bth4hci));
   1171  1.1  kiyohara 		else
   1172  1.1  kiyohara 			error = EINVAL;
   1173  1.1  kiyohara 		break;
   1174  1.1  kiyohara 
   1175  1.1  kiyohara 	case BTUART_INITSPEED:
   1176  1.1  kiyohara 		sc->sc_bth4hci.init_baud = *(uint32_t *)data;
   1177  1.1  kiyohara 		break;
   1178  1.1  kiyohara 
   1179  1.1  kiyohara 	case BTUART_START:
   1180  1.1  kiyohara 		error = bth4init(sc);
   1181  1.1  kiyohara 		break;
   1182  1.1  kiyohara 
   1183  1.1  kiyohara 	default:
   1184  1.1  kiyohara 		error = EPASSTHROUGH;
   1185  1.1  kiyohara 		break;
   1186  1.1  kiyohara 	}
   1187  1.1  kiyohara 
   1188  1.1  kiyohara 	return error;
   1189  1.1  kiyohara }
   1190  1.1  kiyohara 
   1191  1.1  kiyohara static int
   1192  1.1  kiyohara bth4input(int c, struct tty *tp)
   1193  1.1  kiyohara {
   1194  1.1  kiyohara 	struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
   1195  1.1  kiyohara 	struct mbuf *m = sc->sc_rxp;
   1196  1.1  kiyohara 	int space = 0;
   1197  1.1  kiyohara 
   1198  1.1  kiyohara 	c &= TTY_CHARMASK;
   1199  1.1  kiyohara 
   1200  1.1  kiyohara 	/* If we already started a packet, find the trailing end of it. */
   1201  1.1  kiyohara 	if (m) {
   1202  1.1  kiyohara 		while (m->m_next)
   1203  1.1  kiyohara 			m = m->m_next;
   1204  1.1  kiyohara 
   1205  1.1  kiyohara 		space = M_TRAILINGSPACE(m);
   1206  1.1  kiyohara 	}
   1207  1.1  kiyohara 
   1208  1.1  kiyohara 	if (space == 0) {
   1209  1.1  kiyohara 		if (m == NULL) {
   1210  1.1  kiyohara 			/* new packet */
   1211  1.1  kiyohara 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1212  1.1  kiyohara 			if (m == NULL) {
   1213  1.1  kiyohara 				printf("%s: out of memory\n",
   1214  1.1  kiyohara 				    sc->sc_dev.dv_xname);
   1215  1.1  kiyohara 				++sc->sc_unit.hci_stats.err_rx;
   1216  1.1  kiyohara 				return 0;	/* (lost sync) */
   1217  1.1  kiyohara 			}
   1218  1.1  kiyohara 
   1219  1.1  kiyohara 			sc->sc_rxp = m;
   1220  1.1  kiyohara 			m->m_pkthdr.len = m->m_len = 0;
   1221  1.1  kiyohara 			space = MHLEN;
   1222  1.1  kiyohara 
   1223  1.1  kiyohara 			sc->sc_state = BTUART_RECV_PKT_TYPE;
   1224  1.1  kiyohara 			sc->sc_want = 1;
   1225  1.1  kiyohara 		} else {
   1226  1.1  kiyohara 			/* extend mbuf */
   1227  1.1  kiyohara 			MGET(m->m_next, M_DONTWAIT, MT_DATA);
   1228  1.1  kiyohara 			if (m->m_next == NULL) {
   1229  1.1  kiyohara 				printf("%s: out of memory\n",
   1230  1.1  kiyohara 				    sc->sc_dev.dv_xname);
   1231  1.1  kiyohara 				++sc->sc_unit.hci_stats.err_rx;
   1232  1.1  kiyohara 				return 0;	/* (lost sync) */
   1233  1.1  kiyohara 			}
   1234  1.1  kiyohara 
   1235  1.1  kiyohara 			m = m->m_next;
   1236  1.1  kiyohara 			m->m_len = 0;
   1237  1.1  kiyohara 			space = MLEN;
   1238  1.1  kiyohara 
   1239  1.1  kiyohara 			if (sc->sc_want > MINCLSIZE) {
   1240  1.1  kiyohara 				MCLGET(m, M_DONTWAIT);
   1241  1.1  kiyohara 				if (m->m_flags & M_EXT)
   1242  1.1  kiyohara 					space = MCLBYTES;
   1243  1.1  kiyohara 			}
   1244  1.1  kiyohara 		}
   1245  1.1  kiyohara 	}
   1246  1.1  kiyohara 
   1247  1.1  kiyohara 	mtod(m, uint8_t *)[m->m_len++] = c;
   1248  1.1  kiyohara 	sc->sc_rxp->m_pkthdr.len++;
   1249  1.1  kiyohara 	sc->sc_unit.hci_stats.byte_rx++;
   1250  1.1  kiyohara 
   1251  1.1  kiyohara 	sc->sc_want--;
   1252  1.1  kiyohara 	if (sc->sc_want > 0)
   1253  1.1  kiyohara 		return 0;	/* want more */
   1254  1.1  kiyohara 
   1255  1.1  kiyohara 	switch (sc->sc_state) {
   1256  1.1  kiyohara 	case BTUART_RECV_PKT_TYPE:	/* Got packet type */
   1257  1.1  kiyohara 
   1258  1.1  kiyohara 		switch (c) {
   1259  1.1  kiyohara 		case HCI_ACL_DATA_PKT:
   1260  1.1  kiyohara 			sc->sc_state = BTUART_RECV_ACL_HDR;
   1261  1.1  kiyohara 			sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
   1262  1.1  kiyohara 			break;
   1263  1.1  kiyohara 
   1264  1.1  kiyohara 		case HCI_SCO_DATA_PKT:
   1265  1.1  kiyohara 			sc->sc_state = BTUART_RECV_SCO_HDR;
   1266  1.1  kiyohara 			sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
   1267  1.1  kiyohara 			break;
   1268  1.1  kiyohara 
   1269  1.1  kiyohara 		case HCI_EVENT_PKT:
   1270  1.1  kiyohara 			sc->sc_state = BTUART_RECV_EVENT_HDR;
   1271  1.1  kiyohara 			sc->sc_want = sizeof(hci_event_hdr_t) - 1;
   1272  1.1  kiyohara 			break;
   1273  1.1  kiyohara 
   1274  1.1  kiyohara 		default:
   1275  1.1  kiyohara 			printf("%s: Unknown packet type=%#x!\n",
   1276  1.1  kiyohara 			    sc->sc_dev.dv_xname, c);
   1277  1.1  kiyohara 			sc->sc_unit.hci_stats.err_rx++;
   1278  1.1  kiyohara 			m_freem(sc->sc_rxp);
   1279  1.1  kiyohara 			sc->sc_rxp = NULL;
   1280  1.1  kiyohara 			return 0;	/* (lost sync) */
   1281  1.1  kiyohara 		}
   1282  1.1  kiyohara 
   1283  1.1  kiyohara 		break;
   1284  1.1  kiyohara 
   1285  1.1  kiyohara 	/*
   1286  1.1  kiyohara 	 * we assume (correctly of course :) that the packet headers all fit
   1287  1.1  kiyohara 	 * into a single pkthdr mbuf
   1288  1.1  kiyohara 	 */
   1289  1.1  kiyohara 	case BTUART_RECV_ACL_HDR:	/* Got ACL Header */
   1290  1.1  kiyohara 		sc->sc_state = BTUART_RECV_ACL_DATA;
   1291  1.1  kiyohara 		sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
   1292  1.1  kiyohara 		sc->sc_want = le16toh(sc->sc_want);
   1293  1.1  kiyohara 		break;
   1294  1.1  kiyohara 
   1295  1.1  kiyohara 	case BTUART_RECV_SCO_HDR:	/* Got SCO Header */
   1296  1.1  kiyohara 		sc->sc_state = BTUART_RECV_SCO_DATA;
   1297  1.1  kiyohara 		sc->sc_want =  mtod(m, hci_scodata_hdr_t *)->length;
   1298  1.1  kiyohara 		break;
   1299  1.1  kiyohara 
   1300  1.1  kiyohara 	case BTUART_RECV_EVENT_HDR:	/* Got Event Header */
   1301  1.1  kiyohara 		sc->sc_state = BTUART_RECV_EVENT_DATA;
   1302  1.1  kiyohara 		sc->sc_want =  mtod(m, hci_event_hdr_t *)->length;
   1303  1.1  kiyohara 		break;
   1304  1.1  kiyohara 
   1305  1.1  kiyohara 	case BTUART_RECV_ACL_DATA:	/* ACL Packet Complete */
   1306  1.1  kiyohara 		(*sc->sc_input_acl)(&sc->sc_unit, sc->sc_rxp);
   1307  1.1  kiyohara 		sc->sc_unit.hci_stats.acl_rx++;
   1308  1.1  kiyohara 		sc->sc_rxp = m = NULL;
   1309  1.1  kiyohara 		break;
   1310  1.1  kiyohara 
   1311  1.1  kiyohara 	case BTUART_RECV_SCO_DATA:	/* SCO Packet Complete */
   1312  1.1  kiyohara 		(*sc->sc_input_sco)(&sc->sc_unit, sc->sc_rxp);
   1313  1.1  kiyohara 		sc->sc_unit.hci_stats.sco_rx++;
   1314  1.1  kiyohara 		sc->sc_rxp = m = NULL;
   1315  1.1  kiyohara 		break;
   1316  1.1  kiyohara 
   1317  1.1  kiyohara 	case BTUART_RECV_EVENT_DATA:	/* Event Packet Complete */
   1318  1.1  kiyohara 		sc->sc_unit.hci_stats.evt_rx++;
   1319  1.1  kiyohara 		(*sc->sc_input_event)(&sc->sc_unit, sc->sc_rxp);
   1320  1.1  kiyohara 		sc->sc_rxp = m = NULL;
   1321  1.1  kiyohara 		break;
   1322  1.1  kiyohara 
   1323  1.1  kiyohara 	default:
   1324  1.1  kiyohara 		panic("%s: invalid state %d!\n",
   1325  1.1  kiyohara 		    sc->sc_dev.dv_xname, sc->sc_state);
   1326  1.1  kiyohara 	}
   1327  1.1  kiyohara 
   1328  1.1  kiyohara 	return 0;
   1329  1.1  kiyohara }
   1330  1.1  kiyohara 
   1331  1.1  kiyohara static int
   1332  1.1  kiyohara bth4start(struct tty *tp)
   1333  1.1  kiyohara {
   1334  1.1  kiyohara 	struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
   1335  1.1  kiyohara 	struct mbuf *m;
   1336  1.1  kiyohara 	int count, rlen;
   1337  1.1  kiyohara 	uint8_t *rptr;
   1338  1.1  kiyohara 
   1339  1.1  kiyohara 	m = sc->sc_txp;
   1340  1.1  kiyohara 	if (m == NULL) {
   1341  1.1  kiyohara 		sc->sc_unit.hci_flags &= ~BTF_XMIT;
   1342  1.1  kiyohara 		bth4_start(&sc->sc_unit);
   1343  1.1  kiyohara 		return 0;
   1344  1.1  kiyohara 	}
   1345  1.1  kiyohara 
   1346  1.1  kiyohara 	count = 0;
   1347  1.1  kiyohara 	rlen = 0;
   1348  1.1  kiyohara 	rptr = mtod(m, uint8_t *);
   1349  1.1  kiyohara 
   1350  1.1  kiyohara 	for(;;) {
   1351  1.1  kiyohara 		if (rlen >= m->m_len) {
   1352  1.1  kiyohara 			m = m->m_next;
   1353  1.1  kiyohara 			if (m == NULL) {
   1354  1.1  kiyohara 				m = sc->sc_txp;
   1355  1.1  kiyohara 				sc->sc_txp = NULL;
   1356  1.1  kiyohara 
   1357  1.1  kiyohara 				if (M_GETCTX(m, void *) == NULL)
   1358  1.1  kiyohara 					m_freem(m);
   1359  1.1  kiyohara 				else
   1360  1.1  kiyohara 					hci_complete_sco(&sc->sc_unit, m);
   1361  1.1  kiyohara 
   1362  1.1  kiyohara 				break;
   1363  1.1  kiyohara 			}
   1364  1.1  kiyohara 
   1365  1.1  kiyohara 			rlen = 0;
   1366  1.1  kiyohara 			rptr = mtod(m, uint8_t *);
   1367  1.1  kiyohara 			continue;
   1368  1.1  kiyohara 		}
   1369  1.1  kiyohara 
   1370  1.1  kiyohara 		if (putc(*rptr++, &tp->t_outq) < 0) {
   1371  1.1  kiyohara 			m_adj(m, rlen);
   1372  1.1  kiyohara 			break;
   1373  1.1  kiyohara 		}
   1374  1.1  kiyohara 		rlen++;
   1375  1.1  kiyohara 		count++;
   1376  1.1  kiyohara 	}
   1377  1.1  kiyohara 
   1378  1.1  kiyohara 	sc->sc_unit.hci_stats.byte_tx += count;
   1379  1.1  kiyohara 
   1380  1.1  kiyohara 	if (tp->t_outq.c_cc != 0)
   1381  1.1  kiyohara 		(*tp->t_oproc)(tp);
   1382  1.1  kiyohara 
   1383  1.1  kiyohara 	return 0;
   1384  1.1  kiyohara }
   1385  1.1  kiyohara 
   1386  1.1  kiyohara 
   1387  1.1  kiyohara /*
   1388  1.1  kiyohara  * HCI UART (H4) functions.
   1389  1.1  kiyohara  */
   1390  1.1  kiyohara static int
   1391  1.1  kiyohara bth4_enable(struct hci_unit *unit)
   1392  1.1  kiyohara {
   1393  1.1  kiyohara 
   1394  1.1  kiyohara 	if (unit->hci_flags & BTF_RUNNING)
   1395  1.1  kiyohara 		return 0;
   1396  1.1  kiyohara 
   1397  1.1  kiyohara 	unit->hci_flags |= BTF_RUNNING;
   1398  1.1  kiyohara 	unit->hci_flags &= ~BTF_XMIT;
   1399  1.1  kiyohara 
   1400  1.1  kiyohara 	return 0;
   1401  1.1  kiyohara }
   1402  1.1  kiyohara 
   1403  1.1  kiyohara static void
   1404  1.1  kiyohara bth4_disable(struct hci_unit *unit)
   1405  1.1  kiyohara {
   1406  1.1  kiyohara 	struct btuart_softc *sc = unit->hci_softc;
   1407  1.1  kiyohara 
   1408  1.1  kiyohara 	if ((unit->hci_flags & BTF_RUNNING) == 0)
   1409  1.1  kiyohara 		return;
   1410  1.1  kiyohara 
   1411  1.1  kiyohara 	if (sc->sc_rxp) {
   1412  1.1  kiyohara 		m_freem(sc->sc_rxp);
   1413  1.1  kiyohara 		sc->sc_rxp = NULL;
   1414  1.1  kiyohara 	}
   1415  1.1  kiyohara 
   1416  1.1  kiyohara 	if (sc->sc_txp) {
   1417  1.1  kiyohara 		m_freem(sc->sc_txp);
   1418  1.1  kiyohara 		sc->sc_txp = NULL;
   1419  1.1  kiyohara 	}
   1420  1.1  kiyohara 
   1421  1.1  kiyohara 	unit->hci_flags &= ~BTF_RUNNING;
   1422  1.1  kiyohara }
   1423  1.1  kiyohara 
   1424  1.1  kiyohara static void
   1425  1.1  kiyohara bth4_start(struct hci_unit *unit)
   1426  1.1  kiyohara {
   1427  1.1  kiyohara 	struct btuart_softc *sc = unit->hci_softc;
   1428  1.1  kiyohara 	struct mbuf *m;
   1429  1.1  kiyohara 
   1430  1.1  kiyohara 	KASSERT((unit->hci_flags & BTF_XMIT) == 0);
   1431  1.1  kiyohara 	KASSERT(sc->sc_txp == NULL);
   1432  1.1  kiyohara 
   1433  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_cmdq)) {
   1434  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_cmdq, m);
   1435  1.1  kiyohara 		unit->hci_stats.cmd_tx++;
   1436  1.1  kiyohara 		M_SETCTX(m, NULL);
   1437  1.1  kiyohara 		goto start;
   1438  1.1  kiyohara 	}
   1439  1.1  kiyohara 
   1440  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_scotxq)) {
   1441  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_scotxq, m);
   1442  1.1  kiyohara 		unit->hci_stats.sco_tx++;
   1443  1.1  kiyohara 		goto start;
   1444  1.1  kiyohara 	}
   1445  1.1  kiyohara 
   1446  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_acltxq)) {
   1447  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_acltxq, m);
   1448  1.1  kiyohara 		unit->hci_stats.acl_tx++;
   1449  1.1  kiyohara 		M_SETCTX(m, NULL);
   1450  1.1  kiyohara 		goto start;
   1451  1.1  kiyohara 	}
   1452  1.1  kiyohara 
   1453  1.1  kiyohara 	/* Nothing to send */
   1454  1.1  kiyohara 	return;
   1455  1.1  kiyohara 
   1456  1.1  kiyohara start:
   1457  1.1  kiyohara 	sc->sc_txp = m;
   1458  1.1  kiyohara 	unit->hci_flags |= BTF_XMIT;
   1459  1.1  kiyohara 	bth4start(sc->sc_tp);
   1460  1.1  kiyohara }
   1461