| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <errno.h> |
| 6 | #include <gpiod.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/param.h> |
| 10 | |
| 11 | #include "internal.h" |
| 12 | |
| 13 | #define LINES_MAX (GPIO_V2_LINES_MAX) |
| 14 | |
| 15 | struct settings_node { |
| 16 | struct settings_node *prev; |
| 17 | struct settings_node *next; |
| 18 | struct gpiod_line_settings *settings; |
| 19 | unsigned int refcnt; |
| 20 | }; |
| 21 | |
| 22 | struct per_line_config { |
| 23 | unsigned int offset; |
| 24 | struct settings_node *node; |
| 25 | }; |
| 26 | |
| 27 | struct gpiod_line_config { |
| 28 | struct per_line_config line_configs[LINES_MAX]; |
| 29 | size_t num_configs; |
| 30 | enum gpiod_line_value output_values[LINES_MAX]; |
| 31 | size_t num_output_values; |
| 32 | struct settings_node *sref_list; |
| 33 | }; |
| 34 | |
| 35 | GPIOD_API struct gpiod_line_config *gpiod_line_config_new(void) |
| 36 | { |
| 37 | struct gpiod_line_config *config; |
| 38 | |
| 39 | config = malloc(sizeof(*config)); |
| 40 | if (!config) |
| 41 | return NULL; |
| 42 | |
| 43 | memset(config, 0, sizeof(*config)); |
| 44 | |
| 45 | return config; |
| 46 | } |
| 47 | |
| 48 | static void free_refs(struct gpiod_line_config *config) |
| 49 | { |
| 50 | struct settings_node *node, *tmp; |
| 51 | |
| 52 | for (node = config->sref_list; node;) { |
| 53 | tmp = node->next; |
| 54 | gpiod_line_settings_free(node->settings); |
| 55 | free(node); |
| 56 | node = tmp; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | GPIOD_API void gpiod_line_config_free(struct gpiod_line_config *config) |
| 61 | { |
| 62 | if (!config) |
| 63 | return; |
| 64 | |
| 65 | free_refs(config); |
| 66 | free(config); |
| 67 | } |
| 68 | |
| 69 | GPIOD_API void gpiod_line_config_reset(struct gpiod_line_config *config) |
| 70 | { |
| 71 | assert(config); |
| 72 | |
| 73 | free_refs(config); |
| 74 | memset(config, 0, sizeof(*config)); |
| 75 | } |
| 76 | |
| 77 | static struct per_line_config *find_config(struct gpiod_line_config *config, |
| 78 | unsigned int offset) |
| 79 | { |
| 80 | struct per_line_config *per_line; |
| 81 | size_t i; |
| 82 | |
| 83 | for (i = 0; i < config->num_configs; i++) { |
| 84 | per_line = &config->line_configs[i]; |
| 85 | |
| 86 | if (offset == per_line->offset) |
| 87 | return per_line; |
| 88 | } |
| 89 | |
| 90 | return &config->line_configs[config->num_configs++]; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Count offsets in the input array that would require a new slot in the |
| 95 | * config: those not already present in config->line_configs and not repeated |
| 96 | * earlier in the same offsets array. |
| 97 | */ |
| 98 | static size_t count_new_offsets(const struct gpiod_line_config *config, |
| 99 | const unsigned int *offsets, size_t num_offsets) |
| 100 | { |
| 101 | size_t i, j, num_new = 0; |
| 102 | bool existing; |
| 103 | |
| 104 | for (i = 0; i < num_offsets; i++) { |
| 105 | existing = false; |
| 106 | |
| 107 | for (j = 0; j < config->num_configs; j++) { |
| 108 | if (config->line_configs[j].offset == offsets[i]) { |
| 109 | existing = true; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (!existing) { |
| 115 | for (j = 0; j < i; j++) { |
| 116 | if (offsets[j] == offsets[i]) { |
| 117 | existing = true; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (!existing) |
| 124 | num_new++; |
| 125 | } |
| 126 | |
| 127 | return num_new; |
| 128 | } |
| 129 | |
| 130 | GPIOD_API int gpiod_line_config_add_line_settings( |
| 131 | struct gpiod_line_config *config, const unsigned int *offsets, |
| 132 | size_t num_offsets, struct gpiod_line_settings *settings) |
| 133 | { |
| 134 | struct per_line_config *per_line; |
| 135 | struct settings_node *node, *old; |
| 136 | size_t i, new_offsets; |
| 137 | |
| 138 | assert(config); |
| 139 | |
| 140 | if (!offsets || num_offsets == 0) { |
| 141 | errno = EINVAL; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | new_offsets = count_new_offsets(config, offsets, num_offsets); |
| 146 | |
| 147 | if ((config->num_configs + new_offsets) > LINES_MAX) { |
| 148 | errno = E2BIG; |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | node = malloc(sizeof(*node)); |
| 153 | if (!node) |
| 154 | return -1; |
| 155 | |
| 156 | if (!settings) |
| 157 | node->settings = gpiod_line_settings_new(); |
| 158 | else |
| 159 | node->settings = gpiod_line_settings_copy(settings); |
| 160 | if (!node->settings) { |
| 161 | free(node); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | node->refcnt = 0; |
| 166 | node->next = config->sref_list; |
| 167 | if (config->sref_list) |
| 168 | config->sref_list->prev = node; |
| 169 | node->prev = NULL; |
| 170 | config->sref_list = node; |
| 171 | |
| 172 | for (i = 0; i < num_offsets; i++) { |
| 173 | per_line = find_config(config, offsets[i]); |
| 174 | |
| 175 | node->refcnt++; |
| 176 | per_line->offset = offsets[i]; |
| 177 | old = per_line->node; |
| 178 | per_line->node = node; |
| 179 | |
| 180 | if (old && !(--old->refcnt)) { |
| 181 | if (old->prev) |
| 182 | old->prev->next = old->next; |
| 183 | if (old->next) |
| 184 | old->next->prev = old->prev; |
| 185 | gpiod_line_settings_free(old->settings); |
| 186 | free(old); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | GPIOD_API struct gpiod_line_settings * |
| 194 | gpiod_line_config_get_line_settings(struct gpiod_line_config *config, |
| 195 | unsigned int offset) |
| 196 | { |
| 197 | struct gpiod_line_settings *settings; |
| 198 | struct per_line_config *per_line; |
| 199 | size_t i; |
| 200 | int ret; |
| 201 | |
| 202 | assert(config); |
| 203 | |
| 204 | for (i = 0; i < config->num_configs; i++) { |
| 205 | per_line = &config->line_configs[i]; |
| 206 | |
| 207 | if (per_line->offset == offset) { |
| 208 | settings = gpiod_line_settings_copy( |
| 209 | per_line->node->settings); |
| 210 | if (!settings) |
| 211 | return NULL; |
| 212 | |
| 213 | /* |
| 214 | * If a global output value was set for this line - use |
| 215 | * it and override the one stored in settings. |
| 216 | */ |
| 217 | if (config->num_output_values > i) { |
| 218 | ret = gpiod_line_settings_set_output_value( |
| 219 | settings, |
| 220 | config->output_values[i]); |
| 221 | if (ret) { |
| 222 | gpiod_line_settings_free(settings); |
| 223 | return NULL; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return settings; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | errno = ENOENT; |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | GPIOD_API int |
| 236 | gpiod_line_config_set_output_values(struct gpiod_line_config *config, |
| 237 | const enum gpiod_line_value *values, |
| 238 | size_t num_values) |
| 239 | { |
| 240 | size_t i; |
| 241 | int ret; |
| 242 | |
| 243 | assert(config); |
| 244 | |
| 245 | if (!num_values || num_values > LINES_MAX || !values) { |
| 246 | errno = EINVAL; |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < num_values; i++) { |
| 251 | ret = gpiod_set_output_value(values[i], |
| 252 | &config->output_values[i]); |
| 253 | if (ret) { |
| 254 | config->num_output_values = 0; |
| 255 | return ret; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | config->num_output_values = num_values; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | GPIOD_API size_t |
| 265 | gpiod_line_config_get_num_configured_offsets(struct gpiod_line_config *config) |
| 266 | { |
| 267 | assert(config); |
| 268 | |
| 269 | return config->num_configs; |
| 270 | } |
| 271 | |
| 272 | GPIOD_API size_t |
| 273 | gpiod_line_config_get_configured_offsets(struct gpiod_line_config *config, |
| 274 | unsigned int *offsets, |
| 275 | size_t max_offsets) |
| 276 | { |
| 277 | size_t num_offsets, i; |
| 278 | |
| 279 | assert(config); |
| 280 | |
| 281 | if (!offsets || !max_offsets || !config->num_configs) |
| 282 | return 0; |
| 283 | |
| 284 | num_offsets = MIN(config->num_configs, max_offsets); |
| 285 | |
| 286 | for (i = 0; i < num_offsets; i++) |
| 287 | offsets[i] = config->line_configs[i].offset; |
| 288 | |
| 289 | return num_offsets; |
| 290 | } |
| 291 | |
| 292 | static void set_offsets(struct gpiod_line_config *config, |
| 293 | struct gpio_v2_line_request *uapi_cfg) |
| 294 | { |
| 295 | size_t i; |
| 296 | |
| 297 | uapi_cfg->num_lines = config->num_configs; |
| 298 | |
| 299 | for (i = 0; i < config->num_configs; i++) |
| 300 | uapi_cfg->offsets[i] = config->line_configs[i].offset; |
| 301 | } |
| 302 | |
| 303 | static bool has_at_least_one_output_direction(struct gpiod_line_config *config) |
| 304 | { |
| 305 | size_t i; |
| 306 | |
| 307 | for (i = 0; i < config->num_configs; i++) { |
| 308 | if (gpiod_line_settings_get_direction( |
| 309 | config->line_configs[i].node->settings) == |
| 310 | GPIOD_LINE_DIRECTION_OUTPUT) |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | static void set_output_value(uint64_t *vals, size_t bit, |
| 318 | enum gpiod_line_value value) |
| 319 | { |
| 320 | gpiod_line_mask_assign_bit(vals, bit, |
| 321 | value == GPIOD_LINE_VALUE_ACTIVE ? 1 : 0); |
| 322 | } |
| 323 | |
| 324 | static void set_kernel_output_values(uint64_t *mask, uint64_t *vals, |
| 325 | struct gpiod_line_config *config) |
| 326 | { |
| 327 | struct per_line_config *per_line; |
| 328 | enum gpiod_line_value value; |
| 329 | size_t i; |
| 330 | |
| 331 | gpiod_line_mask_zero(mask); |
| 332 | gpiod_line_mask_zero(vals); |
| 333 | |
| 334 | for (i = 0; i < config->num_configs; i++) { |
| 335 | per_line = &config->line_configs[i]; |
| 336 | |
| 337 | if (gpiod_line_settings_get_direction( |
| 338 | per_line->node->settings) != |
| 339 | GPIOD_LINE_DIRECTION_OUTPUT) |
| 340 | continue; |
| 341 | |
| 342 | gpiod_line_mask_set_bit(mask, i); |
| 343 | value = gpiod_line_settings_get_output_value( |
| 344 | per_line->node->settings); |
| 345 | set_output_value(vals, i, value); |
| 346 | } |
| 347 | |
| 348 | /* "Global" output values override the ones from per-line settings. */ |
| 349 | for (i = 0; i < config->num_output_values; i++) { |
| 350 | gpiod_line_mask_set_bit(mask, i); |
| 351 | value = config->output_values[i]; |
| 352 | set_output_value(vals, i, value); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void set_output_values(struct gpiod_line_config *config, |
| 357 | struct gpio_v2_line_request *uapi_cfg, |
| 358 | unsigned int *attr_idx) |
| 359 | { |
| 360 | struct gpio_v2_line_config_attribute *attr; |
| 361 | uint64_t mask, values; |
| 362 | |
| 363 | if (!has_at_least_one_output_direction(config)) |
| 364 | return; |
| 365 | |
| 366 | attr = &uapi_cfg->config.attrs[(*attr_idx)++]; |
| 367 | attr->attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES; |
| 368 | set_kernel_output_values(&mask, &values, config); |
| 369 | attr->attr.values = values; |
| 370 | attr->mask = mask; |
| 371 | } |
| 372 | |
| 373 | static int set_debounce_periods(struct gpiod_line_config *config, |
| 374 | struct gpio_v2_line_config *uapi_cfg, |
| 375 | unsigned int *attr_idx) |
| 376 | { |
| 377 | struct gpio_v2_line_config_attribute *attr; |
| 378 | unsigned long period_i, period_j; |
| 379 | uint64_t done, mask; |
| 380 | size_t i, j; |
| 381 | |
| 382 | gpiod_line_mask_zero(&done); |
| 383 | |
| 384 | for (i = 0; i < config->num_configs; i++) { |
| 385 | if (gpiod_line_mask_test_bit(&done, i)) |
| 386 | continue; |
| 387 | |
| 388 | gpiod_line_mask_set_bit(&done, i); |
| 389 | gpiod_line_mask_zero(&mask); |
| 390 | |
| 391 | period_i = gpiod_line_settings_get_debounce_period_us( |
| 392 | config->line_configs[i].node->settings); |
| 393 | if (!period_i) |
| 394 | continue; |
| 395 | |
| 396 | if (*attr_idx == GPIO_V2_LINE_NUM_ATTRS_MAX) { |
| 397 | errno = E2BIG; |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | attr = &uapi_cfg->attrs[(*attr_idx)++]; |
| 402 | attr->attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; |
| 403 | attr->attr.debounce_period_us = period_i; |
| 404 | gpiod_line_mask_set_bit(&mask, i); |
| 405 | |
| 406 | for (j = i; j < config->num_configs; j++) { |
| 407 | period_j = gpiod_line_settings_get_debounce_period_us( |
| 408 | config->line_configs[j].node->settings); |
| 409 | if (period_i == period_j) { |
| 410 | gpiod_line_mask_set_bit(&mask, j); |
| 411 | gpiod_line_mask_set_bit(&done, j); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | attr->mask = mask; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static uint64_t make_kernel_flags(struct gpiod_line_settings *settings) |
| 422 | { |
| 423 | uint64_t flags = 0; |
| 424 | |
| 425 | switch (gpiod_line_settings_get_direction(settings)) { |
| 426 | case GPIOD_LINE_DIRECTION_INPUT: |
| 427 | flags |= GPIO_V2_LINE_FLAG_INPUT; |
| 428 | break; |
| 429 | case GPIOD_LINE_DIRECTION_OUTPUT: |
| 430 | flags |= GPIO_V2_LINE_FLAG_OUTPUT; |
| 431 | break; |
| 432 | default: |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | switch (gpiod_line_settings_get_edge_detection(settings)) { |
| 437 | case GPIOD_LINE_EDGE_FALLING: |
| 438 | flags |= (GPIO_V2_LINE_FLAG_EDGE_FALLING | |
| 439 | GPIO_V2_LINE_FLAG_INPUT); |
| 440 | break; |
| 441 | case GPIOD_LINE_EDGE_RISING: |
| 442 | flags |= (GPIO_V2_LINE_FLAG_EDGE_RISING | |
| 443 | GPIO_V2_LINE_FLAG_INPUT); |
| 444 | break; |
| 445 | case GPIOD_LINE_EDGE_BOTH: |
| 446 | flags |= (GPIO_V2_LINE_FLAG_EDGE_FALLING | |
| 447 | GPIO_V2_LINE_FLAG_EDGE_RISING | |
| 448 | GPIO_V2_LINE_FLAG_INPUT); |
| 449 | break; |
| 450 | default: |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | switch (gpiod_line_settings_get_drive(settings)) { |
| 455 | case GPIOD_LINE_DRIVE_OPEN_DRAIN: |
| 456 | flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; |
| 457 | break; |
| 458 | case GPIOD_LINE_DRIVE_OPEN_SOURCE: |
| 459 | flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; |
| 460 | break; |
| 461 | default: |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | switch (gpiod_line_settings_get_bias(settings)) { |
| 466 | case GPIOD_LINE_BIAS_DISABLED: |
| 467 | flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; |
| 468 | break; |
| 469 | case GPIOD_LINE_BIAS_PULL_UP: |
| 470 | flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; |
| 471 | break; |
| 472 | case GPIOD_LINE_BIAS_PULL_DOWN: |
| 473 | flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; |
| 474 | break; |
| 475 | default: |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | if (gpiod_line_settings_get_active_low(settings)) |
| 480 | flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; |
| 481 | |
| 482 | switch (gpiod_line_settings_get_event_clock(settings)) { |
| 483 | case GPIOD_LINE_CLOCK_REALTIME: |
| 484 | flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; |
| 485 | break; |
| 486 | case GPIOD_LINE_CLOCK_HTE: |
| 487 | flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; |
| 488 | break; |
| 489 | default: |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | return flags; |
| 494 | } |
| 495 | |
| 496 | static bool settings_equal(struct gpiod_line_settings *left, |
| 497 | struct gpiod_line_settings *right) |
| 498 | { |
| 499 | if (gpiod_line_settings_get_direction(left) != |
| 500 | gpiod_line_settings_get_direction(right)) |
| 501 | return false; |
| 502 | |
| 503 | if (gpiod_line_settings_get_edge_detection(left) != |
| 504 | gpiod_line_settings_get_edge_detection(right)) |
| 505 | return false; |
| 506 | |
| 507 | if (gpiod_line_settings_get_bias(left) != |
| 508 | gpiod_line_settings_get_bias(right)) |
| 509 | return false; |
| 510 | |
| 511 | if (gpiod_line_settings_get_drive(left) != |
| 512 | gpiod_line_settings_get_drive(right)) |
| 513 | return false; |
| 514 | |
| 515 | if (gpiod_line_settings_get_active_low(left) != |
| 516 | gpiod_line_settings_get_active_low(right)) |
| 517 | return false; |
| 518 | |
| 519 | if (gpiod_line_settings_get_event_clock(left) != |
| 520 | gpiod_line_settings_get_event_clock(right)) |
| 521 | return false; |
| 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | static int set_flags(struct gpiod_line_config *config, |
| 527 | struct gpio_v2_line_config *uapi_cfg, |
| 528 | unsigned int *attr_idx) |
| 529 | { |
| 530 | struct gpiod_line_settings *settings_i, *settings_j; |
| 531 | struct gpio_v2_line_config_attribute *attr; |
| 532 | bool globals_taken = false; |
| 533 | uint64_t done, mask; |
| 534 | size_t i, j; |
| 535 | |
| 536 | gpiod_line_mask_zero(&done); |
| 537 | |
| 538 | for (i = 0; i < config->num_configs; i++) { |
| 539 | if (gpiod_line_mask_test_bit(&done, i)) |
| 540 | continue; |
| 541 | |
| 542 | gpiod_line_mask_set_bit(&done, i); |
| 543 | |
| 544 | settings_i = config->line_configs[i].node->settings; |
| 545 | |
| 546 | if (!globals_taken) { |
| 547 | globals_taken = true; |
| 548 | uapi_cfg->flags = make_kernel_flags(settings_i); |
| 549 | |
| 550 | for (j = i; j < config->num_configs; j++) { |
| 551 | settings_j = |
| 552 | config->line_configs[j].node->settings; |
| 553 | if (settings_equal(settings_i, settings_j)) |
| 554 | gpiod_line_mask_set_bit(&done, j); |
| 555 | } |
| 556 | } else { |
| 557 | gpiod_line_mask_zero(&mask); |
| 558 | gpiod_line_mask_set_bit(&mask, i); |
| 559 | |
| 560 | if (*attr_idx == GPIO_V2_LINE_NUM_ATTRS_MAX) { |
| 561 | errno = E2BIG; |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | attr = &uapi_cfg->attrs[(*attr_idx)++]; |
| 566 | attr->attr.id = GPIO_V2_LINE_ATTR_ID_FLAGS; |
| 567 | attr->attr.flags = make_kernel_flags(settings_i); |
| 568 | |
| 569 | for (j = i; j < config->num_configs; j++) { |
| 570 | settings_j = |
| 571 | config->line_configs[j].node->settings; |
| 572 | if (settings_equal(settings_i, settings_j)) { |
| 573 | gpiod_line_mask_set_bit(&done, j); |
| 574 | gpiod_line_mask_set_bit(&mask, j); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | attr->mask = mask; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int gpiod_line_config_to_uapi(struct gpiod_line_config *config, |
| 586 | struct gpio_v2_line_request *uapi_cfg) |
| 587 | { |
| 588 | unsigned int attr_idx = 0; |
| 589 | int ret; |
| 590 | |
| 591 | set_offsets(config, uapi_cfg); |
| 592 | set_output_values(config, uapi_cfg, &attr_idx); |
| 593 | |
| 594 | ret = set_debounce_periods(config, &uapi_cfg->config, &attr_idx); |
| 595 | if (ret) |
| 596 | return -1; |
| 597 | |
| 598 | ret = set_flags(config, &uapi_cfg->config, &attr_idx); |
| 599 | if (ret) |
| 600 | return -1; |
| 601 | |
| 602 | uapi_cfg->config.num_attrs = attr_idx; |
| 603 | |
| 604 | return 0; |
| 605 | } |