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