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>
25 #include <core/launch.h>
34 static struct usb_sysfs {
38 { "/sys/class/usb_mode/usb0/enable", "0" },
39 { "/sys/class/usb_mode/usb0/idVendor", "04e8" },
40 { "/sys/class/usb_mode/usb0/idProduct", NULL },
41 { "/sys/class/usb_mode/usb0/funcs_fconf", NULL },
42 { "/sys/class/usb_mode/usb0/funcs_sconf", NULL },
43 { "/sys/class/usb_mode/usb0/bDeviceClass", "239" },
44 { "/sys/class/usb_mode/usb0/bDeviceSubClass", "2" },
45 { "/sys/class/usb_mode/usb0/bDeviceProtocol", "1" },
46 { "/sys/class/usb_mode/usb0/enable", "1" },
49 static int launch_app(char **argv)
53 if (!argv || !argv[0])
59 printf("Failed to call fork().\n");
63 if (pid > 0) { /*parent*/
69 if (execvp(argv[0], argv) < 0)
70 printf("Failed to call execvp: %d\n", errno);
75 static int write_sysfs(char *path, char *value)
83 fp = fopen(path, "w");
85 printf("Failed to open '%s'.\n", path);
89 ret = fwrite(value, sizeof(char), strlen(value), fp);
91 if (ret < strlen(value)) {
92 printf("Failed to write '%s'.\n", value);
99 static int set_usb_configuration(char *idproduct, char *fconf, char *sconf)
103 usb_confs[2].value = idproduct;
104 usb_confs[3].value = fconf;
105 usb_confs[4].value = sconf;
107 for (i = 0; i < ARRAY_SIZE(usb_confs); i++) {
108 ret = write_sysfs(usb_confs[i].path, usb_confs[i].value);
110 printf("Failed to write usb setting. path=%s value=%s\n", usb_confs[i].path, usb_confs[i].value);
118 static int divide_cmd(char **command, int len, char *cmd)
120 char *param, *next, *term;
126 term = strchr(cmd, '\0');
130 memset(command, 0, len);
141 next = strchr(param, ' ');
143 command[cnt++] = param;
153 command[cnt++] = param;
160 static int run_cmd(char *cmd)
163 char *command[ARG_MAX];
164 char in_cmd[CMD_MAX];
169 snprintf(in_cmd, sizeof(in_cmd), "%s", cmd);
171 ret = divide_cmd(command, sizeof(command), in_cmd);
175 ret = launch_app(command);
183 static int load_sdb(void)
187 ret = set_usb_configuration("6860", "mtp", "mtp,acm,sdb");
191 return run_cmd("/usr/bin/systemctl start sdbd.service");
194 static int load_ssh(void)
198 ret = set_usb_configuration("6863", "rndis", " ");
202 ret = run_cmd("/sbin/ifconfig usb0 192.168.129.3 up");
206 ret = run_cmd("/sbin/route add -net 192.168.129.0 netmask 255.255.255.0 dev usb0");
210 ret = run_cmd("/usr/bin/systemctl start sshd.service");
217 static int unload_sdb(void)
221 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
225 ret = run_cmd("/usr/bin/systemctl stop sdbd.service");
232 static int unload_ssh(void)
236 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
240 ret = run_cmd("/sbin/ifconfig usb0 down");
244 ret = run_cmd("/usr/bin/systemctl stop sshd.service");
251 int load_usb_mode(char *opt)
254 printf("Failed: Forth parameter is NULL.\n");
258 if (!strncmp(opt, USB_SDB, strlen(opt)))
261 if (!strncmp(opt, USB_SSH, strlen(opt)))
264 printf("Failed: Forth parameter(%s) is invalid.\n", opt);
268 int unload_usb_mode(char *opt)
271 printf("Failed: Forth parameter is NULL.\n");
275 if (!strncmp(opt, USB_SDB, strlen(opt)))
278 if (!strncmp(opt, USB_SSH, strlen(opt)))
281 printf("Failed: Forth parameter(%s) is invalid.\n", opt);