Debian Codegraph @

dpll/dpll.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * dpll.c DPLL tool
4 *
5 * Authors: Petr Oros <[email protected]>
6 */
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include <inttypes.h>
12 #include <poll.h>
13 #include <signal.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/signalfd.h>
19 #include <unistd.h>
20 #include <linux/dpll.h>
21 #include <linux/genetlink.h>
22 #include <libmnl/libmnl.h>
23
24 #include <mnlg.h>
25 #include "mnl_utils.h"
26 #include "version.h"
27 #include "utils.h"
28 #include "json_print.h"
29
30 #define pr_err(args...) fprintf(stderr, ##args)
31
32 int json;
33
34 struct dpll {
35 struct mnlu_gen_socket nlg;
36 int argc;
37 char **argv;
38 };
39
40 static const char *str_enable_disable(bool v)
41 {
42 return v ? "enable" : "disable";
43 }
44
45 static struct str_num_map dpll_mode_map[] = {
46 { .str = "automatic", .num = DPLL_MODE_AUTOMATIC },
47 { .str = "manual", .num = DPLL_MODE_MANUAL },
48 {
49 .str = NULL,
50 },
51 };
52
53 static struct str_num_map pin_state_map[] = {
54 { .str = "connected", .num = DPLL_PIN_STATE_CONNECTED },
55 { .str = "disconnected", .num = DPLL_PIN_STATE_DISCONNECTED },
56 { .str = "selectable", .num = DPLL_PIN_STATE_SELECTABLE },
57 {
58 .str = NULL,
59 },
60 };
61
62 static struct str_num_map pin_type_map[] = {
63 { .str = "mux", .num = DPLL_PIN_TYPE_MUX },
64 { .str = "ext", .num = DPLL_PIN_TYPE_EXT },
65 { .str = "synce-eth-port", .num = DPLL_PIN_TYPE_SYNCE_ETH_PORT },
66 { .str = "int-oscillator", .num = DPLL_PIN_TYPE_INT_OSCILLATOR },
67 { .str = "gnss", .num = DPLL_PIN_TYPE_GNSS },
68 {
69 .str = NULL,
70 },
71 };
72
73 static struct str_num_map pin_direction_map[] = {
74 { .str = "input", .num = DPLL_PIN_DIRECTION_INPUT },
75 { .str = "output", .num = DPLL_PIN_DIRECTION_OUTPUT },
76 {
77 .str = NULL,
78 },
79 };
80
81 static int dpll_argc(struct dpll *dpll)
82 {
83 return dpll->argc;
84 }
85
86 static const char *dpll_argv(struct dpll *dpll)
87 {
88 if (dpll_argc(dpll) == 0)
89 return NULL;
90 return *dpll->argv;
91 }
92
93 static void dpll_arg_inc(struct dpll *dpll)
94 {
95 if (dpll_argc(dpll) == 0)
96 return;
97 dpll->argc--;
98 dpll->argv++;
99 }
100
101 static const char *dpll_argv_next(struct dpll *dpll)
102 {
103 const char *ret;
104
105 dpll_arg_inc(dpll);
106 if (dpll_argc(dpll) == 0)
107 return NULL;
108
109 ret = *dpll->argv;
110 dpll_arg_inc(dpll);
111 return ret;
112 }
113
114 static bool dpll_argv_match(struct dpll *dpll, const char *pattern)
115 {
116 if (dpll_argc(dpll) == 0)
117 return false;
118 return strcmp(dpll_argv(dpll), pattern) == 0;
119 }
120
121 static int dpll_arg_required(struct dpll *dpll, const char *arg_name)
122 {
123 if (dpll_argc(dpll) == 0) {
124 pr_err("%s requires an argument\n", arg_name);
125 return -EINVAL;
126 }
127 return 0;
128 }
129
130 static bool dpll_argv_match_inc(struct dpll *dpll, const char *pattern)
131 {
132 if (!dpll_argv_match(dpll, pattern))
133 return false;
134 dpll_arg_inc(dpll);
135 return true;
136 }
137
138 static bool dpll_no_arg(struct dpll *dpll)
139 {
140 return dpll_argc(dpll) == 0;
141 }
142
143 static int str_to_dpll_mode(const char *mode_str, __u32 *mode)
144 {
145 int num;
146
147 num = str_map_lookup_str(dpll_mode_map, mode_str);
148 if (num < 0)
149 return num;
150 *mode = num;
151 return 0;
152 }
153
154 static int str_to_dpll_pin_state(const char *state_str, __u32 *state)
155 {
156 int num;
157
158 num = str_map_lookup_str(pin_state_map, state_str);
159 if (num < 0)
160 return num;
161 *state = num;
162 return 0;
163 }
164
165 static int str_to_dpll_pin_type(const char *type_str, __u32 *type)
166 {
167 int num;
168
169 num = str_map_lookup_str(pin_type_map, type_str);
170 if (num < 0)
171 return num;
172 *type = num;
173 return 0;
174 }
175
176 static int dpll_parse_mode(struct dpll *dpll, __u32 *mode)
177 {
178 const char *str = dpll_argv(dpll);
179
180 if (str_to_dpll_mode(str, mode)) {
181 pr_err("invalid state: %s (use automatic/manual)\n", str);
182 return -EINVAL;
183 }
184 dpll_arg_inc(dpll);
185 return 0;
186 }
187
188 static int dpll_parse_state(struct dpll *dpll, __u32 *state)
189 {
190 const char *str = dpll_argv(dpll);
191
192 if (str_to_dpll_pin_state(str, state)) {
193 pr_err("invalid state: %s (use connected/disconnected/selectable)\n",
194 str);
195 return -EINVAL;
196 }
197 dpll_arg_inc(dpll);
198 return 0;
199 }
200
201 static int dpll_parse_direction(struct dpll *dpll, __u32 *direction)
202 {
203 const char *str = dpll_argv(dpll);
204 int num;
205
206 num = str_map_lookup_str(pin_direction_map, str);
207 if (num < 0) {
208 pr_err("invalid direction: %s (use input/output)\n", str);
209 return num;
210 }
211 *direction = num;
212 dpll_arg_inc(dpll);
213 return 0;
214 }
215
216 static int dpll_parse_pin_type(struct dpll *dpll, __u32 *type)
217 {
218 const char *str = dpll_argv(dpll);
219
220 if (str_to_dpll_pin_type(str, type)) {
221 pr_err("invalid type: %s (use mux/ext/synce-eth-port/int-oscillator/gnss)\n",
222 str);
223 return -EINVAL;
224 }
225 dpll_arg_inc(dpll);
226 return 0;
227 }
228
229 static int dpll_parse_u32(struct dpll *dpll, const char *arg_name,
230 __u32 *val_ptr)
231 {
232 const char *__str = dpll_argv_next(dpll);
233
234 if (!__str) {
235 pr_err("%s requires an argument\n", arg_name);
236 return -EINVAL;
237 }
238 if (get_u32(val_ptr, __str, 0)) {
239 pr_err("invalid %s: %s\n", arg_name, __str);
240 return -EINVAL;
241 }
242 return 0;
243 }
244
245 static int dpll_parse_attr_u32(struct dpll *dpll, struct nlmsghdr *nlh,
246 const char *arg_name, int attr_id)
247 {
248 __u32 val;
249
250 if (dpll_parse_u32(dpll, arg_name, &val))
251 return -EINVAL;
252 mnl_attr_put_u32(nlh, attr_id, val);
253 return 0;
254 }
255
256 static int dpll_parse_attr_s32(struct dpll *dpll, struct nlmsghdr *nlh,
257 const char *arg_name, int attr_id)
258 {
259 const char *str = dpll_argv_next(dpll);
260 __s32 val;
261
262 if (!str) {
263 pr_err("%s requires an argument\n", arg_name);
264 return -EINVAL;
265 }
266 if (get_s32(&val, str, 0)) {
267 pr_err("invalid %s: %s\n", arg_name, str);
268 return -EINVAL;
269 }
270 mnl_attr_put(nlh, attr_id, sizeof(val), &val);
271 return 0;
272 }
273
274 static int dpll_parse_attr_u64(struct dpll *dpll, struct nlmsghdr *nlh,
275 const char *arg_name, int attr_id)
276 {
277 const char *str = dpll_argv_next(dpll);
278 __u64 val;
279
280 if (!str) {
281 pr_err("%s requires an argument\n", arg_name);
282 return -EINVAL;
283 }
284 if (get_u64(&val, str, 0)) {
285 pr_err("invalid %s: %s\n", arg_name, str);
286 return -EINVAL;
287 }
288 mnl_attr_put_u64(nlh, attr_id, val);
289 return 0;
290 }
291
292 static int dpll_parse_attr_str(struct dpll *dpll, struct nlmsghdr *nlh,
293 const char *arg_name, int attr_id)
294 {
295 const char *str = dpll_argv_next(dpll);
296
297 if (!str) {
298 pr_err("%s requires an argument\n", arg_name);
299 return -EINVAL;
300 }
301 mnl_attr_put_strz(nlh, attr_id, str);
302 return 0;
303 }
304
305 static int dpll_parse_attr_enum(struct dpll *dpll, struct nlmsghdr *nlh,
306 const char *arg_name, int attr_id,
307 int (*parse_func)(struct dpll *, __u32 *))
308 {
309 __u32 val;
310
311 if (dpll_arg_required(dpll, arg_name))
312 return -EINVAL;
313 if (parse_func(dpll, &val))
314 return -EINVAL;
315 mnl_attr_put_u32(nlh, attr_id, val);
316 return 0;
317 }
318
319 /* Macros for printing netlink attributes
320 * These macros combine the common pattern of:
321 *
322 * if (tb[ATTR])
323 * print_xxx(PRINT_ANY, "name", "format", mnl_attr_get_xxx(tb[ATTR]));
324 *
325 * Generic versions with custom format string (_FMT suffix)
326 * Simple versions auto-generate format string: " name: %d\n"
327 */
328
329 #define DPLL_PR_INT_FMT(tb, attr_id, name, format_str) \
330 do { \
331 if (tb[attr_id]) \
332 print_int( \
333 PRINT_ANY, name, format_str, \
334 *(__s32 *)mnl_attr_get_payload(tb[attr_id])); \
335 } while (0)
336
337 #define DPLL_PR_UINT_FMT(tb, attr_id, name, format_str) \
338 do { \
339 if (tb[attr_id]) \
340 print_uint(PRINT_ANY, name, format_str, \
341 mnl_attr_get_u32(tb[attr_id])); \
342 } while (0)
343
344 #define DPLL_PR_U64_FMT(tb, attr_id, name, format_str) \
345 do { \
346 if (tb[attr_id]) \
347 print_lluint(PRINT_ANY, name, format_str, \
348 mnl_attr_get_u64(tb[attr_id])); \
349 } while (0)
350
351 #define DPLL_PR_STR_FMT(tb, attr_id, name, format_str) \
352 do { \
353 if (tb[attr_id]) \
354 print_string(PRINT_ANY, name, format_str, \
355 mnl_attr_get_str(tb[attr_id])); \
356 } while (0)
357
358 /* Simple versions with auto-generated format */
359 #define DPLL_PR_INT(tb, attr_id, name) \
360 DPLL_PR_INT_FMT(tb, attr_id, name, " " name ": %d\n")
361
362 #define DPLL_PR_UINT(tb, attr_id, name) \
363 DPLL_PR_UINT_FMT(tb, attr_id, name, " " name ": %u\n")
364
365 #define DPLL_PR_U64(tb, attr_id, name) \
366 DPLL_PR_U64_FMT(tb, attr_id, name, " " name ": %" PRIu64 "\n")
367
368 /* Helper to read signed int (can be s32 or s64 depending on value) */
369 static __s64 mnl_attr_get_sint(const struct nlattr *attr)
370 {
371 if (mnl_attr_get_payload_len(attr) == sizeof(__s32))
372 return *(__s32 *)mnl_attr_get_payload(attr);
373 else
374 return *(__s64 *)mnl_attr_get_payload(attr);
375 }
376
377 #define DPLL_PR_SINT_FMT(tb, attr_id, name, format_str) \
378 do { \
379 if (tb[attr_id]) \
380 print_s64(PRINT_ANY, name, format_str, \
381 mnl_attr_get_sint(tb[attr_id])); \
382 } while (0)
383
384 #define DPLL_PR_SINT(tb, attr_id, name) \
385 DPLL_PR_SINT_FMT(tb, attr_id, name, " " name ": %" PRId64 "\n")
386
387 #define DPLL_PR_STR(tb, attr_id, name) \
388 DPLL_PR_STR_FMT(tb, attr_id, name, " " name ": %s\n")
389
390 /* Temperature macro - JSON prints raw millidegrees, human prints formatted */
391 #define DPLL_PR_TEMP(tb, attr_id) \
392 do { \
393 if (tb[attr_id]) { \
394 __s32 temp = mnl_attr_get_u32(tb[attr_id]); \
395 div_t d = div(temp, 1000); \
396 print_int(PRINT_JSON, "temp", NULL, temp); \
397 print_int(PRINT_FP, NULL, " temp: %d.", d.quot); \
398 print_int(PRINT_FP, NULL, "%03d C\n", d.rem); \
399 } \
400 } while (0)
401
402 /* Generic version with custom format */
403 #define DPLL_PR_ENUM_STR_FMT(tb, attr_id, name, format_str, name_func) \
404 do { \
405 if (tb[attr_id]) \
406 print_string( \
407 PRINT_ANY, name, format_str, \
408 name_func(mnl_attr_get_u32(tb[attr_id]))); \
409 } while (0)
410
411 /* Simple version with auto-generated format */
412 #define DPLL_PR_ENUM_STR(tb, attr_id, name, name_func) \
413 DPLL_PR_ENUM_STR_FMT(tb, attr_id, name, " " name ": %s\n", name_func)
414
415 /* Multi-attr enum printer - handles multiple occurrences of same attribute */
416 static void dpll_pr_multi_enum_str(const struct nlmsghdr *nlh, int attr_id,
417 const char *name,
418 const char *(*name_func)(__u32))
419 {
420 struct nlattr *attr;
421 bool first = true;
422
423 if (!nlh)
424 return;
425
426 mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr))
427 {
428 if (mnl_attr_get_type(attr) == attr_id) {
429 __u32 val = mnl_attr_get_u32(attr);
430
431 if (first) {
432 open_json_array(PRINT_JSON, name);
433 print_string(PRINT_FP, NULL, " %s:", name);
434 first = false;
435 }
436 print_string(PRINT_ANY, NULL, " %s", name_func(val));
437 }
438 }
439
440 if (first)
441 return;
442
443 close_json_array(PRINT_JSON, NULL);
444 print_nl();
445 }
446
447 /* Print frequency range (or single value if min==max) */
448 static void dpll_pr_freq_range(__u64 freq_min, __u64 freq_max)
449 {
450 open_json_object(NULL);
451
452 /* JSON: always print both min and max */
453 print_lluint(PRINT_JSON, "frequency-min", NULL, freq_min);
454 print_lluint(PRINT_JSON, "frequency-max", NULL, freq_max);
455
456 /* FP: print range or single value */
457 print_string(PRINT_FP, NULL, " ", NULL);
458 if (freq_min == freq_max) {
459 print_lluint(PRINT_FP, NULL, "%" PRIu64 " Hz\n", freq_min);
460 } else {
461 print_lluint(PRINT_FP, NULL, "%" PRIu64, freq_min);
462 print_string(PRINT_FP, NULL, "-", NULL);
463 print_lluint(PRINT_FP, NULL, "%" PRIu64 " Hz\n", freq_max);
464 }
465
466 close_json_object();
467 }
468
469 static void help(void)
470 {
471 pr_err("Usage: dpll [ OPTIONS ] OBJECT { COMMAND | help }\n"
472 " dpll [ -j[son] ] [ -p[retty] ]\n"
473 "where OBJECT := { device | pin | monitor }\n"
474 " OPTIONS := { -V[ersion] | -j[son] | -p[retty] }\n");
475 }
476
477 static int cmd_device(struct dpll *dpll);
478 static int cmd_pin(struct dpll *dpll);
479 static int cmd_monitor(struct dpll *dpll);
480
481 static int dpll_cmd(struct dpll *dpll, int argc, char **argv)
482 {
483 dpll->argc = argc;
484 dpll->argv = argv;
485
486 if (dpll_argv_match(dpll, "help") || dpll_no_arg(dpll)) {
487 help();
488 return 0;
489 } else if (dpll_argv_match_inc(dpll, "device")) {
490 return cmd_device(dpll);
491 } else if (dpll_argv_match_inc(dpll, "pin")) {
492 return cmd_pin(dpll);
493 } else if (dpll_argv_match_inc(dpll, "monitor")) {
494 return cmd_monitor(dpll);
495 }
496 pr_err("Object \"%s\" not found\n", dpll_argv(dpll));
497 return -ENOENT;
498 }
499
500 static int dpll_init(struct dpll *dpll)
501 {
502 int err;
503
504 err = mnlu_gen_socket_open(&dpll->nlg, "dpll", DPLL_FAMILY_VERSION);
505 if (err) {
506 pr_err("Failed to connect to DPLL Netlink (DPLL subsystem not available in kernel?)\n");
507 return -1;
508 }
509 return 0;
510 }
511
512 static void dpll_fini(struct dpll *dpll)
513 {
514 mnlu_gen_socket_close(&dpll->nlg);
515 }
516
517 static struct dpll *dpll_alloc(void)
518 {
519 struct dpll *dpll;
520
521 dpll = calloc(1, sizeof(*dpll));
522 if (!dpll)
523 return NULL;
524 return dpll;
525 }
526
527 static void dpll_free(struct dpll *dpll)
528 {
529 free(dpll);
530 }
531
532 int main(int argc, char **argv)
533 {
534 static const struct option long_options[] = {
535 { "Version", no_argument, NULL, 'V' },
536 { "json", no_argument, NULL, 'j' },
537 { "pretty", no_argument, NULL, 'p' },
538 { NULL, 0, NULL, 0 }
539 };
540 const char *opt_short = "Vjp";
541 struct dpll *dpll;
542 int err, opt, ret;
543
544 dpll = dpll_alloc();
545 if (!dpll) {
546 pr_err("Failed to allocate memory\n");
547 return EXIT_FAILURE;
548 }
549
550 while ((opt = getopt_long(argc, argv, opt_short, long_options, NULL)) >=
551 0) {
552 switch (opt) {
553 case 'V':
554 printf("dpll utility, iproute2-%s\n", version);
555 ret = EXIT_SUCCESS;
556 goto dpll_free;
557 case 'j':
558 json = 1;
559 break;
560 case 'p':
561 pretty = true;
562 break;
563 default:
564 pr_err("Unknown option.\n");
565 help();
566 ret = EXIT_FAILURE;
567 goto dpll_free;
568 }
569 }
570
571 argc -= optind;
572 argv += optind;
573
574 new_json_obj_plain(json);
575 open_json_object(NULL);
576
577 /* Skip netlink init for help commands */
578 bool need_nl = true;
579
580 if (argc > 0 && strcmp(argv[0], "help") == 0)
581 need_nl = false;
582 if (argc > 1 && strcmp(argv[1], "help") == 0)
583 need_nl = false;
584
585 if (need_nl) {
586 err = dpll_init(dpll);
587 if (err) {
588 ret = EXIT_FAILURE;
589 goto json_cleanup;
590 }
591 }
592
593 err = dpll_cmd(dpll, argc, argv);
594 if (err) {
595 ret = EXIT_FAILURE;
596 goto dpll_fini;
597 }
598
599 ret = EXIT_SUCCESS;
600
601 dpll_fini:
602 if (need_nl)
603 dpll_fini(dpll);
604 json_cleanup:
605 close_json_object();
606 delete_json_obj_plain();
607 dpll_free:
608 dpll_free(dpll);
609 return ret;
610 }
611
612 /*
613 * Device commands
614 */
615
616 static void cmd_device_help(void)
617 {
618 pr_err("Usage: dpll device show [ id DEVICE_ID ]\n");
619 pr_err(" dpll device set id DEVICE_ID [ mode { automatic | manual } ]\n");
620 pr_err(" [ phase-offset-monitor { enable | disable } ]\n");
621 pr_err(" [ phase-offset-avg-factor NUM ]\n");
622 pr_err(" dpll device id-get [ module-name NAME ] [ clock-id ID ] [ type TYPE ]\n");
623 }
624
625 static const char *dpll_mode_name(__u32 mode)
626 {
627 const char *str;
628
629 str = str_map_lookup_uint(dpll_mode_map, mode);
630 return str ? str : "unknown";
631 }
632
633 static const char *dpll_lock_status_name(__u32 status)
634 {
635 switch (status) {
636 case DPLL_LOCK_STATUS_UNLOCKED:
637 return "unlocked";
638 case DPLL_LOCK_STATUS_LOCKED:
639 return "locked";
640 case DPLL_LOCK_STATUS_LOCKED_HO_ACQ:
641 return "locked-ho-acq";
642 case DPLL_LOCK_STATUS_HOLDOVER:
643 return "holdover";
644 default:
645 return "unknown";
646 }
647 }
648
649 static const char *dpll_type_name(__u32 type)
650 {
651 switch (type) {
652 case DPLL_TYPE_PPS:
653 return "pps";
654 case DPLL_TYPE_EEC:
655 return "eec";
656 default:
657 return "unknown";
658 }
659 }
660
661 static int str_to_dpll_type(const char *s, __u32 *type)
662 {
663 if (!strcmp(s, "pps"))
664 *type = DPLL_TYPE_PPS;
665 else if (!strcmp(s, "eec"))
666 *type = DPLL_TYPE_EEC;
667 else
668 return -EINVAL;
669 return 0;
670 }
671
672 static const char *dpll_lock_status_error_name(__u32 error)
673 {
674 switch (error) {
675 case DPLL_LOCK_STATUS_ERROR_NONE:
676 return "none";
677 case DPLL_LOCK_STATUS_ERROR_UNDEFINED:
678 return "undefined";
679 case DPLL_LOCK_STATUS_ERROR_MEDIA_DOWN:
680 return "media-down";
681 case DPLL_LOCK_STATUS_ERROR_FRACTIONAL_FREQUENCY_OFFSET_TOO_HIGH:
682 return "fractional-frequency-offset-too-high";
683 default:
684 return "unknown";
685 }
686 }
687
688 static const char *dpll_clock_quality_level_name(__u32 level)
689 {
690 switch (level) {
691 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_PRC:
692 return "itu-opt1-prc";
693 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_SSU_A:
694 return "itu-opt1-ssu-a";
695 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_SSU_B:
696 return "itu-opt1-ssu-b";
697 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_EEC1:
698 return "itu-opt1-eec1";
699 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_PRTC:
700 return "itu-opt1-prtc";
701 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_EPRTC:
702 return "itu-opt1-eprtc";
703 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_EEEC:
704 return "itu-opt1-eeec";
705 case DPLL_CLOCK_QUALITY_LEVEL_ITU_OPT1_EPRC:
706 return "itu-opt1-eprc";
707 default:
708 return "unknown";
709 }
710 }
711
712 /* Netlink attribute parsing - device attributes */
713 static int attr_cb(const struct nlattr *attr, void *data)
714 {
715 int type = mnl_attr_get_type(attr);
716 const struct nlattr **tb = data;
717
718 if (mnl_attr_type_valid(attr, DPLL_A_MAX) < 0)
719 return MNL_CB_OK;
720
721 tb[type] = attr;
722 return MNL_CB_OK;
723 }
724
725 /* Netlink attribute parsing - pin attributes */
726 static int attr_pin_cb(const struct nlattr *attr, void *data)
727 {
728 int type = mnl_attr_get_type(attr);
729 const struct nlattr **tb = data;
730
731 if (mnl_attr_type_valid(attr, DPLL_A_PIN_MAX) < 0)
732 return MNL_CB_OK;
733
734 tb[type] = attr;
735 return MNL_CB_OK;
736 }
737
738 /* Print device attributes */
739 static void dpll_device_print_attrs(const struct nlmsghdr *nlh,
740 struct nlattr **tb)
741 {
742 DPLL_PR_UINT_FMT(tb, DPLL_A_ID, "id", "device id %u:\n");
743 DPLL_PR_STR(tb, DPLL_A_MODULE_NAME, "module-name");
744 DPLL_PR_ENUM_STR(tb, DPLL_A_MODE, "mode", dpll_mode_name);
745 DPLL_PR_U64(tb, DPLL_A_CLOCK_ID, "clock-id");
746 DPLL_PR_ENUM_STR(tb, DPLL_A_TYPE, "type", dpll_type_name);
747 DPLL_PR_ENUM_STR(tb, DPLL_A_LOCK_STATUS, "lock-status",
748 dpll_lock_status_name);
749 DPLL_PR_ENUM_STR(tb, DPLL_A_LOCK_STATUS_ERROR, "lock-status-error",
750 dpll_lock_status_error_name);
751 dpll_pr_multi_enum_str(nlh, DPLL_A_CLOCK_QUALITY_LEVEL,
752 "clock-quality-level",
753 dpll_clock_quality_level_name);
754 DPLL_PR_TEMP(tb, DPLL_A_TEMP);
755 dpll_pr_multi_enum_str(nlh, DPLL_A_MODE_SUPPORTED, "mode-supported",
756 dpll_mode_name);
757 DPLL_PR_ENUM_STR_FMT(tb, DPLL_A_PHASE_OFFSET_MONITOR,
758 "phase-offset-monitor",
759 " phase-offset-monitor: %s\n",
760 str_enable_disable);
761 DPLL_PR_UINT(tb, DPLL_A_PHASE_OFFSET_AVG_FACTOR,
762 "phase-offset-avg-factor");
763 }
764
765 /* Netlink callback - device get (single device) */
766 static int cmd_device_show_cb(const struct nlmsghdr *nlh, void *data)
767 {
768 struct nlattr *tb[DPLL_A_MAX + 1] = {};
769
770 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb);
771 dpll_device_print_attrs(nlh, tb);
772
773 return MNL_CB_OK;
774 }
775
776 /* Netlink callback - device dump (multiple devices) */
777 static int cmd_device_show_dump_cb(const struct nlmsghdr *nlh, void *data)
778 {
779 struct nlattr *tb[DPLL_A_MAX + 1] = {};
780
781 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb);
782
783 open_json_object(NULL);
784 dpll_device_print_attrs(nlh, tb);
785 close_json_object();
786
787 return MNL_CB_OK;
788 }
789
790 static int cmd_device_show_id(struct dpll *dpll, __u32 id)
791 {
792 struct nlmsghdr *nlh;
793 int err;
794
795 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_DEVICE_GET,
796 NLM_F_REQUEST | NLM_F_ACK);
797 mnl_attr_put_u32(nlh, DPLL_A_ID, id);
798
799 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_device_show_cb, NULL);
800 if (err < 0) {
801 pr_err("Failed to get device %u\n", id);
802 return -1;
803 }
804
805 return 0;
806 }
807
808 static int cmd_device_show_dump(struct dpll *dpll)
809 {
810 struct nlmsghdr *nlh;
811 int err;
812
813 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_DEVICE_GET,
814 NLM_F_REQUEST | NLM_F_ACK |
815 NLM_F_DUMP);
816
817 open_json_array(PRINT_JSON, "device");
818
819 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_device_show_dump_cb,
820 NULL);
821 if (err < 0) {
822 pr_err("Failed to dump devices\n");
823 close_json_array(PRINT_JSON, NULL);
824 return -1;
825 }
826
827 close_json_array(PRINT_JSON, NULL);
828
829 return 0;
830 }
831
832 static int cmd_device_show(struct dpll *dpll)
833 {
834 bool has_id = false;
835 __u32 id = 0;
836
837 while (dpll_argc(dpll) > 0) {
838 if (dpll_argv_match(dpll, "id")) {
839 if (dpll_parse_u32(dpll, "id", &id))
840 return -EINVAL;
841 has_id = true;
842 } else {
843 pr_err("unknown option: %s\n", dpll_argv(dpll));
844 return -EINVAL;
845 }
846 }
847
848 if (has_id)
849 return cmd_device_show_id(dpll, id);
850
851 return cmd_device_show_dump(dpll);
852 }
853
854 static int cmd_device_set(struct dpll *dpll)
855 {
856 struct nlmsghdr *nlh;
857 bool has_id = false;
858 __u32 id = 0;
859 int err;
860
861 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_DEVICE_SET,
862 NLM_F_REQUEST | NLM_F_ACK);
863
864 while (dpll_argc(dpll) > 0) {
865 if (dpll_argv_match(dpll, "id")) {
866 if (dpll_parse_u32(dpll, "id", &id))
867 return -EINVAL;
868 mnl_attr_put_u32(nlh, DPLL_A_ID, id);
869 has_id = true;
870 } else if (dpll_argv_match_inc(dpll, "mode")) {
871 if (dpll_parse_attr_enum(dpll, nlh, "mode", DPLL_A_MODE,
872 dpll_parse_mode))
873 return -EINVAL;
874 } else if (dpll_argv_match(dpll, "phase-offset-monitor")) {
875 const char *str = dpll_argv_next(dpll);
876 bool val;
877
878 if (!str) {
879 pr_err("phase-offset-monitor requires an argument\n");
880 return -EINVAL;
881 }
882 if (str_to_bool(str, &val)) {
883 pr_err("invalid phase-offset-monitor value: %s (use enable/disable)\n",
884 str);
885 return -EINVAL;
886 }
887 mnl_attr_put_u32(nlh, DPLL_A_PHASE_OFFSET_MONITOR,
888 val ? 1 : 0);
889 } else if (dpll_argv_match(dpll, "phase-offset-avg-factor")) {
890 if (dpll_parse_attr_u32(dpll, nlh,
891 "phase-offset-avg-factor",
892 DPLL_A_PHASE_OFFSET_AVG_FACTOR))
893 return -EINVAL;
894 } else {
895 pr_err("unknown option: %s\n", dpll_argv(dpll));
896 return -EINVAL;
897 }
898 }
899
900 if (!has_id) {
901 pr_err("device id is required\n");
902 return -EINVAL;
903 }
904
905 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, NULL, NULL);
906 if (err < 0) {
907 pr_err("Failed to set device\n");
908 return -1;
909 }
910
911 return 0;
912 }
913
914 /* Netlink callback - print device ID found by query */
915 static int cmd_device_id_get_cb(const struct nlmsghdr *nlh, void *data)
916 {
917 struct nlattr *tb[DPLL_A_MAX + 1] = {};
918 int *found = data;
919
920 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb);
921
922 if (tb[DPLL_A_ID]) {
923 __u32 id = mnl_attr_get_u32(tb[DPLL_A_ID]);
924
925 print_uint(PRINT_ANY, "id", "%u\n", id);
926 if (found)
927 *found = 1;
928 }
929
930 return MNL_CB_OK;
931 }
932
933 static int cmd_device_id_get(struct dpll *dpll)
934 {
935 struct nlmsghdr *nlh;
936 int found = 0;
937 int err;
938
939 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_DEVICE_ID_GET,
940 NLM_F_REQUEST | NLM_F_ACK);
941
942 while (dpll_argc(dpll) > 0) {
943 if (dpll_argv_match(dpll, "module-name")) {
944 if (dpll_parse_attr_str(dpll, nlh, "module-name",
945 DPLL_A_MODULE_NAME))
946 return -EINVAL;
947 } else if (dpll_argv_match(dpll, "clock-id")) {
948 if (dpll_parse_attr_u64(dpll, nlh, "clock-id",
949 DPLL_A_CLOCK_ID))
950 return -EINVAL;
951 } else if (dpll_argv_match(dpll, "type")) {
952 const char *str = dpll_argv_next(dpll);
953 __u32 val;
954
955 if (!str) {
956 pr_err("type requires an argument\n");
957 return -EINVAL;
958 }
959 if (str_to_dpll_type(str, &val)) {
960 pr_err("invalid type: %s (use pps/eec)\n", str);
961 return -EINVAL;
962 }
963 mnl_attr_put_u32(nlh, DPLL_A_TYPE, val);
964 } else {
965 pr_err("unknown option: %s\n", dpll_argv(dpll));
966 return -EINVAL;
967 }
968 }
969
970 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_device_id_get_cb,
971 &found);
972 if (err < 0) {
973 pr_err("Failed to get device id\n");
974 return -1;
975 }
976
977 if (!found) {
978 pr_err("No device found matching the criteria\n");
979 return -1;
980 }
981
982 return 0;
983 }
984
985 static int cmd_device(struct dpll *dpll)
986 {
987 if (dpll_argv_match(dpll, "help") || dpll_no_arg(dpll)) {
988 cmd_device_help();
989 return 0;
990 } else if (dpll_argv_match_inc(dpll, "show")) {
991 return cmd_device_show(dpll);
992 } else if (dpll_argv_match_inc(dpll, "set")) {
993 return cmd_device_set(dpll);
994 } else if (dpll_argv_match_inc(dpll, "id-get")) {
995 return cmd_device_id_get(dpll);
996 }
997
998 pr_err("Command \"%s\" not found\n",
999 dpll_argv(dpll) ? dpll_argv(dpll) : "");
1000 return -ENOENT;
1001 }
1002
1003 /*
1004 * Pin commands
1005 */
1006
1007 static void cmd_pin_help(void)
1008 {
1009 pr_err("Usage: dpll pin show [ id PIN_ID ] [ device DEVICE_ID ]\n");
1010 pr_err(" dpll pin set id PIN_ID [ frequency FREQ ]\n");
1011 pr_err(" [ phase-adjust ADJUST ]\n");
1012 pr_err(" [ esync-frequency FREQ ]\n");
1013 pr_err(" [ parent-device DEVICE_ID [ direction DIR ]\n");
1014 pr_err(" [ prio PRIO ]\n");
1015 pr_err(" [ state STATE ] ]\n");
1016 pr_err(" [ parent-pin PIN_ID [ state STATE ] ]\n");
1017 pr_err(" [ reference-sync PIN_ID [ state STATE ] ]\n");
1018 pr_err(" dpll pin id-get [ module-name NAME ] [ clock-id ID ]\n");
1019 pr_err(" [ board-label LABEL ] [ panel-label LABEL ]\n");
1020 pr_err(" [ package-label LABEL ] [ type TYPE ]\n");
1021 }
1022
1023 static const char *dpll_pin_type_name(__u32 type)
1024 {
1025 const char *str;
1026
1027 str = str_map_lookup_uint(pin_type_map, type);
1028 return str ? str : "unknown";
1029 }
1030
1031 static const char *dpll_pin_state_name(__u32 state)
1032 {
1033 const char *str;
1034
1035 str = str_map_lookup_uint(pin_state_map, state);
1036 return str ? str : "unknown";
1037 }
1038
1039 static const char *dpll_pin_direction_name(__u32 direction)
1040 {
1041 const char *str;
1042
1043 str = str_map_lookup_uint(pin_direction_map, direction);
1044 return str ? str : "unknown";
1045 }
1046
1047 static void dpll_pin_capabilities_name(__u32 capabilities)
1048 {
1049 if (capabilities & DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE)
1050 print_string(PRINT_FP, NULL, " state-can-change", NULL);
1051 if (capabilities & DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE)
1052 print_string(PRINT_FP, NULL, " priority-can-change", NULL);
1053 if (capabilities & DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE)
1054 print_string(PRINT_FP, NULL, " direction-can-change", NULL);
1055 }
1056
1057 /* Multi-attribute collection context */
1058 struct multi_attr_ctx {
1059 int count;
1060 struct nlattr **entries;
1061 };
1062
1063 static void dpll_pin_print_freq_supported(struct nlattr *attr)
1064 {
1065 struct multi_attr_ctx *ctx = (struct multi_attr_ctx *)attr;
1066 int i;
1067
1068 if (!attr)
1069 return;
1070
1071 open_json_array(PRINT_JSON, "frequency-supported");
1072 print_string(PRINT_FP, NULL, " frequency-supported:\n", NULL);
1073
1074 /* Iterate through all collected frequency-supported entries */
1075 for (i = 0; i < ctx->count; i++) {
1076 struct nlattr *tb_freq[DPLL_A_PIN_MAX + 1] = {};
1077 __u64 freq_min = 0, freq_max = 0;
1078
1079 mnl_attr_parse_nested(ctx->entries[i], attr_pin_cb, tb_freq);
1080
1081 if (tb_freq[DPLL_A_PIN_FREQUENCY_MIN])
1082 freq_min = mnl_attr_get_u64(
1083 tb_freq[DPLL_A_PIN_FREQUENCY_MIN]);
1084 if (tb_freq[DPLL_A_PIN_FREQUENCY_MAX])
1085 freq_max = mnl_attr_get_u64(
1086 tb_freq[DPLL_A_PIN_FREQUENCY_MAX]);
1087
1088 dpll_pr_freq_range(freq_min, freq_max);
1089 }
1090 close_json_array(PRINT_JSON, NULL);
1091 }
1092
1093 static void dpll_pin_print_capabilities(struct nlattr *attr)
1094 {
1095 __u32 caps;
1096
1097 if (!attr)
1098 return;
1099
1100 caps = mnl_attr_get_u32(attr);
1101 open_json_array(PRINT_JSON, "capabilities");
1102 if (caps & DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE)
1103 print_string(PRINT_JSON, NULL, NULL, "state-can-change");
1104 if (caps & DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE)
1105 print_string(PRINT_JSON, NULL, NULL, "priority-can-change");
1106 if (caps & DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE)
1107 print_string(PRINT_JSON, NULL, NULL, "direction-can-change");
1108 close_json_array(PRINT_JSON, NULL);
1109
1110 print_hex(PRINT_FP, NULL, " capabilities: 0x%x", caps);
1111 dpll_pin_capabilities_name(caps);
1112 print_nl();
1113 }
1114
1115 static void dpll_pin_print_esync_freq_supported(struct nlattr *attr)
1116 {
1117 struct multi_attr_ctx *ctx = (struct multi_attr_ctx *)attr;
1118 int i;
1119
1120 if (!attr)
1121 return;
1122
1123 open_json_array(PRINT_JSON, "esync-frequency-supported");
1124 print_string(PRINT_FP, NULL, " esync-frequency-supported:\n", NULL);
1125
1126 /* Iterate through all collected esync-frequency-supported entries */
1127 for (i = 0; i < ctx->count; i++) {
1128 struct nlattr *tb_freq[DPLL_A_PIN_MAX + 1] = {};
1129 __u64 freq_min = 0, freq_max = 0;
1130
1131 mnl_attr_parse_nested(ctx->entries[i], attr_pin_cb, tb_freq);
1132
1133 if (tb_freq[DPLL_A_PIN_FREQUENCY_MIN])
1134 freq_min = mnl_attr_get_u64(
1135 tb_freq[DPLL_A_PIN_FREQUENCY_MIN]);
1136 if (tb_freq[DPLL_A_PIN_FREQUENCY_MAX])
1137 freq_max = mnl_attr_get_u64(
1138 tb_freq[DPLL_A_PIN_FREQUENCY_MAX]);
1139
1140 dpll_pr_freq_range(freq_min, freq_max);
1141 }
1142 close_json_array(PRINT_JSON, NULL);
1143 }
1144
1145 static void dpll_pin_print_parent_devices(struct nlattr *attr)
1146 {
1147 struct multi_attr_ctx *ctx = (struct multi_attr_ctx *)attr;
1148 int i;
1149
1150 if (!attr)
1151 return;
1152
1153 open_json_array(PRINT_JSON, "parent-device");
1154 print_string(PRINT_FP, NULL, " parent-device:\n", NULL);
1155
1156 /* Iterate through all collected parent-device entries */
1157 for (i = 0; i < ctx->count; i++) {
1158 struct nlattr *tb_parent[DPLL_A_PIN_MAX + 1] = {};
1159
1160 mnl_attr_parse_nested(ctx->entries[i], attr_pin_cb, tb_parent);
1161
1162 open_json_object(NULL);
1163 print_string(PRINT_FP, NULL, " ", NULL);
1164
1165 DPLL_PR_UINT_FMT(tb_parent, DPLL_A_PIN_PARENT_ID, "parent-id",
1166 "id %u");
1167 DPLL_PR_ENUM_STR_FMT(tb_parent, DPLL_A_PIN_DIRECTION,
1168 "direction", " direction %s",
1169 dpll_pin_direction_name);
1170 DPLL_PR_UINT_FMT(tb_parent, DPLL_A_PIN_PRIO, "prio",
1171 " prio %u");
1172 DPLL_PR_ENUM_STR_FMT(tb_parent, DPLL_A_PIN_STATE, "state",
1173 " state %s", dpll_pin_state_name);
1174 DPLL_PR_SINT_FMT(tb_parent, DPLL_A_PIN_PHASE_OFFSET,
1175 "phase-offset", " phase-offset %" PRId64);
1176
1177 print_nl();
1178 close_json_object();
1179 }
1180 close_json_array(PRINT_JSON, NULL);
1181 }
1182
1183 static void dpll_pin_print_parent_pins(struct nlattr *attr)
1184 {
1185 struct multi_attr_ctx *ctx = (struct multi_attr_ctx *)attr;
1186 int i;
1187
1188 if (!attr)
1189 return;
1190
1191 open_json_array(PRINT_JSON, "parent-pin");
1192 print_string(PRINT_FP, NULL, " parent-pin:\n", NULL);
1193
1194 for (i = 0; i < ctx->count; i++) {
1195 struct nlattr *tb_parent[DPLL_A_PIN_MAX + 1] = {};
1196
1197 mnl_attr_parse_nested(ctx->entries[i], attr_pin_cb, tb_parent);
1198
1199 open_json_object(NULL);
1200 print_string(PRINT_FP, NULL, " ", NULL);
1201
1202 DPLL_PR_UINT_FMT(tb_parent, DPLL_A_PIN_PARENT_ID, "parent-id",
1203 "id %u");
1204 DPLL_PR_ENUM_STR_FMT(tb_parent, DPLL_A_PIN_STATE, "state",
1205 " state %s", dpll_pin_state_name);
1206
1207 print_nl();
1208 close_json_object();
1209 }
1210 close_json_array(PRINT_JSON, NULL);
1211 }
1212
1213 static void dpll_pin_print_refsync_pins(struct nlattr *attr)
1214 {
1215 struct multi_attr_ctx *ctx = (struct multi_attr_ctx *)attr;
1216 int i;
1217
1218 if (!attr)
1219 return;
1220
1221 open_json_array(PRINT_JSON, "reference-sync");
1222 print_string(PRINT_FP, NULL, " reference-sync:\n", NULL);
1223
1224 for (i = 0; i < ctx->count; i++) {
1225 struct nlattr *tb_ref[DPLL_A_PIN_MAX + 1] = {};
1226
1227 mnl_attr_parse_nested(ctx->entries[i], attr_pin_cb, tb_ref);
1228
1229 open_json_object(NULL);
1230 print_string(PRINT_FP, NULL, " ", NULL);
1231
1232 DPLL_PR_UINT_FMT(tb_ref, DPLL_A_PIN_ID, "id", "pin %u");
1233 DPLL_PR_ENUM_STR_FMT(tb_ref, DPLL_A_PIN_STATE, "state",
1234 " state %s", dpll_pin_state_name);
1235
1236 print_nl();
1237 close_json_object();
1238 }
1239 close_json_array(PRINT_JSON, NULL);
1240 }
1241
1242 /* Print pin attributes */
1243 static void dpll_pin_print_attrs(struct nlattr **tb)
1244 {
1245 DPLL_PR_UINT_FMT(tb, DPLL_A_PIN_ID, "id", "pin id %u:\n");
1246 DPLL_PR_STR(tb, DPLL_A_PIN_MODULE_NAME, "module-name");
1247 DPLL_PR_U64(tb, DPLL_A_PIN_CLOCK_ID, "clock-id");
1248 DPLL_PR_STR(tb, DPLL_A_PIN_BOARD_LABEL, "board-label");
1249 DPLL_PR_STR(tb, DPLL_A_PIN_PANEL_LABEL, "panel-label");
1250 DPLL_PR_STR(tb, DPLL_A_PIN_PACKAGE_LABEL, "package-label");
1251 DPLL_PR_ENUM_STR(tb, DPLL_A_PIN_TYPE, "type", dpll_pin_type_name);
1252 DPLL_PR_U64_FMT(tb, DPLL_A_PIN_FREQUENCY, "frequency",
1253 " frequency: %" PRIu64 " Hz\n");
1254
1255 dpll_pin_print_freq_supported(tb[DPLL_A_PIN_FREQUENCY_SUPPORTED]);
1256
1257 dpll_pin_print_capabilities(tb[DPLL_A_PIN_CAPABILITIES]);
1258
1259 DPLL_PR_INT(tb, DPLL_A_PIN_PHASE_ADJUST_MIN, "phase-adjust-min");
1260 DPLL_PR_INT(tb, DPLL_A_PIN_PHASE_ADJUST_MAX, "phase-adjust-max");
1261 DPLL_PR_UINT(tb, DPLL_A_PIN_PHASE_ADJUST_GRAN, "phase-adjust-gran");
1262 DPLL_PR_INT(tb, DPLL_A_PIN_PHASE_ADJUST, "phase-adjust");
1263
1264 if (json || !tb[DPLL_A_PIN_FRACTIONAL_FREQUENCY_OFFSET_PPT])
1265 DPLL_PR_SINT(tb, DPLL_A_PIN_FRACTIONAL_FREQUENCY_OFFSET,
1266 "fractional-frequency-offset");
1267 DPLL_PR_SINT(tb, DPLL_A_PIN_FRACTIONAL_FREQUENCY_OFFSET_PPT,
1268 "fractional-frequency-offset-ppt");
1269
1270 DPLL_PR_U64_FMT(tb, DPLL_A_PIN_ESYNC_FREQUENCY, "esync-frequency",
1271 " esync-frequency: %" PRIu64 " Hz\n");
1272
1273 dpll_pin_print_esync_freq_supported(
1274 tb[DPLL_A_PIN_ESYNC_FREQUENCY_SUPPORTED]);
1275
1276 DPLL_PR_UINT_FMT(tb, DPLL_A_PIN_ESYNC_PULSE, "esync-pulse",
1277 " esync-pulse: %u\n");
1278
1279 dpll_pin_print_parent_devices(tb[DPLL_A_PIN_PARENT_DEVICE]);
1280
1281 dpll_pin_print_parent_pins(tb[DPLL_A_PIN_PARENT_PIN]);
1282
1283 dpll_pin_print_refsync_pins(tb[DPLL_A_PIN_REFERENCE_SYNC]);
1284 }
1285
1286 struct multi_attr_counter {
1287 int attr_type;
1288 int count;
1289 };
1290
1291 /* Count how many times a specific attribute type appears */
1292 static int count_multi_attr_cb(const struct nlattr *attr, void *data)
1293 {
1294 struct multi_attr_counter *counter = data;
1295 int type = mnl_attr_get_type(attr);
1296
1297 if (type == counter->attr_type)
1298 counter->count++;
1299 return MNL_CB_OK;
1300 }
1301
1302 /* Helper to count specific multi-attr type occurrences */
1303 static unsigned int multi_attr_count_get(const struct nlmsghdr *nlh,
1304 struct genlmsghdr *genl, int attr_type)
1305 {
1306 struct multi_attr_counter counter;
1307
1308 counter.attr_type = attr_type;
1309 counter.count = 0;
1310 mnl_attr_parse(nlh, sizeof(*genl), count_multi_attr_cb, &counter);
1311 return counter.count;
1312 }
1313
1314 /* Initialize multi-attr context with proper allocation */
1315 static int multi_attr_ctx_init(struct multi_attr_ctx *ctx, unsigned int count)
1316 {
1317 if (count == 0) {
1318 ctx->count = 0;
1319 ctx->entries = NULL;
1320 return 0;
1321 }
1322
1323 ctx->entries = calloc(count, sizeof(struct nlattr *));
1324 if (!ctx->entries)
1325 return -ENOMEM;
1326 ctx->count = 0;
1327 return 0;
1328 }
1329
1330 /* Free multi-attr context */
1331 static void multi_attr_ctx_free(struct multi_attr_ctx *ctx)
1332 {
1333 free(ctx->entries);
1334 ctx->entries = NULL;
1335 ctx->count = 0;
1336 }
1337
1338 /* Generic helper to collect specific multi-attr type */
1339 struct multi_attr_collector {
1340 int attr_type;
1341 struct multi_attr_ctx *ctx;
1342 };
1343
1344 static int collect_multi_attr_cb(const struct nlattr *attr, void *data)
1345 {
1346 struct multi_attr_collector *collector = data;
1347 int type = mnl_attr_get_type(attr);
1348
1349 if (type == collector->attr_type) {
1350 collector->ctx->entries[collector->ctx->count++] =
1351 (struct nlattr *)attr;
1352 }
1353 return MNL_CB_OK;
1354 }
1355
1356 static void dpll_multi_attr_parse(const struct nlmsghdr *nlh, int attr_type,
1357 struct multi_attr_ctx *ctx)
1358 {
1359 struct multi_attr_collector collector;
1360
1361 collector.attr_type = attr_type;
1362 collector.ctx = ctx;
1363 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), collect_multi_attr_cb,
1364 &collector);
1365 }
1366
1367 /* Callback for pin get (single) */
1368 static int cmd_pin_show_cb(const struct nlmsghdr *nlh, void *data)
1369 {
1370 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
1371 struct multi_attr_ctx parent_dev_ctx = { 0 }, parent_pin_ctx = { 0 },
1372 ref_sync_ctx = { 0 };
1373 struct multi_attr_ctx freq_supp_ctx = { 0 },
1374 esync_freq_supp_ctx = { 0 };
1375 struct nlattr *tb[DPLL_A_PIN_MAX + 1] = {};
1376 unsigned int count;
1377 int ret;
1378
1379 /* First parse to get main attributes */
1380 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_pin_cb, tb);
1381
1382 /* Pass 1: Count multi-attr occurrences and allocate */
1383 count = multi_attr_count_get(nlh, genl, DPLL_A_PIN_PARENT_DEVICE);
1384 if (count > 0 && multi_attr_ctx_init(&parent_dev_ctx, count) < 0)
1385 goto err_alloc;
1386
1387 count = multi_attr_count_get(nlh, genl, DPLL_A_PIN_PARENT_PIN);
1388 if (count > 0 && multi_attr_ctx_init(&parent_pin_ctx, count) < 0)
1389 goto err_alloc;
1390
1391 count = multi_attr_count_get(nlh, genl, DPLL_A_PIN_REFERENCE_SYNC);
1392 if (count > 0 && multi_attr_ctx_init(&ref_sync_ctx, count) < 0)
1393 goto err_alloc;
1394
1395 count = multi_attr_count_get(nlh, genl, DPLL_A_PIN_FREQUENCY_SUPPORTED);
1396 if (count > 0 && multi_attr_ctx_init(&freq_supp_ctx, count) < 0)
1397 goto err_alloc;
1398
1399 count = multi_attr_count_get(nlh, genl,
1400 DPLL_A_PIN_ESYNC_FREQUENCY_SUPPORTED);
1401 if (count > 0 && multi_attr_ctx_init(&esync_freq_supp_ctx, count) < 0)
1402 goto err_alloc;
1403
1404 /* Pass 2: Collect multi-attr entries */
1405 if (parent_dev_ctx.entries)
1406 dpll_multi_attr_parse(nlh, DPLL_A_PIN_PARENT_DEVICE,
1407 &parent_dev_ctx);
1408 if (parent_pin_ctx.entries)
1409 dpll_multi_attr_parse(nlh, DPLL_A_PIN_PARENT_PIN,
1410 &parent_pin_ctx);
1411 if (ref_sync_ctx.entries)
1412 dpll_multi_attr_parse(nlh, DPLL_A_PIN_REFERENCE_SYNC,
1413 &ref_sync_ctx);
1414 if (freq_supp_ctx.entries)
1415 dpll_multi_attr_parse(nlh, DPLL_A_PIN_FREQUENCY_SUPPORTED,
1416 &freq_supp_ctx);
1417 if (esync_freq_supp_ctx.entries)
1418 dpll_multi_attr_parse(nlh, DPLL_A_PIN_ESYNC_FREQUENCY_SUPPORTED,
1419 &esync_freq_supp_ctx);
1420
1421 /* Replace tb entries with contexts */
1422 if (parent_dev_ctx.count > 0)
1423 tb[DPLL_A_PIN_PARENT_DEVICE] = (struct nlattr *)&parent_dev_ctx;
1424 if (parent_pin_ctx.count > 0)
1425 tb[DPLL_A_PIN_PARENT_PIN] = (struct nlattr *)&parent_pin_ctx;
1426 if (ref_sync_ctx.count > 0)
1427 tb[DPLL_A_PIN_REFERENCE_SYNC] = (struct nlattr *)&ref_sync_ctx;
1428 if (freq_supp_ctx.count > 0)
1429 tb[DPLL_A_PIN_FREQUENCY_SUPPORTED] =
1430 (struct nlattr *)&freq_supp_ctx;
1431 if (esync_freq_supp_ctx.count > 0)
1432 tb[DPLL_A_PIN_ESYNC_FREQUENCY_SUPPORTED] =
1433 (struct nlattr *)&esync_freq_supp_ctx;
1434
1435 dpll_pin_print_attrs(tb);
1436
1437 ret = MNL_CB_OK;
1438 goto cleanup;
1439
1440 err_alloc:
1441 fprintf(stderr,
1442 "Failed to allocate memory for multi-attr collection\n");
1443 ret = MNL_CB_ERROR;
1444
1445 cleanup:
1446 /* Free allocated memory */
1447 multi_attr_ctx_free(&parent_dev_ctx);
1448 multi_attr_ctx_free(&parent_pin_ctx);
1449 multi_attr_ctx_free(&ref_sync_ctx);
1450 multi_attr_ctx_free(&freq_supp_ctx);
1451 multi_attr_ctx_free(&esync_freq_supp_ctx);
1452
1453 return ret;
1454 }
1455
1456 /* Callback for pin dump (multiple) - wraps each pin in object */
1457 static int cmd_pin_show_dump_cb(const struct nlmsghdr *nlh, void *data)
1458 {
1459 int ret;
1460
1461 open_json_object(NULL);
1462 ret = cmd_pin_show_cb(nlh, data);
1463 close_json_object();
1464
1465 return ret;
1466 }
1467
1468 static int cmd_pin_show_id(struct dpll *dpll, __u32 id)
1469 {
1470 struct nlmsghdr *nlh;
1471 int err;
1472
1473 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_PIN_GET,
1474 NLM_F_REQUEST | NLM_F_ACK);
1475 mnl_attr_put_u32(nlh, DPLL_A_PIN_ID, id);
1476
1477 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_pin_show_cb, NULL);
1478 if (err < 0) {
1479 pr_err("Failed to get pin %u\n", id);
1480 return -1;
1481 }
1482
1483 return 0;
1484 }
1485
1486 static int cmd_pin_show_dump(struct dpll *dpll, bool has_device_id,
1487 __u32 device_id)
1488 {
1489 struct nlmsghdr *nlh;
1490 int err;
1491
1492 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_PIN_GET,
1493 NLM_F_REQUEST | NLM_F_ACK |
1494 NLM_F_DUMP);
1495
1496 /* If device_id specified, filter pins by device */
1497 if (has_device_id)
1498 mnl_attr_put_u32(nlh, DPLL_A_ID, device_id);
1499
1500 /* Open JSON array for multiple pins */
1501 open_json_array(PRINT_JSON, "pin");
1502
1503 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_pin_show_dump_cb,
1504 NULL);
1505 if (err < 0) {
1506 pr_err("Failed to dump pins\n");
1507 close_json_array(PRINT_JSON, NULL);
1508 return -1;
1509 }
1510
1511 /* Close JSON array */
1512 close_json_array(PRINT_JSON, NULL);
1513
1514 return 0;
1515 }
1516
1517 static int cmd_pin_show(struct dpll *dpll)
1518 {
1519 bool has_pin_id = false, has_device_id = false;
1520 __u32 pin_id = 0, device_id = 0;
1521
1522 while (dpll_argc(dpll) > 0) {
1523 if (dpll_argv_match(dpll, "id")) {
1524 if (dpll_parse_u32(dpll, "id", &pin_id))
1525 return -EINVAL;
1526 has_pin_id = true;
1527 } else if (dpll_argv_match(dpll, "device")) {
1528 if (dpll_parse_u32(dpll, "device", &device_id))
1529 return -EINVAL;
1530 has_device_id = true;
1531 } else {
1532 pr_err("unknown option: %s\n", dpll_argv(dpll));
1533 return -EINVAL;
1534 }
1535 }
1536
1537 if (has_pin_id)
1538 return cmd_pin_show_id(dpll, pin_id);
1539 else
1540 return cmd_pin_show_dump(dpll, has_device_id, device_id);
1541 }
1542
1543 static int cmd_pin_parse_parent_device(struct dpll *dpll, struct nlmsghdr *nlh)
1544 {
1545 struct nlattr *nest;
1546 __u32 parent_id;
1547
1548 dpll_arg_inc(dpll);
1549 if (dpll_arg_required(dpll, "parent-device"))
1550 return -EINVAL;
1551
1552 if (get_u32(&parent_id, dpll_argv(dpll), 0)) {
1553 pr_err("invalid parent-device id: %s\n", dpll_argv(dpll));
1554 return -EINVAL;
1555 }
1556 dpll_arg_inc(dpll);
1557
1558 nest = mnl_attr_nest_start(nlh, DPLL_A_PIN_PARENT_DEVICE);
1559 mnl_attr_put_u32(nlh, DPLL_A_PIN_PARENT_ID, parent_id);
1560
1561 /* Parse optional parent-device attributes */
1562 while (dpll_argc(dpll) > 0) {
1563 if (dpll_argv_match_inc(dpll, "direction")) {
1564 if (dpll_parse_attr_enum(dpll, nlh, "direction",
1565 DPLL_A_PIN_DIRECTION,
1566 dpll_parse_direction))
1567 return -EINVAL;
1568 } else if (dpll_argv_match(dpll, "prio")) {
1569 if (dpll_parse_attr_u32(dpll, nlh, "prio",
1570 DPLL_A_PIN_PRIO))
1571 return -EINVAL;
1572 } else if (dpll_argv_match_inc(dpll, "state")) {
1573 if (dpll_parse_attr_enum(dpll, nlh, "state",
1574 DPLL_A_PIN_STATE,
1575 dpll_parse_state))
1576 return -EINVAL;
1577 } else {
1578 /* Not a parent-device attribute, break to parse
1579 * next option.
1580 */
1581 break;
1582 }
1583 }
1584
1585 mnl_attr_nest_end(nlh, nest);
1586
1587 return 0;
1588 }
1589
1590 static int cmd_pin_parse_parent_pin(struct dpll *dpll, struct nlmsghdr *nlh)
1591 {
1592 struct nlattr *nest;
1593 __u32 parent_id;
1594
1595 dpll_arg_inc(dpll);
1596 if (dpll_arg_required(dpll, "parent-pin"))
1597 return -EINVAL;
1598
1599 if (get_u32(&parent_id, dpll_argv(dpll), 0)) {
1600 pr_err("invalid parent-pin id: %s\n", dpll_argv(dpll));
1601 return -EINVAL;
1602 }
1603 dpll_arg_inc(dpll);
1604
1605 nest = mnl_attr_nest_start(nlh, DPLL_A_PIN_PARENT_PIN);
1606 mnl_attr_put_u32(nlh, DPLL_A_PIN_PARENT_ID, parent_id);
1607
1608 /* Parse optional parent-pin attributes */
1609 while (dpll_argc(dpll) > 0) {
1610 if (dpll_argv_match_inc(dpll, "state")) {
1611 if (dpll_parse_attr_enum(dpll, nlh, "state",
1612 DPLL_A_PIN_STATE,
1613 dpll_parse_state))
1614 return -EINVAL;
1615 } else {
1616 /* Not a parent-pin attribute, break to parse next
1617 * option.
1618 */
1619 break;
1620 }
1621 }
1622
1623 mnl_attr_nest_end(nlh, nest);
1624
1625 return 0;
1626 }
1627
1628 static int cmd_pin_parse_reference_sync(struct dpll *dpll, struct nlmsghdr *nlh)
1629 {
1630 struct nlattr *nest;
1631 __u32 ref_pin_id;
1632
1633 dpll_arg_inc(dpll);
1634 if (dpll_arg_required(dpll, "reference-sync"))
1635 return -EINVAL;
1636
1637 if (get_u32(&ref_pin_id, dpll_argv(dpll), 0)) {
1638 pr_err("invalid reference-sync pin id: %s\n", dpll_argv(dpll));
1639 return -EINVAL;
1640 }
1641 dpll_arg_inc(dpll);
1642
1643 nest = mnl_attr_nest_start(nlh, DPLL_A_PIN_REFERENCE_SYNC);
1644 mnl_attr_put_u32(nlh, DPLL_A_PIN_ID, ref_pin_id);
1645
1646 /* Parse optional reference-sync attributes */
1647 while (dpll_argc(dpll) > 0) {
1648 if (dpll_argv_match_inc(dpll, "state")) {
1649 if (dpll_parse_attr_enum(dpll, nlh, "state",
1650 DPLL_A_PIN_STATE,
1651 dpll_parse_state))
1652 return -EINVAL;
1653 } else {
1654 /* Not a reference-sync attribute, break to parse
1655 * next option.
1656 */
1657 break;
1658 }
1659 }
1660
1661 mnl_attr_nest_end(nlh, nest);
1662
1663 return 0;
1664 }
1665
1666 static int cmd_pin_set(struct dpll *dpll)
1667 {
1668 struct nlmsghdr *nlh;
1669 bool has_id = false;
1670 __u32 id = 0;
1671 int err;
1672
1673 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_PIN_SET,
1674 NLM_F_REQUEST | NLM_F_ACK);
1675
1676 while (dpll_argc(dpll) > 0) {
1677 if (dpll_argv_match(dpll, "id")) {
1678 if (dpll_parse_u32(dpll, "id", &id))
1679 return -EINVAL;
1680 mnl_attr_put_u32(nlh, DPLL_A_PIN_ID, id);
1681 has_id = true;
1682 } else if (dpll_argv_match(dpll, "frequency")) {
1683 if (dpll_parse_attr_u64(dpll, nlh, "frequency",
1684 DPLL_A_PIN_FREQUENCY))
1685 return -EINVAL;
1686 } else if (dpll_argv_match(dpll, "phase-adjust")) {
1687 if (dpll_parse_attr_s32(dpll, nlh, "phase-adjust",
1688 DPLL_A_PIN_PHASE_ADJUST))
1689 return -EINVAL;
1690 } else if (dpll_argv_match(dpll, "esync-frequency")) {
1691 if (dpll_parse_attr_u64(dpll, nlh, "esync-frequency",
1692 DPLL_A_PIN_ESYNC_FREQUENCY))
1693 return -EINVAL;
1694 } else if (dpll_argv_match(dpll, "parent-device")) {
1695 if (cmd_pin_parse_parent_device(dpll, nlh))
1696 return -EINVAL;
1697 } else if (dpll_argv_match(dpll, "parent-pin")) {
1698 if (cmd_pin_parse_parent_pin(dpll, nlh))
1699 return -EINVAL;
1700 } else if (dpll_argv_match(dpll, "reference-sync")) {
1701 if (cmd_pin_parse_reference_sync(dpll, nlh))
1702 return -EINVAL;
1703 } else {
1704 pr_err("unknown option: %s\n", dpll_argv(dpll));
1705 return -EINVAL;
1706 }
1707 }
1708
1709 if (!has_id) {
1710 pr_err("pin id is required\n");
1711 return -EINVAL;
1712 }
1713
1714 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, NULL, NULL);
1715 if (err < 0) {
1716 pr_err("Failed to set pin\n");
1717 return -1;
1718 }
1719
1720 return 0;
1721 }
1722
1723 static int cmd_pin_id_get_cb(const struct nlmsghdr *nlh, void *data)
1724 {
1725 struct nlattr *tb[DPLL_A_PIN_MAX + 1] = {};
1726 int *found = data;
1727
1728 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_pin_cb, tb);
1729
1730 if (tb[DPLL_A_PIN_ID]) {
1731 __u32 id = mnl_attr_get_u32(tb[DPLL_A_PIN_ID]);
1732
1733 print_uint(PRINT_ANY, "id", "%u\n", id);
1734 if (found)
1735 *found = 1;
1736 }
1737
1738 return MNL_CB_OK;
1739 }
1740
1741 static int cmd_pin_id_get(struct dpll *dpll)
1742 {
1743 struct nlmsghdr *nlh;
1744 int found = 0;
1745 int err;
1746
1747 nlh = mnlu_gen_socket_cmd_prepare(&dpll->nlg, DPLL_CMD_PIN_ID_GET,
1748 NLM_F_REQUEST | NLM_F_ACK);
1749
1750 while (dpll_argc(dpll) > 0) {
1751 if (dpll_argv_match(dpll, "module-name")) {
1752 if (dpll_parse_attr_str(dpll, nlh, "module-name",
1753 DPLL_A_PIN_MODULE_NAME))
1754 return -EINVAL;
1755 } else if (dpll_argv_match(dpll, "clock-id")) {
1756 if (dpll_parse_attr_u64(dpll, nlh, "clock-id",
1757 DPLL_A_PIN_CLOCK_ID))
1758 return -EINVAL;
1759 } else if (dpll_argv_match(dpll, "board-label")) {
1760 if (dpll_parse_attr_str(dpll, nlh, "board-label",
1761 DPLL_A_PIN_BOARD_LABEL))
1762 return -EINVAL;
1763 } else if (dpll_argv_match(dpll, "panel-label")) {
1764 if (dpll_parse_attr_str(dpll, nlh, "panel-label",
1765 DPLL_A_PIN_PANEL_LABEL))
1766 return -EINVAL;
1767 } else if (dpll_argv_match(dpll, "package-label")) {
1768 if (dpll_parse_attr_str(dpll, nlh, "package-label",
1769 DPLL_A_PIN_PACKAGE_LABEL))
1770 return -EINVAL;
1771 } else if (dpll_argv_match_inc(dpll, "type")) {
1772 if (dpll_parse_attr_enum(dpll, nlh, "type",
1773 DPLL_A_PIN_TYPE,
1774 dpll_parse_pin_type))
1775 return -EINVAL;
1776 } else {
1777 pr_err("unknown option: %s\n", dpll_argv(dpll));
1778 return -EINVAL;
1779 }
1780 }
1781
1782 err = mnlu_gen_socket_sndrcv(&dpll->nlg, nlh, cmd_pin_id_get_cb,
1783 &found);
1784 if (err < 0) {
1785 pr_err("Failed to get pin id\n");
1786 return -1;
1787 }
1788
1789 if (!found) {
1790 pr_err("No pin found matching the criteria\n");
1791 return -1;
1792 }
1793
1794 return 0;
1795 }
1796
1797 static int cmd_pin(struct dpll *dpll)
1798 {
1799 if (dpll_argv_match(dpll, "help") || dpll_no_arg(dpll)) {
1800 cmd_pin_help();
1801 return 0;
1802 } else if (dpll_argv_match_inc(dpll, "show")) {
1803 return cmd_pin_show(dpll);
1804 } else if (dpll_argv_match_inc(dpll, "set")) {
1805 return cmd_pin_set(dpll);
1806 } else if (dpll_argv_match_inc(dpll, "id-get")) {
1807 return cmd_pin_id_get(dpll);
1808 }
1809
1810 pr_err("Command \"%s\" not found\n",
1811 dpll_argv(dpll) ? dpll_argv(dpll) : "");
1812 return -ENOENT;
1813 }
1814
1815 /* Monitor command - notification handling */
1816 static int cmd_monitor_cb(const struct nlmsghdr *nlh, void *data)
1817 {
1818 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
1819 const char *cmd_name = "UNKNOWN";
1820 const char *json_name = "unknown";
1821 int ret = MNL_CB_OK;
1822
1823 switch (genl->cmd) {
1824 case DPLL_CMD_DEVICE_CREATE_NTF:
1825 cmd_name = "DEVICE_CREATE";
1826 json_name = "device-create-ntf";
1827 /* fallthrough */
1828 case DPLL_CMD_DEVICE_CHANGE_NTF:
1829 if (genl->cmd == DPLL_CMD_DEVICE_CHANGE_NTF) {
1830 cmd_name = "DEVICE_CHANGE";
1831 json_name = "device-change-ntf";
1832 }
1833 /* fallthrough */
1834 case DPLL_CMD_DEVICE_DELETE_NTF: {
1835 if (genl->cmd == DPLL_CMD_DEVICE_DELETE_NTF) {
1836 cmd_name = "DEVICE_DELETE";
1837 json_name = "device-delete-ntf";
1838 }
1839 struct nlattr *tb[DPLL_A_MAX + 1] = {};
1840
1841 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb);
1842
1843 open_json_object(NULL);
1844 print_string(PRINT_JSON, "name", NULL, json_name);
1845 open_json_object("msg");
1846 print_string(PRINT_FP, NULL, "[%s] ", cmd_name);
1847
1848 dpll_device_print_attrs(nlh, tb);
1849
1850 close_json_object();
1851 close_json_object();
1852 break;
1853 }
1854 case DPLL_CMD_PIN_CREATE_NTF:
1855 cmd_name = "PIN_CREATE";
1856 json_name = "pin-create-ntf";
1857 /* fallthrough */
1858 case DPLL_CMD_PIN_CHANGE_NTF:
1859 if (genl->cmd == DPLL_CMD_PIN_CHANGE_NTF) {
1860 cmd_name = "PIN_CHANGE";
1861 json_name = "pin-change-ntf";
1862 }
1863 /* fallthrough */
1864 case DPLL_CMD_PIN_DELETE_NTF: {
1865 if (genl->cmd == DPLL_CMD_PIN_DELETE_NTF) {
1866 cmd_name = "PIN_DELETE";
1867 json_name = "pin-delete-ntf";
1868 }
1869
1870 open_json_object(NULL);
1871 print_string(PRINT_JSON, "name", NULL, json_name);
1872 open_json_object("msg");
1873 print_string(PRINT_FP, NULL, "[%s] ", cmd_name);
1874
1875 ret = cmd_pin_show_cb(nlh, NULL);
1876
1877 close_json_object();
1878 close_json_object();
1879 break;
1880 }
1881 default:
1882 pr_err("Unknown notification command: %d\n", genl->cmd);
1883 break;
1884 }
1885
1886 fflush(stdout);
1887 return ret;
1888 }
1889
1890 static int cmd_monitor(struct dpll *dpll)
1891 {
1892 int netlink_fd, signal_fd = -1;
1893 struct pollfd pfds[2];
1894 sigset_t mask;
1895 int ret = 0;
1896
1897 ret = mnlg_socket_group_add(&dpll->nlg, "monitor");
1898 if (ret) {
1899 pr_err("Failed to subscribe to monitor group: %s\n",
1900 strerror(errno));
1901 return ret;
1902 }
1903
1904 print_string(PRINT_FP, NULL,
1905 "Monitoring DPLL events (Press Ctrl+C to stop)...\n",
1906 NULL);
1907
1908 sigemptyset(&mask);
1909 sigaddset(&mask, SIGINT);
1910 sigaddset(&mask, SIGTERM);
1911 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
1912 pr_err("Failed to block signals: %s\n", strerror(errno));
1913 return -errno;
1914 }
1915
1916 signal_fd = signalfd(-1, &mask, SFD_CLOEXEC);
1917 if (signal_fd < 0) {
1918 pr_err("Failed to create signalfd: %s\n", strerror(errno));
1919 ret = -errno;
1920 goto err_sigmask;
1921 }
1922
1923 netlink_fd = mnlg_socket_get_fd(&dpll->nlg);
1924 if (netlink_fd < 0) {
1925 pr_err("Failed to get netlink socket fd\n");
1926 ret = -1;
1927 goto err_signalfd;
1928 }
1929
1930 ret = fcntl(netlink_fd, F_GETFL);
1931 if (ret < 0) {
1932 pr_err("Failed to get netlink socket flags: %s\n",
1933 strerror(errno));
1934 ret = -errno;
1935 goto err_signalfd;
1936 }
1937 if (fcntl(netlink_fd, F_SETFL, ret | O_NONBLOCK) < 0) {
1938 pr_err("Failed to set netlink socket to non-blocking: %s\n",
1939 strerror(errno));
1940 ret = -errno;
1941 goto err_signalfd;
1942 }
1943
1944 open_json_array(PRINT_JSON, "monitor");
1945
1946 pfds[0].fd = signal_fd;
1947 pfds[0].events = POLLIN;
1948 pfds[1].fd = netlink_fd;
1949 pfds[1].events = POLLIN;
1950
1951 while (1) {
1952 ret = poll(pfds, ARRAY_SIZE(pfds), -1);
1953 if (ret < 0) {
1954 if (errno == EINTR)
1955 continue;
1956 pr_err("poll() failed: %s\n", strerror(errno));
1957 ret = -errno;
1958 break;
1959 }
1960
1961 if (pfds[0].revents & POLLIN) {
1962 ret = 0;
1963 break;
1964 }
1965
1966 if (pfds[1].revents & POLLIN) {
1967 ret = mnlu_gen_socket_recv_run(&dpll->nlg,
1968 cmd_monitor_cb, NULL);
1969 if (ret < 0 && errno != EAGAIN &&
1970 errno != EWOULDBLOCK) {
1971 pr_err("Failed to receive notifications: %s\n",
1972 strerror(errno));
1973 break;
1974 }
1975 }
1976 }
1977
1978 close_json_array(PRINT_JSON, NULL);
1979
1980 err_signalfd:
1981 if (signal_fd >= 0)
1982 close(signal_fd);
1983 err_sigmask:
1984 sigprocmask(SIG_UNBLOCK, &mask, NULL);
1985
1986 return ret < 0 ? ret : 0;
1987 }