1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/adc.h>
8 #include <zephyr/internal/syscall_handler.h>
9 #include <zephyr/kernel.h>
10 
z_vrfy_adc_channel_setup(const struct device * dev,const struct adc_channel_cfg * user_channel_cfg)11 static inline int z_vrfy_adc_channel_setup(const struct device *dev,
12 					   const struct adc_channel_cfg *user_channel_cfg)
13 {
14 	struct adc_channel_cfg channel_cfg;
15 
16 	K_OOPS(K_SYSCALL_DRIVER_ADC(dev, channel_setup));
17 	K_OOPS(k_usermode_from_copy(&channel_cfg,
18 				(struct adc_channel_cfg *)user_channel_cfg,
19 				sizeof(struct adc_channel_cfg)));
20 
21 	return z_impl_adc_channel_setup((const struct device *)dev,
22 					&channel_cfg);
23 }
24 #include <syscalls/adc_channel_setup_mrsh.c>
25 
copy_sequence(struct adc_sequence * dst,struct adc_sequence_options * options,struct adc_sequence * src)26 static bool copy_sequence(struct adc_sequence *dst,
27 			  struct adc_sequence_options *options,
28 			  struct adc_sequence *src)
29 {
30 	if (k_usermode_from_copy(dst, src, sizeof(struct adc_sequence)) != 0) {
31 		printk("couldn't copy adc_sequence struct\n");
32 		return false;
33 	}
34 
35 	if (dst->options) {
36 		if (k_usermode_from_copy(options, dst->options,
37 				sizeof(struct adc_sequence_options)) != 0) {
38 			printk("couldn't copy adc_options struct\n");
39 			return false;
40 		}
41 		dst->options = options;
42 	}
43 
44 	if (K_SYSCALL_MEMORY_WRITE(dst->buffer, dst->buffer_size) != 0) {
45 		printk("no access to buffer memory\n");
46 		return false;
47 	}
48 	return true;
49 }
50 
z_vrfy_adc_read(const struct device * dev,const struct adc_sequence * user_sequence)51 static inline int z_vrfy_adc_read(const struct device *dev,
52 				  const struct adc_sequence *user_sequence)
53 
54 {
55 	struct adc_sequence sequence;
56 	struct adc_sequence_options options;
57 
58 	K_OOPS(K_SYSCALL_DRIVER_ADC(dev, read));
59 	K_OOPS(K_SYSCALL_VERIFY_MSG(copy_sequence(&sequence, &options,
60 					(struct adc_sequence *)user_sequence),
61 				    "invalid ADC sequence"));
62 	if (sequence.options != NULL) {
63 		K_OOPS(K_SYSCALL_VERIFY_MSG(sequence.options->callback == NULL,
64 			    "ADC sequence callbacks forbidden from user mode"));
65 	}
66 
67 	return z_impl_adc_read((const struct device *)dev, &sequence);
68 }
69 #include <syscalls/adc_read_mrsh.c>
70 
71 #ifdef CONFIG_ADC_ASYNC
z_vrfy_adc_read_async(const struct device * dev,const struct adc_sequence * user_sequence,struct k_poll_signal * async)72 static inline int z_vrfy_adc_read_async(const struct device *dev,
73 					const struct adc_sequence *user_sequence,
74 					struct k_poll_signal *async)
75 {
76 	struct adc_sequence sequence;
77 	struct adc_sequence_options options;
78 
79 	K_OOPS(K_SYSCALL_DRIVER_ADC(dev, read_async));
80 	K_OOPS(K_SYSCALL_VERIFY_MSG(copy_sequence(&sequence, &options,
81 					(struct adc_sequence *)user_sequence),
82 				    "invalid ADC sequence"));
83 	if (sequence.options != NULL) {
84 		K_OOPS(K_SYSCALL_VERIFY_MSG(sequence.options->callback == NULL,
85 			    "ADC sequence callbacks forbidden from user mode"));
86 	}
87 	K_OOPS(K_SYSCALL_OBJ(async, K_OBJ_POLL_SIGNAL));
88 
89 	return z_impl_adc_read_async((const struct device *)dev, &sequence,
90 				     (struct k_poll_signal *)async);
91 }
92 #include <syscalls/adc_read_async_mrsh.c>
93 #endif /* CONFIG_ADC_ASYNC */
94