-
Notifications
You must be signed in to change notification settings - Fork 11
/
system76-io.c
304 lines (236 loc) · 7.83 KB
/
system76-io.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* system76-io.c
*
* Copyright (C) 2018 Jeremy Soller <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/suspend.h>
#include <linux/usb.h>
#define IO_VENDOR 0x1209
#define IO_DEVICE 0x1776
#define IO_INTF_CTRL 0
#define IO_EP_CTRL 0x00
#define IO_INTF_DATA 1
#define IO_EP_IN 0x83
#define IO_EP_OUT 0x04
#define IO_MSG_SIZE 32
#define IO_TIMEOUT 1000
#include "system76-io_dev.c"
#include "system76-io_hwmon.c"
#define BAUD 1000000
static u8 line_encoding[7] = {
(u8)BAUD,
(u8)(BAUD >> 8),
(u8)(BAUD >> 16),
(u8)(BAUD >> 24),
0,
0,
8
};
static ssize_t show_bootloader(struct device *dev, struct device_attribute *attr, char *buf) {
return sprintf(buf, "%d\n", 0);
}
static ssize_t set_bootloader(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
unsigned int val;
int ret;
struct io_dev * io_dev = dev_get_drvdata(dev);
mutex_lock(&io_dev->lock);
ret = kstrtouint(buf, 10, &val);
if (!ret) {
if (val) {
ret = io_dev_bootloader(io_dev, IO_TIMEOUT);
if(!ret) {
ret = size;
}
}
}
mutex_unlock(&io_dev->lock);
return ret;
}
static DEVICE_ATTR(bootloader, S_IRUGO | S_IWUSR, show_bootloader, set_bootloader);
static ssize_t show_revision(struct device *dev, struct device_attribute *attr, char *buf) {
int ret;
struct io_dev * io_dev = dev_get_drvdata(dev);
mutex_lock(&io_dev->lock);
ret = io_dev_revision(io_dev, buf, PAGE_SIZE, IO_TIMEOUT);
mutex_unlock(&io_dev->lock);
return ret;
}
static DEVICE_ATTR(revision, S_IRUGO, show_revision, NULL);
#ifdef CONFIG_PM_SLEEP
static int io_pm(struct notifier_block *nb, unsigned long action, void *data) {
struct io_dev * io_dev = container_of(nb, struct io_dev, pm_notifier);
mutex_lock(&io_dev->lock);
switch (action) {
case PM_HIBERNATION_PREPARE:
case PM_SUSPEND_PREPARE:
io_dev_set_suspend(io_dev, 1, IO_TIMEOUT);
break;
case PM_POST_HIBERNATION:
case PM_POST_SUSPEND:
io_dev_set_suspend(io_dev, 0, IO_TIMEOUT);
break;
case PM_POST_RESTORE:
case PM_RESTORE_PREPARE:
default:
break;
}
mutex_unlock(&io_dev->lock);
return NOTIFY_DONE;
}
#endif
static int io_probe(struct usb_interface *interface, const struct usb_device_id *id) {
int retry;
int result;
struct io_dev * io_dev;
dev_info(&interface->dev, "id %04X:%04X interface %d probe\n", id->idVendor, id->idProduct, id->bInterfaceNumber);
if (id->bInterfaceNumber == IO_INTF_CTRL) {
result = usb_control_msg(
interface_to_usbdev(interface),
usb_sndctrlpipe(interface_to_usbdev(interface), IO_EP_CTRL),
0x22,
0x21,
0x03,
0,
NULL,
0,
IO_TIMEOUT
);
if (result < 0) {
dev_err(&interface->dev, "set line state failed: %d\n", -result);
return result;
}
result = usb_control_msg(
interface_to_usbdev(interface),
usb_sndctrlpipe(interface_to_usbdev(interface), IO_EP_CTRL),
0x20,
0x21,
0,
0,
line_encoding,
7,
IO_TIMEOUT
);
if (result < 0) {
dev_err(&interface->dev, "set line encoding failed: %d\n", -result);
return result;
}
return 0;
} else if (id->bInterfaceNumber == IO_INTF_DATA) {
io_dev = kmalloc(sizeof(struct io_dev), GFP_KERNEL);
if (IS_ERR_OR_NULL(io_dev)) {
dev_err(&interface->dev, "kmalloc failed\n");
return -ENOMEM;
}
memset(io_dev, 0, sizeof(struct io_dev));
mutex_init(&io_dev->lock);
mutex_lock(&io_dev->lock);
io_dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
usb_set_intfdata(interface, io_dev);
for(retry = 0; retry < 8; retry++) {
dev_info(&interface->dev, "trying reset: %d\n", retry);
result = io_dev_reset(io_dev, IO_TIMEOUT);
if (result != -ETIMEDOUT) {
break;
}
}
if (result) {
dev_err(&interface->dev, "io_dev_reset failed: %d\n", result);
goto fail1;
}
result = device_create_file(&interface->dev, &dev_attr_bootloader);
if (result) {
dev_err(&interface->dev, "device_create_file failed: %d\n", result);
goto fail1;
}
result = device_create_file(&interface->dev, &dev_attr_revision);
if (result) {
dev_err(&interface->dev, "device_create_file failed: %d\n", result);
goto fail2;
}
io_dev->hwmon_dev = hwmon_device_register_with_groups(&interface->dev, "system76_io", io_dev, io_groups);
if (IS_ERR(io_dev->hwmon_dev)) {
result = PTR_ERR(io_dev->hwmon_dev);
dev_err(&interface->dev, "hwmon_device_register_with_groups failed: %d\n", result);
goto fail3;
}
#ifdef CONFIG_PM_SLEEP
io_dev->pm_notifier.notifier_call = io_pm;
register_pm_notifier(&io_dev->pm_notifier);
#endif
mutex_unlock(&io_dev->lock);
return 0;
fail3:
device_remove_file(&interface->dev, &dev_attr_revision);
fail2:
device_remove_file(&interface->dev, &dev_attr_bootloader);
fail1:
usb_set_intfdata(interface, NULL);
usb_put_dev(io_dev->usb_dev);
mutex_unlock(&io_dev->lock);
mutex_destroy(&io_dev->lock);
kfree(io_dev);
return result;
} else {
return -ENODEV;
}
}
static void io_disconnect(struct usb_interface *interface) {
struct io_dev * io_dev;
dev_info(&interface->dev, "disconnect\n");
io_dev = usb_get_intfdata(interface);
if (io_dev) {
mutex_lock(&io_dev->lock);
#ifdef CONFIG_PM_SLEEP
unregister_pm_notifier(&io_dev->pm_notifier);
#endif
hwmon_device_unregister(io_dev->hwmon_dev);
device_remove_file(&interface->dev, &dev_attr_revision);
device_remove_file(&interface->dev, &dev_attr_bootloader);
usb_set_intfdata(interface, NULL);
usb_put_dev(io_dev->usb_dev);
mutex_unlock(&io_dev->lock);
mutex_destroy(&io_dev->lock);
kfree(io_dev);
}
}
static struct usb_device_id io_table[] = {
{ USB_DEVICE_INTERFACE_NUMBER(IO_VENDOR, IO_DEVICE, IO_INTF_CTRL) },
{ USB_DEVICE_INTERFACE_NUMBER(IO_VENDOR, IO_DEVICE, IO_INTF_DATA) },
{ }
};
MODULE_DEVICE_TABLE(usb, io_table);
static struct usb_driver io_driver = {
.name = "system76-io",
.probe = io_probe,
.disconnect = io_disconnect,
.id_table = io_table,
};
static int __init io_init(void) {
return usb_register(&io_driver);
}
static void __exit io_exit(void) {
usb_deregister(&io_driver);
}
module_init(io_init);
module_exit(io_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeremy Soller <[email protected]>");
MODULE_DESCRIPTION("System76 Io driver");