Skip to content

Instantly share code, notes, and snippets.

@skyostil
Created January 28, 2022 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyostil/13d60a288919d18d00b20e81dfe018cd to your computer and use it in GitHub Desktop.
Save skyostil/13d60a288919d18d00b20e81dfe018cd to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-2.0
/*
* Userspace ioctl example for the ChromeOS snooping prevention sensor (HPS).
*
* Copyright 2022 Google LLC.
*/
#include <linux/i2c.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define HPS_IOC_TRANSFER _IOWR('h', 0x01, struct hps_transfer_ioctl_data)
#define HPS_MAGIC 0x9df2
#define HPS_CMD_READ_REG(n) (0x80 | n)
struct hps_transfer_ioctl_data {
__u32 isize; /* Number of bytes to send */
unsigned char *ibuf; /* Input buffer */
__u32 osize; /* Number of bytes to receive */
unsigned char *obuf; /* Output buffer */
};
int main() {
int fd = open("/dev/hps", O_RDWR);
int ret;
int magic;
unsigned char cmd[1] = { HPS_CMD_READ_REG(0) };
unsigned char out[2];
struct hps_transfer_ioctl_data args = {
.isize = sizeof(cmd),
.ibuf = &cmd[0],
.osize = sizeof(out),
.obuf = &out[0],
};
if (fd < 0) {
perror("open");
return EXIT_FAILURE;
}
/* Wait for the sensor to come online. */
usleep(1000 * 1000);
ret = ioctl(fd, HPS_IOC_TRANSFER, &args);
if (ret != 2) {
perror("ioctl");
close(fd);
return EXIT_FAILURE;
}
magic = (out[0] << 8) | out[1];
printf("HPS magic: 0x%04x (expected 0x%04x)\n", magic, HPS_MAGIC);
close(fd);
return magic == HPS_MAGIC;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment