virtual rfkill button
Just so I don't lose the code again ...
johannes
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <linux/uinput.h>
int main(int argc, char **argv)
{
struct uinput_user_dev dev;
struct input_event ev;
int fd, ret;
fd = open("/dev/input/uinput", O_WRONLY);
if (fd < 0) {
perror("open");
if (errno == ENOENT)
fprintf(stderr, "Try to \"modprobe uinput\".\n");
return 1;
}
/* set up virtual device */
memset(&dev, 0, sizeof(dev));
strcpy(dev.name, "virt-rfkill");
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, KEY_WLAN);
ret = write(fd, &dev, sizeof(dev));
if (ret != sizeof(dev)) {
perror("write setup");
return 1;
}
/* register device */
ret = ioctl(fd, UI_DEV_CREATE);
if (ret) {
perror("create");
return 1;
}
/* write event */
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_WLAN;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
return 0;
}That didn't work, the attached one does... Also a module to show rfkill working (for my new API rfkill).
The Makefile for the test module is a simple:
obj-m += fake-rfkill.o
and then you do
make -f /lib/modules/$(uname -r)/build/Makefile M=$(pwd)
johannes
#include <linux/rfkill.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");MODULE_AUTHOR("Johannes Berg <[email protected]>");
static struct rfkill *rfk;
static void test_poll(struct rfkill *rfkill, void *data)
{
printk(KERN_DEBUG "poll test rfkill\n");
}static void test_query(struct rfkill *rfkill, void *data)
{
printk(KERN_DEBUG "query test rfkill\n");
}static int test_set_block(void *data, bool blocked)
{
printk(KERN_DEBUG "set test rfkill (%s)\n",
blocked ? "blocked" : "active");
return 0;
}static struct rfkill_ops ops = {
.poll = test_poll,
.query = test_query,
.set_block = test_set_block,
};
int mod_init(void)
{
int err;
rfk = rfkill_alloc("fake", NULL, RFKILL_TYPE_WLAN, &ops, NULL);
if (!rfk)
return -ENOMEM;
err = rfkill_register(rfk);
if (err)
rfkill_destroy(rfk);
return err;
}module_init(mod_init);
void mod_exit(void)
{
rfkill_unregister(rfk);
rfkill_destroy(rfk);
}module_exit(mod_exit);
/*
* Copyright (c) 2009 Johannes Berg
* Copyright (c) 2009 Luis R. Rodriguez
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies. *
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include <unistd.h>
/*
* You will need CONFIG_INPUT_UINPUT enabled, if
* enabled as a module modprobe uinput
*/
int main(int argc, char **argv)
{
struct uinput_user_dev dev;
struct input_event ev;
int fd, ret;
/* Tells the kernel to kmalloc() a uinput device for us */
fd = open("/dev/input/uinput", O_WRONLY | O_NDELAY);
if (fd < 0) {
perror("open uinput");
if (errno == ENOENT)
fprintf(stderr, "Try \"modprobe uinput\".\n");
return 1;
}
memset(&dev, 0, sizeof(dev));
strcpy(dev.name, "virt-rfkill");
/* sets up uinput device, all we need is a name */
ret = write(fd, &dev, sizeof(dev));
if (ret != sizeof(dev)) {
perror("write setup");
return 1;
}
/* sets "EV_KEY" bit on the the uinput dev's evbit */
if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) {
perror("set key event");
return 1;
}
/* sets "KEY_WLAN" bit on the the uinput dev's keybit */
if (ioctl(fd, UI_SET_KEYBIT, KEY_WLAN)) {
perror("set wlan key");
return 1;
}
/* create an input device for uinput device */
ret = ioctl(fd, UI_DEV_CREATE);
if (ret) {
perror("create input device");
return 1;
}
/*
* write events -- once a uinput device has been set up writes
* inject events which show up in the virtual input device.
*/
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_WLAN;
/* button down */
ev.value = 1;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
/* and up again - that's a press */
ev.value = 0;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
return 0;
}