blob: 787a593067960cf557933e0e20c91759d012349d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2012 Corey Tabaka
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __DEV_DRIVER_H
24#define __DEV_DRIVER_H
25
26#include <sys/types.h>
27#include <list.h> // for containerof
28#include <compiler.h>
29
30struct driver;
31
32/*
33 * Contains the data pertaining to an instance of a device. More than one
34 * instance may exist for a given driver type (i.e. uart0, uart1, etc..).
35 */
36struct device {
37 const char *name;
38 const struct driver *driver;
39
40 /* instance specific config data populated at instantiation */
41 const void *config;
42
43 /* instance specific data populated by the driver at init */
44 void *state;
45
46 // TODO: add generic state, such as suspend/resume state, etc...
47};
48
49/* device class, mainly used as a unique magic pointer to validate ops */
50struct device_class {
51 const char *name;
52};
53
54/* standard driver ops; extensions should contain this ops structure */
55struct driver_ops {
56 const struct device_class *device_class;
57
58 status_t (*init)(struct device *dev);
59 status_t (*fini)(struct device *dev);
60
61 status_t (*suspend)(struct device *dev);
62 status_t (*resume)(struct device *dev);
63};
64
65/* describes a driver, one per driver type */
66struct driver {
67 const char *type;
68 const struct driver_ops *ops;
69};
70
71#define DRIVER_EXPORT(type_, ops_) \
72 const struct driver concat(__driver_, type_) \
73 __ALIGNED(sizeof(void *)) __SECTION(".drivers") = { \
74 .type = #type_, \
75 .ops = ops_, \
76 }
77
78#define DEVICE_INSTANCE(type_, name_, config_) \
79 extern struct driver concat(__driver_, type_); \
80 struct device concat(__device_, concat(type_, concat(_, name_))) \
81 __ALIGNED(sizeof(void *)) __SECTION(".devices") = { \
82 .name = #name_, \
83 .driver = &concat(__driver_, type_), \
84 .config = config_, \
85 }
86
87/*
88 * returns the driver specific ops pointer given the device instance, specifc
89 * ops type, and generic ops member name within the specific ops structure.
90 */
91#define device_get_driver_ops(dev, type, member) ({ \
92 type *__ops = NULL; \
93 if (dev && dev->driver && dev->driver->ops) \
94 __ops = containerof(dev->driver->ops, type, member); \
95 __ops; \
96})
97
98#define device_get_by_name(type_, name_) ({ \
99 extern struct device concat(__device_, concat(type_, concat(_, name_))); \
100 &concat(__device_, concat(type_, concat(_, name_))); \
101})
102
103status_t device_init_all(void);
104status_t device_fini_all(void);
105
106status_t device_init(struct device *dev);
107status_t device_fini(struct device *dev);
108
109status_t device_suspend(struct device *dev);
110status_t device_resume(struct device *dev);
111
112#endif
113