Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gpio_handler.h" |
| 6 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 7 | #include <base/memory/scoped_ptr.h> |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 8 | #include <base/string_util.h> |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 9 | #include <base/stringprintf.h> |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 10 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 11 | #include "update_engine/file_descriptor.h" |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 12 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 13 | using base::Time; |
| 14 | using base::TimeDelta; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 15 | using std::string; |
| 16 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 17 | using namespace chromeos_update_engine; |
| 18 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 19 | namespace chromeos_update_engine { |
| 20 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 21 | const char* StandardGpioHandler::gpio_dirs_[kGpioDirMax] = { |
| 22 | "in", // kGpioDirIn |
| 23 | "out", // kGpioDirOut |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 26 | const char* StandardGpioHandler::gpio_vals_[kGpioValMax] = { |
| 27 | "1", // kGpioValUp |
| 28 | "0", // kGpioValDown |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 29 | }; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 30 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 31 | const StandardGpioHandler::GpioDef |
| 32 | StandardGpioHandler::gpio_defs_[kGpioIdMax] = { |
| 33 | { "dutflaga", "ID_GPIO_DUTFLAGA" }, // kGpioDutflaga |
| 34 | { "dutflagb", "ID_GPIO_DUTFLAGB" }, // kGpioDutflagb |
| 35 | }; |
| 36 | |
| 37 | unsigned StandardGpioHandler::num_instances_ = 0; |
| 38 | |
| 39 | |
| 40 | StandardGpioHandler::StandardGpioHandler(UdevInterface* udev_iface, |
| 41 | bool is_defer_discovery) |
| 42 | : udev_iface_(udev_iface), |
| 43 | is_discovery_attempted_(false) { |
| 44 | CHECK(udev_iface); |
| 45 | |
| 46 | // Ensure there's only one instance of this class. |
| 47 | CHECK_EQ(num_instances_, static_cast<unsigned>(0)); |
| 48 | num_instances_++; |
| 49 | |
| 50 | // If GPIO discovery not deferred, do it. |
| 51 | if (!(is_defer_discovery || DiscoverGpios())) { |
| 52 | LOG(WARNING) << "GPIO discovery failed"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | StandardGpioHandler::~StandardGpioHandler() { |
| 57 | num_instances_--; |
| 58 | } |
| 59 | |
| 60 | // TODO(garnold) currently, this function always returns false and avoids the |
| 61 | // GPIO signaling protocol altogether; to be extended later. |
| 62 | bool StandardGpioHandler::IsTestModeSignaled() { |
| 63 | // Attempt GPIO discovery. |
| 64 | if (!DiscoverGpios()) { |
| 65 | LOG(WARNING) << "GPIO discovery failed"; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool StandardGpioHandler::GpioChipUdevEnumHelper::SetupEnumFilters( |
| 72 | udev_enumerate* udev_enum) { |
| 73 | CHECK(udev_enum); |
| 74 | |
| 75 | return !(gpio_handler_->udev_iface_->EnumerateAddMatchSubsystem( |
| 76 | udev_enum, "gpio") || |
| 77 | gpio_handler_->udev_iface_->EnumerateAddMatchSysname( |
| 78 | udev_enum, "gpiochip*")); |
| 79 | } |
| 80 | |
| 81 | bool StandardGpioHandler::GpioChipUdevEnumHelper::ProcessDev(udev_device* dev) { |
| 82 | CHECK(dev); |
| 83 | |
| 84 | // Ensure we did not encounter more than one chip. |
| 85 | if (num_gpio_chips_++) { |
| 86 | LOG(ERROR) << "enumerated multiple GPIO chips"; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 90 | // Obtain GPIO descriptors. |
| 91 | for (int id = 0; id < kGpioIdMax; id++) { |
| 92 | const GpioDef* gpio_def = &gpio_defs_[id]; |
| 93 | const char* descriptor = |
| 94 | gpio_handler_->udev_iface_->DeviceGetPropertyValue( |
| 95 | dev, gpio_def->udev_property); |
| 96 | if (!descriptor) { |
| 97 | LOG(ERROR) << "could not obtain " << gpio_def->name |
| 98 | << " descriptor using property " << gpio_def->udev_property; |
| 99 | return false; |
| 100 | } |
| 101 | gpio_handler_->gpios_[id].descriptor = descriptor; |
| 102 | } |
| 103 | |
| 104 | return true; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 107 | bool StandardGpioHandler::GpioChipUdevEnumHelper::Finalize() { |
| 108 | if (num_gpio_chips_ != 1) { |
| 109 | LOG(ERROR) << "could not enumerate a GPIO chip"; |
| 110 | return false; |
| 111 | } |
| 112 | return true; |
| 113 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 114 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 115 | bool StandardGpioHandler::GpioUdevEnumHelper::SetupEnumFilters( |
| 116 | udev_enumerate* udev_enum) { |
| 117 | CHECK(udev_enum); |
| 118 | const string gpio_pattern = |
| 119 | string("*").append(gpio_handler_->gpios_[id_].descriptor); |
| 120 | return !( |
| 121 | gpio_handler_->udev_iface_->EnumerateAddMatchSubsystem( |
| 122 | udev_enum, "gpio") || |
| 123 | gpio_handler_->udev_iface_->EnumerateAddMatchSysname( |
| 124 | udev_enum, gpio_pattern.c_str())); |
| 125 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 126 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 127 | bool StandardGpioHandler::GpioUdevEnumHelper::ProcessDev(udev_device* dev) { |
| 128 | CHECK(dev); |
| 129 | |
| 130 | // Ensure we did not encounter more than one GPIO device. |
| 131 | if (num_gpios_++) { |
| 132 | LOG(ERROR) << "enumerated multiple GPIO devices for a given descriptor"; |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // Obtain GPIO device sysfs path. |
| 137 | const char* dev_path = gpio_handler_->udev_iface_->DeviceGetSyspath(dev); |
| 138 | if (!dev_path) { |
| 139 | LOG(ERROR) << "failed to obtain device syspath for GPIO " |
| 140 | << gpio_defs_[id_].name; |
| 141 | return false; |
| 142 | } |
| 143 | gpio_handler_->gpios_[id_].dev_path = dev_path; |
| 144 | |
| 145 | LOG(INFO) << "obtained device syspath: " << gpio_defs_[id_].name << " -> " |
| 146 | << gpio_handler_->gpios_[id_].dev_path; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool StandardGpioHandler::GpioUdevEnumHelper::Finalize() { |
| 151 | if (num_gpios_ != 1) { |
| 152 | LOG(ERROR) << "could not enumerate GPIO device " << gpio_defs_[id_].name; |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | bool StandardGpioHandler::InitUdevEnum(struct udev* udev, |
| 160 | UdevEnumHelper* enum_helper) { |
| 161 | // Obtain a udev enumerate object. |
| 162 | struct udev_enumerate* udev_enum; |
| 163 | if (!(udev_enum = udev_iface_->EnumerateNew(udev))) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 164 | LOG(ERROR) << "failed to obtain udev enumerate context"; |
| 165 | return false; |
| 166 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 167 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 168 | // Assign enumerate object to closer. |
| 169 | scoped_ptr<UdevInterface::UdevEnumerateCloser> |
| 170 | udev_enum_closer(udev_iface_->NewUdevEnumerateCloser(&udev_enum)); |
| 171 | |
| 172 | // Setup enumeration filters. |
| 173 | if (!enum_helper->SetupEnumFilters(udev_enum)) { |
| 174 | LOG(ERROR) << "failed to setup udev enumerate filters"; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 178 | // Scan for matching devices. |
| 179 | if (udev_iface_->EnumerateScanDevices(udev_enum)) { |
| 180 | LOG(ERROR) << "udev enumerate scan failed"; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 184 | // Iterate over matching devices. |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 185 | struct udev_list_entry* list_entry; |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 186 | for (list_entry = udev_iface_->EnumerateGetListEntry(udev_enum); |
| 187 | list_entry; list_entry = udev_iface_->ListEntryGetNext(list_entry)) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 188 | // Obtain device name. |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 189 | const char* dev_path = udev_iface_->ListEntryGetName(list_entry); |
| 190 | if (!dev_path) { |
| 191 | LOG(ERROR) << "enumerated device has a null name string"; |
| 192 | return false; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Obtain device object. |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 196 | struct udev_device* dev = udev_iface_->DeviceNewFromSyspath(udev, dev_path); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 197 | if (!dev) { |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 198 | LOG(ERROR) << "obtained a null device object for enumerated device"; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 199 | return false; |
| 200 | } |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 201 | scoped_ptr<UdevInterface::UdevDeviceCloser> |
| 202 | dev_closer(udev_iface_->NewUdevDeviceCloser(&dev)); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 203 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 204 | if (!enum_helper->ProcessDev(dev)) |
| 205 | return false; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 208 | // Make sure postconditions were met. |
| 209 | return enum_helper->Finalize(); |
| 210 | } |
| 211 | |
| 212 | bool StandardGpioHandler::DiscoverGpios() { |
| 213 | if (is_discovery_attempted_) |
| 214 | return true; |
| 215 | |
| 216 | is_discovery_attempted_ = true; |
| 217 | |
| 218 | // Obtain libudev instance and attach to a dedicated closer. |
| 219 | struct udev* udev; |
| 220 | if (!(udev = udev_iface_->New())) { |
| 221 | LOG(ERROR) << "failed to obtain libudev instance"; |
| 222 | return false; |
| 223 | } |
| 224 | scoped_ptr<UdevInterface::UdevCloser> |
| 225 | udev_closer(udev_iface_->NewUdevCloser(&udev)); |
| 226 | |
| 227 | // Enumerate GPIO chips, scanning for GPIO descriptors. |
| 228 | GpioChipUdevEnumHelper chip_enum_helper(this); |
| 229 | if (!InitUdevEnum(udev, &chip_enum_helper)) { |
| 230 | LOG(ERROR) << "enumeration error, aborting GPIO discovery"; |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // Obtain device names for all discovered GPIOs, reusing the udev instance. |
| 235 | for (int id = 0; id < kGpioIdMax; id++) { |
| 236 | GpioUdevEnumHelper gpio_enum_helper(this, static_cast<GpioId>(id)); |
| 237 | if (!InitUdevEnum(udev, &gpio_enum_helper)) { |
| 238 | LOG(ERROR) << "enumeration error, aborting GPIO discovery"; |
| 239 | return false; |
| 240 | } |
| 241 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 246 | bool StandardGpioHandler::GetGpioDevName(StandardGpioHandler::GpioId id, |
| 247 | string* dev_path_p) { |
| 248 | CHECK(id >= 0 && id < kGpioIdMax && dev_path_p); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 249 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame^] | 250 | *dev_path_p = gpios_[id].dev_path; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
| 254 | } // namespace chromeos_update_engine |