1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <stdio.h>
3*4882a593Smuzhiyun #include <errno.h>
4*4882a593Smuzhiyun #include <unistd.h>
5*4882a593Smuzhiyun #include <stdlib.h>
6*4882a593Smuzhiyun #include <termios.h>
7*4882a593Smuzhiyun #include <time.h>
8*4882a593Smuzhiyun #include <sys/time.h>
9*4882a593Smuzhiyun #include <sys/types.h>
10*4882a593Smuzhiyun #include <sys/param.h>
11*4882a593Smuzhiyun #include <sys/ioctl.h>
12*4882a593Smuzhiyun #include <sys/socket.h>
13*4882a593Smuzhiyun #include <sys/uio.h>
14*4882a593Smuzhiyun #include <sys/stat.h>
15*4882a593Smuzhiyun #include <fcntl.h>
16*4882a593Smuzhiyun #include <signal.h>
17*4882a593Smuzhiyun #include <stdint.h>
18*4882a593Smuzhiyun #include <string.h>
19*4882a593Smuzhiyun #include <endian.h>
20*4882a593Smuzhiyun #include <byteswap.h>
21*4882a593Smuzhiyun #include <netinet/in.h>
22*4882a593Smuzhiyun #include <ctype.h>
23*4882a593Smuzhiyun #include <poll.h>
24*4882a593Smuzhiyun #include <sys/timerfd.h>
25*4882a593Smuzhiyun #include <sys/epoll.h>
26*4882a593Smuzhiyun #include "hciattach.h"
27*4882a593Smuzhiyun #include "hciattach_h4.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun extern struct rtb_struct rtb_cfg;
30*4882a593Smuzhiyun
start_xfer_wait(int fd,uint8_t * cmd,uint16_t len,uint32_t msec,int retry,uint8_t * resp,uint16_t * resp_len)31*4882a593Smuzhiyun static int start_xfer_wait(int fd, uint8_t *cmd, uint16_t len, uint32_t msec,
32*4882a593Smuzhiyun int retry, uint8_t *resp, uint16_t *resp_len)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun uint8_t buf[64];
35*4882a593Smuzhiyun int result;
36*4882a593Smuzhiyun int state = 1;
37*4882a593Smuzhiyun int count = 0;
38*4882a593Smuzhiyun int params_len;
39*4882a593Smuzhiyun struct pollfd p[2];
40*4882a593Smuzhiyun uint16_t opcode;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun if (fd == -1 || !cmd || len < 4) {
43*4882a593Smuzhiyun RS_ERR("%s: invalid parameter", __func__);
44*4882a593Smuzhiyun return -1;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun opcode = ((uint16_t)cmd[2] << 8) + cmd[1];
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun start_xfer:
50*4882a593Smuzhiyun result = write(fd, cmd, len);
51*4882a593Smuzhiyun if (result != len) {
52*4882a593Smuzhiyun RS_ERR("%s: Write cmd %04x error, %s", __func__, opcode,
53*4882a593Smuzhiyun strerror(errno));
54*4882a593Smuzhiyun return -1;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun start_recv:
58*4882a593Smuzhiyun memset(buf, 0, sizeof(buf));
59*4882a593Smuzhiyun memset(p, 0, sizeof(p));
60*4882a593Smuzhiyun state = 1;
61*4882a593Smuzhiyun count = 0;
62*4882a593Smuzhiyun p[0].fd = fd;
63*4882a593Smuzhiyun p[0].events = POLLERR | POLLHUP | POLLIN;
64*4882a593Smuzhiyun for (;;) {
65*4882a593Smuzhiyun p[0].revents = 0;
66*4882a593Smuzhiyun result = poll(p, 1, msec);
67*4882a593Smuzhiyun if (result < 0) {
68*4882a593Smuzhiyun RS_ERR("Poll call error, %s", strerror(errno));
69*4882a593Smuzhiyun result = -1;
70*4882a593Smuzhiyun break;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (result == 0) {
74*4882a593Smuzhiyun RS_WARN("%s: Timeout", __func__);
75*4882a593Smuzhiyun if (retry <= 0) {
76*4882a593Smuzhiyun RS_ERR("%s: Transfer exhausted", __func__);
77*4882a593Smuzhiyun tcflush(fd, TCIOFLUSH);
78*4882a593Smuzhiyun exit(EXIT_FAILURE);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun retry--;
81*4882a593Smuzhiyun goto start_xfer;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (p[0].revents & (POLLERR | POLLHUP)) {
85*4882a593Smuzhiyun RS_ERR("POLLERR or POLLUP happens, %s",
86*4882a593Smuzhiyun strerror(errno));
87*4882a593Smuzhiyun result = -1;
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (state == 1) {
92*4882a593Smuzhiyun result = read(p[0].fd, buf, 1);
93*4882a593Smuzhiyun if (result == -1 || result != 1) {
94*4882a593Smuzhiyun RS_ERR("%s: Read pkt type error, %s", __func__,
95*4882a593Smuzhiyun strerror(errno));
96*4882a593Smuzhiyun result = -1;
97*4882a593Smuzhiyun break;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun if (result == 1 && buf[0] == 0x04) {
100*4882a593Smuzhiyun count = 1;
101*4882a593Smuzhiyun state = 2;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun } else if (state == 2) {
104*4882a593Smuzhiyun result = read(p[0].fd, buf + count, 2);
105*4882a593Smuzhiyun if (result == -1 || result != 2) {
106*4882a593Smuzhiyun RS_ERR("%s: Read pkt header error, %s",
107*4882a593Smuzhiyun __func__, strerror(errno));
108*4882a593Smuzhiyun break;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun count += result;
111*4882a593Smuzhiyun state = 3;
112*4882a593Smuzhiyun params_len = buf[2];
113*4882a593Smuzhiyun if (params_len + 3 > sizeof(buf)) {
114*4882a593Smuzhiyun result = -1;
115*4882a593Smuzhiyun RS_ERR("%s: hci event too long", __func__);
116*4882a593Smuzhiyun break;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun } else if (state == 3) {
119*4882a593Smuzhiyun result = read(p[0].fd, buf + count, params_len);
120*4882a593Smuzhiyun if (result == -1) {
121*4882a593Smuzhiyun RS_ERR("%s: Read pkt payload error, %s",
122*4882a593Smuzhiyun __func__, strerror(errno));
123*4882a593Smuzhiyun break;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun count += result;
126*4882a593Smuzhiyun params_len -= result;
127*4882a593Smuzhiyun if (!params_len)
128*4882a593Smuzhiyun break;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun if (result >= 0) {
133*4882a593Smuzhiyun if (buf[1] == 0x0e) {
134*4882a593Smuzhiyun uint16_t tmp_opcode;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun tmp_opcode = (uint16_t)buf[4] | buf[5] << 8;
137*4882a593Smuzhiyun if (tmp_opcode == opcode) {
138*4882a593Smuzhiyun RS_INFO("Cmd complete event for cmd %04x",
139*4882a593Smuzhiyun opcode);
140*4882a593Smuzhiyun /* Status is not zero indicating command not
141*4882a593Smuzhiyun * succeeded */
142*4882a593Smuzhiyun if (buf[6])
143*4882a593Smuzhiyun return -1;
144*4882a593Smuzhiyun if (!resp)
145*4882a593Smuzhiyun return 0;
146*4882a593Smuzhiyun if (*resp_len > count)
147*4882a593Smuzhiyun *resp_len = count;
148*4882a593Smuzhiyun memcpy(resp, buf, *resp_len);
149*4882a593Smuzhiyun return 0;
150*4882a593Smuzhiyun } else {
151*4882a593Smuzhiyun RS_WARN("Unexpected cmd complete event, %04x",
152*4882a593Smuzhiyun tmp_opcode);
153*4882a593Smuzhiyun return -1;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun } else {
156*4882a593Smuzhiyun RS_INFO("%s: Unexpected hci event packet", __func__);
157*4882a593Smuzhiyun util_hexdump(buf, count);
158*4882a593Smuzhiyun /* Continue receiving */
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun goto start_recv;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun return result;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
h4_download_patch(int fd,int index,uint8_t * data,int len)166*4882a593Smuzhiyun int h4_download_patch(int fd, int index, uint8_t *data, int len)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun uint8_t buf[257];
169*4882a593Smuzhiyun uint16_t total_len;
170*4882a593Smuzhiyun int result;
171*4882a593Smuzhiyun uint8_t resp[8];
172*4882a593Smuzhiyun uint16_t rlen = sizeof(resp);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun RS_DBG("fd: %d, index: %d, len: %d", fd, index, len);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (data)
177*4882a593Smuzhiyun memcpy(&buf[5], data, len);
178*4882a593Smuzhiyun buf[0] = 0x01;
179*4882a593Smuzhiyun buf[1] = 0x20;
180*4882a593Smuzhiyun buf[2] = 0xfc;
181*4882a593Smuzhiyun buf[3] = len + 1;
182*4882a593Smuzhiyun buf[4] = (uint8_t)index;
183*4882a593Smuzhiyun total_len = len + 5;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun result = start_xfer_wait(fd, buf, total_len, 1000, 0, resp, &rlen);
186*4882a593Smuzhiyun if (result < 0) {
187*4882a593Smuzhiyun RS_ERR("Transfer patch failed, index %d", index);
188*4882a593Smuzhiyun return -1;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (rlen != 8) {
192*4882a593Smuzhiyun RS_ERR("%s: Unexpected length %u", __func__, rlen);
193*4882a593Smuzhiyun return -1;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return resp[7];
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
h4_vendor_change_speed(int fd,uint32_t baudrate)199*4882a593Smuzhiyun int h4_vendor_change_speed(int fd, uint32_t baudrate)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun int res;
202*4882a593Smuzhiyun uint8_t cmd[8] = { 0 };
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun cmd[0] = 1;
205*4882a593Smuzhiyun cmd[1] = 0x17;
206*4882a593Smuzhiyun cmd[2] = 0xfc;
207*4882a593Smuzhiyun cmd[3] = 4;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun baudrate = cpu_to_le32(baudrate);
210*4882a593Smuzhiyun #ifdef BAUDRATE_4BYTES
211*4882a593Smuzhiyun memcpy((uint16_t *) & cmd[4], &baudrate, 4);
212*4882a593Smuzhiyun #else
213*4882a593Smuzhiyun memcpy((uint16_t *) & cmd[4], &baudrate, 2);
214*4882a593Smuzhiyun cmd[6] = 0;
215*4882a593Smuzhiyun cmd[7] = 0;
216*4882a593Smuzhiyun #endif
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* TODO: Wait for a while for device to up, just h4 need it */
219*4882a593Smuzhiyun sleep(1);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun RS_DBG("baudrate in change speed command: 0x%02x 0x%02x 0x%02x 0x%02x",
222*4882a593Smuzhiyun cmd[4], cmd[5], cmd[6], cmd[7]);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun res = start_xfer_wait(fd, cmd, 8, 1000, 0, NULL, 0);
225*4882a593Smuzhiyun if (res < 0)
226*4882a593Smuzhiyun RS_ERR("Change Controller baud failed");
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return res;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
h4_hci_reset(int fd)231*4882a593Smuzhiyun int h4_hci_reset(int fd)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun int result;
234*4882a593Smuzhiyun uint8_t cmd[4] = { 0x01, 0x03, 0x0c, 0x00};
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun RS_INFO("%s: Issue hci reset cmd", __func__);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun result = start_xfer_wait(fd, cmd, sizeof(cmd), 1000, 0, NULL, 0);
239*4882a593Smuzhiyun if (result < 0) {
240*4882a593Smuzhiyun RS_ERR("%s: Failed to send reset cmd", __func__);
241*4882a593Smuzhiyun return -1;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
h4_read_local_ver(int fd)247*4882a593Smuzhiyun int h4_read_local_ver(int fd)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun uint8_t cmd[4] = { 0x01, 0x01, 0x10, 0x00 };
250*4882a593Smuzhiyun uint8_t resp[16];
251*4882a593Smuzhiyun uint16_t len = sizeof(resp);
252*4882a593Smuzhiyun int result;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun result = start_xfer_wait(fd, cmd, sizeof(cmd), 1000, 0,
255*4882a593Smuzhiyun resp, &len);
256*4882a593Smuzhiyun if (result < 0) {
257*4882a593Smuzhiyun RS_ERR("HCI Read local version info error");
258*4882a593Smuzhiyun return -1;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (len != 15) {
262*4882a593Smuzhiyun RS_ERR("%s: Unexpected length %u", __func__, len);
263*4882a593Smuzhiyun return -1;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun rtb_cfg.hci_ver = resp[7];
266*4882a593Smuzhiyun rtb_cfg.hci_rev = (uint32_t)resp[9] << 8 | resp[8];
267*4882a593Smuzhiyun rtb_cfg.lmp_subver = (uint32_t)resp[14] << 8 | resp[13];
268*4882a593Smuzhiyun RS_INFO("hci ver %02x, hci_rev %04x, lmp_subver %04x",
269*4882a593Smuzhiyun rtb_cfg.hci_ver, rtb_cfg.hci_rev, rtb_cfg.lmp_subver);
270*4882a593Smuzhiyun return 0;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
h4_vendor_read_rom_ver(int fd)273*4882a593Smuzhiyun int h4_vendor_read_rom_ver(int fd)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun uint8_t cmd[4] = { 0x01, 0x6d, 0xfc, 0x00 };
276*4882a593Smuzhiyun uint8_t resp[16];
277*4882a593Smuzhiyun uint16_t len = sizeof(resp);
278*4882a593Smuzhiyun int result;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun result = start_xfer_wait(fd, cmd, sizeof(cmd), 1000, 0,
281*4882a593Smuzhiyun resp, &len);
282*4882a593Smuzhiyun if (result < 0) {
283*4882a593Smuzhiyun RS_ERR("HCI Read local version info error");
284*4882a593Smuzhiyun return -1;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun if (len != 8) {
288*4882a593Smuzhiyun RS_ERR("%s: Unexpected length %u", __func__, len);
289*4882a593Smuzhiyun return -1;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun rtb_cfg.eversion = resp[7];
292*4882a593Smuzhiyun RS_INFO("eversion %02x", rtb_cfg.eversion);
293*4882a593Smuzhiyun return 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296