rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* 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 | #include <string.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <errno.h> |
| 41 | #include <unistd.h> |
| 42 | #include <sys/socket.h> |
| 43 | #include <sys/ioctl.h> |
| 44 | |
| 45 | #include "boots.h" |
| 46 | |
| 47 | #define LOG_TAG "boots_hci" |
| 48 | |
| 49 | #define HCI_MAX_DEV 1 |
| 50 | #define BTPROTO_HCI 1 |
| 51 | |
| 52 | #define HCIDEVUP _IOW('H', 201, int) |
| 53 | #define HCIDEVDOWN _IOW('H', 202, int) |
| 54 | #define HCIDEVRESET _IOW('H', 203, int) |
| 55 | #define HCIDEVRESTAT _IOW('H', 204, int) |
| 56 | #define HCIGETDEVLIST _IOR('H', 210, int) |
| 57 | #define HCISETRAW _IOW('H', 220, int) |
| 58 | |
| 59 | enum { |
| 60 | HCI_UP, |
| 61 | HCI_INIT, |
| 62 | HCI_RUNNING, |
| 63 | |
| 64 | HCI_PSCAN, |
| 65 | HCI_ISCAN, |
| 66 | HCI_AUTH, |
| 67 | HCI_ENCRYPT, |
| 68 | HCI_INQUIRY, |
| 69 | |
| 70 | HCI_RAW, |
| 71 | |
| 72 | HCI_RESET, |
| 73 | }; |
| 74 | |
| 75 | struct sockaddr_hci { |
| 76 | unsigned short family; |
| 77 | unsigned short device; |
| 78 | unsigned short channel; |
| 79 | }; |
| 80 | |
| 81 | struct dev_info { |
| 82 | int index; |
| 83 | int skt_fd; |
| 84 | }; |
| 85 | |
| 86 | struct hci_dev_req { |
| 87 | uint16_t dev_id; |
| 88 | uint32_t dev_opt; |
| 89 | }; |
| 90 | |
| 91 | struct hci_dev_list_req { |
| 92 | uint16_t dev_num; |
| 93 | struct hci_dev_req dev_req[0]; /* hci_dev_req structures */ |
| 94 | }; |
| 95 | |
| 96 | static int commPort = 0; |
| 97 | static struct dev_info hci_dev_info[HCI_MAX_DEV]; |
| 98 | |
| 99 | //--------------------------------------------------------------------------- |
| 100 | static int boots_hci_open_dev(uint16_t index) |
| 101 | { |
| 102 | struct sockaddr_hci addr_hci; |
| 103 | int skt_fd; |
| 104 | |
| 105 | /* Create HCI socket */ |
| 106 | skt_fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
| 107 | if (skt_fd < 0) |
| 108 | return skt_fd; |
| 109 | |
| 110 | BPRINT_D("sock = %x", skt_fd); |
| 111 | /* Bind socket to the HCI device */ |
| 112 | memset(&addr_hci, 0, sizeof(addr_hci)); |
| 113 | addr_hci.family = AF_BLUETOOTH; |
| 114 | addr_hci.device = index; |
| 115 | /* 1 means HCI_USER_CHANNEL */ |
| 116 | addr_hci.channel = 1; |
| 117 | if (bind(skt_fd, (struct sockaddr *) &addr_hci, sizeof(addr_hci)) < 0) |
| 118 | goto failed; |
| 119 | |
| 120 | return skt_fd; |
| 121 | |
| 122 | failed: |
| 123 | close(skt_fd); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | //--------------------------------------------------------------------------- |
| 128 | static int boots_hci_close_dev(int skt_fd) |
| 129 | { |
| 130 | if (skt_fd < 0) { |
| 131 | BPRINT_E("boots_hci_close_dev %d", skt_fd); |
| 132 | return -1; |
| 133 | } |
| 134 | return close(skt_fd); |
| 135 | } |
| 136 | |
| 137 | //--------------------------------------------------------------------------- |
| 138 | static struct dev_info *boots_hci_init_device(uint16_t index) |
| 139 | { |
| 140 | struct dev_info *dev = NULL; |
| 141 | int skt_fd; |
| 142 | |
| 143 | BPRINT_I("open HCI%d", index); |
| 144 | skt_fd = boots_hci_open_dev(index); |
| 145 | if (skt_fd < 0) { |
| 146 | BPRINT_E("Unable to open HCI%d: %s (%d)", index, strerror(errno), errno); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | dev = &hci_dev_info[index]; |
| 151 | memset(dev, 0, sizeof(*dev)); |
| 152 | |
| 153 | dev->index = index; |
| 154 | dev->skt_fd = skt_fd; |
| 155 | commPort = skt_fd; |
| 156 | |
| 157 | /* HCI_CHANNEL_USER don't need to set filter */ |
| 158 | |
| 159 | /* Start HCI device */ |
| 160 | /* If we use HCI_CHANNEL_RAW, we must be send HCIDEVUP to bluez stack */ |
| 161 | /* |
| 162 | while (ioctl(dd, HCIDEVUP, index) < 0 && errno != EALREADY) { |
| 163 | BPRINT_E("Can't init device HCI%d: %s (%d)", |
| 164 | index, strerror(errno), errno); |
| 165 | sleep(1); |
| 166 | }*/ |
| 167 | return dev; |
| 168 | } |
| 169 | |
| 170 | //--------------------------------------------------------------------------- |
| 171 | static int boots_hci_init_all_devices(void) |
| 172 | { |
| 173 | int skt_fd; |
| 174 | struct hci_dev_list_req *dev_list = NULL; |
| 175 | struct hci_dev_req *dev_req = NULL; |
| 176 | int i, ret = 0; |
| 177 | |
| 178 | /* Create HCI socket to get HCI devices number*/ |
| 179 | skt_fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
| 180 | if (skt_fd < 0) { |
| 181 | BPRINT_E("Can't open HCI socket: %s (%d)", strerror(errno), errno); |
| 182 | ret = -1; |
| 183 | goto exit; |
| 184 | } |
| 185 | BPRINT_D("sock = %d", skt_fd); |
| 186 | |
| 187 | dev_list = (struct hci_dev_list_req *)malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); |
| 188 | if (!dev_list) { |
| 189 | BPRINT_E("Can't allocate devlist buffer"); |
| 190 | ret = -1; |
| 191 | goto exit; |
| 192 | } |
| 193 | dev_req = dev_list->dev_req; |
| 194 | |
| 195 | dev_list->dev_num = HCI_MAX_DEV; |
| 196 | if (ioctl(skt_fd, HCIGETDEVLIST, dev_list) < 0) { |
| 197 | BPRINT_E("Can't get device list: %s (%d)", strerror(errno), errno); |
| 198 | ret = -1; |
| 199 | goto exit; |
| 200 | } |
| 201 | |
| 202 | if (dev_list->dev_num == 0) { |
| 203 | BPRINT_E("No BT device."); |
| 204 | ret = -1; |
| 205 | goto exit; |
| 206 | } |
| 207 | |
| 208 | BPRINT_I("BT dev_num = %d", dev_list->dev_num); |
| 209 | for (i = 0; i < dev_list->dev_num; i++, dev_req++) { |
| 210 | /* only support HCI0 */ |
| 211 | if (dev_req->dev_id >= HCI_MAX_DEV) |
| 212 | continue; |
| 213 | |
| 214 | boots_hci_init_device(dev_req->dev_id); |
| 215 | } |
| 216 | exit: |
| 217 | if (skt_fd >= 0) |
| 218 | close(skt_fd); |
| 219 | if (dev_list) |
| 220 | free(dev_list); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | //--------------------------------------------------------------------------- |
| 225 | static int boots_hci_deinit_device(int index) |
| 226 | { |
| 227 | struct dev_info *dev = NULL; |
| 228 | |
| 229 | dev = &hci_dev_info[index]; |
| 230 | if (dev->skt_fd < 0) |
| 231 | return -1; |
| 232 | |
| 233 | BPRINT_I("Stopping HCI%d socket %d", index, dev->skt_fd); |
| 234 | |
| 235 | boots_hci_close_dev(dev->skt_fd); |
| 236 | memset(dev, 0, sizeof(*dev)); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | //--------------------------------------------------------------------------- |
| 241 | static int boots_hci_deinit_all_devices(void) |
| 242 | { |
| 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < HCI_MAX_DEV; i++) |
| 246 | boots_hci_deinit_device(i); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | //--------------------------------------------------------------------------- |
| 252 | int boots_hci_enable(char *arg, void *func_cb) |
| 253 | { |
| 254 | UNUSED(boots_btif); |
| 255 | UNUSED(arg); |
| 256 | UNUSED(func_cb); |
| 257 | |
| 258 | if (boots_hci_init_all_devices() < 0) { |
| 259 | BPRINT_E("adapter_ops_setup failed"); |
| 260 | return -1; |
| 261 | } |
| 262 | else { |
| 263 | return commPort; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | //--------------------------------------------------------------------------- |
| 268 | int boots_hci_disable(int bt_fd) |
| 269 | { |
| 270 | UNUSED(bt_fd); |
| 271 | |
| 272 | if (commPort >= 0) { |
| 273 | boots_hci_deinit_all_devices(); |
| 274 | commPort = -1; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |