1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "usbd_port.h"
17 #include <fcntl.h>
18 #include <hdf_sbuf.h>
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21 #include "hdf_log.h"
22 #include "osal_time.h"
23 #include "securec.h"
24 #include "usbd_function.h"
25 #include "usbd_publisher.h"
26 #include "default_config.h"
27
28 #define DEFAULT_PORT_ID 1
29
30 #define PORT_MODE_HOST_STR "host"
31 #define PORT_MODE_DEVICE_STR "device"
32
33 #define POWER_ROLE_NONE 0
34 #define POWER_ROLE_SOURCE 1
35 #define POWER_ROLE_SINK 2
36 #define POWER_ROLE_MAX 3
37
38 #define DATA_ROLE_NONE 0
39 #define DATA_ROLE_HOST 1
40 #define DATA_ROLE_DEVICE 2
41 #define DATA_ROLE_MAX 3
42
43 #define PORT_MODE_NONE 0
44 #define PORT_MODE_DEVICE 1
45 #define PORT_MODE_HOST 2
46
47 static int32_t currentPortId = DEFAULT_PORT_ID;
48 static int32_t currentPowerRole = POWER_ROLE_SINK;
49 static int32_t currentDataRole = DATA_ROLE_DEVICE;
50 static int32_t currentMode = PORT_MODE_DEVICE;
51
52 int32_t SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole);
53
IfCanSwitch(int32_t portId,int32_t powerRole,int32_t dataRole)54 static int32_t IfCanSwitch(int32_t portId, int32_t powerRole, int32_t dataRole)
55 {
56 if (portId != DEFAULT_PORT_ID) {
57 HDF_LOGE("%{public}s: portId error", __func__);
58 return HDF_FAILURE;
59 }
60 if (powerRole <= POWER_ROLE_NONE || powerRole >= POWER_ROLE_MAX) {
61 HDF_LOGE("%{public}s: powerRole error", __func__);
62 return HDF_FAILURE;
63 }
64 if (dataRole <= DATA_ROLE_NONE || dataRole >= DATA_ROLE_MAX) {
65 HDF_LOGE("%{public}s: dataRole error", __func__);
66 return HDF_FAILURE;
67 }
68 return HDF_SUCCESS;
69 }
70
WritePortFile(int32_t powerRole,int32_t dataRole,int32_t mode)71 static int32_t WritePortFile(int32_t powerRole, int32_t dataRole, int32_t mode)
72 {
73 char *fn = PORT_FILE_PATH;
74 char *modeStr = NULL;
75
76 if (mode == PORT_MODE_HOST || mode == PORT_MODE_DEVICE) {
77 switch (mode) {
78 case PORT_MODE_HOST:
79 modeStr = PORT_MODE_HOST_STR;
80 UsbdSetFunction(USB_FUNCTION_NONE);
81 break;
82 case PORT_MODE_DEVICE:
83 modeStr = PORT_MODE_DEVICE_STR;
84 break;
85 default:
86 modeStr = PORT_MODE_NONE;
87 break;
88 }
89 }
90 if (modeStr == NULL) {
91 HDF_LOGE("%{public}s: modeStr error", __func__);
92 return HDF_FAILURE;
93 }
94 int32_t len = strlen(modeStr);
95 int32_t fd = open(fn, O_WRONLY | O_TRUNC);
96 if (fd < 0) {
97 HDF_LOGE("%{public}s: file open error fd= %{public}d", __func__, fd);
98 return HDF_FAILURE;
99 }
100 int32_t ret = write(fd, modeStr, len);
101 close(fd);
102 if (ret == len) {
103 return HDF_SUCCESS;
104 }
105 return HDF_FAILURE;
106 }
107
SetPortInit(int32_t portId,int32_t powerRole,int32_t dataRole)108 int32_t SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole)
109 {
110 int32_t mode = PORT_MODE_DEVICE;
111 if (IfCanSwitch(portId, powerRole, dataRole)) {
112 return HDF_FAILURE;
113 }
114 if (powerRole == POWER_ROLE_SOURCE && dataRole == DATA_ROLE_HOST) {
115 mode = PORT_MODE_HOST;
116 }
117 if (powerRole == POWER_ROLE_SINK && dataRole == DATA_ROLE_DEVICE) {
118 mode = PORT_MODE_DEVICE;
119 }
120 if (WritePortFile(powerRole, dataRole, mode)) {
121 return HDF_FAILURE;
122 }
123 currentPortId = portId;
124 currentPowerRole = powerRole;
125 currentDataRole = dataRole;
126 currentMode = mode;
127 if (currentMode == PORT_MODE_DEVICE) {
128 UsbdSetFunction(USB_FUNCTION_HDC);
129 }
130 return HDF_SUCCESS;
131 }
132
SetPort(int32_t portId,int32_t powerRole,int32_t dataRole,const struct UsbdService * service)133 int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole, const struct UsbdService *service)
134 {
135 int32_t ret = SetPortInit(portId, powerRole, dataRole);
136 if (ret != HDF_SUCCESS) {
137 HDF_LOGE("%{public}s: SetPortInit fail! ret:%{public}d", __func__, ret);
138 return ret;
139 }
140
141 NotifyUsbPortSubscriber(service->subscriber, currentPortId, currentPowerRole, currentDataRole, currentMode);
142 return HDF_SUCCESS;
143 }
144
QueryPort(int32_t * portId,int32_t * powerRole,int32_t * dataRole,int32_t * mode,struct UsbdService * service)145 int32_t QueryPort(int32_t *portId, int32_t *powerRole, int32_t *dataRole, int32_t *mode, struct UsbdService *service)
146 {
147 *portId = currentPortId;
148 *powerRole = currentPowerRole;
149 *dataRole = currentDataRole;
150 *mode = currentMode;
151 return HDF_SUCCESS;
152 }
153