Compare commits
9 Commits
7351f6826c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d5989a5e3 | |||
| baf457901e | |||
| 8796d8c6bb | |||
| 1e403dcd4a | |||
| 592142cef2 | |||
| 437e0c1f81 | |||
| a61165f33d | |||
| 007aa8dc21 | |||
| 6161415847 |
@@ -1,5 +1,6 @@
|
|||||||
# Device Drivers
|
|
||||||
----------------------------------
|
Device Driver
|
||||||
|
=============
|
||||||
|
|
||||||
This repository contains Linux **kernel modules** (``.ko``) that implement low-level USB input drivers and expose device events through the Linux **input subsystem** (evdev).
|
This repository contains Linux **kernel modules** (``.ko``) that implement low-level USB input drivers and expose device events through the Linux **input subsystem** (evdev).
|
||||||
|
|
||||||
@@ -9,7 +10,6 @@ A helper tool (``usb_driver_manager.py``) is included to make it easier to:
|
|||||||
- unbind/bind a selected interface to a chosen driver during development
|
- unbind/bind a selected interface to a chosen driver during development
|
||||||
|
|
||||||
## Drivers
|
## Drivers
|
||||||
----------
|
|
||||||
|
|
||||||
### Mouse driver (``mouse/``)
|
### Mouse driver (``mouse/``)
|
||||||
USB HID boot-protocol mouse driver.
|
USB HID boot-protocol mouse driver.
|
||||||
@@ -37,7 +37,6 @@ make
|
|||||||
After building, the module (``*.ko``) is typically placed under ``build/`` by the provided Makefiles.
|
After building, the module (``*.ko``) is typically placed under ``build/`` by the provided Makefiles.
|
||||||
|
|
||||||
## USB Driver Manager
|
## USB Driver Manager
|
||||||
---------------------
|
|
||||||
|
|
||||||
**Usage examples:**
|
**Usage examples:**
|
||||||
```bash
|
```bash
|
||||||
@@ -55,7 +54,7 @@ sudo python3 usb_driver_manager.py ./mouse/build ./g29_media_usb/build
|
|||||||
4. Confirm; the tool unbinds the current driver, reloads the module if needed, and binds the chosen interface.
|
4. Confirm; the tool unbinds the current driver, reloads the module if needed, and binds the chosen interface.
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
----------
|
|
||||||
After binding the driver, use ``evtest`` to confirm key events:
|
After binding the driver, use ``evtest`` to confirm key events:
|
||||||
```bash
|
```bash
|
||||||
sudo evtest
|
sudo evtest
|
||||||
|
|||||||
+420
-458
File diff suppressed because it is too large
Load Diff
+2
-3
@@ -35,7 +35,7 @@
|
|||||||
#define G29_BTN_RED_CW 0x02000000u
|
#define G29_BTN_RED_CW 0x02000000u
|
||||||
#define G29_BTN_RED_CCW 0x04000000u
|
#define G29_BTN_RED_CCW 0x04000000u
|
||||||
#define G29_BTN_RETURN 0x08000000u
|
#define G29_BTN_RETURN 0x08000000u
|
||||||
#define G29_BTN_PS3_LOGO 0xF0000000u
|
#define G29_BTN_PS3 0x10000000u
|
||||||
|
|
||||||
#define G29_DPAD_MASK 0x0000000Eu
|
#define G29_DPAD_MASK 0x0000000Eu
|
||||||
#define G29_DPAD_UP 0x00000000u
|
#define G29_DPAD_UP 0x00000000u
|
||||||
@@ -59,8 +59,7 @@
|
|||||||
#define G29_GEARSHIFT_Z_NEUTRAL 0x9C /* Neutral position */
|
#define G29_GEARSHIFT_Z_NEUTRAL 0x9C /* Neutral position */
|
||||||
#define G29_GEARSHIFT_Z_PRESSED 0xDC /* Pressed down */
|
#define G29_GEARSHIFT_Z_PRESSED 0xDC /* Pressed down */
|
||||||
|
|
||||||
#define WHEEL_CENTER 32768
|
#define WHEEL_CENTER 0x8000
|
||||||
#define WHEEL_MAX_DIST 32768
|
|
||||||
|
|
||||||
struct g29_state {
|
struct g29_state {
|
||||||
u32 buttons_le; /* Button bitmask (little-endian) */
|
u32 buttons_le; /* Button bitmask (little-endian) */
|
||||||
|
|||||||
+2
-1
@@ -45,7 +45,8 @@ Logitech G29 USB Protocol
|
|||||||
- `0x02000000` - Red rotation clockwise
|
- `0x02000000` - Red rotation clockwise
|
||||||
- `0x04000000` - Red rotation counterclockwise
|
- `0x04000000` - Red rotation counterclockwise
|
||||||
- `0x08000000` - Return
|
- `0x08000000` - Return
|
||||||
- `0xF0000000` - Playstation 3 Logo Button (verify)
|
- `0x10000000` - PS3 Logo
|
||||||
|
- `0xE0000000` - ?
|
||||||
- `Rot`: Wheel rotation (little-endian). `0x0000` (leftmost) - `0xFFFF` (rightmost).
|
- `Rot`: Wheel rotation (little-endian). `0x0000` (leftmost) - `0xFFFF` (rightmost).
|
||||||
- `Gas`: Gas pedal. `0xFF` (up, default) - `0x00` (down).
|
- `Gas`: Gas pedal. `0xFF` (up, default) - `0x00` (down).
|
||||||
- `Brk`: Brake pedal. `0xFF` (up, default) - `0x00` (down).
|
- `Brk`: Brake pedal. `0xFF` (up, default) - `0x00` (down).
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
|
|
||||||
.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
|
|
||||||
@@ -1,223 +0,0 @@
|
|||||||
|
|
||||||
#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");
|
|
||||||
Reference in New Issue
Block a user