2.0 alpha
[platform/core/system/devman.git] / src / device_engine.c
1 /*
2  *  devman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */
21
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <dirent.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include "device_engine.h"
34
35 #define BUFF_MAX 255
36
37 struct device *dev_head = NULL;
38
39 void add_dev(struct device *dev)
40 {
41         dev->next = dev_head;
42         dev_head = dev;
43 }
44
45 void print_devices()
46 {
47         struct device *dev;
48         dev = dev_head;
49
50         while (dev) {
51                 DBG("%s - %d", dev->devname, dev->devtype);
52                 dev = dev->next;
53         }
54 }
55
56 void reset_devtype()
57 {
58         struct device *dev;
59         dev = dev_head;
60
61         while (dev) {
62                 dev->devtype = -1;
63                 dev = dev->next;
64         }
65 }
66
67 struct device *find_device(struct device *root_dev, devtype_t devtype)
68 {
69         struct device *dev;
70
71         if (devtype == -1)
72                 return NULL;
73
74         if (root_dev == NULL)
75                 dev = dev_head;
76         else
77                 dev = root_dev;
78
79         while (dev) {
80                 if (dev->devtype == devtype)
81                         return dev;
82                 dev = dev->next;
83         }
84
85         return NULL;
86 }
87
88 int set_devtype(char *devname, devtype_t devtype)
89 {
90         int ret;
91         struct device *dev;
92         dev = dev_head;
93
94         while (dev) {
95                 if (strstr(dev->devname, devname)) {
96                         if ((strstr(dev->devname, "auto") != NULL) &&
97                             (dev->probe != NULL)) {
98                                 ret = dev->probe();
99                                 if (ret < 0) {
100                                         DBG("auto probe failed");
101                                         return -1;
102                                 }
103                         }
104
105                         dev->devtype = devtype;
106                         return 0;
107                 }
108                 dev = dev->next;
109         }
110
111         return -1;
112 }