4 * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <shared/common.h>
33 static struct usb_sysfs {
37 { "/sys/class/usb_mode/usb0/enable", "0" },
38 { "/sys/class/usb_mode/usb0/idVendor", "04e8" },
39 { "/sys/class/usb_mode/usb0/idProduct", NULL },
40 { "/sys/class/usb_mode/usb0/funcs_fconf", NULL },
41 { "/sys/class/usb_mode/usb0/funcs_sconf", NULL },
42 { "/sys/class/usb_mode/usb0/bDeviceClass", "239" },
43 { "/sys/class/usb_mode/usb0/bDeviceSubClass", "2" },
44 { "/sys/class/usb_mode/usb0/bDeviceProtocol", "1" },
45 { "/sys/class/usb_mode/usb0/enable", "1" },
48 static int launch_app(char **argv)
52 if (!argv || !argv[0])
58 printf("Failed to call fork().\n");
62 if (pid > 0) { /*parent*/
68 if (execvp(argv[0], argv) < 0)
69 printf("Failed to call execvp: %d\n", errno);
74 static int write_sysfs(char *path, char *value)
82 fp = fopen(path, "w");
84 printf("Failed to open '%s'.\n", path);
88 ret = fwrite(value, sizeof(char), strlen(value), fp);
90 if (ret < strlen(value)) {
91 printf("Failed to write '%s'.\n", value);
98 static int set_usb_configuration(char *idproduct, char *fconf, char *sconf)
102 usb_confs[2].value = idproduct;
103 usb_confs[3].value = fconf;
104 usb_confs[4].value = sconf;
106 for (i = 0; i < ARRAY_SIZE(usb_confs); i++) {
107 ret = write_sysfs(usb_confs[i].path, usb_confs[i].value);
109 printf("Failed to write usb setting. path=%s value=%s\n", usb_confs[i].path, usb_confs[i].value);
117 static int divide_cmd(char **command, int len, char *cmd)
119 char *param, *next, *term;
125 term = strchr(cmd, '\0');
129 memset(command, 0, len);
140 next = strchr(param, ' ');
142 command[cnt++] = param;
152 command[cnt++] = param;
159 static int run_cmd(char *cmd)
162 char *command[ARG_MAX];
163 char in_cmd[CMD_MAX];
168 snprintf(in_cmd, sizeof(in_cmd), "%s", cmd);
170 ret = divide_cmd(command, sizeof(command), in_cmd);
174 ret = launch_app(command);
182 static int load_sdb(void)
186 ret = set_usb_configuration("6860", "mtp", "mtp,acm,sdb");
190 return run_cmd("/usr/bin/systemctl start sdbd.service");
193 static int load_ssh(void)
197 ret = set_usb_configuration("6863", "rndis", " ");
201 ret = run_cmd("/sbin/ifconfig usb0 192.168.129.3 up");
205 ret = run_cmd("/sbin/route add -net 192.168.129.0 netmask 255.255.255.0 dev usb0");
209 ret = run_cmd("/usr/bin/systemctl start sshd.service");
216 static int unload_sdb(void)
220 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
224 ret = run_cmd("/usr/bin/systemctl stop sdbd.service");
231 static int unload_ssh(void)
235 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
239 ret = run_cmd("/sbin/ifconfig usb0 down");
243 ret = run_cmd("/usr/bin/systemctl stop sshd.service");
250 int load_usb_mode(char *opt)
253 printf("Failed: Forth parameter is NULL.\n");
257 if (!strncmp(opt, USB_SDB, strlen(opt)))
260 if (!strncmp(opt, USB_SSH, strlen(opt)))
263 printf("Failed: Forth parameter(%s) is invalid.\n", opt);
267 int unload_usb_mode(char *opt)
270 printf("Failed: Forth parameter is NULL.\n");
274 if (!strncmp(opt, USB_SDB, strlen(opt)))
277 if (!strncmp(opt, USB_SSH, strlen(opt)))
280 printf("Failed: Forth parameter(%s) is invalid.\n", opt);