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