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