blob: a5c4d14f4d119bcfdfb5238e83603607daa990d5 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* Copyright Statement:
2 *
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein is
5 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
6 * the prior written permission of MediaTek inc. and/or its licensors, any
7 * reproduction, modification, use or disclosure of MediaTek Software, and
8 * information contained herein, in whole or in part, shall be strictly
9 * prohibited.
10 *
11 * MediaTek Inc. (C) 2016~2017. All rights reserved.
12 *
13 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
14 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
15 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
16 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
17 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
20 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
21 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
22 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
23 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
24 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
25 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
26 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
27 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
28 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
29 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
30 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
31 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
32 *
33 * The following software/firmware and/or related documentation ("MediaTek
34 * Software") have been modified by MediaTek Inc. All revisions are subject to
35 * any receiver's applicable license agreements with MediaTek Inc.
36 */
37
38//- vim: set ts=4 sts=4 sw=4 et: --------------------------------------------
39#include <errno.h>
40#include <unistd.h>
41#include <fcntl.h>
42
43#include "boots.h"
44
45//---------------------------------------------------------------------------
46#define LOG_TAG "boots_btif"
47
48//---------------------------------------------------------------------------
49extern int fd[BOOTS_BTIF_ALL];
50extern int mtk_66xx_Init(int comPort, unsigned int chipId);
51extern uint32_t mtk_66xx_detect(void);
52extern int boots_hci_enable(char *arg, void *func_cb);
53extern int boots_hci_disable(int bt_fd);
54
55//---------------------------------------------------------------------------
56static char *get_inf_path(boots_if_e interface)
57{
58 int i = 0;
59
60 while (boots_btif[i].inf != interface) {
61 i++;
62 }
63 BPRINT_D("path: %s", boots_btif[i].p);
64 return boots_btif[i].p;
65}
66
67//---------------------------------------------------------------------------
68static int bt_init(int fd, uint32_t chip)
69{
70 /* Patch download is moved to WMT driver on combo chip */
71 /* Invoke HCI transport entrance */
72 if (mtk_66xx_Init(fd, chip) != 0) {
73 BPRINT_E("mtk_66xx_Init failed");
74 return -1;
75 }
76 return 0;
77}
78
79//---------------------------------------------------------------------------
80static int stpbt_enable(char *dev)
81{
82 int fd = -1;
83
84 if (dev == NULL) return fd;
85
86 fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
87 if (fd < 0) {
88 BPRINT_E("Can't open BT port, errno: %d", fd);
89 return -1;
90 } else {
91 BPRINT_D("Open %s(%d) successfully", dev, fd);
92 }
93
94 return fd;
95}
96
97//---------------------------------------------------------------------------
98static void stpbt_disable(int fd)
99{
100 if (fd > 0)
101 close(fd);
102
103 return;
104}
105
106//---------------------------------------------------------------------------
107int boots_btif_open(boots_if_e interface)
108{
109 uint32_t chip = 0x0000;
110 /**
111 * Depends on different interface:
112 * HCI, STPBT, UART & ETHERNET
113 * to do.
114 */
115 switch (interface) {
116 case BOOTS_BTIF_STPBT:
117 fd[interface] = stpbt_enable(get_inf_path(interface));
118 break;
119 case BOOTS_BTIF_HCI:
120 fd[interface] = boots_hci_enable(get_inf_path(interface), NULL);
121 break;
122 default:
123 BPRINT_E("%s: Incorrect interface: %d", __func__, interface);
124 return -1;
125 break;
126 }
127
128 if (fd[interface] > 0) {
129 chip = mtk_66xx_detect();
130 switch (chip) {
131 case 0x6630:
132// case 0x8167:
133 BPRINT_I("chip: %04X", chip);
134 if (bt_init(fd[interface], chip) != 0) {
135 return -1;
136 }
137 break;
138 default:
139 break;
140 };
141 }
142
143 return fd[interface];
144}
145
146//---------------------------------------------------------------------------
147int boots_btif_close(boots_if_e interface)
148{
149 /** bt deinit */
150 if (fd[interface] < 0) {
151 return -1;
152 }
153
154 switch (interface) {
155 case BOOTS_BTIF_STPBT:
156 stpbt_disable(fd[interface]);
157 break;
158 case BOOTS_BTIF_HCI:
159 boots_hci_disable(fd[interface]);
160 break;
161 default:
162 BPRINT_E("%s: Incorrect interface: %d", __func__, interface);
163 break;
164 }
165 return 0;
166}
167
168//---------------------------------------------------------------------------
169ssize_t boots_btif_read(int fd, uint8_t *buf, size_t size)
170{
171 ssize_t nRead = 0;
172
173 if (fd < 0) {
174 BPRINT_E("File descriptor in bad state");
175 return -EIO;
176 } else if (!buf || !size) {
177 BPRINT_E("%s: Invalid argument buf:%p, size:%d", __func__, buf, (int)size);
178 return -EINVAL;
179 }
180
181 nRead = read(fd, buf, size);
182 if (nRead <= 0) {
183 if (errno == EINTR || errno == EAGAIN) {
184 BPRINT_W("%s: %s(%d)", __func__, strerror(errno), errno);
185 return 0;
186 } else {
187 BPRINT_E("%s: %s(%d)", __func__, strerror(errno), errno);
188 return -errno;
189 }
190 }
191
192 return nRead;
193}
194
195//---------------------------------------------------------------------------
196ssize_t boots_btif_write(int fd, uint8_t *buf, size_t len)
197{
198 ssize_t ret = 0;
199 size_t bytesToWrite = len;
200
201 if (fd < 0) {
202 BPRINT_E("%s: No available com port", __func__);
203 return -EIO;
204 }
205
206 while (bytesToWrite > 0) {
207 ret = write(fd, buf, bytesToWrite);
208 if (ret <= 0) {
209 if (errno == EINTR || errno == EAGAIN) {
210 BPRINT_W("%s: %s(%d)", __func__, strerror(errno), errno);
211 return 0;
212 } else {
213 BPRINT_E("%s: %s(%d)", __func__, strerror(errno), errno);
214 return -errno;
215 }
216 }
217 bytesToWrite -= ret;
218 buf += ret;
219 }
220
221 return (len - bytesToWrite);
222}
223
224//---------------------------------------------------------------------------