Refactor media

This commit is contained in:
2026-01-17 12:52:30 +01:00
parent c77c23c6ed
commit aedde65362
3 changed files with 76 additions and 155 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,15 @@
#include <linux/usb.h> #include <linux/usb.h>
#include <linux/usb/input.h> #include <linux/usb/input.h>
#include <linux/input.h> #include <linux/input.h>
#include <asm/unaligned.h>
MODULE_AUTHOR("LLP group 32"); MODULE_AUTHOR("LLP group 16");
MODULE_DESCRIPTION("Logitech G29 -> Media keys (USB driver)"); MODULE_DESCRIPTION("Logitech G29 USB driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
#define USB_VENDOR_ID_LOGITECH 0x046d #define USB_VENDOR_ID_LOGITECH 0x046d
#define USB_DEVICE_ID_LOGITECH_G29 0xc24f #define USB_DEVICE_ID_LOGITECH_G29 0xc24f
#define USB_DEVICE_ID_LOGITECH_G29_ALT 0xc260 #define USB_DEVICE_ID_LOGITECH_G29_ALT 0xc260
#define G29_REPORT_LEN 12
/*
* Button masks
*/
#define G29_BTN_PLUS 0x00800000u #define G29_BTN_PLUS 0x00800000u
#define G29_BTN_MINUS 0x01000000u #define G29_BTN_MINUS 0x01000000u
#define G29_BTN_RED_CW 0x02000000u #define G29_BTN_RED_CW 0x02000000u
@@ -53,19 +47,16 @@ module_param(mode, int, 0444);
MODULE_PARM_DESC(mode, "Mapping mode (0=MEDIA)"); MODULE_PARM_DESC(mode, "Mapping mode (0=MEDIA)");
struct g29_state { struct g29_state {
u32 buttons; u32 buttons_le;
u16 rot; u16 rot_le;
u8 gas; u8 gas;
u8 brk; u8 brk;
u8 clt; u8 clt;
u8 grx; u8 gr_x;
u8 gry; u8 gr_y;
u8 grz; u8 gr_z;
}; };
/*
* Mapping table entry
*/
struct g29_keymap_edge { struct g29_keymap_edge {
u32 mask; u32 mask;
unsigned short keycode; unsigned short keycode;
@@ -94,79 +85,41 @@ struct g29_dev {
struct urb *urb; struct urb *urb;
u8 *buf; u8 *buf;
dma_addr_t buf_dma; dma_addr_t buf_dma;
int maxp;
int interval;
int endpoint;
struct g29_state last; struct g29_state last;
}; };
/* Parsing */ 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);
static bool g29_parse_report(struct g29_state *out, const u8 *data, int len) for (int i = 0; i < ARRAY_SIZE(g29_media_edge_map); i++) {
{
if (len < G29_REPORT_LEN)
return false;
/* bytes 0..3 buttons bitfield */
out->buttons = get_unaligned_le32(&data[0]);
/* bytes 4..5 rotation */
out->rot = get_unaligned_le16(&data[4]);
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;
}
/* 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++) {
const struct g29_keymap_edge *e = &g29_media_edge_map[i]; const struct g29_keymap_edge *e = &g29_media_edge_map[i];
if (pressed & e->mask) if (pressed & e->mask) {
g29_pulse_key(g29->input, e->keycode); input_report_key(g29->input, e->keycode, 1);
input_report_key(g29->input, e->keycode, 0);
}
} }
input_sync(g29->input); input_sync(g29->input);
} }
static void g29_process_report(struct g29_dev *g29, const u8 *data, int len) static void g29_process_report(struct g29_dev *g29, const u8 *data, unsigned int len) {
{ if (len < 12) return;
struct g29_state cur;
if (!g29_parse_report(&cur, data, len))
return;
struct g29_state *cur = (void *) data;
switch (mode) { switch (mode) {
case G29_MODE_MEDIA: case G29_MODE_MEDIA:
default: default:
g29_apply_media_mode(g29, &g29->last, &cur); g29_apply_media_mode(g29, cur, &g29->last);
break; 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; struct g29_dev *g29 = urb->context;
int ret; int ret;
@@ -189,8 +142,7 @@ resubmit:
dev_err(&g29->udev->dev, "usb_submit_urb failed: %d\n", ret); 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); struct g29_dev *g29 = input_get_drvdata(input);
g29->urb->dev = g29->udev; g29->urb->dev = g29->udev;
@@ -200,68 +152,58 @@ static int g29_input_open(struct input_dev *input)
return 0; 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); struct g29_dev *g29 = input_get_drvdata(input);
usb_kill_urb(g29->urb); 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_device *udev = interface_to_usbdev(intf);
struct usb_host_interface *alts = intf->cur_altsetting; int ret;
struct usb_endpoint_descriptor *ep = NULL;
struct g29_dev *g29;
struct input_dev *input;
int i, pipe, maxp, error;
/* Find an interrupt IN endpoint capable of carrying the 12-byte report. */ /* Find an interrupt IN endpoint capable of carrying the 12-byte report. */
for (i = 0; i < alts->desc.bNumEndpoints; i++) { struct usb_endpoint_descriptor *ep = NULL;
struct usb_endpoint_descriptor *cand = &alts->endpoint[i].desc; const struct usb_host_interface *alts = intf->cur_altsetting;
if (!usb_endpoint_is_int_in(cand)) 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; continue;
pipe = usb_rcvintpipe(udev, cand->bEndpointAddress); if (usb_maxpacket(udev, usb_rcvintpipe(udev, d->bEndpointAddress)) >= 12) {
maxp = usb_maxpacket(udev, pipe); ep = d;
if (maxp >= G29_REPORT_LEN) {
ep = cand;
break; break;
} }
} }
if (!ep) return -ENODEV;
if (!ep) struct g29_dev *g29;
return -ENODEV; if ((g29 = kzalloc(sizeof(*g29), GFP_KERNEL)) == NULL) {
g29 = kzalloc(sizeof(*g29), GFP_KERNEL);
if (!g29)
return -ENOMEM; return -ENOMEM;
}
input = input_allocate_device(); struct input_dev *input;
if (!input) { if ((input = input_allocate_device()) == NULL) {
error = -ENOMEM; ret = -ENOMEM;
goto err_free_g29; goto err_free_g29;
} }
g29->udev = udev; g29->udev = udev;
g29->input = input; 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)); memset(&g29->last, 0, sizeof(g29->last));
/* Allocate a fixed-size report buffer (12 bytes). */ if ((g29->buf = usb_alloc_coherent(udev, g29->maxp, GFP_KERNEL, &g29->buf_dma)) == NULL) {
g29->buf = usb_alloc_coherent(udev, G29_REPORT_LEN, GFP_KERNEL, &g29->buf_dma); ret = -ENOMEM;
if (!g29->buf) {
error = -ENOMEM;
goto err_free_input; goto err_free_input;
} }
g29->urb = usb_alloc_urb(0, GFP_KERNEL); if ((g29->urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
if (!g29->urb) { ret = -ENOMEM;
error = -ENOMEM;
goto err_free_buf; goto err_free_buf;
} }
/* Build a friendly input device name. */
if (udev->manufacturer) if (udev->manufacturer)
strscpy(g29->name, udev->manufacturer, sizeof(g29->name)); strscpy(g29->name, udev->manufacturer, sizeof(g29->name));
if (udev->product) { if (udev->product) {
@@ -271,7 +213,7 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
} }
if (!strlen(g29->name)) if (!strlen(g29->name))
snprintf(g29->name, sizeof(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.idVendor),
le16_to_cpu(udev->descriptor.idProduct)); le16_to_cpu(udev->descriptor.idProduct));
@@ -296,16 +238,15 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
input->open = g29_input_open; input->open = g29_input_open;
input->close = g29_input_close; input->close = g29_input_close;
pipe = usb_rcvintpipe(udev, ep->bEndpointAddress); usb_fill_int_urb(g29->urb, udev, usb_rcvintpipe(udev, ep->bEndpointAddress),
usb_fill_int_urb(g29->urb, udev, pipe, g29->buf, g29->maxp,
g29->buf, G29_REPORT_LEN,
g29_urb_complete, g29, ep->bInterval); g29_urb_complete, g29, ep->bInterval);
g29->urb->transfer_dma = g29->buf_dma; g29->urb->transfer_dma = g29->buf_dma;
g29->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; g29->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
error = input_register_device(input); if ((ret = input_register_device(input)) != 0) {
if (error)
goto err_free_urb; goto err_free_urb;
}
usb_set_intfdata(intf, g29); usb_set_intfdata(intf, g29);
@@ -318,30 +259,24 @@ static int g29_probe(struct usb_interface *intf, const struct usb_device_id *id)
err_free_urb: err_free_urb:
usb_free_urb(g29->urb); usb_free_urb(g29->urb);
err_free_buf: 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: err_free_input:
input_free_device(input); input_free_device(input);
err_free_g29: err_free_g29:
kfree(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); struct g29_dev *g29 = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL); usb_set_intfdata(intf, NULL);
if (!g29) if (!g29) return;
return;
usb_kill_urb(g29->urb); usb_kill_urb(g29->urb);
input_unregister_device(g29->input); input_unregister_device(g29->input);
usb_free_urb(g29->urb); usb_free_urb(g29->urb);
usb_free_coherent(interface_to_usbdev(intf), G29_REPORT_LEN, usb_free_coherent(interface_to_usbdev(intf), g29->maxp, g29->buf, g29->buf_dma);
g29->buf, g29->buf_dma);
kfree(g29); kfree(g29);
dev_info(&intf->dev, "G29 driver disconnected\n");
dev_info(&intf->dev, "G29 media driver disconnected\n");
} }
static const struct usb_device_id g29_id_table[] = { static const struct usb_device_id g29_id_table[] = {
@@ -352,10 +287,10 @@ static const struct usb_device_id g29_id_table[] = {
MODULE_DEVICE_TABLE(usb, g29_id_table); MODULE_DEVICE_TABLE(usb, g29_id_table);
static struct usb_driver g29_driver = { static struct usb_driver g29_driver = {
.name = "g29_media_usb", .name = "g29_usb",
.id_table = g29_id_table,
.probe = g29_probe, .probe = g29_probe,
.disconnect = g29_disconnect, .disconnect = g29_disconnect,
.id_table = g29_id_table,
}; };
module_usb_driver(g29_driver); 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