1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
|
/*
MIT License
Copyright (c) 2022 Rajaram Regupathy <rajaram.regupathy@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// SPDX-License-Identifier: MIT
/**
* @file libtypec_sysfs_ops.c
* @author Rajaram Regupathy <rajaram.regupathy@gmail.com>
* @brief Functions for libtypec sysfs based operations
*/
/**
* required for enalbing nftw(), which is part of SUSv1.
*/
#define _XOPEN_SOURCE 500
#include "libtypec_ops.h"
#include <dirent.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>
#include <ftw.h>
#include <fcntl.h>
#include <unistd.h>
#include <libudev.h>
#define MAX_PORT_STR 7 /* port%d with 7 bit numPorts */
#define MAX_PORT_MODE_STR 7 /* port%d with 5+2 bit numPorts */
#define OS_TYPE_CHROME 1
static int num_bb_if;
#define MAX_BB_PATH_STORED 4
char bb_dev_path[MAX_BB_PATH_STORED][512];
static int get_os_type(void)
{
FILE *fp = fopen("/etc/os-release", "r");
char buf[128], *p = NULL;
if (fp)
{
while (fgets(buf, 128, fp))
{
p = strstr(buf, "chrome");
if (p)
{
fclose(fp);
return OS_TYPE_CHROME;
}
}
fclose(fp);
}
return 0;
}
static unsigned long get_hex_dword_from_path(char *path)
{
char buf[64];
unsigned long dword;
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
else
{
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
dword = strtol(buf, NULL, 16);
fclose(fp);
}
return dword;
}
static unsigned long get_dword_from_path(char *path)
{
char buf[64];
unsigned long dword=0;
FILE *fp = fopen(path, "r");
if (fp)
{
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
dword = strtoul(buf, NULL, 10);
fclose(fp);
}
return dword;
}
unsigned char get_opr_mode(char *path)
{
char buf[64];
char *pEnd;
short ret = OPR_MODE_RD_ONLY; /*Rd sink*/
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
pEnd = strstr(buf, "source");
if (pEnd != NULL)
{
pEnd = strstr(buf, "sink");
if (pEnd != NULL)
ret = OPR_MODE_DRP_ONLY; /*DRP*/
else
ret = OPR_MODE_RP_ONLY; /*Rp only*/
}
fclose(fp);
return ret;
}
static short get_bcd_from_rev_file(char *path)
{
char buf[10];
short bcd = 0;
FILE *fp = fopen(path, "r");
if (fp)
{
if (fgets(buf, 10, fp) == NULL)
{
fclose(fp);
return -1;
}
bcd = ((buf[0] - '0') << 8) | ((buf[2] - '0') << 4);
fclose(fp);
}
return bcd;
}
static int get_pd_rev(char *path)
{
char buf[10];
int rev = 0;
FILE *fp = fopen(path, "r");
if (fp)
{
if (fgets(buf, 10, fp) == NULL)
{
fclose(fp);
return -1;
}
rev = ((buf[0] - '0') << 8 ) | (buf[2] - '0');
fclose(fp);
}
return rev;
}
static int get_cable_plug_type(char *path)
{
char buf[64];
char *pEnd;
short ret = PLUG_TYPE_OTH; /*not USB*/
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
pEnd = strstr(buf, "type-c");
if (pEnd == NULL)
{
pEnd = strstr(buf, "type-a");
if (pEnd == NULL)
{
pEnd = strstr(buf, "type-b");
if (pEnd == NULL)
{
ret = PLUG_TYPE_OTH;
}
else
{
ret = PLUG_TYPE_B;
}
}
else
ret = PLUG_TYPE_A;
}
else
ret = PLUG_TYPE_C;
fclose(fp);
return ret;
}
static int get_cable_type(char *path)
{
char buf[64];
char *pEnd;
short ret = CABLE_TYPE_PASSIVE;
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
pEnd = strstr(buf, "passive");
if (pEnd == NULL)
{
pEnd = strstr(buf, "active");
if (pEnd == NULL)
ret = CABLE_TYPE_UNKNOWN;
else
ret = CABLE_TYPE_ACTIVE;
}
fclose(fp);
return ret;
}
static int get_cable_mode_support(char *path)
{
char buf[64];
short ret;
FILE *fp = fopen(path, "r");
if (fp == NULL)
return -1;
if (fgets(buf, 64, fp) == NULL)
{
fclose(fp);
return -1;
}
ret = (buf[0] - '0') ? 1 : 0;
fclose(fp);
return ret;
}
static unsigned int get_variable_supply_pdo(char *path, int src_snk)
{
char path_str[512], port_content[1024];
union libtypec_variable_supply_src var_src;
unsigned int tmp;
var_src.obj_var_sply.type = PDO_VARIABLE;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_voltage");
tmp = get_dword_from_path(port_content);
var_src.obj_var_sply.max_volt = tmp/50;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "minimum_voltage");
tmp = get_dword_from_path(port_content);
var_src.obj_var_sply.min_volt = tmp/50;
if(src_snk)
{
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_current");
tmp = get_dword_from_path(port_content);
var_src.obj_var_sply.max_cur = tmp/10;
}
else
{
snprintf(port_content, sizeof(port_content), "%s/%s", path, "operational_current");
tmp = get_dword_from_path(port_content);
var_src.obj_var_sply.max_cur = tmp/10;
}
return var_src.variable_supply;
}
static unsigned int get_battery_supply_pdo(char *path, int src_snk)
{
char path_str[512], port_content[512 + 512];
union libtypec_battery_supply_src bat_src;
unsigned int tmp;
bat_src.obj_bat_sply.type = 2;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_voltage");
tmp = get_dword_from_path(port_content);
bat_src.obj_bat_sply.max_volt = tmp/50;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "minimum_voltage");
tmp = get_dword_from_path(port_content);
bat_src.obj_bat_sply.min_volt = tmp/50;
if(src_snk)
{
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_power");
tmp = get_dword_from_path(port_content);
bat_src.obj_bat_sply.max_pwr = tmp/250;
}
else
{
snprintf(port_content, sizeof(port_content), "%s/%s", path, "operational_power");
tmp = get_dword_from_path(port_content);
bat_src.obj_bat_sply.max_pwr = tmp/250;
}
return bat_src.battery_supply;
}
static unsigned int get_programmable_supply_pdo(char *path, int src_snk)
{
char path_str[512], port_content[512 + 512];
union libtypec_pps_src pps_src={0};
unsigned int tmp;
pps_src.obj_pps_sply.type = 3;
if(src_snk)
{
snprintf(port_content, sizeof(port_content), "%s/%s", path, "pps_power_limited");
pps_src.obj_pps_sply.pwr_ltd = get_dword_from_path(port_content);
}
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_voltage");
tmp = get_dword_from_path(port_content);
pps_src.obj_pps_sply.max_volt = tmp/100;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "minimum_voltage");
tmp = get_dword_from_path(port_content);
pps_src.obj_pps_sply.min_volt = tmp/100;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_current");
tmp = get_dword_from_path(port_content);
pps_src.obj_pps_sply.max_cur = tmp/50;
return pps_src.spr_pps_supply;
}
static unsigned int get_fixed_supply_pdo(char *path, int src_snk)
{
char path_str[512], port_content[512 + 512];
union libtypec_fixed_supply_src fxd_src;
union libtypec_fixed_supply_snk fxd_snk;
unsigned int tmp;
memset(&fxd_src, 0, sizeof(fxd_src));
memset(&fxd_snk, 0, sizeof(fxd_snk));
if(src_snk)
{
fxd_src.obj_fixed_sply.type = 0;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "dual_role_power");
fxd_src.obj_fixed_sply.dual_pwr = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "usb_suspend_supported");
fxd_src.obj_fixed_sply.usb_suspend = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "unconstrained_power");
fxd_src.obj_fixed_sply.uncons_pwr = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "usb_communication_capable");
fxd_src.obj_fixed_sply.usb_comm = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "dual_role_data");
fxd_src.obj_fixed_sply.drd = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "unchunked_extended_messages_supported");
fxd_src.obj_fixed_sply.unchunked = get_dword_from_path(port_content);
fxd_src.obj_fixed_sply.epr = 0;
fxd_src.obj_fixed_sply.peak_cur = 0;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "voltage");
tmp = get_dword_from_path(port_content);
fxd_src.obj_fixed_sply.volt = tmp/50;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "maximum_current");
tmp = get_dword_from_path(port_content);
fxd_src.obj_fixed_sply.max_cur = tmp/10;
return fxd_src.fixed_supply;
}
else
{
fxd_snk.obj_fixed_supply.type = 0;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "dual_role_power");
fxd_snk.obj_fixed_supply.drp = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "higher_capability");
fxd_snk.obj_fixed_supply.higher_caps = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "unconstrained_power");
fxd_snk.obj_fixed_supply.uncons_pwr = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "usb_communication_capable");
fxd_snk.obj_fixed_supply.usb_comm_cap = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "dual_role_data");
fxd_snk.obj_fixed_supply.drd = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "fast_role_swap_current");
fxd_snk.obj_fixed_supply.fr_swp = get_dword_from_path(port_content);
snprintf(port_content, sizeof(port_content), "%s/%s", path, "voltage");
tmp = get_dword_from_path(port_content);
fxd_snk.obj_fixed_supply.volt = tmp/50;
snprintf(port_content, sizeof(port_content), "%s/%s", path, "operational_current");
tmp = get_dword_from_path(port_content);
fxd_snk.obj_fixed_supply.opr_cur = tmp/10;
return fxd_snk.fixed_supply;
}
}
static int count_billbrd_if(const char *usb_path, const struct stat *sb, int typeflag, struct FTW *ftw)
{
FILE *fd;
union {
char buf[256];
struct usb_interface_descriptor intf;
} u_usb_if;
if (typeflag != FTW_F)
return 0;
fd = fopen(usb_path, "rb");
if (!fd) {
return -EIO;
}
for (;;) {
if (fread(u_usb_if.buf, 1, 1, fd) != 1)
break;
if (fread(u_usb_if.buf + 1, (unsigned char)u_usb_if.buf[0] - 1, 1, fd) != 1)
break;
if (u_usb_if.intf.bLength == sizeof u_usb_if.intf
&& u_usb_if.intf.bDescriptorType == USB_DT_INTERFACE
&& u_usb_if.intf.bNumEndpoints == 0
&& u_usb_if.intf.bInterfaceClass == 0x11
&& u_usb_if.intf.bInterfaceSubClass == 0
&& u_usb_if.intf.bInterfaceProtocol == 0)
{
if(num_bb_if < MAX_BB_PATH_STORED)
{
int len = strlen(usb_path);
if(len > 512 ) { /*exceeds buffer size*/
fclose(fd);
return 0;
}
strcpy(bb_dev_path[num_bb_if],usb_path);
}
num_bb_if++;
}
}
fclose(fd);
return 0;
}
static int read_bb_bos_descriptor(int num_billboards,char * bb_data)
{
int fd1 = open(bb_dev_path[num_billboards-1],O_RDWR );
if(fd1 < 0)
return -errno;
int len,ret;
struct usbdevfs_ctrltransfer msg;
msg.bRequestType = 0x80;
msg.bRequest = 6;
msg.wValue = 15 << 8;
msg.wIndex = 0;
msg.wLength = 5;
msg.data = bb_data;
msg.timeout = 5000;
ret = ioctl(fd1,USBDEVFS_CONTROL,&msg);
len = (bb_data[3] << 8 | bb_data[2]);
memset(&msg,0,sizeof(struct usbdevfs_ctrltransfer));
memset(bb_data,0,512);
msg.bRequestType = 0x80;
msg.bRequest = 6;
msg.wValue = 15 << 8;
msg.wIndex = 0;
msg.wLength = len;
msg.data = bb_data;
msg.timeout = 5000;
ret = ioctl(fd1,USBDEVFS_CONTROL,&msg);
close(fd1);
return ret;
}
static int libtypec_sysfs_init(char **session_info)
{
return 0;
}
static int libtypec_sysfs_exit(void)
{
return 0;
}
static int libtypec_sysfs_get_capability_ops(struct libtypec_capability_data *cap_data)
{
DIR *typec_path = opendir(SYSFS_TYPEC_PATH), *port_path;
struct dirent *typec_entry, *port_entry;
int num_ports = 0, num_alt_mode = 0, num_port_alt = 0;
char path_str[512], port_content[512 + 64];
if (!typec_path)
{
printf("opendir typec class failed, %s", SYSFS_TYPEC_PATH);
return -1;
}
while ((typec_entry = readdir(typec_path)))
{
if (!(strncmp(typec_entry->d_name, "port", 4)) && (strlen(typec_entry->d_name) <= MAX_PORT_STR))
{
num_ports++;
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/%s", typec_entry->d_name);
/*Scan the port capability*/
port_path = opendir(path_str);
num_port_alt = 0;
while ((port_entry = readdir(port_path)))
{
if (!(strncmp(port_entry->d_name, "port", 4)) && (strlen(port_entry->d_name) <= MAX_PORT_MODE_STR))
{
num_port_alt++;
}
}
/*Counting different alt modes supported by the PPM*/
if(num_alt_mode < num_port_alt)
num_alt_mode = num_port_alt;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "usb_power_delivery_revision");
cap_data->bcdPDVersion = get_bcd_from_rev_file(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "usb_typec_revision");
cap_data->bcdTypeCVersion = get_bcd_from_rev_file(port_content);
closedir(port_path);
}
}
cap_data->bNumConnectors = num_ports;
cap_data->bNumAltModes = num_alt_mode;
closedir(typec_path);
return 0;
}
static int libtypec_sysfs_get_conn_capability_ops(int conn_num, struct libtypec_connector_cap_data *conn_cap_data)
{
struct stat sb;
struct dirent *port_entry;
char path_str[512], port_content[512 + 64];
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d", conn_num);
if (lstat(path_str, &sb) == -1)
{
printf("Incorrect connector number : failed to open, %s\n", path_str);
return -1;
}
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "power_role");
conn_cap_data->opr_mode.raw_operationmode = get_opr_mode(port_content);
if (conn_cap_data->opr_mode.raw_operationmode == OPR_MODE_DRP_ONLY)
{
conn_cap_data->provider = 1;
conn_cap_data->consumer = 1;
}
else if (conn_cap_data->opr_mode.raw_operationmode == OPR_MODE_RD_ONLY)
conn_cap_data->consumer = 1;
else
conn_cap_data->provider = 1;
conn_cap_data->opr_mode.raw_operationmode = 1 << conn_cap_data->opr_mode.raw_operationmode;
if (get_os_type() == OS_TYPE_CHROME)
{
snprintf(port_content, sizeof(port_content), "%s/port%d-partner/%s", path_str, conn_num, "usb_power_delivery_revision");
conn_cap_data->partner_pd_rev = get_pd_rev(port_content);
}
return 0;
}
static int libtypec_sysfs_get_alternate_modes(int recipient, int conn_num, struct altmode_data *alt_mode_data)
{
struct stat sb;
int num_alt_mode = 0;
char path_str[512], port_content[512 + 64];
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d", conn_num);
if (lstat(path_str, &sb) == -1)
{
printf("Incorrect connector number : failed to open, %s\n", path_str);
return -1;
}
if (recipient == AM_CONNECTOR)
{
do
{
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d/port%d.%d", conn_num, conn_num, num_alt_mode);
if (lstat(path_str, &sb) == -1)
break;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "svid");
alt_mode_data[num_alt_mode].svid = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "vdo");
alt_mode_data[num_alt_mode].vdo = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
memset(path_str, 0, sizeof(path_str));
num_alt_mode++;
} while (1);
}
else if (recipient == AM_SOP)
{
do
{
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d/port%d-partner/port%d-partner.%d", conn_num, conn_num, conn_num, num_alt_mode);
if (lstat(path_str, &sb) == -1)
break;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "svid");
alt_mode_data[num_alt_mode].svid = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "vdo");
alt_mode_data[num_alt_mode].vdo = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
memset(path_str, 0, sizeof(path_str));
num_alt_mode++;
} while (1);
}
else if (recipient == AM_SOP_PR)
{
do
{
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-cable/port%d-plug0/port%d-plug0.%d", conn_num, conn_num, conn_num, num_alt_mode);
if (lstat(path_str, &sb) == -1)
break;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "svid");
alt_mode_data[num_alt_mode].svid = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "vdo");
alt_mode_data[num_alt_mode].vdo = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
memset(path_str, 0, sizeof(path_str));
num_alt_mode++;
} while (1);
}
else
{
}
return num_alt_mode;
}
static int libtypec_sysfs_get_cable_properties_ops(int conn_num, struct libtypec_cable_property *cbl_prop_data)
{
struct stat sb;
struct dirent *port_entry;
char path_str[512], port_content[512 + 64];
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-cable", conn_num);
/* No cable identified or connector number is incorrect */
if (lstat(path_str, &sb) == -1)
return -1;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "plug_type");
cbl_prop_data->plug_end_type = get_cable_plug_type(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "type");
cbl_prop_data->cable_type = get_cable_type(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), SYSFS_TYPEC_PATH "/port%d-plug0/%s", conn_num, "number_of_alternate_modes");
cbl_prop_data->mode_support = get_cable_mode_support(port_content);
return 0;
}
static int libtypec_sysfs_get_connector_status_ops(int conn_num, struct libtypec_connector_status *conn_sts)
{
struct stat sb;
struct dirent *port_entry;
char path_str[512], port_content[512 + 64];
int ret;
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d", conn_num);
if (lstat(path_str, &sb) == -1)
{
printf("Incorrect connector number : failed to open, %s\n", path_str);
return -1;
}
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d/port%d-partner", conn_num, conn_num);
conn_sts->ConnectStatus = (lstat(path_str, &sb) == -1) ? 0 : 1;
snprintf(path_str, sizeof(path_str), SYSFS_PSY_PATH "/ucsi-source-psy-USBC000:00%d", conn_num + 1);
if (lstat(path_str, &sb) == -1)
{
printf("Non UCSI based Type-C connector Class - PSY not supported\n:%s\n", path_str);
return 0;
}
else
{
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "online");
ret = get_hex_dword_from_path(port_content);
if (ret)
{
unsigned long cur, volt, op_mw, max_mw;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "current_now");
cur = get_dword_from_path(port_content) / 1000;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "voltage_now");
volt = get_dword_from_path(port_content) / 1000;
op_mw = (cur * volt) / (250 * 1000);
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "current_max");
cur = get_dword_from_path(port_content) / 1000;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "voltage_max");
volt = get_dword_from_path(port_content) / 1000;
max_mw = (cur * volt) / (250 * 1000);
conn_sts->RequestDataObject = ((op_mw << 10)) | (max_mw)&0x3FF;
}
}
return 0;
}
static int libtypec_sysfs_get_discovered_identity_ops(int recipient, int conn_num, char *pd_resp_data)
{
struct stat sb;
char path_str[512], port_content[512 + 64];
union libtypec_discovered_identity *id = (void *)pd_resp_data;
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d", conn_num);
if (lstat(path_str, &sb) == -1)
{
printf("Incorrect connector number : failed to open, %s\n", path_str);
return -1;
}
if (recipient == AM_SOP)
{
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-partner/identity", conn_num);
if (lstat(path_str, &sb) == -1)
return -1;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "cert_stat");
id->disc_id.cert_stat = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "id_header");
id->disc_id.id_header = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product");
id->disc_id.product = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo1");
id->disc_id.product_type_vdo1 = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo2");
id->disc_id.product_type_vdo2 = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo3");
id->disc_id.product_type_vdo3 = get_hex_dword_from_path(port_content);
}
else if (recipient == AM_SOP_PR)
{
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-cable/identity", conn_num);
if (lstat(path_str, &sb) == -1)
return -1;
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "cert_stat");
id->disc_id.cert_stat = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "id_header");
id->disc_id.id_header = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product");
id->disc_id.product = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo1");
id->disc_id.product_type_vdo1 = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo2");
id->disc_id.product_type_vdo2 = get_hex_dword_from_path(port_content);
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str, "product_type_vdo3");
id->disc_id.product_type_vdo3 = get_hex_dword_from_path(port_content);
}
return 0;
}
static int libtypec_sysfs_get_pd_message_ops(int recipient, int conn_num, int num_bytes, int resp_type, char *pd_msg_resp)
{
if (resp_type == DISCOVER_ID_REQ)
{
return libtypec_sysfs_get_discovered_identity_ops(recipient, conn_num, pd_msg_resp);
}
return 0;
}
static int libtypec_sysfs_get_pdos_ops(int conn_num, int partner, int offset, int *num_pdo, int src_snk, int type, struct libtypec_get_pdos *pdo_data)
{
int num_pdos_read = 0;
char path_str[512], port_content[512 + 256];
DIR *typec_path , *port_path;
struct dirent *typec_entry, *port_entry;
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d", conn_num);
typec_path = opendir(path_str);
if (typec_path == NULL)
{
printf("Incorrect connector number : failed to open, %s\n", path_str);
return -1;
}
closedir(typec_path);
if (partner == 0)
{
if(src_snk)
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d/usb_power_delivery/source-capabilities", conn_num);
else
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d/usb_power_delivery/sink-capabilities", conn_num);
}
else
{
if(src_snk)
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-partner/usb_power_delivery/source-capabilities", conn_num);
else
snprintf(path_str, sizeof(path_str), SYSFS_TYPEC_PATH "/port%d-partner/usb_power_delivery/sink-capabilities", conn_num);
}
typec_path = opendir(path_str);
if (typec_path == NULL)
goto finalize; /*No PDOs*/
while ((typec_entry = readdir(typec_path)))
{
memset(port_content, 0, sizeof(port_content));
snprintf(port_content, sizeof(port_content), "%s/%s", path_str,typec_entry->d_name);
if(strstr(typec_entry->d_name, "fixed"))
{
pdo_data->pdo[num_pdos_read++] = get_fixed_supply_pdo(port_content,src_snk);
}
else if(strstr(typec_entry->d_name, "variable"))
{
pdo_data->pdo[num_pdos_read++] = get_variable_supply_pdo(port_content,src_snk);
}
else if(strstr(typec_entry->d_name, "battery"))
{
pdo_data->pdo[num_pdos_read++] = get_battery_supply_pdo(port_content,src_snk);
}
else if(strstr(typec_entry->d_name, "programmable"))
{
pdo_data->pdo[num_pdos_read++] = get_programmable_supply_pdo(port_content,src_snk);
}
}
closedir(typec_path);
finalize:
*num_pdo = num_pdos_read;
return num_pdos_read;
}
static int libtypec_sysfs_get_bb_status(unsigned int *num_bb_instance)
{
num_bb_if = 0;
int fd_limit = getdtablesize() - 5;
if (nftw ("/dev/bus/usb/", count_billbrd_if, fd_limit, 0) != 0)
{
return -EIO;
}
*num_bb_instance = num_bb_if;
return 0;
}
static int libtypec_sysfs_get_bb_data(int num_billboards,char* bb_data)
{
int ret = 0, count;
ret = libtypec_sysfs_get_bb_status(&count);
if(num_billboards >count)
return -EINVAL;
ret = read_bb_bos_descriptor(num_billboards,bb_data);
return ret;
}
void libtypec_lnx_monitor_udev_events() {
struct udev *udev = udev_new();
struct udev_monitor *mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "typec", NULL);
udev_monitor_enable_receiving(mon);
while (1) {
struct udev_device *dev = udev_monitor_receive_device(mon);
if (dev) {
const char *subsystem = udev_device_get_subsystem(dev);
enum usb_typec_event event;
if (strcmp(subsystem, "typec") == 0) {
// typec event
const char *action = udev_device_get_action(dev);
if (strcmp(action, "add") == 0) {
event = USBC_DEVICE_CONNECTED;
} else if (strcmp(action, "remove") == 0) {
event = USBC_DEVICE_DISCONNECTED;
}
}
udev_device_unref(dev);
// call all callbacks for this event
libtypec_notification_list_t* node = registered_callbacks[event];
while (node) {
node->cb_func(event, node->data);
node = node->next;
}
}
}
udev_unref(udev);
}
libtypec_notification_list_t* registered_callbacks[USBC_EVENT_COUNT] = {0};
const struct libtypec_os_backend libtypec_lnx_sysfs_backend = {
.init = libtypec_sysfs_init,
.exit = libtypec_sysfs_exit,
.get_capability_ops = libtypec_sysfs_get_capability_ops,
.get_conn_capability_ops = libtypec_sysfs_get_conn_capability_ops,
.get_alternate_modes = libtypec_sysfs_get_alternate_modes,
.get_cam_supported_ops = NULL,
.get_current_cam_ops = NULL,
.get_pdos_ops = libtypec_sysfs_get_pdos_ops,
.get_cable_properties_ops = libtypec_sysfs_get_cable_properties_ops,
.get_connector_status_ops = libtypec_sysfs_get_connector_status_ops,
.get_pd_message_ops = libtypec_sysfs_get_pd_message_ops,
.get_bb_status = libtypec_sysfs_get_bb_status,
.get_bb_data = libtypec_sysfs_get_bb_data,
.monitor_events = libtypec_lnx_monitor_udev_events
};
|