init_bcm43xx.c revision 1.6 1 1.6 mlelstv /* $NetBSD: init_bcm43xx.c,v 1.6 2023/02/07 20:45:44 mlelstv Exp $ */
2 1.1 nat
3 1.1 nat /*-
4 1.5 nat * Copyright (c) 2017 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
5 1.1 nat * All rights reserved.
6 1.1 nat *
7 1.1 nat * Copyright (c) 2008 Iain Hibbert
8 1.1 nat * All rights reserved.
9 1.1 nat *
10 1.1 nat * Redistribution and use in source and binary forms, with or without
11 1.1 nat * modification, are permitted provided that the following conditions
12 1.1 nat * are met:
13 1.1 nat * 1. Redistributions of source code must retain the above copyright
14 1.1 nat * notice, this list of conditions and the following disclaimer.
15 1.1 nat * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nat * notice, this list of conditions and the following disclaimer in the
17 1.1 nat * documentation and/or other materials provided with the distribution.
18 1.1 nat *
19 1.1 nat * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 nat * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 nat * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 nat * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 nat * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 nat * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 nat * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 nat * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 nat * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 nat * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 nat */
30 1.1 nat
31 1.1 nat /*
32 1.1 nat * init information in this file gleaned from hciattach(8)
33 1.1 nat * command from BlueZ for Linux - see http://www.bluez.org/
34 1.1 nat */
35 1.1 nat
36 1.1 nat #include <sys/cdefs.h>
37 1.6 mlelstv __RCSID("$NetBSD: init_bcm43xx.c,v 1.6 2023/02/07 20:45:44 mlelstv Exp $");
38 1.3 jmcneill
39 1.3 jmcneill #include <sys/param.h>
40 1.1 nat
41 1.1 nat #include <bluetooth.h>
42 1.1 nat #include <err.h>
43 1.1 nat #include <errno.h>
44 1.1 nat #include <fcntl.h>
45 1.1 nat #include <stdlib.h>
46 1.1 nat #include <string.h>
47 1.1 nat #include <termios.h>
48 1.1 nat #include <unistd.h>
49 1.1 nat
50 1.1 nat #include "btattach.h"
51 1.3 jmcneill #include "firmload.h"
52 1.1 nat
53 1.1 nat #define HCI_CMD_BCM43XX_SET_UART_BAUD_RATE \
54 1.1 nat HCI_OPCODE(HCI_OGF_VENDOR, 0x018)
55 1.1 nat
56 1.1 nat #define HCI_CMD_BCM43XX_SET_BDADDR \
57 1.1 nat HCI_OPCODE(HCI_OGF_VENDOR, 0x006)
58 1.1 nat
59 1.4 nat #define HCI_CMD_BCM43XX_SET_CLOCK \
60 1.4 nat HCI_OPCODE(HCI_OGF_VENDOR, 0x045)
61 1.4 nat
62 1.1 nat #define HCI_CMD_43XXFWDN \
63 1.1 nat HCI_OPCODE(HCI_OGF_VENDOR, 0x02e)
64 1.1 nat
65 1.3 jmcneill #define HCI_CMD_GET_LOCAL_NAME 0x0c14
66 1.3 jmcneill
67 1.4 nat #define BCM43XX_CLK_48 1
68 1.4 nat #define BCM43XX_CLK_24 2
69 1.4 nat
70 1.3 jmcneill static int
71 1.3 jmcneill bcm43xx_get_local_name(int fd, char *name, size_t namelen)
72 1.3 jmcneill {
73 1.3 jmcneill char buf[256];
74 1.3 jmcneill size_t len;
75 1.3 jmcneill
76 1.3 jmcneill memset(buf, 0, sizeof(buf));
77 1.3 jmcneill
78 1.3 jmcneill uart_send_cmd(fd, HCI_CMD_GET_LOCAL_NAME, NULL, 0);
79 1.3 jmcneill len = uart_recv_cc(fd, HCI_CMD_GET_LOCAL_NAME, buf, sizeof(buf));
80 1.3 jmcneill if (len == 0)
81 1.3 jmcneill return EIO;
82 1.3 jmcneill
83 1.3 jmcneill strlcpy(name, &buf[1], MIN(len - 1, namelen));
84 1.3 jmcneill
85 1.3 jmcneill if (strlen(name) == 0)
86 1.3 jmcneill return EIO;
87 1.3 jmcneill
88 1.3 jmcneill return 0;
89 1.3 jmcneill }
90 1.3 jmcneill
91 1.1 nat void
92 1.1 nat init_bcm43xx(int fd, unsigned int speed)
93 1.1 nat {
94 1.4 nat uint8_t rate[6], clock;
95 1.1 nat uint8_t fw_buf[1024];
96 1.2 jakllsch int fwfd, fw_len;
97 1.1 nat uint8_t resp[7];
98 1.1 nat uint16_t fw_cmd;
99 1.3 jmcneill char local_name[256];
100 1.3 jmcneill char fw[260];
101 1.1 nat
102 1.1 nat memset(rate, 0, sizeof(rate));
103 1.3 jmcneill memset(local_name, 0, sizeof(local_name));
104 1.1 nat
105 1.6 mlelstv if (uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0))
106 1.6 mlelstv return;
107 1.1 nat uart_recv_cc(fd, HCI_CMD_RESET, &resp, sizeof(resp));
108 1.1 nat /* assume it succeeded? */
109 1.1 nat
110 1.3 jmcneill if (bcm43xx_get_local_name(fd, local_name, sizeof(local_name)) != 0) {
111 1.3 jmcneill fprintf(stderr, "Couldn't read local name\n");
112 1.3 jmcneill return;
113 1.3 jmcneill }
114 1.3 jmcneill snprintf(fw, sizeof(fw), "%s.hcd", local_name);
115 1.3 jmcneill
116 1.3 jmcneill fwfd = firmware_open("bcm43xx", fw);
117 1.1 nat if (fwfd < 0) {
118 1.3 jmcneill fprintf(stderr, "Unable to open firmware bcm43xx/%s: %s\n",
119 1.3 jmcneill fw, strerror(errno));
120 1.1 nat return;
121 1.1 nat }
122 1.1 nat
123 1.1 nat uart_send_cmd(fd, HCI_CMD_43XXFWDN, NULL, 0);
124 1.1 nat uart_recv_cc(fd, HCI_CMD_43XXFWDN, &resp, sizeof(resp));
125 1.1 nat sleep(1);
126 1.1 nat
127 1.1 nat while (read(fwfd, &fw_buf[1], 3) == 3) {
128 1.1 nat fw_buf[0] = HCI_CMD_PKT;
129 1.1 nat fw_len = fw_buf[3];
130 1.1 nat if (read(fwfd, &fw_buf[4], fw_len) != fw_len)
131 1.1 nat break;
132 1.1 nat fw_cmd = fw_buf[2] << 8 | fw_buf[1];
133 1.1 nat uart_send_cmd(fd, fw_cmd, &fw_buf[4], fw_len);
134 1.1 nat uart_recv_cc(fd, fw_cmd, &resp, sizeof(resp));
135 1.1 nat }
136 1.1 nat
137 1.1 nat close(fwfd);
138 1.1 nat
139 1.1 nat sleep(4);
140 1.1 nat uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
141 1.1 nat uart_recv_cc(fd, HCI_CMD_RESET, &resp, sizeof(resp));
142 1.1 nat /* assume it succeeded? */
143 1.1 nat
144 1.4 nat if (speed >= 3000000)
145 1.4 nat clock = BCM43XX_CLK_48;
146 1.4 nat else
147 1.4 nat clock = BCM43XX_CLK_24;
148 1.4 nat
149 1.4 nat uart_send_cmd(fd, HCI_CMD_BCM43XX_SET_CLOCK, &clock, sizeof(clock));
150 1.4 nat uart_recv_cc(fd, HCI_CMD_BCM43XX_SET_CLOCK, &resp, sizeof(resp));
151 1.4 nat
152 1.1 nat rate[2] = speed;
153 1.1 nat rate[3] = speed >> 8;
154 1.1 nat rate[4] = speed >> 16;
155 1.1 nat rate[5] = speed >> 24;
156 1.1 nat
157 1.1 nat uart_send_cmd(fd, HCI_CMD_BCM43XX_SET_UART_BAUD_RATE, &rate, sizeof(rate));
158 1.1 nat uart_recv_cc(fd, HCI_CMD_BCM43XX_SET_UART_BAUD_RATE, &resp, sizeof(resp));
159 1.1 nat }
160