3 Commits

Author SHA1 Message Date
87217f1150 Add wheel WASD 2026-01-17 15:18:58 +01:00
aedde65362 Refactor media 2026-01-17 12:52:30 +01:00
c77c23c6ed Add wasd-emulator 2026-01-17 11:49:02 +01:00
5 changed files with 348 additions and 149 deletions

14
g29-wheel/Makefile Normal file
View File

@@ -0,0 +1,14 @@
.PHONY: all clean install uninstall
obj-m += g29_usb.o
PWD := $(CURDIR)
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
mkdir -p build
-mv -f -- *.ko *.mod.c *.o .*.o *.mod modules.order .*.cmd *.symvers build/
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -rf build

View File

@@ -21,21 +21,18 @@
#include <linux/usb.h>
#include <linux/usb/input.h>
#include <linux/input.h>
#include <asm/unaligned.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/hid.h>
MODULE_AUTHOR("LLP group 32");
MODULE_DESCRIPTION("Logitech G29 -> Media keys (USB driver)");
MODULE_AUTHOR("LLP group 16");
MODULE_DESCRIPTION("Logitech G29 USB driver");
MODULE_LICENSE("GPL");
#define USB_VENDOR_ID_LOGITECH 0x046d
#define USB_DEVICE_ID_LOGITECH_G29 0xc24f
#define USB_DEVICE_ID_LOGITECH_G29_ALT 0xc260
#define G29_REPORT_LEN 12
/*
* Button masks
*/
#define G29_BTN_PLUS 0x00800000u
#define G29_BTN_MINUS 0x01000000u
#define G29_BTN_RED_CW 0x02000000u
@@ -53,19 +50,16 @@ module_param(mode, int, 0444);
MODULE_PARM_DESC(mode, "Mapping mode (0=MEDIA)");
struct g29_state {
u32 buttons;
u16 rot;
u32 buttons_le;
u16 rot_le;
u8 gas;
u8 brk;
u8 clt;
u8 grx;
u8 gry;
u8 grz;
u8 gr_x;
u8 gr_y;
u8 gr_z;
};
/*
* Mapping table entry
*/
struct g29_keymap_edge {
u32 mask;
unsigned short keycode;
@@ -94,79 +88,58 @@ struct g29_dev {
struct urb *urb;
u8 *buf;
dma_addr_t buf_dma;
int maxp;
int interval;
int endpoint;
struct timer_list steer_timer;
u32 steer_phase_ms;
struct g29_state last;
};
/* Parsing */
static bool g29_parse_report(struct g29_state *out, const u8 *data, int len)
{
if (len < G29_REPORT_LEN)
return false;
static void g29_steer_timer_fn(struct timer_list *t) {
struct g29_dev *g29 = timer_container_of(g29, t, steer_timer);
/* bytes 0..3 buttons bitfield */
out->buttons = get_unaligned_le32(&data[0]);
const int rot = le16_to_cpu(g29->last.rot_le);
/* bytes 4..5 rotation */
out->rot = get_unaligned_le16(&data[4]);
input_report_key(g29->input, KEY_W, g29->last.gas <= 0x80);
input_report_key(g29->input, KEY_S, g29->last.clt <= 0x80);
input_report_key(g29->input, KEY_A, rot <= 0x6000);
input_report_key(g29->input, KEY_D, rot >= 0xA000);
out->gas = data[6];
out->brk = data[7];
out->clt = data[8];
out->grx = data[9];
out->gry = data[10];
out->grz = data[11];
return true;
mod_timer(&g29->steer_timer, jiffies + msecs_to_jiffies(2));
}
/* Mapping policy */
static void g29_pulse_key(struct input_dev *input, unsigned short keycode)
{
/* A pulse is a press+release within one report frame. */
input_report_key(input, keycode, 1);
input_report_key(input, keycode, 0);
}
static void g29_apply_media_mode(struct g29_dev *g29,
const struct g29_state *prev,
const struct g29_state *cur)
{
u32 pressed = cur->buttons & ~prev->buttons;
size_t i;
for (i = 0; i < ARRAY_SIZE(g29_media_edge_map); i++) {
static void g29_apply_media_mode(struct g29_dev *g29, const struct g29_state *cur, const struct g29_state *prev) {
u32 pressed = le32_to_cpu(cur->buttons_le & ~prev->buttons_le);
for (int i = 0; i < ARRAY_SIZE(g29_media_edge_map); i++) {
const struct g29_keymap_edge *e = &g29_media_edge_map[i];
if (pressed & e->mask)
g29_pulse_key(g29->input, e->keycode);
if (pressed & e->mask) {
input_report_key(g29->input, e->keycode, 1);
input_report_key(g29->input, e->keycode, 0);
}
}
input_sync(g29->input);
}
static void g29_process_report(struct g29_dev *g29, const u8 *data, int len)
{
struct g29_state cur;
if (!g29_parse_report(&cur, data, len))
return;
static void g29_process_report(struct g29_dev *g29, const u8 *data, unsigned int len) {
if (len < 12) return;
struct g29_state *cur = (void *) data;
switch (mode) {
case G29_MODE_MEDIA:
default:
g29_apply_media_mode(g29, &g29->last, &cur);
g29_apply_media_mode(g29, cur, &g29->last);
break;
}
g29->last = cur;
g29->last = *cur;
}
/* URB plumbing */
static void g29_urb_complete(struct urb *urb)
{
static void g29_urb_complete(struct urb *urb) {
struct g29_dev *g29 = urb->context;
int ret;
@@ -189,79 +162,73 @@ resubmit:
dev_err(&g29->udev->dev, "usb_submit_urb failed: %d\n", ret);
}
static int g29_input_open(struct input_dev *input)
{
static int g29_input_open(struct input_dev *input) {
struct g29_dev *g29 = input_get_drvdata(input);
g29->urb->dev = g29->udev;
if (usb_submit_urb(g29->urb, GFP_KERNEL))
return -EIO;
mod_timer(&g29->steer_timer, jiffies + msecs_to_jiffies(2));
return 0;
}
static void g29_input_close(struct input_dev *input)
{
static void g29_input_close(struct input_dev *input) {
struct g29_dev *g29 = input_get_drvdata(input);
timer_delete_sync(&g29->steer_timer);
usb_kill_urb(g29->urb);
}
/* USB driver binding */
static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id) {
struct usb_device *udev = interface_to_usbdev(intf);
struct usb_host_interface *alts = intf->cur_altsetting;
struct usb_endpoint_descriptor *ep = NULL;
struct g29_dev *g29;
struct input_dev *input;
int i, pipe, maxp, error;
int ret;
/* Find an interrupt IN endpoint capable of carrying the 12-byte report. */
for (i = 0; i < alts->desc.bNumEndpoints; i++) {
struct usb_endpoint_descriptor *cand = &alts->endpoint[i].desc;
if (!usb_endpoint_is_int_in(cand))
struct usb_endpoint_descriptor *ep = NULL;
const struct usb_host_interface *alts = intf->cur_altsetting;
for (int i = 0; i < alts->desc.bNumEndpoints; i++) {
struct usb_endpoint_descriptor *d = &alts->endpoint[i].desc;
if (!usb_endpoint_is_int_in(d))
continue;
pipe = usb_rcvintpipe(udev, cand->bEndpointAddress);
maxp = usb_maxpacket(udev, pipe);
if (maxp >= G29_REPORT_LEN) {
ep = cand;
if (usb_maxpacket(udev, usb_rcvintpipe(udev, d->bEndpointAddress)) >= 12) {
ep = d;
break;
}
}
if (!ep) return -ENODEV;
if (!ep)
return -ENODEV;
g29 = kzalloc(sizeof(*g29), GFP_KERNEL);
if (!g29)
struct g29_dev *g29;
if ((g29 = kzalloc(sizeof(*g29), GFP_KERNEL)) == NULL) {
return -ENOMEM;
}
input = input_allocate_device();
if (!input) {
error = -ENOMEM;
struct input_dev *input;
if ((input = input_allocate_device()) == NULL) {
ret = -ENOMEM;
goto err_free_g29;
}
g29->udev = udev;
g29->input = input;
g29->endpoint = usb_endpoint_num(ep);
g29->maxp = usb_endpoint_maxp(ep);
g29->interval = ep->bInterval;
memset(&g29->last, 0, sizeof(g29->last));
/* Allocate a fixed-size report buffer (12 bytes). */
g29->buf = usb_alloc_coherent(udev, G29_REPORT_LEN, GFP_KERNEL, &g29->buf_dma);
if (!g29->buf) {
error = -ENOMEM;
timer_setup(&g29->steer_timer, g29_steer_timer_fn, 0);
if ((g29->buf = usb_alloc_coherent(udev, g29->maxp, GFP_KERNEL, &g29->buf_dma)) == NULL) {
ret = -ENOMEM;
goto err_free_input;
}
g29->urb = usb_alloc_urb(0, GFP_KERNEL);
if (!g29->urb) {
error = -ENOMEM;
if ((g29->urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto err_free_buf;
}
/* Build a friendly input device name. */
if (udev->manufacturer)
strscpy(g29->name, udev->manufacturer, sizeof(g29->name));
if (udev->product) {
@@ -271,7 +238,7 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
}
if (!strlen(g29->name))
snprintf(g29->name, sizeof(g29->name),
"Logitech G29 Media %04x:%04x",
"Logitech G29 USB %04x:%04x",
le16_to_cpu(udev->descriptor.idVendor),
le16_to_cpu(udev->descriptor.idProduct));
@@ -292,20 +259,24 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
input_set_capability(input, EV_KEY, KEY_NEXTSONG);
input_set_capability(input, EV_KEY, KEY_PREVIOUSSONG);
input_set_capability(input, EV_KEY, KEY_W);
input_set_capability(input, EV_KEY, KEY_A);
input_set_capability(input, EV_KEY, KEY_S);
input_set_capability(input, EV_KEY, KEY_D);
input_set_drvdata(input, g29);
input->open = g29_input_open;
input->close = g29_input_close;
pipe = usb_rcvintpipe(udev, ep->bEndpointAddress);
usb_fill_int_urb(g29->urb, udev, pipe,
g29->buf, G29_REPORT_LEN,
usb_fill_int_urb(g29->urb, udev, usb_rcvintpipe(udev, ep->bEndpointAddress),
g29->buf, g29->maxp,
g29_urb_complete, g29, ep->bInterval);
g29->urb->transfer_dma = g29->buf_dma;
g29->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
error = input_register_device(input);
if (error)
if ((ret = input_register_device(input)) != 0) {
goto err_free_urb;
}
usb_set_intfdata(intf, g29);
@@ -318,30 +289,24 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
err_free_urb:
usb_free_urb(g29->urb);
err_free_buf:
usb_free_coherent(udev, G29_REPORT_LEN, g29->buf, g29->buf_dma);
usb_free_coherent(udev, g29->maxp, g29->buf, g29->buf_dma);
err_free_input:
input_free_device(input);
err_free_g29:
kfree(g29);
return error;
return ret;
}
static void g29_disconnect(struct usb_interface *intf)
{
static void g29_disconnect(struct usb_interface *intf) {
struct g29_dev *g29 = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
if (!g29)
return;
if (!g29) return;
usb_kill_urb(g29->urb);
input_unregister_device(g29->input);
usb_free_urb(g29->urb);
usb_free_coherent(interface_to_usbdev(intf), G29_REPORT_LEN,
g29->buf, g29->buf_dma);
usb_free_coherent(interface_to_usbdev(intf), g29->maxp, g29->buf, g29->buf_dma);
kfree(g29);
dev_info(&intf->dev, "G29 media driver disconnected\n");
dev_info(&intf->dev, "G29 driver disconnected\n");
}
static const struct usb_device_id g29_id_table[] = {
@@ -352,10 +317,10 @@ static const struct usb_device_id g29_id_table[] = {
MODULE_DEVICE_TABLE(usb, g29_id_table);
static struct usb_driver g29_driver = {
.name = "g29_media_usb",
.name = "g29_usb",
.id_table = g29_id_table,
.probe = g29_probe,
.disconnect = g29_disconnect,
.id_table = g29_id_table,
};
module_usb_driver(g29_driver);

View File

@@ -1,28 +0,0 @@
obj-m += g29_media_usb.o
PWD := $(CURDIR)
.PHONY: all clean install uninstall
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
mkdir -p build
mv -f *.ko build/ 2>/dev/null || true
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -rf build
install:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
depmod -a
uninstall:
@modpath=$$(modinfo -n g29_media_usb 2>/dev/null); \
if [ -z "$$modpath" ]; then \
echo "Module g29_media_usb not found via modinfo (not installed?)"; \
exit 1; \
fi; \
echo "Removing $$modpath"; \
rm -f "$$modpath"; \
depmod -a

25
wasd-emulator/Makefile Normal file
View File

@@ -0,0 +1,25 @@
.PHONY: all
obj-m += wasd.o
KVER := $(shell uname -r)
KDIR ?= $(firstword $(wildcard /lib/modules/$(KVER)/build) $(wildcard /usr/lib/modules/$(KVER)/build))
PWD := $(shell pwd)
OUT := $(PWD)/out
all:
@if [ -z "$(KDIR)" ]; then \
echo "ERROR: kernel build dir not found for $(KVER). Install kernel headers (e.g. linux-headers)"; \
exit 2; \
fi
mkdir -p $(OUT)
$(MAKE) -C $(KDIR) M=$(PWD) modules
-mv -f -- *.ko *.mod.c *.o .*.o *.mod modules.order .*.cmd *.symvers $(OUT)
clean:
@if [ -z "$(KDIR)" ]; then \
echo "ERROR: kernel build dir not found for $(KVER)."; \
exit 2; \
fi
$(MAKE) -C $(KDIR) M=$(PWD) clean
rm -rf $(OUT) *.cmd *.order *.mod *.o

223
wasd-emulator/wasd.c Normal file
View File

@@ -0,0 +1,223 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/usb/ch9.h>
#include <linux/usb/input.h>
#include <linux/input.h>
#include <linux/slab.h>
#define DRV_NAME "usb_steeringwheel_wasd"
struct wheel_evt {
uint32_t buttons_be;
uint16_t rot_be;
uint8_t gas;
uint8_t brk;
uint8_t clt;
uint8_t gr_x;
uint8_t gr_y;
uint8_t gr_z;
};
struct wheel {
struct usb_device *udev;
struct usb_interface *intf;
struct input_dev *input;
struct urb *irq_urb;
struct wheel_evt *irq_data;
dma_addr_t irq_dma;
int irq_len;
int irq_interval;
int irq_ep;
char phys[64];
atomic_t opened;
};
static int drv_open(struct input_dev *dev) {
struct wheel *w = input_get_drvdata(dev);
if (!w) return -ENODEV;
atomic_set(&w->opened, 1);
int ret;
if ((ret = usb_submit_urb(w->irq_urb, GFP_KERNEL))) {
atomic_set(&w->opened, 0);
return ret;
}
return 0;
}
static void drv_irq(struct urb *urb) {
// called every 2ms?
struct wheel *w = urb->context;
if (!w || !atomic_read(&w->opened))
return;
const int status = urb->status;
if (status) {
if (status == -ENOENT || status == -ECONNRESET || status == -ESHUTDOWN)
return;
dev_dbg(&w->intf->dev, "irq urb status %d\n", status);
goto resubmit;
}
const struct wheel_evt *data = w->irq_data;
const int rot = be16_to_cpu(data->buttons_be);
// TODO set keys according to ratio
input_report_key(w->input, KEY_W, data->gas <= 0x80);
input_report_key(w->input, KEY_S, data->brk <= 0x80);
input_report_key(w->input, KEY_A, rot <= 0x6000);
input_report_key(w->input, KEY_D, rot >= 0xA000);
input_sync(w->input);
resubmit:
usb_submit_urb(w->irq_urb, GFP_ATOMIC);
}
static void drv_close(struct input_dev *dev) {
struct wheel *w = input_get_drvdata(dev);
if (!w) return;
atomic_set(&w->opened, 0);
usb_kill_urb(w->irq_urb);
}
static int drv_probe(struct usb_interface *intf, const struct usb_device_id *id) {
struct usb_device *udev = interface_to_usbdev(intf);
int ret;
// Logitech G29
//if (le16_to_cpu(udev->descriptor.idVendor) != 0x046d || le16_to_cpu(udev->descriptor.idProduct) != 0xc24f)
// return -ENODEV;
struct usb_endpoint_descriptor *ep = NULL;
const struct usb_host_interface *alts = intf->cur_altsetting;
for (int i = 0; i < alts->desc.bNumEndpoints; i++) {
struct usb_endpoint_descriptor *d = &alts->endpoint[i].desc;
if (usb_endpoint_is_int_in(d)) {
ep = d;
break;
}
}
if (!ep) return -ENODEV;
struct wheel *w;
if ((w = kzalloc(sizeof(*w), GFP_KERNEL)) == NULL)
return -ENOMEM;
w->udev = usb_get_dev(udev);
w->intf = intf;
atomic_set(&w->opened, 0);
w->irq_ep = usb_endpoint_num(ep);
w->irq_len = usb_endpoint_maxp(ep);
w->irq_interval = ep->bInterval;
if ((w->irq_urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto err_free;
}
if ((w->irq_data = usb_alloc_coherent(udev, w->irq_len, GFP_KERNEL, &w->irq_dma)) == NULL) {
ret = -ENOMEM;
goto err_free_urb;
}
if ((w->input = input_allocate_device()) == NULL) {
ret = -ENOMEM;
goto err_free_buf;
}
usb_make_path(udev, w->phys, sizeof(w->phys));
strlcat(w->phys, "/input0", sizeof(w->phys));
w->input->name = "USB Boot Mouse (example driver)";
w->input->phys = w->phys;
usb_to_input_id(udev, &w->input->id);
w->input->dev.parent = &intf->dev;
w->input->open = drv_open;
w->input->close = drv_close;
input_set_drvdata(w->input, w);
usb_fill_int_urb(
w->irq_urb,
udev,
usb_rcvintpipe(udev, ep->bEndpointAddress),
w->irq_data,
w->irq_len,
drv_irq,
w,
w->irq_interval);
w->irq_urb->transfer_dma = w->irq_dma;
w->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_set_intfdata(intf, w);
if ((ret = input_register_device(w->input)) != 0)
goto err_clear_intfdata;
dev_info(&intf->dev,
"bound to %04x:%04x, int-in ep 0x%02x maxp %u interval %u\n",
le16_to_cpu(udev->descriptor.idVendor),
le16_to_cpu(udev->descriptor.idProduct),
ep->bEndpointAddress,
w->irq_len,
w->irq_interval);
return 0;
err_clear_intfdata:
usb_set_intfdata(intf, NULL);
input_free_device(w->input);
w->input = NULL;
err_free_buf:
usb_free_coherent(udev, w->irq_len, w->irq_data, w->irq_dma);
err_free_urb:
usb_free_urb(w->irq_urb);
err_free:
usb_put_dev(w->udev);
kfree(w);
return ret;
}
static void drv_disconnect(struct usb_interface *intf) {
struct wheel *w = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
if (!w) return;
if (w->input) {
input_unregister_device(w->input);
w->input = NULL;
}
usb_kill_urb(w->irq_urb);
usb_free_coherent(w->udev, w->irq_len, w->irq_data, w->irq_dma);
usb_free_urb(w->irq_urb);
usb_put_dev(w->udev);
kfree(w);
dev_info(&intf->dev, "disconnected\n");
}
static const struct usb_device_id drv_id_table[] = {
{ USB_DEVICE_INTERFACE_NUMBER(0x046d, 0xc24f, 0) },
{ USB_INTERFACE_INFO(3, 1, 1) },
{}
};
MODULE_DEVICE_TABLE(usb, drv_id_table);
static struct usb_driver drv = {
.name = DRV_NAME,
.probe = drv_probe,
.disconnect = drv_disconnect,
.id_table = drv_id_table,
};
module_usb_driver(drv);
MODULE_AUTHOR("Lorenz Stechauner");
MODULE_DESCRIPTION("Steering wheel WASD emulator");
MODULE_LICENSE("GPL");