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 <core/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("fork() failed\n");
63 if (pid > 0) { /*parent*/
69 if (execvp(argv[0], argv) < 0)
70 printf("execvp failed (%d)\n", errno);
75 static int write_sysfs(char *path, char *value)
83 fp = fopen(path, "w");
85 printf("FAIL: fopen(%s)\n", path);
89 ret = fwrite(value, sizeof(char), strlen(value), fp);
91 if (ret < strlen(value)) {
92 printf("FAIL: fwrite(%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("usb setting fails (%s), (%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)
188 ret = set_usb_configuration("6860", "mtp", "mtp,acm,sdb");
192 return run_cmd("/usr/bin/systemctl start sdbd.service");
195 static int load_ssh(void)
199 ret = set_usb_configuration("6863", "rndis", " ");
203 ret = run_cmd("/sbin/ifconfig usb0 192.168.129.3 up");
207 ret = run_cmd("/sbin/route add -net 192.168.129.0 netmask 255.255.255.0 dev usb0");
211 ret = run_cmd("/usr/bin/systemctl start sshd.service");
218 static int unload_sdb(void)
222 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
226 ret = run_cmd("/usr/bin/systemctl stop sdbd.service");
233 static int unload_ssh(void)
237 ret = write_sysfs(usb_confs[0].path, usb_confs[0].value);
241 ret = run_cmd("/sbin/ifconfig usb0 down");
245 ret = run_cmd("/usr/bin/systemctl stop sshd.service");
252 int load_usb_mode(char *opt)
255 printf("Failed: Forth parameter is NULL\n");
259 if (!strncmp(opt, USB_SDB, strlen(opt)))
262 if (!strncmp(opt, USB_SSH, strlen(opt)))
265 printf("Failed: Forth parameter is invalid (%s)\n", opt);
269 int unload_usb_mode(char *opt)
272 printf("Failed: Forth parameter is NULL\n");
276 if (!strncmp(opt, USB_SDB, strlen(opt)))
279 if (!strncmp(opt, USB_SSH, strlen(opt)))
282 printf("Failed: Forth parameter is invalid (%s)\n", opt);