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) 2014,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 <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <unistd.h> |
| 44 | #include <termios.h> |
| 45 | #include <sys/time.h> |
| 46 | #include <sys/select.h> |
| 47 | |
| 48 | #include "boots.h" |
| 49 | #include "boots_osi.h" |
| 50 | |
| 51 | //--------------------------------------------------------------------------- |
| 52 | #define LOG_TAG "boots_uart" |
| 53 | |
| 54 | #define VCOM_PROP_NAME "sys.usb.config" |
| 55 | #define VCOM_PROP_VALUE_TTYGS0 "acm" |
| 56 | #define VCOM_PROP_VALUE_TTYGS2 "acm_third" |
| 57 | |
| 58 | #define CHECK_VCOM_DEV "/sys/class/android_usb/android0/enable" |
| 59 | #ifdef SPECIAL_USB_RELAY |
| 60 | #define OPEN_VCOM_FOR_TTYGS2 "usb_switch adb,bt" |
| 61 | #else |
| 62 | #define OPEN_VCOM_FOR_TTYGS2 \ |
| 63 | "echo > /sys/class/android_usb/android0/enable 0;" \ |
| 64 | "sleep 1;" \ |
| 65 | "echo > /sys/class/android_usb/android0/idVendor 0E8D;" \ |
| 66 | "echo > /sys/class/android_usb/android0/idProduct 2006;" \ |
| 67 | "echo > /sys/class/android_usb/android0/f_acm/port_index 3;" \ |
| 68 | "echo > /sys/class/android_usb/android0/functions mass_storage,adb,acm;" \ |
| 69 | "sleep 1;" \ |
| 70 | "echo > /sys/class/android_usb/android0/enable 1" |
| 71 | #endif |
| 72 | |
| 73 | #define CHECK_VCOM_DEV_FOR_NEW_USB_1 "/sys/kernel/config/usb_gadget/g1/UDC" |
| 74 | #define CHECK_VCOM_DEV_FOR_NEW_USB_2 "/config/usb_gadget/g1/UDC" |
| 75 | #ifdef UDC_SSUSB //for new USB IP SSUSB |
| 76 | #define OPEN_VCOM_FOR_TTYGS2_NEW_USB \ |
| 77 | "echo -n none > /sys/kernel/config/usb_gadget/g1/UDC;" \ |
| 78 | "rm /sys/kernel/config/usb_gadget/g1/configs/c.1/f*;" \ |
| 79 | "sleep 1;" \ |
| 80 | "mount -t configfs none /sys/kernel/config;" \ |
| 81 | "mkdir /sys/kernel/config/usb_gadget/g1;" \ |
| 82 | "echo -n 0x0E8D > /sys/kernel/config/usb_gadget/g1/idVendor;" \ |
| 83 | "echo -n 0x2010 > /sys/kernel/config/usb_gadget/g1/idProduct;" \ |
| 84 | "mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409;" \ |
| 85 | "echo -n 0123456789ABCDEF > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber;" \ |
| 86 | "echo -n Mediatek Inc. > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer;" \ |
| 87 | "echo -n rndis_adb_acm > /sys/kernel/config/usb_gadget/g1/strings/0x409/product;" \ |
| 88 | "mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1;" \ |
| 89 | "mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409;" \ |
| 90 | "echo -n rndis_adb_acm > /sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409/configuration;" \ |
| 91 | "echo -n 400 > /sys/kernel/config/usb_gadget/g1/configs/c.1/MaxPower;" \ |
| 92 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/ffs.adb;" \ |
| 93 | "mkdir /dev/usb-ffs;" \ |
| 94 | "chmod 775 /dev/usb-ffs -R;" \ |
| 95 | "mkdir /dev/usb-ffs/adb;" \ |
| 96 | "chmod 775 /dev/usb-ffs/adb -R;" \ |
| 97 | "mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb;" \ |
| 98 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/rndis.gs4;" \ |
| 99 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/acm.gs0;" \ |
| 100 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/rndis.gs4 /sys/kernel/config/usb_gadget/g1/configs/c.1/f1;" \ |
| 101 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/ffs.adb /sys/kernel/config/usb_gadget/g1/configs/c.1/f2;" \ |
| 102 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/acm.gs0 /sys/kernel/config/usb_gadget/g1/configs/c.1/f3;" \ |
| 103 | "sleep 1;" \ |
| 104 | "echo -n '11201000.usb' > /sys/kernel/config/usb_gadget/g1/UDC;" |
| 105 | #else //for MUSB |
| 106 | #define OPEN_VCOM_FOR_TTYGS2_NEW_USB \ |
| 107 | "echo -n none > /sys/kernel/config/usb_gadget/g1/UDC;" \ |
| 108 | "rm /sys/kernel/config/usb_gadget/g1/configs/c.1/f*;" \ |
| 109 | "sleep 1;" \ |
| 110 | "mount -t configfs none /sys/kernel/config;" \ |
| 111 | "mkdir /sys/kernel/config/usb_gadget/g1;" \ |
| 112 | "echo -n 0x0E8D > /sys/kernel/config/usb_gadget/g1/idVendor;" \ |
| 113 | "echo -n 0x2010 > /sys/kernel/config/usb_gadget/g1/idProduct;" \ |
| 114 | "mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409;" \ |
| 115 | "echo -n 0123456789ABCDEF > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber;" \ |
| 116 | "echo -n Mediatek Inc. > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer;" \ |
| 117 | "echo -n rndis_adb_acm > /sys/kernel/config/usb_gadget/g1/strings/0x409/product;" \ |
| 118 | "mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1;" \ |
| 119 | "mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409;" \ |
| 120 | "echo -n rndis_adb_acm > /sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409/configuration;" \ |
| 121 | "echo -n 400 > /sys/kernel/config/usb_gadget/g1/configs/c.1/MaxPower;" \ |
| 122 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/ffs.adb;" \ |
| 123 | "mkdir /dev/usb-ffs;" \ |
| 124 | "chmod 775 /dev/usb-ffs -R;" \ |
| 125 | "mkdir /dev/usb-ffs/adb;" \ |
| 126 | "chmod 775 /dev/usb-ffs/adb -R;" \ |
| 127 | "mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb;" \ |
| 128 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/rndis.gs4;" \ |
| 129 | "mkdir /sys/kernel/config/usb_gadget/g1/functions/acm.gs0;" \ |
| 130 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/rndis.gs4 /sys/kernel/config/usb_gadget/g1/configs/c.1/f1;" \ |
| 131 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/ffs.adb /sys/kernel/config/usb_gadget/g1/configs/c.1/f2;" \ |
| 132 | "ln -s /sys/kernel/config/usb_gadget/g1/functions/acm.gs0 /sys/kernel/config/usb_gadget/g1/configs/c.1/f3;" \ |
| 133 | "sleep 1;" \ |
| 134 | "echo -n musb-hdrc > /sys/kernel/config/usb_gadget/g1/UDC;" |
| 135 | #endif |
| 136 | |
| 137 | //--------------------------------------------------------------------------- |
| 138 | static int boots_uart_speed(int speed) |
| 139 | { |
| 140 | switch (speed) { |
| 141 | case 9600: |
| 142 | return B9600; |
| 143 | case 19200: |
| 144 | return B19200; |
| 145 | case 38400: |
| 146 | return B38400; |
| 147 | case 57600: |
| 148 | return B57600; |
| 149 | case 115200: |
| 150 | return B115200; |
| 151 | case 230400: |
| 152 | return B230400; |
| 153 | case 460800: |
| 154 | return B460800; |
| 155 | case 500000: |
| 156 | return B500000; |
| 157 | case 576000: |
| 158 | return B576000; |
| 159 | case 921600: |
| 160 | return B921600; |
| 161 | } |
| 162 | return B57600; |
| 163 | } |
| 164 | |
| 165 | //--------------------------------------------------------------------------- |
| 166 | int boots_uart_init(char *dev, int speed) |
| 167 | { |
| 168 | UNUSED(boots_btif); |
| 169 | struct termios ti; |
| 170 | int fd, retry = 0; |
| 171 | int baudenum; |
| 172 | #ifndef VCOM_OPENED |
| 173 | char usb_prop[128]; |
| 174 | char *prop_value = NULL; |
| 175 | bool set_vcom_cmd = false; |
| 176 | #endif |
| 177 | |
| 178 | memset(&ti, 0x0, sizeof(ti)); |
| 179 | /* USB VCOM port */ |
| 180 | if (0 == speed) { |
| 181 | #ifndef VCOM_OPENED |
| 182 | /* Set USB property to acm_third: add 1 acm port to /dev/ttyGS2 */ |
| 183 | if (!memcmp(dev, "/dev/ttyGS0", strlen("/dev/ttyGS0"))) |
| 184 | prop_value = VCOM_PROP_VALUE_TTYGS0; |
| 185 | else if (!memcmp(dev, "/dev/ttyGS2", strlen("/dev/ttyGS2"))) |
| 186 | prop_value = VCOM_PROP_VALUE_TTYGS2; |
| 187 | else { |
| 188 | BPRINT_E("%s do not support VCOM port", dev); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | if (osi_property_get(VCOM_PROP_NAME, usb_prop, NULL) < 0) { |
| 193 | set_vcom_cmd = true; |
| 194 | } else { |
| 195 | if (strcmp(usb_prop, prop_value)) { |
| 196 | if (osi_property_set(VCOM_PROP_NAME, prop_value) < 0) |
| 197 | set_vcom_cmd = true; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (set_vcom_cmd == true) { |
| 202 | #ifndef SPECIAL_USB_RELAY |
| 203 | if (access(CHECK_VCOM_DEV, F_OK) == 0) { |
| 204 | #endif |
| 205 | if (osi_system(OPEN_VCOM_FOR_TTYGS2) < 0) { |
| 206 | BPRINT_E("Open default VCOM failed!"); |
| 207 | return -1; |
| 208 | } else { |
| 209 | BPRINT_I("Open default VCOM successfully"); |
| 210 | } |
| 211 | #ifndef SPECIAL_USB_RELAY |
| 212 | } else { |
| 213 | BPRINT_I("Use new usb"); |
| 214 | } |
| 215 | |
| 216 | if (access(CHECK_VCOM_DEV_FOR_NEW_USB_1, F_OK) == 0 || |
| 217 | access(CHECK_VCOM_DEV_FOR_NEW_USB_2, F_OK) == 0) { |
| 218 | if (osi_system(OPEN_VCOM_FOR_TTYGS2_NEW_USB) < 0) { |
| 219 | BPRINT_E("Open default VCOM failed!"); |
| 220 | return -1; |
| 221 | } else { |
| 222 | BPRINT_I("Open default VCOM successfully"); |
| 223 | } |
| 224 | } |
| 225 | #endif |
| 226 | } |
| 227 | #endif |
| 228 | } |
| 229 | |
| 230 | while(1) { |
| 231 | sleep(1); |
| 232 | fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 233 | if (fd >= 0) |
| 234 | break; |
| 235 | if (retry > 10) { |
| 236 | BPRINT_E("Can't open serial port %s", dev); |
| 237 | return -1; |
| 238 | } |
| 239 | retry++; |
| 240 | } |
| 241 | |
| 242 | if (0 != speed) { |
| 243 | ti.c_cflag |= CLOCAL; |
| 244 | ti.c_lflag = 0; |
| 245 | |
| 246 | ti.c_cflag &= ~CRTSCTS; |
| 247 | ti.c_iflag &= ~(IXON | IXOFF | IXANY); |
| 248 | |
| 249 | /* Set baudrate */ |
| 250 | baudenum = boots_uart_speed(speed); |
| 251 | if ((baudenum == B57600) && (speed != 57600)) { |
| 252 | BPRINT_E("Serial port baudrate not supported!"); |
| 253 | close(fd); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | cfsetospeed(&ti, baudenum); |
| 258 | cfsetispeed(&ti, baudenum); |
| 259 | } |
| 260 | |
| 261 | tcflush(fd, TCIOFLUSH); |
| 262 | |
| 263 | if (tcgetattr(fd, &ti) < 0) { |
| 264 | BPRINT_E("Can't get serial port setting"); |
| 265 | close(fd); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | cfmakeraw(&ti); |
| 270 | |
| 271 | sleep(1); |
| 272 | |
| 273 | if (tcsetattr(fd, TCSANOW, &ti) < 0) { |
| 274 | BPRINT_E("Can't set serial port setting"); |
| 275 | close(fd); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | tcflush(fd, TCIOFLUSH); |
| 280 | |
| 281 | return fd; |
| 282 | } |
| 283 | |
| 284 | //--------------------------------------------------------------------------- |
| 285 | static ssize_t uart_read(int fd, uint8_t *buf, size_t len) |
| 286 | { |
| 287 | ssize_t bytesRead = 0; |
| 288 | size_t bytesToRead = len; |
| 289 | int ret = 0; |
| 290 | struct timeval tv; |
| 291 | fd_set readfd; |
| 292 | |
| 293 | tv.tv_sec = 1; /* SECOND */ |
| 294 | tv.tv_usec = 0; /* USECOND */ |
| 295 | FD_ZERO(&readfd); |
| 296 | |
| 297 | if (fd < 0) |
| 298 | return -1; |
| 299 | |
| 300 | /* Try to receive len bytes */ |
| 301 | while (bytesToRead > 0) { |
| 302 | FD_SET(fd, &readfd); |
| 303 | |
| 304 | ret = select(fd + 1, &readfd, NULL, NULL, &tv); |
| 305 | if (ret > 0) { |
| 306 | bytesRead = read(fd, buf, bytesToRead); |
| 307 | if (bytesRead < 0) { |
| 308 | if (errno == EINTR || errno == EAGAIN) |
| 309 | continue; |
| 310 | else |
| 311 | return -1; |
| 312 | } else { |
| 313 | bytesToRead -= bytesRead; |
| 314 | buf += bytesRead; |
| 315 | } |
| 316 | } else if (ret == 0) { |
| 317 | /* Read com port timeout */ |
| 318 | return -1; |
| 319 | } else if ((ret == -1) && (errno == EINTR)) { |
| 320 | continue; |
| 321 | } else { |
| 322 | return -1; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return (len - bytesToRead); |
| 327 | } |
| 328 | |
| 329 | //--------------------------------------------------------------------------- |
| 330 | ssize_t boots_uart_write(int fd, uint8_t const *buf, size_t len) |
| 331 | { |
| 332 | ssize_t ret = 0; |
| 333 | size_t bytesToWrite = len; |
| 334 | |
| 335 | if (buf == NULL) { |
| 336 | BPRINT_E("%s: buffer is NULL!", __func__); |
| 337 | return -EINVAL; |
| 338 | } else if (fd < 0) { |
| 339 | BPRINT_E("%s: File descriptor in bad state(%d)!", __func__, fd); |
| 340 | return -EBADFD; |
| 341 | } |
| 342 | |
| 343 | while (bytesToWrite > 0) { |
| 344 | ret = write(fd, buf, bytesToWrite); |
| 345 | if (ret < 0) { |
| 346 | if (errno == EINTR || errno == EAGAIN) |
| 347 | continue; |
| 348 | else |
| 349 | return -errno; |
| 350 | } |
| 351 | bytesToWrite -= ret; |
| 352 | buf += ret; |
| 353 | } |
| 354 | |
| 355 | return (len - bytesToWrite); |
| 356 | } |
| 357 | |
| 358 | //--------------------------------------------------------------------------- |
| 359 | ssize_t boots_uart_read(int fd, uint8_t *buf, size_t size) |
| 360 | { |
| 361 | uint8_t ucHeader = 0; |
| 362 | size_t u4Len = 0, pkt_len = 0; |
| 363 | int count = 0; |
| 364 | size_t len = size; |
| 365 | |
| 366 | if (buf == NULL) { |
| 367 | BPRINT_E("%s: buffer is NULL", __func__); |
| 368 | return -EINVAL; |
| 369 | } else if (fd < 0) { |
| 370 | BPRINT_E("%s: File descriptor in bad state(%d)!", __func__, fd); |
| 371 | return -EBADFD; |
| 372 | } |
| 373 | |
| 374 | LOOP: |
| 375 | if (uart_read(fd, &ucHeader, sizeof(ucHeader)) < 0) { |
| 376 | count ++; |
| 377 | if (count < 3) { |
| 378 | goto LOOP; |
| 379 | } else { |
| 380 | return -2; |
| 381 | } |
| 382 | } |
| 383 | buf[0] = ucHeader; |
| 384 | u4Len++; |
| 385 | |
| 386 | switch (ucHeader) { |
| 387 | case 0x01: /* HCI command */ |
| 388 | if (uart_read(fd, buf + u4Len, 3) < 0) { |
| 389 | BPRINT_E("Read command header fails"); |
| 390 | return -3; |
| 391 | } |
| 392 | u4Len += 3; |
| 393 | pkt_len = (size_t)buf[3]; |
| 394 | if ((u4Len + pkt_len) > len) { |
| 395 | BPRINT_E("Too large packet from UART! packet len %d", (int)(u4Len + pkt_len)); |
| 396 | return -4; |
| 397 | } |
| 398 | |
| 399 | if (uart_read(fd, buf + u4Len, pkt_len) < 0) { |
| 400 | BPRINT_E("Read command param fails"); |
| 401 | return -5; |
| 402 | } |
| 403 | u4Len += pkt_len; |
| 404 | break; |
| 405 | |
| 406 | case 0x02: /* ACL data */ |
| 407 | if (uart_read(fd, buf + u4Len, 4) < 0) { |
| 408 | BPRINT_E("Read ACL header fails"); |
| 409 | return -6; |
| 410 | } |
| 411 | u4Len += 4; |
| 412 | /* length 2 bytes is little endian */ |
| 413 | pkt_len = (((size_t)buf[4]) << 8); |
| 414 | pkt_len += (size_t)buf[3]; |
| 415 | if ((u4Len + pkt_len) > len) { |
| 416 | BPRINT_E("Too large packet from UART! packet len %d", (int)(u4Len + pkt_len)); |
| 417 | return -7; |
| 418 | } |
| 419 | |
| 420 | if (uart_read(fd, buf + u4Len, pkt_len) < 0) { |
| 421 | BPRINT_E("Read ACL data fails"); |
| 422 | return -8; |
| 423 | } |
| 424 | u4Len += pkt_len; |
| 425 | break; |
| 426 | |
| 427 | case 0x03: /* SCO data */ |
| 428 | if (uart_read(fd, buf + u4Len, 3) < 0) { |
| 429 | BPRINT_E("Read SCO header fails"); |
| 430 | return -9; |
| 431 | } |
| 432 | u4Len += 3; |
| 433 | pkt_len = (size_t)buf[3]; |
| 434 | if ((u4Len + pkt_len) > len) { |
| 435 | BPRINT_E("Too large packet from UART! packet len %d", (int)(u4Len + pkt_len)); |
| 436 | return -10; |
| 437 | } |
| 438 | |
| 439 | if (uart_read(fd, buf + u4Len, pkt_len) < 0) { |
| 440 | BPRINT_E("Read SCO data fails"); |
| 441 | return -11; |
| 442 | } |
| 443 | u4Len += pkt_len; |
| 444 | break; |
| 445 | |
| 446 | case 0x04: /* Event */ |
| 447 | if (uart_read(fd, buf + u4Len, 2) < 0) { |
| 448 | BPRINT_E("Read Event header fails"); |
| 449 | return -12; |
| 450 | } |
| 451 | u4Len += 2; |
| 452 | pkt_len = (size_t)buf[2]; |
| 453 | if ((u4Len + pkt_len) > len) { |
| 454 | BPRINT_E("Too large packet from UART! packet len %d", (int)(u4Len + pkt_len)); |
| 455 | return -13; |
| 456 | } |
| 457 | |
| 458 | if (uart_read(fd, buf + u4Len, pkt_len) < 0) { |
| 459 | BPRINT_E("Read Event data fails"); |
| 460 | return -14; |
| 461 | } |
| 462 | u4Len += pkt_len; |
| 463 | break; |
| 464 | |
| 465 | default: /* Filter UART garbage data */ |
| 466 | BPRINT_E("Invalid packet type %02x from UART", ucHeader); |
| 467 | return -15; |
| 468 | } |
| 469 | |
| 470 | return u4Len; |
| 471 | } |
| 472 | |
| 473 | //--------------------------------------------------------------------------- |