|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 The LineageOS Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#define LOG_TAG "nqnfcinfo" |
| 18 | + |
| 19 | +#include <errno.h> |
| 20 | +#include <fcntl.h> |
| 21 | + |
| 22 | +#include <fmt/format.h> |
| 23 | + |
| 24 | +#include <android-base/logging.h> |
| 25 | +#include <android-base/properties.h> |
| 26 | + |
| 27 | +#include <linux/nfc/nfcinfo.h> |
| 28 | + |
| 29 | +using ::android::base::SetProperty; |
| 30 | + |
| 31 | +#define NQ_NCI_PATH "/dev/nq-nci" |
| 32 | + |
| 33 | +int main() { |
| 34 | + // Open our fd |
| 35 | + int fd = open(NQ_NCI_PATH, O_RDONLY); |
| 36 | + |
| 37 | + // Make sure we could get the fd |
| 38 | + if (fd < 0) { |
| 39 | + LOG(ERROR) << "Could not open " << NQ_NCI_PATH << ": " << strerror(errno); |
| 40 | + return fd; |
| 41 | + } |
| 42 | + |
| 43 | + // NXP's kernel driver returns a struct that is union'ed with an integer |
| 44 | + // as the return value of the IOCTL call here. Presumably this is to avoid |
| 45 | + // dealing with copying memory to/from userspace, but it's a little silly |
| 46 | + // Since this is an unsigned type, I don't know how to check for errors... |
| 47 | + nqx_uinfo info{}; |
| 48 | + info.i = ioctl(fd, NFCC_GET_INFO, 0); |
| 49 | + |
| 50 | + // Close fd |
| 51 | + close(fd); |
| 52 | + |
| 53 | + // Set properties based on IOCTL return |
| 54 | + SetProperty("vendor.qti.nfc.chipid", fmt::format("{:#02x}", info.info.chip_type)); |
| 55 | + SetProperty("vendor.qti.nfc.fwver", fmt::format("{:02x}.{:02x}.{:02x}", info.info.rom_version, |
| 56 | + info.info.fw_major, info.info.fw_minor)); |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
0 commit comments