1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 3 /** This is the original gpio.h file processed by dstep */ 4 module gpiod; 5 6 /* 7 * This file is part of libgpiod. 8 * 9 * Copyright (C) 2017-2018 Bartosz Golaszewski <bartekgola@gmail.com> 10 */ 11 12 import core.sys.posix.sys.select; 13 14 extern (C): 15 16 /** 17 * @mainpage libgpiod public API 18 * 19 * This is the complete documentation of the public API made available to 20 * users of libgpiod. 21 * 22 * <p>The public header is logically split into two high-level parts: the 23 * simple API and the low-level API. The former allows users to easily 24 * interact with the GPIOs in the system without dealing with the low-level 25 * data structures and resource control. The latter gives the user much more 26 * fine-grained control over the GPIO interface. 27 * 28 * <p>The low-level API is further logically split into several parts such 29 * as: GPIO chip & line operators, iterators, GPIO events handling etc. 30 * 31 * <p>General note on error handling: all routines exported by libgpiod set 32 * errno to one of the error values defined in errno.h upon failure. The way 33 * of notifying the caller that an error occurred varies between functions, 34 * but in general a function that returns an int, returns -1 on error, while 35 * a function returning a pointer bails out on error condition by returning 36 * a NULL pointer. 37 */ 38 39 struct gpiod_chip; 40 struct gpiod_line; 41 struct gpiod_chip_iter; 42 struct gpiod_line_iter; 43 44 /** 45 * @defgroup __common__ Common helper macros 46 * @{ 47 * 48 * Commonly used utility macros. 49 */ 50 51 /** 52 * @brief Makes symbol visible. 53 */ 54 55 /** 56 * @brief Marks a function argument or variable as potentially unused. 57 */ 58 59 /** 60 * @brief Shift 1 by given offset. 61 * @param nr Bit position. 62 * @return 1 shifted by nr. 63 */ 64 extern (D) auto GPIOD_BIT(T)(auto ref T nr) 65 { 66 return 1UL << nr; 67 } 68 69 /** 70 * @brief Marks a public function as deprecated. 71 */ 72 73 /** 74 * @} 75 * 76 * @defgroup __high_level__ High-level API 77 * @{ 78 * 79 * Simple high-level routines for straightforward GPIO manipulation without 80 * the need to use the gpiod_* structures or to keep track of resources. 81 */ 82 83 /** 84 * @brief Miscellaneous GPIO flags. 85 */ 86 enum 87 { 88 GPIOD_CTXLESS_FLAG_OPEN_DRAIN = GPIOD_BIT(0), 89 /**< The line is an open-drain port. */ 90 GPIOD_CTXLESS_FLAG_OPEN_SOURCE = GPIOD_BIT(1), 91 /**< The line is an open-source port. */ 92 GPIOD_CTXLESS_FLAG_BIAS_DISABLE = GPIOD_BIT(2), 93 /**< The line has neither either pull-up nor pull-down resistor */ 94 GPIOD_CTXLESS_FLAG_BIAS_PULL_DOWN = GPIOD_BIT(3), 95 /**< The line has pull-down resistor enabled */ 96 GPIOD_CTXLESS_FLAG_BIAS_PULL_UP = GPIOD_BIT(4) 97 /**< The line has pull-up resistor enabled */ 98 } 99 100 /** 101 * @brief Read current value from a single GPIO line. 102 * @param device Name, path, number or label of the gpiochip. 103 * @param offset Offset of the GPIO line. 104 * @param active_low The active state of this line - true if low. 105 * @param consumer Name of the consumer. 106 * @return 0 or 1 (GPIO value) if the operation succeeds, -1 on error. 107 */ 108 int gpiod_ctxless_get_value ( 109 const(char)* device, 110 uint offset, 111 bool active_low, 112 const(char)* consumer); 113 114 /** 115 * @brief Read current value from a single GPIO line. 116 * @param device Name, path, number or label of the gpiochip. 117 * @param offset Offset of the GPIO line. 118 * @param active_low The active state of this line - true if low. 119 * @param consumer Name of the consumer. 120 * @param flags The flags for the line. 121 * @return 0 or 1 (GPIO value) if the operation succeeds, -1 on error. 122 */ 123 int gpiod_ctxless_get_value_ext ( 124 const(char)* device, 125 uint offset, 126 bool active_low, 127 const(char)* consumer, 128 int flags); 129 130 /** 131 * @brief Read current values from a set of GPIO lines. 132 * @param device Name, path, number or label of the gpiochip. 133 * @param offsets Array of offsets of lines whose values should be read. 134 * @param values Buffer in which the values will be stored. 135 * @param num_lines Number of lines, must be > 0. 136 * @param active_low The active state of the lines - true if low. 137 * @param consumer Name of the consumer. 138 * @return 0 if the operation succeeds, -1 on error. 139 */ 140 int gpiod_ctxless_get_value_multiple ( 141 const(char)* device, 142 const(uint)* offsets, 143 int* values, 144 uint num_lines, 145 bool active_low, 146 const(char)* consumer); 147 148 /** 149 * @brief Read current values from a set of GPIO lines. 150 * @param device Name, path, number or label of the gpiochip. 151 * @param offsets Array of offsets of lines whose values should be read. 152 * @param values Buffer in which the values will be stored. 153 * @param num_lines Number of lines, must be > 0. 154 * @param active_low The active state of this line - true if low. 155 * @param consumer Name of the consumer. 156 * @param flags The flags for the lines. 157 * @return 0 if the operation succeeds, -1 on error. 158 */ 159 int gpiod_ctxless_get_value_multiple_ext ( 160 const(char)* device, 161 const(uint)* offsets, 162 int* values, 163 uint num_lines, 164 bool active_low, 165 const(char)* consumer, 166 int flags); 167 168 /** 169 * @brief Simple set value callback signature. 170 */ 171 alias gpiod_ctxless_set_value_cb = void function (void*); 172 173 /** 174 * @brief Set value of a single GPIO line. 175 * @param device Name, path, number or label of the gpiochip. 176 * @param offset The offset of the GPIO line. 177 * @param value New value (0 or 1). 178 * @param active_low The active state of this line - true if low. 179 * @param consumer Name of the consumer. 180 * @param cb Optional callback function that will be called right after setting 181 * the value. Users can use this, for example, to pause the execution 182 * after toggling a GPIO. 183 * @param data Optional user data that will be passed to the callback function. 184 * @return 0 if the operation succeeds, -1 on error. 185 */ 186 int gpiod_ctxless_set_value ( 187 const(char)* device, 188 uint offset, 189 int value, 190 bool active_low, 191 const(char)* consumer, 192 gpiod_ctxless_set_value_cb cb, 193 void* data); 194 195 /** 196 * @brief Set value of a single GPIO line. 197 * @param device Name, path, number or label of the gpiochip. 198 * @param offset The offset of the GPIO line. 199 * @param value New value (0 or 1). 200 * @param active_low The active state of this line - true if low. 201 * @param consumer Name of the consumer. 202 * @param cb Optional callback function that will be called right after setting 203 * the value. Users can use this, for example, to pause the execution 204 * after toggling a GPIO. 205 * @param data Optional user data that will be passed to the callback function. 206 * @param flags The flags for the line. 207 * @return 0 if the operation succeeds, -1 on error. 208 */ 209 int gpiod_ctxless_set_value_ext ( 210 const(char)* device, 211 uint offset, 212 int value, 213 bool active_low, 214 const(char)* consumer, 215 gpiod_ctxless_set_value_cb cb, 216 void* data, 217 int flags); 218 219 /** 220 * @brief Set values of multiple GPIO lines. 221 * @param device Name, path, number or label of the gpiochip. 222 * @param offsets Array of offsets of lines the values of which should be set. 223 * @param values Array of integers containing new values. 224 * @param num_lines Number of lines, must be > 0. 225 * @param active_low The active state of the lines - true if low. 226 * @param consumer Name of the consumer. 227 * @param cb Optional callback function that will be called right after setting 228 * all values. Works the same as in ::gpiod_ctxless_set_value. 229 * @param data Optional user data that will be passed to the callback function. 230 * @return 0 if the operation succeeds, -1 on error. 231 */ 232 int gpiod_ctxless_set_value_multiple ( 233 const(char)* device, 234 const(uint)* offsets, 235 const(int)* values, 236 uint num_lines, 237 bool active_low, 238 const(char)* consumer, 239 gpiod_ctxless_set_value_cb cb, 240 void* data); 241 242 /** 243 * @brief Set values of multiple GPIO lines. 244 * @param device Name, path, number or label of the gpiochip. 245 * @param offsets Array of offsets of lines the values of which should be set. 246 * @param values Array of integers containing new values. 247 * @param num_lines Number of lines, must be > 0. 248 * @param active_low The active state of this line - true if low. 249 * @param consumer Name of the consumer. 250 * @param cb Optional callback function that will be called right after setting 251 * all values. Works the same as in ::gpiod_ctxless_set_value. 252 * @param data Optional user data that will be passed to the callback function. 253 * @param flags The flags for the lines. 254 * @return 0 if the operation succeeds, -1 on error. 255 */ 256 int gpiod_ctxless_set_value_multiple_ext ( 257 const(char)* device, 258 const(uint)* offsets, 259 const(int)* values, 260 uint num_lines, 261 bool active_low, 262 const(char)* consumer, 263 gpiod_ctxless_set_value_cb cb, 264 void* data, 265 int flags); 266 267 /** 268 * @brief Event types that the ctxless event monitor can wait for. 269 */ 270 enum 271 { 272 /**< Wait for rising edge events only. */ 273 GPIOD_CTXLESS_EVENT_RISING_EDGE = 1, 274 /**< Wait for falling edge events only. */ 275 GPIOD_CTXLESS_EVENT_FALLING_EDGE = 2, 276 /**< Wait for both types of events. */ 277 GPIOD_CTXLESS_EVENT_BOTH_EDGES = 3 278 } 279 280 /** 281 * @brief Event types that can be passed to the ctxless event callback. 282 */ 283 enum 284 { 285 GPIOD_CTXLESS_EVENT_CB_TIMEOUT = 1, 286 /**< Waiting for events timed out. */ 287 GPIOD_CTXLESS_EVENT_CB_RISING_EDGE = 2, 288 /**< Rising edge event occured. */ 289 GPIOD_CTXLESS_EVENT_CB_FALLING_EDGE = 3 290 /**< Falling edge event occured. */ 291 } 292 293 /** 294 * @brief Return status values that the ctxless event callback can return. 295 */ 296 enum 297 { 298 GPIOD_CTXLESS_EVENT_CB_RET_ERR = -1, 299 /**< Stop processing events and indicate an error. */ 300 GPIOD_CTXLESS_EVENT_CB_RET_OK = 0, 301 /**< Continue processing events. */ 302 GPIOD_CTXLESS_EVENT_CB_RET_STOP = 1 303 /**< Stop processing events. */ 304 } 305 306 /** 307 * @brief Simple event callback signature. 308 * 309 * The callback function takes the following arguments: event type (int), 310 * GPIO line offset (unsigned int), event timestamp (const struct timespec *) 311 * and a pointer to user data (void *). 312 * 313 * This callback is called by the ctxless event loop functions for each GPIO 314 * event. If the callback returns ::GPIOD_CTXLESS_EVENT_CB_RET_ERR, it should 315 * also set errno. 316 */ 317 alias gpiod_ctxless_event_handle_cb = int function ( 318 int, 319 uint, 320 const(timespec)*, 321 void*); 322 323 /** 324 * @brief Return status values that the ctxless event poll callback can return. 325 * 326 * Positive value returned from the polling callback indicates the number of 327 * events that occurred on the set of monitored lines. 328 */ 329 enum 330 { 331 GPIOD_CTXLESS_EVENT_POLL_RET_STOP = -2, 332 /**< The event loop should stop processing events. */ 333 GPIOD_CTXLESS_EVENT_POLL_RET_ERR = -1, 334 /**< Polling error occurred (the polling function should set errno). */ 335 GPIOD_CTXLESS_EVENT_POLL_RET_TIMEOUT = 0 336 /**< Poll timed out. */ 337 } 338 339 /** 340 * @brief Helper structure for the ctxless event loop poll callback. 341 */ 342 struct gpiod_ctxless_event_poll_fd 343 { 344 int fd; 345 /**< File descriptor number. */ 346 bool event; 347 /**< Indicates whether an event occurred on this file descriptor. */ 348 } 349 350 /** 351 * @brief Simple event poll callback signature. 352 * 353 * The poll callback function takes the following arguments: number of lines 354 * (unsigned int), an array of file descriptors on which input events should 355 * be monitored (struct gpiod_ctxless_event_poll_fd *), poll timeout 356 * (const struct timespec *) and a pointer to user data (void *). 357 * 358 * The callback should poll for input events on the set of descriptors and 359 * return an appropriate value that can be interpreted by the event loop 360 * routine. 361 */ 362 alias gpiod_ctxless_event_poll_cb = int function ( 363 uint, 364 gpiod_ctxless_event_poll_fd*, 365 const(timespec)*, 366 void*); 367 368 /** 369 * @brief Wait for events on a single GPIO line. 370 * @param device Name, path, number or label of the gpiochip. 371 * @param offset GPIO line offset to monitor. 372 * @param active_low The active state of this line - true if low. 373 * @param consumer Name of the consumer. 374 * @param timeout Maximum wait time for each iteration. 375 * @param poll_cb Callback function to call when waiting for events. 376 * @param event_cb Callback function to call for each line event. 377 * @param data User data passed to the callback. 378 * @return 0 if no errors were encountered, -1 if an error occurred. 379 * @note The way the ctxless event loop works is described in detail in 380 * ::gpiod_ctxless_event_loop_multiple - this is just a wrapper aound 381 * this routine which calls it for a single GPIO line. 382 */ 383 int gpiod_ctxless_event_loop ( 384 const(char)* device, 385 uint offset, 386 bool active_low, 387 const(char)* consumer, 388 const(timespec)* timeout, 389 gpiod_ctxless_event_poll_cb poll_cb, 390 gpiod_ctxless_event_handle_cb event_cb, 391 void* data); 392 393 /** 394 * @brief Wait for events on multiple GPIO lines. 395 * @param device Name, path, number or label of the gpiochip. 396 * @param offsets Array of GPIO line offsets to monitor. 397 * @param num_lines Number of lines to monitor. 398 * @param active_low The active state of this line - true if low. 399 * @param consumer Name of the consumer. 400 * @param timeout Maximum wait time for each iteration. 401 * @param poll_cb Callback function to call when waiting for events. Can 402 * be NULL. 403 * @param event_cb Callback function to call on event occurrence. 404 * @param data User data passed to the callback. 405 * @return 0 no errors were encountered, -1 if an error occurred. 406 * @note The poll callback can be NULL in which case the routine will fall 407 * back to a basic, ppoll() based callback. 408 * 409 * Internally this routine opens the GPIO chip, requests the set of lines for 410 * both-edges events and calls the polling callback in a loop. The role of the 411 * polling callback is to detect input events on a set of file descriptors and 412 * notify the caller about the fds ready for reading. 413 * 414 * The ctxless event loop then reads each queued event from marked descriptors 415 * and calls the event callback. Both callbacks can stop the loop at any 416 * point. 417 * 418 * The poll_cb argument can be NULL in which case the function falls back to 419 * a default, ppoll() based callback. 420 */ 421 int gpiod_ctxless_event_loop_multiple ( 422 const(char)* device, 423 const(uint)* offsets, 424 uint num_lines, 425 bool active_low, 426 const(char)* consumer, 427 const(timespec)* timeout, 428 gpiod_ctxless_event_poll_cb poll_cb, 429 gpiod_ctxless_event_handle_cb event_cb, 430 void* data); 431 432 /** 433 * @brief Wait for events on a single GPIO line. 434 * @param device Name, path, number or label of the gpiochip. 435 * @param event_type Type of events to listen for. 436 * @param offset GPIO line offset to monitor. 437 * @param active_low The active state of this line - true if low. 438 * @param consumer Name of the consumer. 439 * @param timeout Maximum wait time for each iteration. 440 * @param poll_cb Callback function to call when waiting for events. 441 * @param event_cb Callback function to call for each line event. 442 * @param data User data passed to the callback. 443 * @return 0 if no errors were encountered, -1 if an error occurred. 444 * @note The way the ctxless event loop works is described in detail in 445 * ::gpiod_ctxless_event_monitor_multiple - this is just a wrapper aound 446 * this routine which calls it for a single GPIO line. 447 */ 448 int gpiod_ctxless_event_monitor ( 449 const(char)* device, 450 int event_type, 451 uint offset, 452 bool active_low, 453 const(char)* consumer, 454 const(timespec)* timeout, 455 gpiod_ctxless_event_poll_cb poll_cb, 456 gpiod_ctxless_event_handle_cb event_cb, 457 void* data); 458 459 /** 460 * @brief Wait for events on a single GPIO line. 461 * @param device Name, path, number or label of the gpiochip. 462 * @param event_type Type of events to listen for. 463 * @param offset GPIO line offset to monitor. 464 * @param active_low The active state of this line - true if low. 465 * @param consumer Name of the consumer. 466 * @param timeout Maximum wait time for each iteration. 467 * @param poll_cb Callback function to call when waiting for events. 468 * @param event_cb Callback function to call for each line event. 469 * @param data User data passed to the callback. 470 * @param flags The flags for the line. 471 * @return 0 if no errors were encountered, -1 if an error occurred. 472 * @note The way the ctxless event loop works is described in detail in 473 * ::gpiod_ctxless_event_monitor_multiple - this is just a wrapper aound 474 * this routine which calls it for a single GPIO line. 475 */ 476 int gpiod_ctxless_event_monitor_ext ( 477 const(char)* device, 478 int event_type, 479 uint offset, 480 bool active_low, 481 const(char)* consumer, 482 const(timespec)* timeout, 483 gpiod_ctxless_event_poll_cb poll_cb, 484 gpiod_ctxless_event_handle_cb event_cb, 485 void* data, 486 int flags); 487 488 /** 489 * @brief Wait for events on multiple GPIO lines. 490 * @param device Name, path, number or label of the gpiochip. 491 * @param event_type Type of events to listen for. 492 * @param offsets Array of GPIO line offsets to monitor. 493 * @param num_lines Number of lines to monitor. 494 * @param active_low The active state of this line - true if low. 495 * @param consumer Name of the consumer. 496 * @param timeout Maximum wait time for each iteration. 497 * @param poll_cb Callback function to call when waiting for events. Can 498 * be NULL. 499 * @param event_cb Callback function to call on event occurrence. 500 * @param data User data passed to the callback. 501 * @return 0 no errors were encountered, -1 if an error occurred. 502 * @note The poll callback can be NULL in which case the routine will fall 503 * back to a basic, ppoll() based callback. 504 * 505 * Internally this routine opens the GPIO chip, requests the set of lines for 506 * the type of events specified in the event_type parameter and calls the 507 * polling callback in a loop. The role of the polling callback is to detect 508 * input events on a set of file descriptors and notify the caller about the 509 * fds ready for reading. 510 * 511 * The ctxless event loop then reads each queued event from marked descriptors 512 * and calls the event callback. Both callbacks can stop the loop at any 513 * point. 514 * 515 * The poll_cb argument can be NULL in which case the function falls back to 516 * a default, ppoll() based callback. 517 */ 518 int gpiod_ctxless_event_monitor_multiple ( 519 const(char)* device, 520 int event_type, 521 const(uint)* offsets, 522 uint num_lines, 523 bool active_low, 524 const(char)* consumer, 525 const(timespec)* timeout, 526 gpiod_ctxless_event_poll_cb poll_cb, 527 gpiod_ctxless_event_handle_cb event_cb, 528 void* data); 529 530 /** 531 * @brief Wait for events on multiple GPIO lines. 532 * @param device Name, path, number or label of the gpiochip. 533 * @param event_type Type of events to listen for. 534 * @param offsets Array of GPIO line offsets to monitor. 535 * @param num_lines Number of lines to monitor. 536 * @param active_low The active state of this line - true if low. 537 * @param consumer Name of the consumer. 538 * @param timeout Maximum wait time for each iteration. 539 * @param poll_cb Callback function to call when waiting for events. Can 540 * be NULL. 541 * @param event_cb Callback function to call on event occurrence. 542 * @param data User data passed to the callback. 543 * @param flags The flags for the lines. 544 * @return 0 no errors were encountered, -1 if an error occurred. 545 * @note The poll callback can be NULL in which case the routine will fall 546 * back to a basic, ppoll() based callback. 547 * 548 * Internally this routine opens the GPIO chip, requests the set of lines for 549 * the type of events specified in the event_type parameter and calls the 550 * polling callback in a loop. The role of the polling callback is to detect 551 * input events on a set of file descriptors and notify the caller about the 552 * fds ready for reading. 553 * 554 * The ctxless event loop then reads each queued event from marked descriptors 555 * and calls the event callback. Both callbacks can stop the loop at any 556 * point. 557 * 558 * The poll_cb argument can be NULL in which case the function falls back to 559 * a default, ppoll() based callback. 560 */ 561 int gpiod_ctxless_event_monitor_multiple_ext ( 562 const(char)* device, 563 int event_type, 564 const(uint)* offsets, 565 uint num_lines, 566 bool active_low, 567 const(char)* consumer, 568 const(timespec)* timeout, 569 gpiod_ctxless_event_poll_cb poll_cb, 570 gpiod_ctxless_event_handle_cb event_cb, 571 void* data, 572 int flags); 573 574 /** 575 * @brief Determine the chip name and line offset of a line with given name. 576 * @param name The name of the GPIO line to lookup. 577 * @param chipname Buffer in which the name of the GPIO chip will be stored. 578 * @param chipname_size Size of the chip name buffer. 579 * @param offset Pointer to an integer in which the line offset will be stored. 580 * @return -1 on error, 0 if the line with given name doesn't exist and 1 if 581 * the line was found. In the first two cases the contents of chipname 582 * and offset remain unchanged. 583 * @note The chip name is truncated if the buffer can't hold its entire size. 584 */ 585 int gpiod_ctxless_find_line ( 586 const(char)* name, 587 char* chipname, 588 size_t chipname_size, 589 uint* offset); 590 591 /** 592 * @} 593 * 594 * @defgroup __chips__ GPIO chip operations 595 * @{ 596 * 597 * Functions and data structures dealing with GPIO chips. 598 */ 599 600 /** 601 * @brief Open a gpiochip by path. 602 * @param path Path to the gpiochip device file. 603 * @return GPIO chip handle or NULL if an error occurred. 604 */ 605 gpiod_chip* gpiod_chip_open (const(char)* path); 606 607 /** 608 * @brief Open a gpiochip by name. 609 * @param name Name of the gpiochip to open. 610 * @return GPIO chip handle or NULL if an error occurred. 611 * 612 * This routine appends name to '/dev/' to create the path. 613 */ 614 gpiod_chip* gpiod_chip_open_by_name (const(char)* name); 615 616 /** 617 * @brief Open a gpiochip by number. 618 * @param num Number of the gpiochip. 619 * @return GPIO chip handle or NULL if an error occurred. 620 * 621 * This routine appends num to '/dev/gpiochip' to create the path. 622 */ 623 gpiod_chip* gpiod_chip_open_by_number (uint num); 624 625 /** 626 * @brief Open a gpiochip by label. 627 * @param label Label of the gpiochip to open. 628 * @return GPIO chip handle or NULL if the chip with given label was not found 629 * or an error occured. 630 * @note If the chip cannot be found but no other error occurred, errno is set 631 * to ENOENT. 632 */ 633 gpiod_chip* gpiod_chip_open_by_label (const(char)* label); 634 635 /** 636 * @brief Open a gpiochip based on the best guess what the path is. 637 * @param descr String describing the gpiochip. 638 * @return GPIO chip handle or NULL if an error occurred. 639 * 640 * This routine tries to figure out whether the user passed it the path to the 641 * GPIO chip, its name, label or number as a string. Then it tries to open it 642 * using one of the gpiod_chip_open** variants. 643 */ 644 gpiod_chip* gpiod_chip_open_lookup (const(char)* descr); 645 646 /** 647 * @brief Close a GPIO chip handle and release all allocated resources. 648 * @param chip The GPIO chip object. 649 */ 650 void gpiod_chip_close (gpiod_chip* chip); 651 652 /** 653 * @brief Get the GPIO chip name as represented in the kernel. 654 * @param chip The GPIO chip object. 655 * @return Pointer to a human-readable string containing the chip name. 656 */ 657 const(char)* gpiod_chip_name (gpiod_chip* chip); 658 659 /** 660 * @brief Get the GPIO chip label as represented in the kernel. 661 * @param chip The GPIO chip object. 662 * @return Pointer to a human-readable string containing the chip label. 663 */ 664 const(char)* gpiod_chip_label (gpiod_chip* chip); 665 666 /** 667 * @brief Get the number of GPIO lines exposed by this chip. 668 * @param chip The GPIO chip object. 669 * @return Number of GPIO lines. 670 */ 671 uint gpiod_chip_num_lines (gpiod_chip* chip); 672 673 /** 674 * @brief Get the handle to the GPIO line at given offset. 675 * @param chip The GPIO chip object. 676 * @param offset The offset of the GPIO line. 677 * @return Pointer to the GPIO line handle or NULL if an error occured. 678 */ 679 gpiod_line* gpiod_chip_get_line (gpiod_chip* chip, uint offset); 680 681 /** 682 * @brief Retrieve a set of lines and store them in a line bulk object. 683 * @param chip The GPIO chip object. 684 * @param offsets Array of offsets of lines to retrieve. 685 * @param num_offsets Number of lines to retrieve. 686 * @param bulk Line bulk object in which to store the line handles. 687 * @return 0 on success, -1 on error. 688 */ 689 int gpiod_chip_get_lines ( 690 gpiod_chip* chip, 691 uint* offsets, 692 uint num_offsets, 693 gpiod_line_bulk* bulk); 694 695 /** 696 * @brief Retrieve all lines exposed by a chip and store them in a bulk object. 697 * @param chip The GPIO chip object. 698 * @param bulk Line bulk object in which to store the line handles. 699 * @return 0 on success, -1 on error. 700 */ 701 int gpiod_chip_get_all_lines (gpiod_chip* chip, gpiod_line_bulk* bulk); 702 703 /** 704 * @brief Find a GPIO line by name among lines associated with given GPIO chip. 705 * @param chip The GPIO chip object. 706 * @param name The name of the GPIO line. 707 * @return Pointer to the GPIO line handle or NULL if the line could not be 708 * found or an error occurred. 709 * @note In case a line with given name is not associated with given chip, the 710 * function sets errno to ENOENT. 711 */ 712 gpiod_line* gpiod_chip_find_line (gpiod_chip* chip, const(char)* name); 713 714 /** 715 * @brief Find a set of GPIO lines by names among lines exposed by this chip. 716 * @param chip The GPIO chip object. 717 * @param names Array of pointers to C-strings containing the names of the 718 * lines to lookup. Must end with a NULL-pointer. 719 * @param bulk Line bulk object in which the located lines will be stored. 720 * @return 0 if all lines were located, -1 on error. 721 * @note If at least one line from the list could not be found among the lines 722 * exposed by this chip, the function sets errno to ENOENT. 723 */ 724 int gpiod_chip_find_lines ( 725 gpiod_chip* chip, 726 const(char*)* names, 727 gpiod_line_bulk* bulk); 728 729 /** 730 * @} 731 * 732 * @defgroup __lines__ GPIO line operations 733 * @{ 734 * 735 * Functions and data structures dealing with GPIO lines. 736 * 737 * @defgroup __line_bulk__ Operating on multiple lines 738 * @{ 739 * 740 * Convenience data structures and helper functions for storing and operating 741 * on multiple lines at once. 742 */ 743 744 /** 745 * @brief Maximum number of GPIO lines that can be requested at once. 746 */ 747 enum GPIOD_LINE_BULK_MAX_LINES = 64; 748 749 /** 750 * @brief Helper structure for storing a set of GPIO line objects. 751 * 752 * This structure is used in all operations involving sets of GPIO lines. If 753 * a bulk object is being passed to a function while containing zero lines, 754 * the result is undefined. 755 */ 756 struct gpiod_line_bulk 757 { 758 gpiod_line*[GPIOD_LINE_BULK_MAX_LINES] lines; 759 /**< Buffer for line pointers. */ 760 uint num_lines; 761 /**< Number of lines currently held in this structure. */ 762 } 763 764 /** 765 * @brief Static initializer for GPIO bulk objects. 766 * 767 * This macro simply sets the internally held number of lines to 0. 768 */ 769 770 /** 771 * @brief Initialize a GPIO bulk object. 772 * @param bulk Line bulk object. 773 * 774 * This routine simply sets the internally held number of lines to 0. 775 */ 776 void gpiod_line_bulk_init (gpiod_line_bulk* bulk); 777 778 /** 779 * @brief Add a single line to a GPIO bulk object. 780 * @param bulk Line bulk object. 781 * @param line Line to add. 782 */ 783 void gpiod_line_bulk_add (gpiod_line_bulk* bulk, gpiod_line* line); 784 785 /** 786 * @brief Retrieve the line handle from a line bulk object at given offset. 787 * @param bulk Line bulk object. 788 * @param offset Line offset. 789 * @return Line handle at given offset. 790 */ 791 gpiod_line* gpiod_line_bulk_get_line (gpiod_line_bulk* bulk, uint offset); 792 793 /** 794 * @brief Retrieve the number of GPIO lines held by this line bulk object. 795 * @param bulk Line bulk object. 796 * @return Number of lines held by this line bulk. 797 */ 798 uint gpiod_line_bulk_num_lines (gpiod_line_bulk* bulk); 799 800 /** 801 * @brief Iterate over all line handles held by a line bulk object. 802 * @param bulk Line bulk object. 803 * @param line GPIO line handle. On each iteration, the subsequent line handle 804 * is assigned to this pointer. 805 * @param lineptr Pointer to a GPIO line handle used to store the loop state. 806 */ 807 808 /** 809 * @brief Iterate over all line handles held by a line bulk object (integer 810 * counter variant). 811 * @param bulk Line bulk object. 812 * @param line GPIO line handle. On each iteration, the subsequent line handle 813 * is assigned to this pointer. 814 * @param offset An integer variable used to store the loop state. 815 * 816 * This is a variant of ::gpiod_line_bulk_foreach_line which uses an integer 817 * variable (either signed or unsigned) to store the loop state. This offset 818 * variable is guaranteed to correspond to the offset of the current line in 819 * the bulk->lines array. 820 */ 821 822 /** 823 * @} 824 * 825 * @defgroup __line_info__ Line info 826 * @{ 827 * 828 * Definitions and functions for retrieving kernel information about both 829 * requested and free lines. 830 */ 831 832 /** 833 * @brief Possible direction settings. 834 */ 835 enum 836 { 837 GPIOD_LINE_DIRECTION_INPUT = 1, 838 /**< Direction is input - we're reading the state of a GPIO line. */ 839 GPIOD_LINE_DIRECTION_OUTPUT = 2 840 /**< Direction is output - we're driving the GPIO line. */ 841 } 842 843 /** 844 * @brief Possible active state settings. 845 */ 846 enum 847 { 848 GPIOD_LINE_ACTIVE_STATE_HIGH = 1, 849 /**< The active state of a GPIO is active-high. */ 850 GPIOD_LINE_ACTIVE_STATE_LOW = 2 851 /**< The active state of a GPIO is active-low. */ 852 } 853 854 /** 855 * @brief Possible internal bias settings. 856 */ 857 enum 858 { 859 GPIOD_LINE_BIAS_AS_IS = 1, 860 /**< The internal bias state is unknown. */ 861 GPIOD_LINE_BIAS_DISABLE = 2, 862 /**< The internal bias is disabled. */ 863 GPIOD_LINE_BIAS_PULL_UP = 3, 864 /**< The internal pull-up bias is enabled. */ 865 GPIOD_LINE_BIAS_PULL_DOWN = 4 866 /**< The internal pull-down bias is enabled. */ 867 } 868 869 /** 870 * @brief Read the GPIO line offset. 871 * @param line GPIO line object. 872 * @return Line offset. 873 */ 874 uint gpiod_line_offset (gpiod_line* line); 875 876 /** 877 * @brief Read the GPIO line name. 878 * @param line GPIO line object. 879 * @return Name of the GPIO line as it is represented in the kernel. This 880 * routine returns a pointer to a null-terminated string or NULL if 881 * the line is unnamed. 882 */ 883 const(char)* gpiod_line_name (gpiod_line* line); 884 885 /** 886 * @brief Read the GPIO line consumer name. 887 * @param line GPIO line object. 888 * @return Name of the GPIO consumer name as it is represented in the 889 * kernel. This routine returns a pointer to a null-terminated string 890 * or NULL if the line is not used. 891 */ 892 const(char)* gpiod_line_consumer (gpiod_line* line); 893 894 /** 895 * @brief Read the GPIO line direction setting. 896 * @param line GPIO line object. 897 * @return Returns GPIOD_LINE_DIRECTION_INPUT or GPIOD_LINE_DIRECTION_OUTPUT. 898 */ 899 int gpiod_line_direction (gpiod_line* line); 900 901 /** 902 * @brief Read the GPIO line active state setting. 903 * @param line GPIO line object. 904 * @return Returns GPIOD_LINE_ACTIVE_STATE_HIGH or GPIOD_LINE_ACTIVE_STATE_LOW. 905 */ 906 int gpiod_line_active_state (gpiod_line* line); 907 908 /** 909 * @brief Read the GPIO line bias setting. 910 * @param line GPIO line object. 911 * @return Returns GPIOD_LINE_BIAS_PULL_UP, GPIOD_LINE_BIAS_PULL_DOWN, 912 * GPIOD_LINE_BIAS_DISABLE or GPIOD_LINE_BIAS_AS_IS. 913 */ 914 int gpiod_line_bias (gpiod_line* line); 915 916 /** 917 * @brief Check if the line is currently in use. 918 * @param line GPIO line object. 919 * @return True if the line is in use, false otherwise. 920 * 921 * The user space can't know exactly why a line is busy. It may have been 922 * requested by another process or hogged by the kernel. It only matters that 923 * the line is used and we can't request it. 924 */ 925 bool gpiod_line_is_used (gpiod_line* line); 926 927 /** 928 * @brief Check if the line is an open-drain GPIO. 929 * @param line GPIO line object. 930 * @return True if the line is an open-drain GPIO, false otherwise. 931 */ 932 bool gpiod_line_is_open_drain (gpiod_line* line); 933 934 /** 935 * @brief Check if the line is an open-source GPIO. 936 * @param line GPIO line object. 937 * @return True if the line is an open-source GPIO, false otherwise. 938 */ 939 bool gpiod_line_is_open_source (gpiod_line* line); 940 941 /** 942 * @brief Re-read the line info. 943 * @param line GPIO line object. 944 * @return 0 if the operation succeeds. In case of an error this routine 945 * returns -1 and sets the last error number. 946 * 947 * The line info is initially retrieved from the kernel by 948 * gpiod_chip_get_line() and is later re-read after every successful request. 949 * Users can use this function to manually re-read the line info when needed. 950 * 951 * We currently have no mechanism provided by the kernel for keeping the line 952 * info synchronized and for the sake of speed and simplicity of this low-level 953 * library we don't want to re-read the line info automatically everytime 954 * a property is retrieved. Any daemon using this library must track the state 955 * of lines on its own and call this routine if needed. 956 * 957 * The state of requested lines is kept synchronized (or rather cannot be 958 * changed by external agents while the ownership of the line is taken) so 959 * there's no need to call this function in that case. 960 */ 961 int gpiod_line_update (gpiod_line* line); 962 963 /** 964 * @brief Check if the line info needs to be updated. 965 * @param line GPIO line object. 966 * @return Deprecated and no longer functional - always returns false. 967 */ 968 bool gpiod_line_needs_update (gpiod_line* line); 969 970 /** 971 * @} 972 * 973 * @defgroup __line_request__ Line requests 974 * @{ 975 * 976 * Interface for requesting GPIO lines from userspace for both values and 977 * events. 978 */ 979 980 /** 981 * @brief Available types of requests. 982 */ 983 enum 984 { 985 GPIOD_LINE_REQUEST_DIRECTION_AS_IS = 1, 986 /**< Request the line(s), but don't change current direction. */ 987 GPIOD_LINE_REQUEST_DIRECTION_INPUT = 2, 988 /**< Request the line(s) for reading the GPIO line state. */ 989 GPIOD_LINE_REQUEST_DIRECTION_OUTPUT = 3, 990 /**< Request the line(s) for setting the GPIO line state. */ 991 GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE = 4, 992 /**< Monitor both types of events. */ 993 GPIOD_LINE_REQUEST_EVENT_RISING_EDGE = 5, 994 /**< Only watch rising edge events. */ 995 GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES = 6 996 /**< Only watch falling edge events. */ 997 } 998 999 /** 1000 * @brief Miscellaneous GPIO request flags. 1001 */ 1002 enum 1003 { 1004 GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN = GPIOD_BIT(0), 1005 /**< The line is an open-drain port. */ 1006 GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE = GPIOD_BIT(1), 1007 /**< The line is an open-source port. */ 1008 GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW = GPIOD_BIT(2), 1009 /**< The active state of the line is low (high is the default). */ 1010 GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE = GPIOD_BIT(3), 1011 /**< The line has neither either pull-up nor pull-down resistor. */ 1012 GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN = GPIOD_BIT(4), 1013 /**< The line has pull-down resistor enabled. */ 1014 GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP = GPIOD_BIT(5) 1015 /**< The line has pull-up resistor enabled. */ 1016 } 1017 1018 /** 1019 * @brief Structure holding configuration of a line request. 1020 */ 1021 struct gpiod_line_request_config 1022 { 1023 const(char)* consumer; 1024 /**< Name of the consumer. */ 1025 int request_type; 1026 /**< Request type. */ 1027 int flags; 1028 /**< Other configuration flags. */ 1029 } 1030 1031 /** 1032 * @brief Reserve a single line. 1033 * @param line GPIO line object. 1034 * @param config Request options. 1035 * @param default_val Initial line value - only relevant if we're setting 1036 * the direction to output. 1037 * @return 0 if the line was properly reserved. In case of an error this 1038 * routine returns -1 and sets the last error number. 1039 * 1040 * If this routine succeeds, the caller takes ownership of the GPIO line until 1041 * it's released. 1042 */ 1043 int gpiod_line_request ( 1044 gpiod_line* line, 1045 const(gpiod_line_request_config)* config, 1046 int default_val); 1047 1048 /** 1049 * @brief Reserve a single line, set the direction to input. 1050 * @param line GPIO line object. 1051 * @param consumer Name of the consumer. 1052 * @return 0 if the line was properly reserved, -1 on failure. 1053 */ 1054 int gpiod_line_request_input (gpiod_line* line, const(char)* consumer); 1055 1056 /** 1057 * @brief Reserve a single line, set the direction to output. 1058 * @param line GPIO line object. 1059 * @param consumer Name of the consumer. 1060 * @param default_val Initial line value. 1061 * @return 0 if the line was properly reserved, -1 on failure. 1062 */ 1063 int gpiod_line_request_output ( 1064 gpiod_line* line, 1065 const(char)* consumer, 1066 int default_val); 1067 1068 /** 1069 * @brief Request rising edge event notifications on a single line. 1070 * @param line GPIO line object. 1071 * @param consumer Name of the consumer. 1072 * @return 0 if the operation succeeds, -1 on failure. 1073 */ 1074 int gpiod_line_request_rising_edge_events ( 1075 gpiod_line* line, 1076 const(char)* consumer); 1077 1078 /** 1079 * @brief Request falling edge event notifications on a single line. 1080 * @param line GPIO line object. 1081 * @param consumer Name of the consumer. 1082 * @return 0 if the operation succeeds, -1 on failure. 1083 */ 1084 int gpiod_line_request_falling_edge_events ( 1085 gpiod_line* line, 1086 const(char)* consumer); 1087 1088 /** 1089 * @brief Request all event type notifications on a single line. 1090 * @param line GPIO line object. 1091 * @param consumer Name of the consumer. 1092 * @return 0 if the operation succeeds, -1 on failure. 1093 */ 1094 int gpiod_line_request_both_edges_events ( 1095 gpiod_line* line, 1096 const(char)* consumer); 1097 1098 /** 1099 * @brief Reserve a single line, set the direction to input. 1100 * @param line GPIO line object. 1101 * @param consumer Name of the consumer. 1102 * @param flags Additional request flags. 1103 * @return 0 if the line was properly reserved, -1 on failure. 1104 */ 1105 int gpiod_line_request_input_flags ( 1106 gpiod_line* line, 1107 const(char)* consumer, 1108 int flags); 1109 1110 /** 1111 * @brief Reserve a single line, set the direction to output. 1112 * @param line GPIO line object. 1113 * @param consumer Name of the consumer. 1114 * @param flags Additional request flags. 1115 * @param default_val Initial line value. 1116 * @return 0 if the line was properly reserved, -1 on failure. 1117 */ 1118 int gpiod_line_request_output_flags ( 1119 gpiod_line* line, 1120 const(char)* consumer, 1121 int flags, 1122 int default_val); 1123 1124 /** 1125 * @brief Request rising edge event notifications on a single line. 1126 * @param line GPIO line object. 1127 * @param consumer Name of the consumer. 1128 * @param flags Additional request flags. 1129 * @return 0 if the operation succeeds, -1 on failure. 1130 */ 1131 int gpiod_line_request_rising_edge_events_flags ( 1132 gpiod_line* line, 1133 const(char)* consumer, 1134 int flags); 1135 1136 /** 1137 * @brief Request falling edge event notifications on a single line. 1138 * @param line GPIO line object. 1139 * @param consumer Name of the consumer. 1140 * @param flags Additional request flags. 1141 * @return 0 if the operation succeeds, -1 on failure. 1142 */ 1143 int gpiod_line_request_falling_edge_events_flags ( 1144 gpiod_line* line, 1145 const(char)* consumer, 1146 int flags); 1147 1148 /** 1149 * @brief Request all event type notifications on a single line. 1150 * @param line GPIO line object. 1151 * @param consumer Name of the consumer. 1152 * @param flags Additional request flags. 1153 * @return 0 if the operation succeeds, -1 on failure. 1154 */ 1155 int gpiod_line_request_both_edges_events_flags ( 1156 gpiod_line* line, 1157 const(char)* consumer, 1158 int flags); 1159 1160 /** 1161 * @brief Reserve a set of GPIO lines. 1162 * @param bulk Set of GPIO lines to reserve. 1163 * @param config Request options. 1164 * @param default_vals Initial line values - only relevant if we're setting 1165 * the direction to output. 1166 * @return 0 if the all lines were properly requested. In case of an error 1167 * this routine returns -1 and sets the last error number. 1168 * 1169 * If this routine succeeds, the caller takes ownership of the GPIO lines 1170 * until they're released. All the requested lines must be prodivided by the 1171 * same gpiochip. 1172 */ 1173 int gpiod_line_request_bulk ( 1174 gpiod_line_bulk* bulk, 1175 const(gpiod_line_request_config)* config, 1176 const(int)* default_vals); 1177 1178 /** 1179 * @brief Reserve a set of GPIO lines, set the direction to input. 1180 * @param bulk Set of GPIO lines to reserve. 1181 * @param consumer Name of the consumer. 1182 * @return 0 if the lines were properly reserved, -1 on failure. 1183 */ 1184 int gpiod_line_request_bulk_input ( 1185 gpiod_line_bulk* bulk, 1186 const(char)* consumer); 1187 1188 /** 1189 * @brief Reserve a set of GPIO lines, set the direction to output. 1190 * @param bulk Set of GPIO lines to reserve. 1191 * @param consumer Name of the consumer. 1192 * @param default_vals Initial line values. 1193 * @return 0 if the lines were properly reserved, -1 on failure. 1194 */ 1195 int gpiod_line_request_bulk_output ( 1196 gpiod_line_bulk* bulk, 1197 const(char)* consumer, 1198 const(int)* default_vals); 1199 1200 /** 1201 * @brief Request rising edge event notifications on a set of lines. 1202 * @param bulk Set of GPIO lines to request. 1203 * @param consumer Name of the consumer. 1204 * @return 0 if the operation succeeds, -1 on failure. 1205 */ 1206 int gpiod_line_request_bulk_rising_edge_events ( 1207 gpiod_line_bulk* bulk, 1208 const(char)* consumer); 1209 1210 /** 1211 * @brief Request falling edge event notifications on a set of lines. 1212 * @param bulk Set of GPIO lines to request. 1213 * @param consumer Name of the consumer. 1214 * @return 0 if the operation succeeds, -1 on failure. 1215 */ 1216 int gpiod_line_request_bulk_falling_edge_events ( 1217 gpiod_line_bulk* bulk, 1218 const(char)* consumer); 1219 1220 /** 1221 * @brief Request all event type notifications on a set of lines. 1222 * @param bulk Set of GPIO lines to request. 1223 * @param consumer Name of the consumer. 1224 * @return 0 if the operation succeeds, -1 on failure. 1225 */ 1226 int gpiod_line_request_bulk_both_edges_events ( 1227 gpiod_line_bulk* bulk, 1228 const(char)* consumer); 1229 1230 /** 1231 * @brief Reserve a set of GPIO lines, set the direction to input. 1232 * @param bulk Set of GPIO lines to reserve. 1233 * @param consumer Name of the consumer. 1234 * @param flags Additional request flags. 1235 * @return 0 if the lines were properly reserved, -1 on failure. 1236 */ 1237 int gpiod_line_request_bulk_input_flags ( 1238 gpiod_line_bulk* bulk, 1239 const(char)* consumer, 1240 int flags); 1241 1242 /** 1243 * @brief Reserve a set of GPIO lines, set the direction to output. 1244 * @param bulk Set of GPIO lines to reserve. 1245 * @param consumer Name of the consumer. 1246 * @param flags Additional request flags. 1247 * @param default_vals Initial line values. 1248 * @return 0 if the lines were properly reserved, -1 on failure. 1249 */ 1250 int gpiod_line_request_bulk_output_flags ( 1251 gpiod_line_bulk* bulk, 1252 const(char)* consumer, 1253 int flags, 1254 const(int)* default_vals); 1255 1256 /** 1257 * @brief Request rising edge event notifications on a set of lines. 1258 * @param bulk Set of GPIO lines to request. 1259 * @param consumer Name of the consumer. 1260 * @param flags Additional request flags. 1261 * @return 0 if the operation succeeds, -1 on failure. 1262 */ 1263 int gpiod_line_request_bulk_rising_edge_events_flags ( 1264 gpiod_line_bulk* bulk, 1265 const(char)* consumer, 1266 int flags); 1267 1268 /** 1269 * @brief Request falling edge event notifications on a set of lines. 1270 * @param bulk Set of GPIO lines to request. 1271 * @param consumer Name of the consumer. 1272 * @param flags Additional request flags. 1273 * @return 0 if the operation succeeds, -1 on failure. 1274 */ 1275 int gpiod_line_request_bulk_falling_edge_events_flags ( 1276 gpiod_line_bulk* bulk, 1277 const(char)* consumer, 1278 int flags); 1279 1280 /** 1281 * @brief Request all event type notifications on a set of lines. 1282 * @param bulk Set of GPIO lines to request. 1283 * @param consumer Name of the consumer. 1284 * @param flags Additional request flags. 1285 * @return 0 if the operation succeeds, -1 on failure. 1286 */ 1287 int gpiod_line_request_bulk_both_edges_events_flags ( 1288 gpiod_line_bulk* bulk, 1289 const(char)* consumer, 1290 int flags); 1291 1292 /** 1293 * @brief Release a previously reserved line. 1294 * @param line GPIO line object. 1295 */ 1296 void gpiod_line_release (gpiod_line* line); 1297 1298 /** 1299 * @brief Release a set of previously reserved lines. 1300 * @param bulk Set of GPIO lines to release. 1301 * 1302 * If the lines were not previously requested together, the behavior is 1303 * undefined. 1304 */ 1305 void gpiod_line_release_bulk (gpiod_line_bulk* bulk); 1306 1307 /** 1308 * @brief Check if the calling user has ownership of this line. 1309 * @param line GPIO line object. 1310 * @return True if given line was requested, false otherwise. 1311 */ 1312 bool gpiod_line_is_requested (gpiod_line* line); 1313 1314 /** 1315 * @brief Check if the calling user has neither requested ownership of this 1316 * line nor configured any event notifications. 1317 * @param line GPIO line object. 1318 * @return True if given line is free, false otherwise. 1319 */ 1320 bool gpiod_line_is_free (gpiod_line* line); 1321 1322 /** 1323 * @} 1324 * 1325 * @defgroup __line_value__ Reading & setting line values 1326 * @{ 1327 * 1328 * Functions allowing to read and set GPIO line values for single lines and 1329 * in bulk. 1330 */ 1331 1332 /** 1333 * @brief Read current value of a single GPIO line. 1334 * @param line GPIO line object. 1335 * @return 0 or 1 if the operation succeeds. On error this routine returns -1 1336 * and sets the last error number. 1337 */ 1338 int gpiod_line_get_value (gpiod_line* line); 1339 1340 /** 1341 * @brief Read current values of a set of GPIO lines. 1342 * @param bulk Set of GPIO lines to reserve. 1343 * @param values An array big enough to hold line_bulk->num_lines values. 1344 * @return 0 is the operation succeeds. In case of an error this routine 1345 * returns -1 and sets the last error number. 1346 * 1347 * If succeeds, this routine fills the values array with a set of values in 1348 * the same order, the lines are added to line_bulk. If the lines were not 1349 * previously requested together, the behavior is undefined. 1350 */ 1351 int gpiod_line_get_value_bulk (gpiod_line_bulk* bulk, int* values); 1352 1353 /** 1354 * @brief Set the value of a single GPIO line. 1355 * @param line GPIO line object. 1356 * @param value New value. 1357 * @return 0 is the operation succeeds. In case of an error this routine 1358 * returns -1 and sets the last error number. 1359 */ 1360 int gpiod_line_set_value (gpiod_line* line, int value); 1361 1362 /** 1363 * @brief Set the values of a set of GPIO lines. 1364 * @param bulk Set of GPIO lines to reserve. 1365 * @param values An array holding line_bulk->num_lines new values for lines. 1366 * A NULL pointer is interpreted as a logical low for all lines. 1367 * @return 0 is the operation succeeds. In case of an error this routine 1368 * returns -1 and sets the last error number. 1369 * 1370 * If the lines were not previously requested together, the behavior is 1371 * undefined. 1372 */ 1373 int gpiod_line_set_value_bulk (gpiod_line_bulk* bulk, const(int)* values); 1374 1375 /** 1376 * @} 1377 * 1378 * @defgroup __line_config__ Setting line configuration 1379 * @{ 1380 * 1381 * Functions allowing modification of config options of GPIO lines requested 1382 * from user-space. 1383 */ 1384 1385 /** 1386 * @brief Update the configuration of a single GPIO line. 1387 * @param line GPIO line object. 1388 * @param direction Updated direction which may be one of 1389 * GPIOD_LINE_REQUEST_DIRECTION_AS_IS, 1390 * GPIOD_LINE_REQUEST_DIRECTION_INPUT, or 1391 * GPIOD_LINE_REQUEST_DIRECTION_OUTPUT. 1392 * @param flags Replacement flags. 1393 * @param value The new output value for the line when direction is 1394 * GPIOD_LINE_REQUEST_DIRECTION_OUTPUT. 1395 * @return 0 is the operation succeeds. In case of an error this routine 1396 * returns -1 and sets the last error number. 1397 */ 1398 int gpiod_line_set_config ( 1399 gpiod_line* line, 1400 int direction, 1401 int flags, 1402 int value); 1403 1404 /** 1405 * @brief Update the configuration of a set of GPIO lines. 1406 * @param bulk Set of GPIO lines. 1407 * @param direction Updated direction which may be one of 1408 * GPIOD_LINE_REQUEST_DIRECTION_AS_IS, 1409 * GPIOD_LINE_REQUEST_DIRECTION_INPUT, or 1410 * GPIOD_LINE_REQUEST_DIRECTION_OUTPUT. 1411 * @param flags Replacement flags. 1412 * @param values An array holding line_bulk->num_lines new logical values 1413 * for lines when direction is 1414 * GPIOD_LINE_REQUEST_DIRECTION_OUTPUT. 1415 * A NULL pointer is interpreted as a logical low for all lines. 1416 * @return 0 is the operation succeeds. In case of an error this routine 1417 * returns -1 and sets the last error number. 1418 * 1419 * If the lines were not previously requested together, the behavior is 1420 * undefined. 1421 */ 1422 int gpiod_line_set_config_bulk ( 1423 gpiod_line_bulk* bulk, 1424 int direction, 1425 int flags, 1426 const(int)* values); 1427 1428 /** 1429 * @brief Update the configuration flags of a single GPIO line. 1430 * @param line GPIO line object. 1431 * @param flags Replacement flags. 1432 * @return 0 is the operation succeeds. In case of an error this routine 1433 * returns -1 and sets the last error number. 1434 */ 1435 int gpiod_line_set_flags (gpiod_line* line, int flags); 1436 1437 /** 1438 * @brief Update the configuration flags of a set of GPIO lines. 1439 * @param bulk Set of GPIO lines. 1440 * @param flags Replacement flags. 1441 * @return 0 is the operation succeeds. In case of an error this routine 1442 * returns -1 and sets the last error number. 1443 * 1444 * If the lines were not previously requested together, the behavior is 1445 * undefined. 1446 */ 1447 int gpiod_line_set_flags_bulk (gpiod_line_bulk* bulk, int flags); 1448 1449 /** 1450 * @brief Set the direction of a single GPIO line to input. 1451 * @param line GPIO line object. 1452 * @return 0 is the operation succeeds. In case of an error this routine 1453 * returns -1 and sets the last error number. 1454 */ 1455 int gpiod_line_set_direction_input (gpiod_line* line); 1456 1457 /** 1458 * @brief Set the direction of a set of GPIO lines to input. 1459 * @param bulk Set of GPIO lines. 1460 * @return 0 is the operation succeeds. In case of an error this routine 1461 * returns -1 and sets the last error number. 1462 * 1463 * If the lines were not previously requested together, the behavior is 1464 * undefined. 1465 */ 1466 int gpiod_line_set_direction_input_bulk (gpiod_line_bulk* bulk); 1467 1468 /** 1469 * @brief Set the direction of a single GPIO line to output. 1470 * @param line GPIO line object. 1471 * @param value The logical value output on the line. 1472 * @return 0 is the operation succeeds. In case of an error this routine 1473 * returns -1 and sets the last error number. 1474 */ 1475 int gpiod_line_set_direction_output (gpiod_line* line, int value); 1476 1477 /** 1478 * @brief Set the direction of a set of GPIO lines to output. 1479 * @param bulk Set of GPIO lines. 1480 * @param values An array holding line_bulk->num_lines new logical values 1481 * for lines. A NULL pointer is interpreted as a logical low 1482 * for all lines. 1483 * @return 0 is the operation succeeds. In case of an error this routine 1484 * returns -1 and sets the last error number. 1485 * 1486 * If the lines were not previously requested together, the behavior is 1487 * undefined. 1488 */ 1489 int gpiod_line_set_direction_output_bulk ( 1490 gpiod_line_bulk* bulk, 1491 const(int)* values); 1492 1493 /** 1494 * @} 1495 * 1496 * @defgroup __line_event__ Line events handling 1497 * @{ 1498 * 1499 * Structures and functions allowing to poll lines for events and read them, 1500 * both for individual lines as well as in bulk. Also contains functions for 1501 * retrieving the associated file descriptors and operate on them for easy 1502 * integration with standard unix interfaces. 1503 */ 1504 1505 /** 1506 * @brief Event types. 1507 */ 1508 enum 1509 { 1510 GPIOD_LINE_EVENT_RISING_EDGE = 1, 1511 /**< Rising edge event. */ 1512 GPIOD_LINE_EVENT_FALLING_EDGE = 2 1513 /**< Falling edge event. */ 1514 } 1515 1516 /** 1517 * @brief Structure holding event info. 1518 */ 1519 struct gpiod_line_event 1520 { 1521 timespec ts; 1522 /**< Best estimate of time of event occurrence. */ 1523 int event_type; 1524 /**< Type of the event that occurred. */ 1525 } 1526 1527 /** 1528 * @brief Wait for an event on a single line. 1529 * @param line GPIO line object. 1530 * @param timeout Wait time limit. 1531 * @return 0 if wait timed out, -1 if an error occurred, 1 if an event 1532 * occurred. 1533 */ 1534 int gpiod_line_event_wait (gpiod_line* line, const(timespec)* timeout); 1535 1536 /** 1537 * @brief Wait for events on a set of lines. 1538 * @param bulk Set of GPIO lines to monitor. 1539 * @param timeout Wait time limit. 1540 * @param event_bulk Bulk object in which to store the line handles on which 1541 * events occurred. Can be NULL. 1542 * @return 0 if wait timed out, -1 if an error occurred, 1 if at least one 1543 * event occurred. 1544 */ 1545 int gpiod_line_event_wait_bulk ( 1546 gpiod_line_bulk* bulk, 1547 const(timespec)* timeout, 1548 gpiod_line_bulk* event_bulk); 1549 1550 /** 1551 * @brief Read next pending event from the GPIO line. 1552 * @param line GPIO line object. 1553 * @param event Buffer to which the event data will be copied. 1554 * @return 0 if the event was read correctly, -1 on error. 1555 * @note This function will block if no event was queued for this line. 1556 */ 1557 int gpiod_line_event_read (gpiod_line* line, gpiod_line_event* event); 1558 1559 /** 1560 * @brief Read up to a certain number of events from the GPIO line. 1561 * @param line GPIO line object. 1562 * @param events Buffer to which the event data will be copied. Must hold at 1563 * least the amount of events specified in num_events. 1564 * @param num_events Specifies how many events can be stored in the buffer. 1565 * @return On success returns the number of events stored in the buffer, on 1566 * failure -1 is returned. 1567 */ 1568 int gpiod_line_event_read_multiple ( 1569 gpiod_line* line, 1570 gpiod_line_event* events, 1571 uint num_events); 1572 1573 /** 1574 * @brief Get the event file descriptor. 1575 * @param line GPIO line object. 1576 * @return Number of the event file descriptor or -1 if the user tries to 1577 * retrieve the descriptor from a line that wasn't configured for 1578 * event monitoring. 1579 * 1580 * Users may want to poll the event file descriptor on their own. This routine 1581 * allows to access it. 1582 */ 1583 int gpiod_line_event_get_fd (gpiod_line* line); 1584 1585 /** 1586 * @brief Read the last GPIO event directly from a file descriptor. 1587 * @param fd File descriptor. 1588 * @param event Buffer in which the event data will be stored. 1589 * @return 0 if the event was read correctly, -1 on error. 1590 * 1591 * Users who directly poll the file descriptor for incoming events can also 1592 * directly read the event data from it using this routine. This function 1593 * translates the kernel representation of the event to the libgpiod format. 1594 */ 1595 int gpiod_line_event_read_fd (int fd, gpiod_line_event* event); 1596 1597 /** 1598 * @brief Read up to a certain number of events directly from a file descriptor. 1599 * @param fd File descriptor. 1600 * @param events Buffer to which the event data will be copied. Must hold at 1601 * least the amount of events specified in num_events. 1602 * @param num_events Specifies how many events can be stored in the buffer. 1603 * @return On success returns the number of events stored in the buffer, on 1604 * failure -1 is returned. 1605 */ 1606 int gpiod_line_event_read_fd_multiple ( 1607 int fd, 1608 gpiod_line_event* events, 1609 uint num_events); 1610 1611 /** 1612 * @} 1613 * 1614 * @defgroup __line_misc__ Misc line functions 1615 * @{ 1616 * 1617 * Functions that didn't fit anywhere else. 1618 */ 1619 1620 /** 1621 * @brief Get a GPIO line handle by GPIO chip description and offset. 1622 * @param device String describing the gpiochip. 1623 * @param offset The offset of the GPIO line. 1624 * @return GPIO line handle or NULL if an error occurred. 1625 * 1626 * This routine provides a shorter alternative to calling 1627 * ::gpiod_chip_open_lookup and ::gpiod_chip_get_line. 1628 * 1629 * If this function succeeds, the caller is responsible for closing the 1630 * associated GPIO chip. 1631 */ 1632 gpiod_line* gpiod_line_get (const(char)* device, uint offset); 1633 1634 /** 1635 * @brief Find a GPIO line by its name. 1636 * @param name Name of the GPIO line. 1637 * @return Returns the GPIO line handle if the line exists in the system or 1638 * NULL if it couldn't be located or an error occurred. 1639 * 1640 * If this routine succeeds, the user must manually close the GPIO chip owning 1641 * this line to avoid memory leaks. If the line could not be found, this 1642 * functions sets errno to ENOENT. 1643 */ 1644 gpiod_line* gpiod_line_find (const(char)* name); 1645 1646 /** 1647 * @brief Close a GPIO chip owning this line and release all resources. 1648 * @param line GPIO line object 1649 * 1650 * After this function returns, the line must no longer be used. 1651 */ 1652 void gpiod_line_close_chip (gpiod_line* line); 1653 1654 /** 1655 * @brief Get the handle to the GPIO chip controlling this line. 1656 * @param line The GPIO line object. 1657 * @return Pointer to the GPIO chip handle controlling this line. 1658 */ 1659 gpiod_chip* gpiod_line_get_chip (gpiod_line* line); 1660 1661 /** 1662 * @} 1663 * 1664 * @} 1665 * 1666 * @defgroup __iterators__ Iterators for GPIO chips and lines 1667 * @{ 1668 * 1669 * These functions and data structures allow easy iterating over GPIO 1670 * chips and lines. 1671 */ 1672 1673 /** 1674 * @brief Create a new gpiochip iterator. 1675 * @return Pointer to a new chip iterator object or NULL if an error occurred. 1676 * 1677 * Internally this routine scans the /dev/ directory for GPIO chip device 1678 * files, opens them and stores their the handles until ::gpiod_chip_iter_free 1679 * or ::gpiod_chip_iter_free_noclose is called. 1680 */ 1681 gpiod_chip_iter* gpiod_chip_iter_new (); 1682 1683 /** 1684 * @brief Release all resources allocated for the gpiochip iterator and close 1685 * the most recently opened gpiochip (if any). 1686 * @param iter The gpiochip iterator object. 1687 */ 1688 void gpiod_chip_iter_free (gpiod_chip_iter* iter); 1689 1690 /** 1691 * @brief Release all resources allocated for the gpiochip iterator but 1692 * don't close the most recently opened gpiochip (if any). 1693 * @param iter The gpiochip iterator object. 1694 * 1695 * Users may want to break the loop when iterating over gpiochips and keep 1696 * the most recently opened chip active while freeing the iterator data. 1697 * This routine enables that. 1698 */ 1699 void gpiod_chip_iter_free_noclose (gpiod_chip_iter* iter); 1700 1701 /** 1702 * @brief Get the next gpiochip handle. 1703 * @param iter The gpiochip iterator object. 1704 * @return Pointer to the next open gpiochip handle or NULL if no more chips 1705 * are present in the system. 1706 * @note The previous chip handle will be closed using ::gpiod_chip_iter_free. 1707 */ 1708 gpiod_chip* gpiod_chip_iter_next (gpiod_chip_iter* iter); 1709 1710 /** 1711 * @brief Get the next gpiochip handle without closing the previous one. 1712 * @param iter The gpiochip iterator object. 1713 * @return Pointer to the next open gpiochip handle or NULL if no more chips 1714 * are present in the system. 1715 * @note This function works just like ::gpiod_chip_iter_next but doesn't 1716 * close the most recently opened chip handle. 1717 */ 1718 gpiod_chip* gpiod_chip_iter_next_noclose (gpiod_chip_iter* iter); 1719 1720 /** 1721 * @brief Iterate over all GPIO chips present in the system. 1722 * @param iter An initialized GPIO chip iterator. 1723 * @param chip Pointer to a GPIO chip handle. On each iteration the newly 1724 * opened chip handle is assigned to this argument. 1725 * 1726 * The user must not close the GPIO chip manually - instead the previous chip 1727 * handle is closed automatically on the next iteration. The last chip to be 1728 * opened is closed internally by ::gpiod_chip_iter_free. 1729 */ 1730 1731 /** 1732 * @brief Iterate over all chips present in the system without closing them. 1733 * @param iter An initialized GPIO chip iterator. 1734 * @param chip Pointer to a GPIO chip handle. On each iteration the newly 1735 * opened chip handle is assigned to this argument. 1736 * 1737 * The user must close all the GPIO chips manually after use, until then, the 1738 * chips remain open. Free the iterator by calling 1739 * ::gpiod_chip_iter_free_noclose to avoid closing the last chip automatically. 1740 */ 1741 1742 /** 1743 * @brief Create a new line iterator. 1744 * @param chip Active gpiochip handle over the lines of which we want 1745 * to iterate. 1746 * @return New line iterator or NULL if an error occurred. 1747 */ 1748 gpiod_line_iter* gpiod_line_iter_new (gpiod_chip* chip); 1749 1750 /** 1751 * @brief Free all resources associated with a GPIO line iterator. 1752 * @param iter Line iterator object. 1753 */ 1754 void gpiod_line_iter_free (gpiod_line_iter* iter); 1755 1756 /** 1757 * @brief Get the next GPIO line handle. 1758 * @param iter The GPIO line iterator object. 1759 * @return Pointer to the next GPIO line handle or NULL if there are no more 1760 * lines left. 1761 */ 1762 gpiod_line* gpiod_line_iter_next (gpiod_line_iter* iter); 1763 1764 /** 1765 * @brief Iterate over all GPIO lines of a single chip. 1766 * @param iter An initialized GPIO line iterator. 1767 * @param line Pointer to a GPIO line handle - on each iteration, the 1768 * next GPIO line will be assigned to this argument. 1769 */ 1770 1771 /** 1772 * @} 1773 * 1774 * @defgroup __misc__ Stuff that didn't fit anywhere else 1775 * @{ 1776 * 1777 * Various libgpiod-related functions. 1778 */ 1779 1780 /** 1781 * @brief Get the API version of the library as a human-readable string. 1782 * @return Human-readable string containing the library version. 1783 */ 1784 const(char)* gpiod_version_string (); 1785 1786 /** 1787 * @} 1788 */ 1789 1790 /* extern "C" */ 1791 1792 /* __LIBGPIOD_GPIOD_H__ */