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