change usb control process name to usb-server
[framework/system/system-server.git] / ss_mmc_handler.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17
18 #include <unistd.h>
19 #include <sys/mount.h>
20 #include <errno.h>
21 #include <vconf.h>
22 #include <devman.h>
23 #include <fcntl.h>
24 #include <dirent.h>
25 #include <sysman.h>
26 #include "ss_log.h"
27 #include "ss_device_handler.h"
28
29 #define MOVINAND_DEV            "/dev/mmcblk0p1"
30 #define FORMAT_MMC              PREFIX"/sbin/mkfs.vfat "
31 #define FORMAT_MOVINAND         PREFIX"/bin/movi_format.sh"
32
33 int mmc_status;
34
35 int get_mmcblk_num()
36 {
37         DIR *dp;
38         struct dirent *dir;
39         struct stat stat;
40         char buf[255];
41         int fd;
42         int r;
43         int mmcblk_num;
44
45         if ((dp = opendir("/sys/block")) == NULL) {
46                 PRT_TRACE_ERR("Can not open directory..\n");
47                 return -1;
48         }
49         chdir("/sys/block");
50
51         while (dir = readdir(dp)) {
52                 memset(&stat, 0, sizeof(struct stat));
53                 lstat(dir->d_name, &stat);
54                 if (S_ISDIR(stat.st_mode) || S_ISLNK(stat.st_mode)) {
55                         if (strncmp(".", dir->d_name, 1) == 0
56                             || strncmp("..", dir->d_name, 2) == 0)
57                                 continue;
58                         if (strncmp("mmcblk", dir->d_name, 6) == 0) {
59                                 snprintf(buf, 255, "/sys/block/%s/device/type",
60                                          dir->d_name);
61
62                                 fd = open(buf, O_RDONLY);
63                                 if (fd == -1)
64                                         PRT_TRACE_ERR("%s open error: %s", buf,
65                                                       strerror(errno));
66                                 r = read(fd, buf, 10);
67                                 if ((r >= 0) && (r < 10))
68                                         buf[r] = '\0';
69                                 else
70                                         PRT_TRACE_ERR("%s read error: %s", buf,
71                                                       strerror(errno));
72                                 close(fd);
73                                 if (strncmp("SD", buf, 2) == 0) {
74                                         char *str_mmcblk_num = strndup((dir->d_name) + 6, 1);
75                                         if (str_mmcblk_num == NULL) {
76                                                 PRT_TRACE_ERR("Memory Allocation Failed");
77                                                 closedir(dp);
78                                                 return -1; 
79                                         }
80                                         mmcblk_num =
81                                             atoi(str_mmcblk_num);
82
83                                         free(str_mmcblk_num);
84                                         closedir(dp);
85                                         PRT_TRACE("%d \n", mmcblk_num);
86                                         return mmcblk_num;
87                                 }
88                         }
89
90                 }
91         }
92         closedir(dp);
93         PRT_TRACE_ERR("Failed to find mmc block number\n");
94         return -1;
95 }
96
97 static int ss_mmc_format(keynode_t *key_nodes, void *data)
98 {
99         PRT_TRACE_ERR("mmc format called");
100         device_set_property(DEVTYPE_MMC, MMC_PROP_FORMAT, 0);
101
102         return 0;
103 }
104
105 int ss_mmc_unmounted(int argc, char **argv)
106 {
107         int option = -1;
108
109         if (argc < 1) {
110                 PRT_TRACE_ERR("Option is wong");
111                 return -1;
112         }
113         if ((option = atoi(argv[0])) < 0) {
114                 PRT_TRACE_ERR("Option is wong : %d", option);
115                 return -1;
116         }
117
118         if (umount2(MMC_MOUNT_POINT, option) != 0) {
119                 PRT_TRACE_ERR("Failed to unmount mmc card\n");
120                 vconf_set_int(VCONFKEY_SYSMAN_MMC_UNMOUNT,
121                               VCONFKEY_SYSMAN_MMC_UNMOUNT_FAILED);
122                 return -1;
123
124         }
125         vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
126                       VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
127         vconf_set_int(VCONFKEY_SYSMAN_MMC_UNMOUNT,
128                       VCONFKEY_SYSMAN_MMC_UNMOUNT_COMPLETED);
129         mmc_status = VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED;
130
131         return 0;
132 }
133
134 int ss_mmc_init()
135 {
136         /* mmc card mount */
137         ss_mmc_inserted();
138
139         ss_action_entry_add_internal(PREDEF_MOUNT_MMC, ss_mmc_inserted, NULL,
140                                      NULL);
141         ss_action_entry_add_internal(PREDEF_UNMOUNT_MMC, ss_mmc_unmounted, NULL,
142                                      NULL);
143         ss_action_entry_add_internal(PREDEF_FORMAT_MMC, ss_mmc_format, NULL,
144                                      NULL);
145         return 0;
146 }
147
148 int ss_mmc_inserted()
149 {
150         char buf[NAME_MAX];
151         int blk_num, ret, retry = 0;
152         char opt[NAME_MAX];
153         char *popt = opt;
154
155         if (mmc_status == VCONFKEY_SYSMAN_MMC_MOUNTED) {
156                 PRT_DBG("Mmc is already mounted.\n");
157                 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
158                               VCONFKEY_SYSMAN_MMC_MOUNTED);
159                 vconf_set_int(VCONFKEY_SYSMAN_MMC_MOUNT,
160                               VCONFKEY_SYSMAN_MMC_MOUNT_ALREADY);
161                 return -1;
162         }
163
164         if (access(MMC_MOUNT_POINT, R_OK) != 0)
165                 mkdir(MMC_MOUNT_POINT, 0755);
166
167         if ((blk_num = get_mmcblk_num()) == -1) {
168                 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
169                               VCONFKEY_SYSMAN_MMC_REMOVED);
170                 vconf_set_int(VCONFKEY_SYSMAN_MMC_MOUNT,
171                               VCONFKEY_SYSMAN_MMC_MOUNT_FAILED);
172                 mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
173                 return 0;
174         }
175         popt = NULL;
176
177         snprintf(buf, sizeof(buf), "%s%d", MMC_DEV, blk_num);
178         if (mount
179             (buf, MMC_MOUNT_POINT, "vfat", 0,
180              "uid=0,gid=0,dmask=0000,fmask=0111,iocharset=iso8859-1,utf8,shortname=mixed,smackfsroot=*,smackfsdef=*")
181             == 0) {
182                 PRT_DBG("Mounted mmc card\n");
183                 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
184                               VCONFKEY_SYSMAN_MMC_MOUNTED);
185                 vconf_set_int(VCONFKEY_SYSMAN_MMC_MOUNT,
186                               VCONFKEY_SYSMAN_MMC_MOUNT_COMPLETED);
187                 mmc_status = VCONFKEY_SYSMAN_MMC_MOUNTED;
188                 return 0;
189         }
190         do {
191                 snprintf(buf, sizeof(buf), "%s%dp1", MMC_DEV, blk_num);
192                 if ((ret =
193                      mount(buf, MMC_MOUNT_POINT, "vfat", 0,
194                            "uid=0,gid=0,dmask=0000,fmask=0111,iocharset=iso8859-1,utf8,shortname=mixed,smackfsroot=*,smackfsdef=*"))
195                     == 0) {
196                         PRT_DBG("Mounted mmc card partition 1(%s)\n", buf);
197                         vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
198                                       VCONFKEY_SYSMAN_MMC_MOUNTED);
199                         vconf_set_int(VCONFKEY_SYSMAN_MMC_MOUNT,
200                                       VCONFKEY_SYSMAN_MMC_MOUNT_COMPLETED);
201                         mmc_status = VCONFKEY_SYSMAN_MMC_MOUNTED;
202                         return 0;
203                 }
204                 usleep(100000);
205         } while (ret == -1 && errno == ENOENT && retry++ < 10);
206
207         vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
208                       VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
209         vconf_set_int(VCONFKEY_SYSMAN_MMC_MOUNT,
210                       VCONFKEY_SYSMAN_MMC_MOUNT_FAILED);
211         mmc_status = VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED;
212         PRT_TRACE_ERR("Failed to mount mmc card\n");
213         return -1;
214 }
215
216 int ss_mmc_removed()
217 {
218         vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS, VCONFKEY_SYSMAN_MMC_REMOVED);
219
220         if (umount2(MMC_MOUNT_POINT, MNT_DETACH) != 0) {
221                 PRT_TRACE_ERR("Failed to unmount mmc card\n");
222         }
223         mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
224
225         return 0;
226 }