forked from hannesmann/gcadapter-oc-kmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcadapter_oc.c
152 lines (124 loc) · 3.9 KB
/
gcadapter_oc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include "gcadapter.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Hannes Mann");
MODULE_DESCRIPTION("Filter kernel module to set the polling rate of the Wii U/Mayflash GameCube Adapter to a custom value.");
MODULE_VERSION("1.0");
static struct usb_device* adapter_device;
static unsigned short rate = GCADAPTER_RECOMMENDED_INTERVAL;
static void patch_endpoints(void)
{
if(adapter_device != NULL && adapter_device->actconfig != NULL)
{
struct usb_interface* interface = adapter_device->actconfig->interface[0];
if(interface != NULL)
{
for(unsigned int altsetting = 0; altsetting < interface->num_altsetting; altsetting++)
{
struct usb_host_interface* altsettingptr = &interface->altsetting[altsetting];
for(__u8 endpoint = 0; endpoint < altsettingptr->desc.bNumEndpoints; endpoint++)
{
if(altsettingptr->endpoint[endpoint].desc.bEndpointAddress == 0x81 || altsettingptr->endpoint[endpoint].desc.bEndpointAddress == 0x02)
{
altsettingptr->endpoint[endpoint].desc.bInterval = rate;
printk(KERN_INFO "gcadapter_oc_kmod: [altsetting %d] -> [endpoint %d addr 0x%x] rate %dhz applied\n",
altsetting,
endpoint,
altsettingptr->endpoint[endpoint].desc.bEndpointAddress,
1000 / rate);
}
}
}
}
usb_reset_device(adapter_device);
}
}
static int on_usb_notified(struct notifier_block* self, unsigned long action, void* dev)
{
struct usb_device* device = dev;
switch(action)
{
case USB_DEVICE_ADD:
if(device->descriptor.idVendor == GCADAPTER_VID && device->descriptor.idProduct == GCADAPTER_PID && adapter_device == NULL)
{
adapter_device = device;
printk(KERN_INFO "gcadapter_oc_kmod: New adapter connected\n");
patch_endpoints();
}
break;
case USB_DEVICE_REMOVE:
if(device->descriptor.idVendor == GCADAPTER_VID && device->descriptor.idProduct == GCADAPTER_PID && adapter_device != NULL)
{
adapter_device = NULL;
printk(KERN_INFO "gcadapter_oc_kmod: Adapter disconnected\n");
}
break;
}
return NOTIFY_OK;
}
// https://stackoverflow.com/questions/5911849/simple-kernel-module-for-usb?rq=1
// Seems to be "bad", but only way to do it w/o unloading usbhid
static struct notifier_block usb_nb =
{
.notifier_call = on_usb_notified
};
// https://stackoverflow.com/questions/34957016/signal-on-kernel-parameter-change
static int on_rate_changed(const char* value, const struct kernel_param* kp)
{
int res = param_set_ushort(value, kp);
if(res == 0)
{
printk(KERN_INFO "gcadapter_oc_kmod: Rate module parameter changed to %dhz\n", 1000 / rate);
if(rate > 255)
{
printk(KERN_WARNING "gcadapter_oc_kmod [warning]: Rate parameter invalid\n");
rate = 255;
}
if(rate == 0)
{
printk(KERN_WARNING "gcadapter_oc_kmod [warning]: Rate parameter was invalid, set to 1000hz\n");
rate = 1;
}
patch_endpoints();
}
return res;
}
static struct kernel_param_ops rate_ops =
{
.set = &on_rate_changed,
.get = ¶m_get_ushort
};
module_param_cb(rate, &rate_ops, &rate, 0644);
MODULE_PARM_DESC(rate, "Polling rate to set for the adapter (default: 2).");
static int __init on_mod_init(void)
{
if(rate > 255)
{
printk(KERN_WARNING "gcadapter_oc_kmod [warning]: Rate parameter was invalid, sanitized to 3.9hz\n");
rate = 255;
}
if(rate == 0)
{
printk(KERN_WARNING "gcadapter_oc_kmod [warning]: Rate parameter was invalid, sanitized to 1000hz\n");
rate = 1;
}
adapter_device = NULL;
usb_register_notify(&usb_nb);
return 0;
}
static void __exit on_mod_exit(void)
{
if(adapter_device != NULL)
{
printk(KERN_INFO "gcadapter_oc_kmod: Restoring default rate for adapter\n");
rate = 8;
patch_endpoints();
adapter_device = NULL;
}
usb_unregister_notify(&usb_nb);
}
module_init(on_mod_init);
module_exit(on_mod_exit);