blob: 4c33a44c93ccb4192f50cb95204d8fe0f2e677d2 [file] [log] [blame]
Christopher Meis75ff1672025-09-23 11:40:06 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3
Patrick Venturedace6802018-11-01 16:52:10 -07004#include "evdev.hpp"
5
6#include "xyz/openbmc_project/Common/error.hpp"
7
Gunnar Mills835dfb82017-07-26 16:15:21 -05008#include <fcntl.h>
Patrick Venturedace6802018-11-01 16:52:10 -07009#include <libevdev/libevdev.h>
10
11#include <phosphor-logging/elog-errors.hpp>
Gunnar Mills835dfb82017-07-26 16:15:21 -050012#include <phosphor-logging/elog.hpp>
George Liu2a8848c2023-08-01 13:49:28 +080013#include <phosphor-logging/lg2.hpp>
Gunnar Mills835dfb82017-07-26 16:15:21 -050014
15namespace phosphor
16{
17namespace gpio
18{
19
20using namespace phosphor::logging;
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22
23// Populate the file descriptor for passed in device
24int Evdev::openDevice()
25{
Gunnar Mills835dfb82017-07-26 16:15:21 -050026 auto fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
27 if (fd < 0)
28 {
George Liu2a8848c2023-08-01 13:49:28 +080029 lg2::error("Failed to open {DEVICEPATH}: {ERRNO}", "DEVICEPATH", path,
30 "ERRNO", errno);
Gunnar Mills835dfb82017-07-26 16:15:21 -050031 elog<InternalFailure>();
32 }
33 return fd;
34}
35
36// Initializes the event device with the fd
37void Evdev::initEvDev()
38{
39 if (devicePtr)
40 {
41 // Init can be done only once per device
42 return;
43 }
44
45 struct libevdev* evdev = nullptr;
46 auto rc = libevdev_new_from_fd((fd)(), &evdev);
47 if (rc < 0)
48 {
George Liu2a8848c2023-08-01 13:49:28 +080049 lg2::error("Failed to initialize evdev");
Gunnar Mills835dfb82017-07-26 16:15:21 -050050 elog<InternalFailure>();
51 return;
52 }
53
54 // Packing in the unique_ptr
55 devicePtr.reset(evdev);
56}
57
58// Attaches the FD to event loop and registers the callback handler
59void Evdev::registerCallback()
60{
61 decltype(eventSource.get()) sourcePtr = nullptr;
Patrick Venturedace6802018-11-01 16:52:10 -070062 auto rc = sd_event_add_io(event.get(), &sourcePtr, (fd)(), EPOLLIN,
63 callbackHandler, this);
Gunnar Mills835dfb82017-07-26 16:15:21 -050064 eventSource.reset(sourcePtr);
65
66 if (rc < 0)
67 {
George Liu2a8848c2023-08-01 13:49:28 +080068 lg2::error("Failed to register callback handler: {RC}", "RC", rc);
Gunnar Mills835dfb82017-07-26 16:15:21 -050069 elog<InternalFailure>();
70 }
71}
72
73} // namespace gpio
74} // namespace phosphor