Skip to content

Commit 1967d1e

Browse files
ycsindkalowsk
authored andcommitted
drivers: syscon: do not assume that a function isn't supported
When a function pointer is `NULL`, it could be that the underlying hardware doesn't support it, or it isn't implemented. If a function isn't supported by the hardware, its driver should return `-ENOTSUP`, the API layer should return `-ENOSYS` when a function isn't implemented. Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
1 parent 9696765 commit 1967d1e

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

include/zephyr/drivers/syscon.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ __subsystem struct syscon_driver_api {
7171
*
7272
* @param dev The device to get the register size for.
7373
* @param addr Where to write the base address.
74-
* @return 0 When addr was written to.
74+
* @return 0 When @a addr was written to.
75+
* @return -ENOSYS If the API or function isn't implemented.
7576
*/
7677
__syscall int syscon_get_base(const struct device *dev, uintptr_t *addr);
7778

7879
static inline int z_impl_syscon_get_base(const struct device *dev, uintptr_t *addr)
7980
{
8081
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
8182

82-
if (api == NULL) {
83-
return -ENOTSUP;
83+
if ((api == NULL) || (api->get_base == NULL)) {
84+
return -ENOSYS;
8485
}
8586

8687
return api->get_base(dev, addr);
@@ -96,16 +97,17 @@ static inline int z_impl_syscon_get_base(const struct device *dev, uintptr_t *ad
9697
* @param reg The register offset
9798
* @param val The returned value read from the syscon register
9899
*
99-
* @return 0 on success, negative on error
100+
* @return 0 on success.
101+
* @return -ENOSYS If the API or function isn't implemented.
100102
*/
101103
__syscall int syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val);
102104

103105
static inline int z_impl_syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val)
104106
{
105107
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
106108

107-
if (api == NULL) {
108-
return -ENOTSUP;
109+
if ((api == NULL) || (api->read == NULL)) {
110+
return -ENOSYS;
109111
}
110112

111113
return api->read(dev, reg, val);
@@ -121,16 +123,17 @@ static inline int z_impl_syscon_read_reg(const struct device *dev, uint16_t reg,
121123
* @param reg The register offset
122124
* @param val The value to be written in the register
123125
*
124-
* @return 0 on success, negative on error
126+
* @return 0 on success.
127+
* @return -ENOSYS If the API or function isn't implemented.
125128
*/
126129
__syscall int syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val);
127130

128131
static inline int z_impl_syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val)
129132
{
130133
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
131134

132-
if (api == NULL) {
133-
return -ENOTSUP;
135+
if ((api == NULL) || (api->write == NULL)) {
136+
return -ENOSYS;
134137
}
135138

136139
return api->write(dev, reg, val);
@@ -142,13 +145,18 @@ static inline int z_impl_syscon_write_reg(const struct device *dev, uint16_t reg
142145
* @param dev The device to get the register size for.
143146
* @param size Pointer to write the size to.
144147
* @return 0 for success.
148+
* @return -ENOSYS If the API or function isn't implemented.
145149
*/
146150
__syscall int syscon_get_size(const struct device *dev, size_t *size);
147151

148152
static inline int z_impl_syscon_get_size(const struct device *dev, size_t *size)
149153
{
150154
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
151155

156+
if ((api == NULL) || (api->get_size == NULL)) {
157+
return -ENOSYS;
158+
}
159+
152160
return api->get_size(dev, size);
153161
}
154162

0 commit comments

Comments
 (0)