tizen 2.3 release
[framework/system/deviced.git] / src / core / devices.h
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #ifndef __DEVICES_H__
21 #define __DEVICES_H__
22
23 #include <errno.h>
24 #include "common.h"
25
26 enum device_priority {
27         DEVICE_PRIORITY_NORMAL = 0,
28         DEVICE_PRIORITY_HIGH,
29 };
30
31 enum device_flags {
32         NORMAL_MODE                   = 0x00000001,
33         AMBIENT_MODE                  = 0x00000002,
34         CORE_LOGIC_MODE               = 0x00010000,
35         TOUCH_SCREEN_OFF_MODE         = 0x00020000,
36         LCD_PANEL_OFF_MODE            = 0x00040000,
37         LCD_PHASED_TRANSIT_MODE       = 0x00080000,
38         LCD_ON_BY_GESTURE             = 0x00100000,
39         LCD_ON_BY_POWER_KEY           = 0x00200000,
40         LCD_ON_BY_EVENT               = 0x00400000,
41         LCD_ON_BY_TOUCH               = 0x00800000,
42 };
43
44 struct device_ops {
45         enum device_priority priority;
46         char *name;
47         void (*init) (void *data);
48         void (*exit) (void *data);
49         int (*start) (enum device_flags flags);
50         int (*stop) (enum device_flags flags);
51         int (*status) (void);
52         int (*execute) (void *data);
53 };
54
55 enum device_ops_status {
56         DEVICE_OPS_STATUS_UNINIT,
57         DEVICE_OPS_STATUS_START,
58         DEVICE_OPS_STATUS_STOP,
59         DEVICE_OPS_STATUS_MAX,
60 };
61
62 void devices_init(void *data);
63 void devices_exit(void *data);
64
65 static inline int device_start(const struct device_ops *dev)
66 {
67         if (dev && dev->start)
68                 return dev->start(NORMAL_MODE);
69
70         return -EINVAL;
71 }
72
73 static inline int device_stop(const struct device_ops *dev)
74 {
75         if (dev && dev->stop)
76                 return dev->stop(NORMAL_MODE);
77
78         return -EINVAL;
79 }
80
81 static inline int device_exit(const struct device_ops *dev, void *data)
82 {
83         if (dev && dev->exit) {
84                 dev->exit(data);
85                 return 0;
86         }
87
88         return -EINVAL;
89 }
90
91 static inline int device_execute(const struct device_ops *dev, void *data)
92 {
93         if (dev && dev->execute)
94                 return dev->execute(data);
95
96         return -EINVAL;
97 }
98
99 static inline int device_get_status(const struct device_ops *dev)
100 {
101         if (dev && dev->status)
102                 return dev->status();
103
104         return -EINVAL;
105 }
106
107 #define DEVICE_OPS_REGISTER(dev)        \
108 static void __CONSTRUCTOR__ module_init(void)   \
109 {       \
110         add_device(dev);        \
111 }       \
112 static void __DESTRUCTOR__ module_exit(void)    \
113 {       \
114         remove_device(dev);     \
115 }
116
117 void add_device(const struct device_ops *dev);
118 void remove_device(const struct device_ops *dev);
119
120 const struct device_ops *find_device(const char *name);
121 int check_default(const struct device_ops *dev);
122
123 #define NOT_SUPPORT_OPS(dev) \
124         ((check_default(dev))? 1 : 0)
125
126 #define FIND_DEVICE_INT(dev, name) do { \
127         if (!dev) dev = find_device(name); if(check_default(dev)) return -ENODEV; \
128 } while(0)
129
130 #define FIND_DEVICE_VOID(dev, name) do { \
131         if (!dev) dev = find_device(name); if(check_default(dev)) return; \
132 } while(0)
133
134 #endif