diff options
Diffstat (limited to 'device_engine.c')
-rw-r--r-- | device_engine.c | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/device_engine.c b/device_engine.c new file mode 100644 index 0000000..766df4b --- /dev/null +++ b/device_engine.c @@ -0,0 +1,245 @@ +/* + * devman + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: DongGi Jang <dg0402.jang@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <dirent.h> +#include <unistd.h> +#include <sys/stat.h> + +#include "devlog.h" +#include "device_engine.h" + +struct device *dev_head = NULL; +#define BUFF_MAX 255 + +void add_dev(struct device *dev) +{ + dev->next = dev_head; + dev_head = dev; +} + +void print_devices() +{ + struct device *dev; + dev = dev_head; + + while (dev) { + DBG("%s - %d", dev->devname, dev->devtype); + dev = dev->next; + } +} + +void reset_devtype() +{ + struct device *dev; + dev = dev_head; + + while (dev) { + dev->devtype = -1; + dev = dev->next; + } +} + +struct device *find_device(struct device *root_dev, devtype_t devtype) +{ + struct device *dev; + + if (devtype == -1) + return NULL; + + if (root_dev == NULL) + dev = dev_head; + else + dev = root_dev; + + while (dev) { + DBG("devname = %s %d %d", dev->devname, dev->devtype, devtype); + if (dev->devtype == devtype) + return dev; + dev = dev->next; + } + + return NULL; +} + +int find_sysfs_node(char *path, char *node_name) +{ + DIR *dp; + struct dirent *entry; + + dp = opendir(path); + if (dp == NULL) { + DBG("path is not existed : %s", path); + return -1; + } + + while ((entry = readdir(dp)) != NULL) { + if (strncmp(entry->d_name, ".", 1) == 0 || + strncmp(entry->d_name, "..", 2) == 0) + continue; + else + break; + } + + /* copy node name */ + if (entry != NULL) { + if (node_name != NULL) + strncpy(node_name, entry->d_name,PATH_MAX); + + } else { + DBG("sysfs node not existed"); + if (closedir(dp) != 0) + DBG("Unable to close directory"); + return -1; + } + + if (closedir(dp) != 0) + DBG("Unable to close directory"); + return 0; +} + +int set_devtype(char *devname, devtype_t devtype) +{ + int ret; + struct device *dev; + dev = dev_head; + + while (dev) { + if (strstr(dev->devname, devname)) { + if ((strstr(dev->devname, "auto") != NULL) && + (dev->probe != NULL)) { + ret = dev->probe(); + if (ret < 0) { + DBG("auto probe failed"); + return -1; + } + } + + dev->devtype = devtype; + return 0; + } + dev = dev->next; + } + + return -1; +} + +static int sys_read_buf(char *file, char *buf) +{ + int fd; + int r; + + fd = open(file, O_RDONLY); + if (fd == -1) { + ERR("%s open error: %s", file, strerror(errno)); + return -1; + } + + r = read(fd, buf, BUFF_MAX); + DBG("!@#read[%s], value[%s], line[%d]", file, buf, __LINE__); + if ((r >= 0) && (r <= BUFF_MAX)) + buf[r] = '\0'; + else { + ERR("%s read error: %s", file, strerror(errno)); + return -1; + } + + close(fd); + INFO("read %s, value= %s", file, buf); + DBG("read[%s], value[%s], line[%d]", file, buf, __LINE__); + return 0; +} + +static int sys_write_buf(char *file, char *buf) +{ + int fd; + int r; + + fd = open(file, O_WRONLY); + if (fd == -1) { + ERR("%s open error: %s", file, strerror(errno)); + return -1; + } + + r = write(fd, buf, strlen(buf)); + close(fd); + if (r < 0) { + ERR("%s write error: %s", file, strerror(errno)); + return -1; + } + INFO("write %s, value= %s", file, buf); + DBG("write[%s], value[%s], line[%d]", file, buf, __LINE__); + return 0; +} + +int sys_get_int(char *fname, int *val) +{ + char buf[BUFF_MAX]; + + if (sys_read_buf(fname, buf) == 0) { + *val = atoi(buf); + return 0; + } else { + *val = -1; + return -1; + } +} + +char *sys_get_str(char *fname) +{ + char buf[BUFF_MAX]; + char *r = NULL; + + if (sys_read_buf(fname, buf) == 0) + r = strdup((char *)buf); + + return r; +} + +int sys_set_int(char *fname, int val) +{ + char buf[BUFF_MAX]; + int r = -1; + snprintf(buf, sizeof(buf), "%d", val); + + if (sys_write_buf(fname, buf) == 0) + r = 0; + + return r; +} + +int sys_set_str(char *fname, char *val) +{ + int r = -1; + + if (val != NULL) { + if (sys_write_buf(fname, val) == 0) + r = 0; + } + + return r; +} |