1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/init.h>
11 #include <zephyr/ztest.h>
12 #include <zephyr/sys/printk.h>
13 #include <zephyr/linker/sections.h>
14 #include "abstract_driver.h"
15
16
17 #define DUMMY_PORT_1 "dummy"
18 #define DUMMY_PORT_2 "dummy_driver"
19 #define DUMMY_NOINIT "dummy_noinit"
20 #define BAD_DRIVER "bad_driver"
21 #define DUMMY_DEINIT "dummy_deinit"
22
23 #define MY_DRIVER_A "my_driver_A"
24 #define MY_DRIVER_B "my_driver_B"
25
26 #define FAKEDEFERDRIVER0 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e7000000))
27 #define FAKEDEFERDRIVER1 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e8000000))
28 #define FAKEDEFERDRIVER2 DEVICE_DT_GET(DT_PATH(fakedeferdriver_f9000000))
29
30 #define FAKEDRIVER0_NODEID DT_PATH(fakedriver_e0000000)
31 #define FAKEDRIVER0_NODELABEL "fake_driver_label"
32
33 /* A device without init call */
34 DEVICE_DEFINE(dummy_noinit, DUMMY_NOINIT, NULL, NULL, NULL, NULL,
35 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
36
37 /* To access from userspace, the device needs an API. Use a dummy GPIO one */
38 static DEVICE_API(gpio, fakedeferdriverapi);
39
40 /* Fake deferred devices */
41 DEVICE_DT_DEFINE(DT_INST(0, fakedeferdriver), NULL, NULL, NULL, NULL,
42 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
43 DEVICE_DT_DEFINE(DT_INST(1, fakedeferdriver), NULL, NULL, NULL, NULL,
44 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
45 &fakedeferdriverapi);
46
47 /* fake devices used to test deferred initialization failure */
48 static int fakedeferdriver_init(const struct device *dev);
49
50 DEVICE_DT_DEFINE(DT_INST(2, fakedeferdriver), fakedeferdriver_init, NULL, NULL, NULL, POST_KERNEL,
51 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
52
53 /**
54 * @brief Test cases to verify device objects
55 *
56 * Verify zephyr device driver apis with different device types
57 *
58 * @defgroup kernel_device_tests Device
59 *
60 * @ingroup all_tests
61 *
62 * @{
63 */
64
65 /**
66 * @brief Test device object binding
67 *
68 * Validates device binding for an existing and a non-existing device object.
69 * It creates a dummy_driver device object with basic init and configuration
70 * information and validates its binding.
71 *
72 * Validates three kinds situations of driver object:
73 * 1. A non-existing device object.
74 * 2. An existing device object with basic init and configuration information.
75 * 3. A failed init device object.
76 *
77 * @ingroup kernel_device_tests
78 *
79 * @see device_get_binding(), DEVICE_DEFINE()
80 */
ZTEST(device,test_dummy_device)81 ZTEST(device, test_dummy_device)
82 {
83 const struct device *dev;
84
85 /* Validates device binding for a non-existing device object */
86 dev = device_get_binding(DUMMY_PORT_1);
87 zassert_is_null(dev);
88
89 /* Validates device binding for an existing device object */
90 dev = device_get_binding(DUMMY_PORT_2);
91 zassert_not_null(dev);
92
93 /* Validates device binding for an existing device object */
94 dev = device_get_binding(DUMMY_NOINIT);
95 zassert_not_null(dev);
96
97 /* device_get_binding() returns false for device object
98 * with failed init.
99 */
100 dev = device_get_binding(BAD_DRIVER);
101 zassert_is_null(dev);
102 }
103
104 /**
105 * @brief Test device binding for existing device
106 *
107 * Validates device binding for an existing device object.
108 *
109 * @see device_get_binding(), DEVICE_DEFINE()
110 */
ZTEST_USER(device,test_dynamic_name)111 ZTEST_USER(device, test_dynamic_name)
112 {
113 const struct device *mux;
114 char name[sizeof(DUMMY_PORT_2)];
115
116 snprintk(name, sizeof(name), "%s", DUMMY_PORT_2);
117 mux = device_get_binding(name);
118 zassert_true(mux != NULL);
119 }
120
121 /**
122 * @brief Test device binding for non-existing device
123 *
124 * Validates binding of a random device driver(non-defined driver) named
125 * "ANOTHER_BOGUS_NAME".
126 *
127 * @see device_get_binding(), DEVICE_DEFINE()
128 */
ZTEST_USER(device,test_bogus_dynamic_name)129 ZTEST_USER(device, test_bogus_dynamic_name)
130 {
131 const struct device *mux;
132 char name[64];
133
134 snprintk(name, sizeof(name), "ANOTHER_BOGUS_NAME");
135 mux = device_get_binding(name);
136 zassert_true(mux == NULL);
137 }
138
139 /**
140 * @brief Test device binding for passing null name
141 *
142 * Validates device binding for device object when given dynamic name is null.
143 *
144 * @see device_get_binding(), DEVICE_DEFINE()
145 */
ZTEST_USER(device,test_null_dynamic_name)146 ZTEST_USER(device, test_null_dynamic_name)
147 {
148 /* Supplying a NULL dynamic name may trigger a SecureFault and
149 * lead to system crash in TrustZone enabled Non-Secure builds.
150 */
151 #if defined(CONFIG_USERSPACE) && !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
152 const struct device *mux;
153 char *drv_name = NULL;
154
155 mux = device_get_binding(drv_name);
156 zassert_equal(mux, 0);
157 #else
158 ztest_test_skip();
159 #endif
160 }
161
162 __pinned_bss
163 static struct init_record {
164 bool pre_kernel;
165 bool is_in_isr;
166 bool is_pre_kernel;
167 bool could_yield;
168 } init_records[4];
169
170 __pinned_data
171 static struct init_record *rp = init_records;
172
173 __pinned_func
add_init_record(bool pre_kernel)174 static int add_init_record(bool pre_kernel)
175 {
176 rp->pre_kernel = pre_kernel;
177 rp->is_pre_kernel = k_is_pre_kernel();
178 rp->is_in_isr = k_is_in_isr();
179 rp->could_yield = k_can_yield();
180 ++rp;
181 return 0;
182 }
183
184 __pinned_func
pre1_fn(void)185 static int pre1_fn(void)
186 {
187 return add_init_record(true);
188 }
189
190 __pinned_func
pre2_fn(void)191 static int pre2_fn(void)
192 {
193 return add_init_record(true);
194 }
195
post_fn(void)196 static int post_fn(void)
197 {
198 return add_init_record(false);
199 }
200
app_fn(void)201 static int app_fn(void)
202 {
203 return add_init_record(false);
204 }
205
206 SYS_INIT(pre1_fn, PRE_KERNEL_1, 0);
207 SYS_INIT(pre2_fn, PRE_KERNEL_2, 0);
208 SYS_INIT(post_fn, POST_KERNEL, 0);
209 SYS_INIT(app_fn, APPLICATION, 0);
210
211 /* This is an error case which driver initializes failed in SYS_INIT .*/
null_driver_init(void)212 static int null_driver_init(void)
213 {
214 return -EINVAL;
215 }
216
217 SYS_INIT(null_driver_init, POST_KERNEL, 0);
218
219 /**
220 * @brief Test detection of initialization before kernel services available.
221 *
222 * Confirms check is correct.
223 *
224 * @see k_is_pre_kernel()
225 */
ZTEST(device,test_pre_kernel_detection)226 ZTEST(device, test_pre_kernel_detection)
227 {
228 struct init_record *rpe = rp;
229
230 zassert_equal(rp - init_records, 4U,
231 "bad record count");
232 rp = init_records;
233 while ((rp < rpe) && rp->pre_kernel) {
234 zassert_equal(rp->is_in_isr, false,
235 "rec %zu isr", rp - init_records);
236 zassert_equal(rp->is_pre_kernel, true,
237 "rec %zu pre-kernel", rp - init_records);
238 zassert_equal(rp->could_yield, false,
239 "rec %zu could-yield", rp - init_records);
240 ++rp;
241 }
242 zassert_equal(rp - init_records, 2U,
243 "bad pre-kernel count");
244
245 while (rp < rpe) {
246 zassert_equal(rp->is_in_isr, false,
247 "rec %zu isr", rp - init_records);
248 zassert_equal(rp->is_pre_kernel, false,
249 "rec %zu post-kernel", rp - init_records);
250 zassert_equal(rp->could_yield, true,
251 "rec %zu could-yield", rp - init_records);
252 ++rp;
253 }
254 }
255
256 /**
257 * @brief Test system device list query API.
258 *
259 * It queries the list of devices in the system, used to suspend or
260 * resume the devices in PM applications.
261 *
262 * @see z_device_get_all_static()
263 */
ZTEST(device,test_device_list)264 ZTEST(device, test_device_list)
265 {
266 struct device const *devices;
267 size_t devcount = z_device_get_all_static(&devices);
268 bool found = false;
269
270 zassert_true(devcount > 0, "Should have at least one static device");
271 zassert_not_null(devices);
272 for (size_t i = 0; i < devcount; i++) {
273 struct device const *dev = devices + i;
274
275 if (strcmp(dev->name, DUMMY_NOINIT) == 0) {
276 found = true;
277 break;
278 }
279 }
280 zassert_true(found, "%s should be present in static device list", DUMMY_NOINIT);
281 }
282
283 static int sys_init_counter;
284
init_fn(void)285 static int init_fn(void)
286 {
287 sys_init_counter++;
288 return 0;
289 }
290
291 SYS_INIT(init_fn, APPLICATION, 0);
292 SYS_INIT_NAMED(init1, init_fn, APPLICATION, 1);
293 SYS_INIT_NAMED(init2, init_fn, APPLICATION, 2);
294 SYS_INIT_NAMED(init3, init_fn, APPLICATION, 2);
295 SYS_INIT_NAMED(init4, init_fn, APPLICATION, 99);
296 SYS_INIT_NAMED(init5, init_fn, APPLICATION, 999);
297
ZTEST(device,test_sys_init_multiple)298 ZTEST(device, test_sys_init_multiple)
299 {
300 zassert_equal(sys_init_counter, 6, "");
301 }
302
303 /* this is for storing sequence during initialization */
304 extern int init_level_sequence[4];
305 extern int init_priority_sequence[4];
306 extern int init_sub_priority_sequence[3];
307 extern unsigned int seq_level_cnt;
308 extern unsigned int seq_priority_cnt;
309
310 /**
311 * @brief Test initialization level for device driver instances
312 *
313 * @details After the defined device instances have initialized, we check the
314 * sequence number that each driver stored during initialization. If the
315 * sequence of initial level stored is corresponding with our expectation, it
316 * means assigning the level for driver instance works.
317 *
318 * @ingroup kernel_device_tests
319 */
ZTEST(device,test_device_init_level)320 ZTEST(device, test_device_init_level)
321 {
322 bool seq_correct = true;
323
324 /* we check if the stored executing sequence for different level is
325 * correct, and it should be 1, 2, 3
326 */
327 for (int i = 0; i < 3; i++) {
328 if (init_level_sequence[i] != (i + 1)) {
329 seq_correct = false;
330 }
331 }
332
333 zassert_true((seq_correct == true),
334 "init sequence is not correct");
335 }
336
337 /**
338 * @brief Test initialization priorities for device driver instances
339 *
340 * @details After the defined device instances have initialized, we check the
341 * sequence number that each driver stored during initialization. If the
342 * sequence of initial priority stored is corresponding with our expectation, it
343 * means assigning the priority for driver instance works.
344 *
345 * @ingroup kernel_device_tests
346 */
ZTEST(device,test_device_init_priority)347 ZTEST(device, test_device_init_priority)
348 {
349 bool sequence_correct = true;
350
351 /* we check if the stored pexecuting sequence for priority is correct,
352 * and it should be 1, 2, 3, 4
353 */
354 for (int i = 0; i < 4; i++) {
355 if (init_priority_sequence[i] != (i + 1)) {
356 sequence_correct = false;
357 }
358 }
359
360 zassert_true((sequence_correct == true),
361 "init sequence is not correct");
362 }
363
364 /**
365 * @brief Test initialization sub-priorities for device driver instances
366 *
367 * @details After the defined device instances have initialized, we check the
368 * sequence number that each driver stored during initialization. If the
369 * sequence of initial priority stored is corresponding with our expectation, it
370 * means using the devicetree for sub-priority sorting works.
371 *
372 * @ingroup kernel_device_tests
373 */
ZTEST(device,test_device_init_sub_priority)374 ZTEST(device, test_device_init_sub_priority)
375 {
376 /* fakedomain_1 depends on fakedomain_0 which depends on fakedomain_2,
377 * therefore we require that the initialisation runs in the reverse order.
378 */
379 zassert_equal(init_sub_priority_sequence[0], 1, "");
380 zassert_equal(init_sub_priority_sequence[1], 2, "");
381 zassert_equal(init_sub_priority_sequence[2], 0, "");
382 }
383
384 /**
385 * @brief Test abstraction of device drivers with common functionalities
386 *
387 * @details Abstraction of device drivers with common functionalities
388 * shall be provided as an intermediate interface between applications
389 * and device drivers, where such interface is implemented by individual
390 * device drivers. We verify this by following step:
391
392 * 1. Define a subsystem api for drivers.
393 * 2. Define and create two driver instances.
394 * 3. Two drivers call the same subsystem API, and we verify that each
395 * driver instance will call their own implementations.
396 *
397 * @ingroup kernel_device_tests
398 */
ZTEST(device,test_abstraction_driver_common)399 ZTEST(device, test_abstraction_driver_common)
400 {
401 const struct device *dev;
402 int ret;
403 int foo = 2;
404 int bar = 1;
405 unsigned int baz = 0;
406
407 /* verify driver A API has called */
408 dev = device_get_binding(MY_DRIVER_A);
409 zassert_false((dev == NULL));
410
411 ret = abstract_do_this(dev, foo, bar);
412 zassert_true(ret == (foo + bar), "common API do_this fail");
413
414 abstract_do_that(dev, &baz);
415 zassert_true(baz == 1, "common API do_that fail");
416
417 /* verify driver B API has called */
418 dev = device_get_binding(MY_DRIVER_B);
419 zassert_false((dev == NULL));
420
421 ret = abstract_do_this(dev, foo, bar);
422 zassert_true(ret == (foo - bar), "common API do_this fail");
423
424 abstract_do_that(dev, &baz);
425 zassert_true(baz == 2, "common API do_that fail");
426 }
427
ZTEST(device,test_deferred_init)428 ZTEST(device, test_deferred_init)
429 {
430 int ret;
431
432 zassert_false(device_is_ready(FAKEDEFERDRIVER0));
433
434 ret = device_init(FAKEDEFERDRIVER0);
435 zassert_true(ret == 0);
436
437 zassert_true(device_is_ready(FAKEDEFERDRIVER0));
438 }
439
fakedeferdriver_init(const struct device * dev)440 static int fakedeferdriver_init(const struct device *dev)
441 {
442 return -EIO;
443 }
444
445 /**
446 * @brief Test deferred initialization error
447 *
448 * @details Verify device_init error cases and expected device states
449 *
450 * - case -errno: if the device initialization fails
451 * - case -EALREADY: if the device is already initialized.
452 *
453 * @see device_init
454 * @ingroup kernel_device_tests
455 */
ZTEST(device,test_deferred_init_failure)456 ZTEST(device, test_deferred_init_failure)
457 {
458 int ret;
459 const struct device *dev = FAKEDEFERDRIVER2;
460
461 zassert_false(device_is_ready(dev));
462 ret = device_init(dev);
463 zassert_equal(ret, -EIO);
464 zassert_false(device_is_ready(dev));
465 zassert_equal(dev->state->init_res, EIO);
466
467 ret = device_init(dev);
468 zassert_equal(ret, -EALREADY);
469 zassert_equal(dev->state->init_res, EIO);
470 }
471
ZTEST(device,test_device_api)472 ZTEST(device, test_device_api)
473 {
474 const struct device *dev;
475
476 dev = device_get_binding(MY_DRIVER_A);
477 zexpect_true(DEVICE_API_IS(abstract, dev));
478
479 dev = device_get_binding(MY_DRIVER_B);
480 zexpect_true(DEVICE_API_IS(abstract, dev));
481
482 dev = device_get_binding(DUMMY_NOINIT);
483 zexpect_false(DEVICE_API_IS(abstract, dev));
484 }
485
ZTEST_USER(device,test_deferred_init_user)486 ZTEST_USER(device, test_deferred_init_user)
487 {
488 int ret;
489
490 zassert_false(device_is_ready(FAKEDEFERDRIVER1));
491
492 ret = device_init(FAKEDEFERDRIVER1);
493 zassert_true(ret == 0);
494
495 zassert_true(device_is_ready(FAKEDEFERDRIVER1));
496 }
497
ZTEST(device,test_deinit_not_supported)498 ZTEST(device, test_deinit_not_supported)
499 {
500 const struct device *dev = device_get_binding(DUMMY_NOINIT);
501 int ret;
502
503 zassert_not_null(dev);
504
505 ret = device_deinit(dev);
506 zassert_equal(ret, -ENOTSUP, "Expected -ENOTSUP for device_deinit when not supported");
507 }
508
dummy_deinit(const struct device * dev)509 static int dummy_deinit(const struct device *dev)
510 {
511 return 0;
512 }
513
514 /* A device with de-initialization function */
515 DEVICE_DEINIT_DEFINE(dummy_deinit, DUMMY_DEINIT, NULL, dummy_deinit, NULL, NULL, NULL, POST_KERNEL,
516 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
517
ZTEST(device,test_deinit_success_and_redeinit)518 ZTEST(device, test_deinit_success_and_redeinit)
519 {
520 const struct device *dev = device_get_binding(DUMMY_DEINIT);
521 int ret;
522
523 zassert_not_null(dev);
524
525 ret = device_deinit(dev);
526 zassert_equal(ret, 0, "device_deinit should succeed");
527
528 ret = device_deinit(dev);
529 zassert_equal(ret, -EPERM, "device_deinit should fail when not init or already deinit");
530 }
531
532 #ifdef CONFIG_DEVICE_DT_METADATA
533 DEVICE_DT_DEFINE(FAKEDRIVER0_NODEID, NULL, NULL, NULL, NULL, POST_KERNEL,
534 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
535
ZTEST(device,test_device_get_by_dt_nodelabel)536 ZTEST(device, test_device_get_by_dt_nodelabel)
537 {
538 const struct device *dev = DEVICE_DT_GET(FAKEDRIVER0_NODEID);
539
540 zassert_not_null(dev);
541
542 const struct device *valid = device_get_by_dt_nodelabel(FAKEDRIVER0_NODELABEL);
543
544 zassert_not_null(valid, "Valid DT nodelabel should return a device");
545
546 const struct device *invalid = device_get_by_dt_nodelabel("does_not_exist");
547
548 zassert_is_null(invalid, "Invalid DT nodelabel should return NULL");
549 }
550 #endif
551
user_setup(void)552 void *user_setup(void)
553 {
554 #ifdef CONFIG_USERSPACE
555 k_object_access_grant(FAKEDEFERDRIVER1, k_current_get());
556 #endif
557
558 return NULL;
559 }
560
561 /**
562 * @}
563 */
564
565 ZTEST_SUITE(device, NULL, user_setup, NULL, NULL, NULL);
566