diff --git a/include/device.h b/include/device.h index 7e2b95a04d9..cbb20dba63d 100644 --- a/include/device.h +++ b/include/device.h @@ -28,6 +28,7 @@ extern "C" { #endif +#define Z_DEVICE_MAX_NAME_LEN 48 /** * @def DEVICE_INIT @@ -37,7 +38,8 @@ extern "C" { * @details This macro defines a device object that is automatically * configured by the kernel during system initialization. * - * @param dev_name Device name. + * @param dev_name Device name. This must be less than Z_DEVICE_MAX_NAME_LEN + * characters in order to be looked up from user mode with device_get_binding(). * * @param drv_name The name this instance of the driver exposes to * the system. @@ -248,7 +250,7 @@ void _sys_device_do_config_level(s32_t level); * * @return pointer to device structure; NULL if not found or cannot be used. */ -struct device *device_get_binding(const char *name); +__syscall struct device *device_get_binding(const char *name); /** * @brief Device Power Management APIs @@ -436,6 +438,8 @@ int device_busy_check(struct device *chk_dev); * @} */ +#include + #ifdef __cplusplus } #endif diff --git a/kernel/device.c b/kernel/device.c index ec3515ccad2..5ab6fc2799e 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -9,6 +9,7 @@ #include #include #include +#include extern struct device __device_init_start[]; extern struct device __device_PRE_KERNEL_1_start[]; @@ -56,7 +57,7 @@ void _sys_device_do_config_level(s32_t level) } } -struct device *device_get_binding(const char *name) +struct device *_impl_device_get_binding(const char *name) { struct device *info; @@ -85,6 +86,20 @@ struct device *device_get_binding(const char *name) return NULL; } +#ifdef CONFIG_USERSPACE +Z_SYSCALL_HANDLER(device_get_binding, name) +{ + char name_copy[Z_DEVICE_MAX_NAME_LEN]; + + if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy)) + != 0) { + return 0; + } + + return (u32_t)_impl_device_get_binding(name_copy); +} +#endif /* CONFIG_USERSPACE */ + #ifdef CONFIG_DEVICE_POWER_MANAGEMENT int device_pm_control_nop(struct device *unused_device, u32_t unused_ctrl_command, void *unused_context)