-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireless_usb_mouse.c
More file actions
173 lines (140 loc) · 5.17 KB
/
wireless_usb_mouse.c
File metadata and controls
173 lines (140 loc) · 5.17 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/input.h>
#include <linux/hid.h> // USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, USB_INTERFACE_PROTOCOL_MOUSE
#define DRIVER_NAME "wireless_usb_mouse"
// more info about this code can be found int the handy_notes.txt file
struct usb_mouse
{
struct usb_device *udev;
struct usb_interface *intf;
struct urb *irq_urb;
unsigned char *irq_buf;
unsigned int irq_buf_len;
struct input_dev *input;
};
static const struct usb_device_id usb_mouse_table[] = {
{USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID,
USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_MOUSE)},
{}};
MODULE_DEVICE_TABLE(usb, usb_mouse_table);
static inline int conv_12_to_32_bit(int v)
{
if (v & 0x800)
v |= ~0xFFF;
return v;
}
static void process_usb_mouse_input(struct usb_mouse *dev, unsigned char *buf, int len)
{
int left = 0, right = 0, middle = 0, dx = 0, dy = 0, wheel = 0;
left = (buf[1] & 0x01) ? 1 : 0;
right = (buf[1] & 0x02) ? 1 : 0;
middle = (buf[1] & 0x04) ? 1 : 0;
// https://github.com/PaulStoffregen/USBHost_t36/commit/99ec892dd7b0bca62917941effad9031ad2aa5bc
dx = buf[3] | ((buf[4] & 0x0F) << 8);
dy = ((buf[5] << 4) | ((buf[4] & 0xF0) >> 4));
dx = conv_12_to_32_bit(dx);
dy = conv_12_to_32_bit(dy);
wheel = (signed char)buf[6];
input_report_key(dev->input, BTN_LEFT, left);
input_report_key(dev->input, BTN_RIGHT, right);
input_report_key(dev->input, BTN_MIDDLE, middle);
input_report_rel(dev->input, REL_X, dx);
input_report_rel(dev->input, REL_Y, dy);
input_report_rel(dev->input, REL_WHEEL, wheel);
input_sync(dev->input);
}
// urb - USB request block
static void usb_mouse_callback(struct urb *urb)
{
struct usb_mouse *dev = urb->context;
if (urb->status == 0)
{
process_usb_mouse_input(dev, urb->transfer_buffer, urb->actual_length);
}
// resubmit the urb
usb_submit_urb(urb, GFP_ATOMIC);
}
// i guess the most important part - connecting the driver to the device
static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(intf);
// kzalloc - malloc dla kernela + zerowanie pamieci
struct usb_mouse *dev = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
struct usb_endpoint_descriptor *endpoint = NULL;
int i;
// finding the endpoint the mouse uses to send data
// usb_endpoint_is_int_in is a crazy macro that checks if the endpoint is an interrupt IN endpoint (input device)
// https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/usb/ch9.h#L597
// this part is thanks to the courtesy of github copilot
for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
{
if (usb_endpoint_is_int_in(&intf->cur_altsetting->endpoint[i].desc))
{
endpoint = &intf->cur_altsetting->endpoint[i].desc;
break;
}
}
dev->udev = udev;
dev->intf = intf;
dev->input = input_allocate_device();
dev->input->name = "Wireless USB Mouse";
set_bit(EV_KEY, dev->input->evbit);
set_bit(EV_REL, dev->input->evbit);
set_bit(BTN_LEFT, dev->input->keybit);
set_bit(BTN_RIGHT, dev->input->keybit);
set_bit(BTN_MIDDLE, dev->input->keybit);
set_bit(REL_X, dev->input->relbit);
set_bit(REL_Y, dev->input->relbit);
set_bit(REL_WHEEL, dev->input->relbit);
input_register_device(dev->input);
dev->irq_buf_len = usb_endpoint_maxp(endpoint);
dev->irq_buf = kmalloc(dev->irq_buf_len, GFP_KERNEL);
dev->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
// macro that initializes an interrput with urb
// https://manpages.debian.org/jessie-backports/linux-manual-4.8/usb_fill_int_urb.9.en.html
usb_fill_int_urb(dev->irq_urb,
dev->udev,
usb_rcvintpipe(dev->udev, endpoint->bEndpointAddress),
dev->irq_buf,
dev->irq_buf_len,
usb_mouse_callback,
dev,
endpoint->bInterval);
usb_set_intfdata(intf, dev);
// this starts the urb and begins listening
usb_submit_urb(dev->irq_urb, GFP_KERNEL);
dev_info(&intf->dev, "wireless_usb_mouse bound\n");
return 0;
}
static void usb_mouse_disconnect(struct usb_interface *intf)
{
struct usb_mouse *dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
if (!dev)
{
dev_info(&intf->dev, "wireless_usb_mouse unbound - no device data\n");
return;
}
if (dev->input)
{
input_unregister_device(dev->input);
}
// cancel the URB
usb_kill_urb(dev->irq_urb);
kfree(dev->irq_buf);
kfree(dev);
dev_info(&intf->dev, "wireless_usb_mouse unbound\n");
}
static struct usb_driver usb_mouse__barebones_driver = {
.name = DRIVER_NAME,
.probe = usb_mouse_probe,
.disconnect = usb_mouse_disconnect,
.id_table = usb_mouse_table,
};
module_usb_driver(usb_mouse__barebones_driver);
MODULE_AUTHOR("me + chat + internet");
MODULE_DESCRIPTION("\"Simple\" USB Mouse driver");
MODULE_LICENSE("GPL");