• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * corsair-cpro.c - Linux driver for Corsair Commander Pro
4  * Copyright (C) 2020 Marius Zachmann <mail@mariuszachmann.de>
5  *
6  * This driver uses hid reports to communicate with the device to allow hidraw userspace drivers
7  * still being used. The device does not use report ids. When using hidraw and this driver
8  * simultaniously, reports could be switched.
9  */
10 
11 #include <linux/bitops.h>
12 #include <linux/completion.h>
13 #include <linux/hid.h>
14 #include <linux/hwmon.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/types.h>
21 
22 #define USB_VENDOR_ID_CORSAIR			0x1b1c
23 #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO	0x0c10
24 #define USB_PRODUCT_ID_CORSAIR_1000D		0x1d00
25 
26 #define OUT_BUFFER_SIZE		63
27 #define IN_BUFFER_SIZE		16
28 #define LABEL_LENGTH		11
29 #define REQ_TIMEOUT		300
30 
31 #define CTL_GET_TMP_CNCT	0x10	/*
32 					 * returns in bytes 1-4 for each temp sensor:
33 					 * 0 not connected
34 					 * 1 connected
35 					 */
36 #define CTL_GET_TMP		0x11	/*
37 					 * send: byte 1 is channel, rest zero
38 					 * rcv:  returns temp for channel in centi-degree celsius
39 					 * in bytes 1 and 2
40 					 * returns 0x11 in byte 0 if no sensor is connected
41 					 */
42 #define CTL_GET_VOLT		0x12	/*
43 					 * send: byte 1 is rail number: 0 = 12v, 1 = 5v, 2 = 3.3v
44 					 * rcv:  returns millivolt in bytes 1,2
45 					 * returns error 0x10 if request is invalid
46 					 */
47 #define CTL_GET_FAN_CNCT	0x20	/*
48 					 * returns in bytes 1-6 for each fan:
49 					 * 0 not connected
50 					 * 1 3pin
51 					 * 2 4pin
52 					 */
53 #define CTL_GET_FAN_RPM		0x21	/*
54 					 * send: byte 1 is channel, rest zero
55 					 * rcv:  returns rpm in bytes 1,2
56 					 */
57 #define CTL_GET_FAN_PWM		0x22	/*
58 					 * send: byte 1 is channel, rest zero
59 					 * rcv:  returns pwm in byte 1 if it was set
60 					 *	 returns error 0x12 if fan is controlled via
61 					 *	 fan_target or fan curve
62 					 */
63 #define CTL_SET_FAN_FPWM	0x23	/*
64 					 * set fixed pwm
65 					 * send: byte 1 is fan number
66 					 * send: byte 2 is percentage from 0 - 100
67 					 */
68 #define CTL_SET_FAN_TARGET	0x24	/*
69 					 * set target rpm
70 					 * send: byte 1 is fan number
71 					 * send: byte 2-3 is target
72 					 * device accepts all values from 0x00 - 0xFFFF
73 					 */
74 
75 #define NUM_FANS		6
76 #define NUM_TEMP_SENSORS	4
77 
78 struct ccp_device {
79 	struct hid_device *hdev;
80 	struct device *hwmon_dev;
81 	/* For reinitializing the completion below */
82 	spinlock_t wait_input_report_lock;
83 	struct completion wait_input_report;
84 	struct mutex mutex; /* whenever buffer is used, lock before send_usb_cmd */
85 	u8 *cmd_buffer;
86 	u8 *buffer;
87 	int buffer_recv_size; /* number of received bytes in buffer */
88 	int target[6];
89 	DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
90 	DECLARE_BITMAP(fan_cnct, NUM_FANS);
91 	char fan_label[6][LABEL_LENGTH];
92 };
93 
94 /* converts response error in buffer to errno */
ccp_get_errno(struct ccp_device * ccp)95 static int ccp_get_errno(struct ccp_device *ccp)
96 {
97 	switch (ccp->buffer[0]) {
98 	case 0x00: /* success */
99 		return 0;
100 	case 0x01: /* called invalid command */
101 		return -EOPNOTSUPP;
102 	case 0x10: /* called GET_VOLT / GET_TMP with invalid arguments */
103 		return -EINVAL;
104 	case 0x11: /* requested temps of disconnected sensors */
105 	case 0x12: /* requested pwm of not pwm controlled channels */
106 		return -ENODATA;
107 	default:
108 		hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
109 		return -EIO;
110 	}
111 }
112 
113 /* send command, check for error in response, response in ccp->buffer */
send_usb_cmd(struct ccp_device * ccp,u8 command,u8 byte1,u8 byte2,u8 byte3)114 static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
115 {
116 	unsigned long t;
117 	int ret;
118 
119 	memset(ccp->cmd_buffer, 0x00, OUT_BUFFER_SIZE);
120 	ccp->cmd_buffer[0] = command;
121 	ccp->cmd_buffer[1] = byte1;
122 	ccp->cmd_buffer[2] = byte2;
123 	ccp->cmd_buffer[3] = byte3;
124 
125 	/*
126 	 * Disable raw event parsing for a moment to safely reinitialize the
127 	 * completion. Reinit is done because hidraw could have triggered
128 	 * the raw event parsing and marked the ccp->wait_input_report
129 	 * completion as done.
130 	 */
131 	spin_lock_bh(&ccp->wait_input_report_lock);
132 	reinit_completion(&ccp->wait_input_report);
133 	spin_unlock_bh(&ccp->wait_input_report_lock);
134 
135 	ret = hid_hw_output_report(ccp->hdev, ccp->cmd_buffer, OUT_BUFFER_SIZE);
136 	if (ret < 0)
137 		return ret;
138 
139 	t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
140 	if (!t)
141 		return -ETIMEDOUT;
142 
143 	if (ccp->buffer_recv_size != IN_BUFFER_SIZE)
144 		return -EPROTO;
145 
146 	return ccp_get_errno(ccp);
147 }
148 
ccp_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)149 static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
150 {
151 	struct ccp_device *ccp = hid_get_drvdata(hdev);
152 
153 	/* only copy buffer when requested */
154 	spin_lock(&ccp->wait_input_report_lock);
155 	if (!completion_done(&ccp->wait_input_report)) {
156 		memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
157 		ccp->buffer_recv_size = size;
158 		complete_all(&ccp->wait_input_report);
159 	}
160 	spin_unlock(&ccp->wait_input_report_lock);
161 
162 	return 0;
163 }
164 
165 /* requests and returns single data values depending on channel */
get_data(struct ccp_device * ccp,int command,int channel,bool two_byte_data)166 static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
167 {
168 	int ret;
169 
170 	mutex_lock(&ccp->mutex);
171 
172 	ret = send_usb_cmd(ccp, command, channel, 0, 0);
173 	if (ret)
174 		goto out_unlock;
175 
176 	ret = ccp->buffer[1];
177 	if (two_byte_data)
178 		ret = (ret << 8) + ccp->buffer[2];
179 
180 out_unlock:
181 	mutex_unlock(&ccp->mutex);
182 	return ret;
183 }
184 
set_pwm(struct ccp_device * ccp,int channel,long val)185 static int set_pwm(struct ccp_device *ccp, int channel, long val)
186 {
187 	int ret;
188 
189 	if (val < 0 || val > 255)
190 		return -EINVAL;
191 
192 	/* The Corsair Commander Pro uses values from 0-100 */
193 	val = DIV_ROUND_CLOSEST(val * 100, 255);
194 
195 	mutex_lock(&ccp->mutex);
196 
197 	ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
198 	if (!ret)
199 		ccp->target[channel] = -ENODATA;
200 
201 	mutex_unlock(&ccp->mutex);
202 	return ret;
203 }
204 
set_target(struct ccp_device * ccp,int channel,long val)205 static int set_target(struct ccp_device *ccp, int channel, long val)
206 {
207 	int ret;
208 
209 	val = clamp_val(val, 0, 0xFFFF);
210 	ccp->target[channel] = val;
211 
212 	mutex_lock(&ccp->mutex);
213 	ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
214 
215 	mutex_unlock(&ccp->mutex);
216 	return ret;
217 }
218 
ccp_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)219 static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
220 			   u32 attr, int channel, const char **str)
221 {
222 	struct ccp_device *ccp = dev_get_drvdata(dev);
223 
224 	switch (type) {
225 	case hwmon_fan:
226 		switch (attr) {
227 		case hwmon_fan_label:
228 			*str = ccp->fan_label[channel];
229 			return 0;
230 		default:
231 			break;
232 		}
233 		break;
234 	default:
235 		break;
236 	}
237 
238 	return -EOPNOTSUPP;
239 }
240 
ccp_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)241 static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
242 		    u32 attr, int channel, long *val)
243 {
244 	struct ccp_device *ccp = dev_get_drvdata(dev);
245 	int ret;
246 
247 	switch (type) {
248 	case hwmon_temp:
249 		switch (attr) {
250 		case hwmon_temp_input:
251 			ret = get_data(ccp, CTL_GET_TMP, channel, true);
252 			if (ret < 0)
253 				return ret;
254 			*val = ret * 10;
255 			return 0;
256 		default:
257 			break;
258 		}
259 		break;
260 	case hwmon_fan:
261 		switch (attr) {
262 		case hwmon_fan_input:
263 			ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
264 			if (ret < 0)
265 				return ret;
266 			*val = ret;
267 			return 0;
268 		case hwmon_fan_target:
269 			/* how to read target values from the device is unknown */
270 			/* driver returns last set value or 0			*/
271 			if (ccp->target[channel] < 0)
272 				return -ENODATA;
273 			*val = ccp->target[channel];
274 			return 0;
275 		default:
276 			break;
277 		}
278 		break;
279 	case hwmon_pwm:
280 		switch (attr) {
281 		case hwmon_pwm_input:
282 			ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
283 			if (ret < 0)
284 				return ret;
285 			*val = DIV_ROUND_CLOSEST(ret * 255, 100);
286 			return 0;
287 		default:
288 			break;
289 		}
290 		break;
291 	case hwmon_in:
292 		switch (attr) {
293 		case hwmon_in_input:
294 			ret = get_data(ccp, CTL_GET_VOLT, channel, true);
295 			if (ret < 0)
296 				return ret;
297 			*val = ret;
298 			return 0;
299 		default:
300 			break;
301 		}
302 		break;
303 	default:
304 		break;
305 	}
306 
307 	return -EOPNOTSUPP;
308 };
309 
ccp_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)310 static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
311 		     u32 attr, int channel, long val)
312 {
313 	struct ccp_device *ccp = dev_get_drvdata(dev);
314 
315 	switch (type) {
316 	case hwmon_pwm:
317 		switch (attr) {
318 		case hwmon_pwm_input:
319 			return set_pwm(ccp, channel, val);
320 		default:
321 			break;
322 		}
323 		break;
324 	case hwmon_fan:
325 		switch (attr) {
326 		case hwmon_fan_target:
327 			return set_target(ccp, channel, val);
328 		default:
329 			break;
330 		}
331 		break;
332 	default:
333 		break;
334 	}
335 
336 	return -EOPNOTSUPP;
337 };
338 
ccp_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)339 static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
340 			      u32 attr, int channel)
341 {
342 	const struct ccp_device *ccp = data;
343 
344 	switch (type) {
345 	case hwmon_temp:
346 		if (!test_bit(channel, ccp->temp_cnct))
347 			break;
348 
349 		switch (attr) {
350 		case hwmon_temp_input:
351 			return 0444;
352 		case hwmon_temp_label:
353 			return 0444;
354 		default:
355 			break;
356 		}
357 		break;
358 	case hwmon_fan:
359 		if (!test_bit(channel, ccp->fan_cnct))
360 			break;
361 
362 		switch (attr) {
363 		case hwmon_fan_input:
364 			return 0444;
365 		case hwmon_fan_label:
366 			return 0444;
367 		case hwmon_fan_target:
368 			return 0644;
369 		default:
370 			break;
371 		}
372 		break;
373 	case hwmon_pwm:
374 		if (!test_bit(channel, ccp->fan_cnct))
375 			break;
376 
377 		switch (attr) {
378 		case hwmon_pwm_input:
379 			return 0644;
380 		default:
381 			break;
382 		}
383 		break;
384 	case hwmon_in:
385 		switch (attr) {
386 		case hwmon_in_input:
387 			return 0444;
388 		default:
389 			break;
390 		}
391 		break;
392 	default:
393 		break;
394 	}
395 
396 	return 0;
397 };
398 
399 static const struct hwmon_ops ccp_hwmon_ops = {
400 	.is_visible = ccp_is_visible,
401 	.read = ccp_read,
402 	.read_string = ccp_read_string,
403 	.write = ccp_write,
404 };
405 
406 static const struct hwmon_channel_info * const ccp_info[] = {
407 	HWMON_CHANNEL_INFO(chip,
408 			   HWMON_C_REGISTER_TZ),
409 	HWMON_CHANNEL_INFO(temp,
410 			   HWMON_T_INPUT,
411 			   HWMON_T_INPUT,
412 			   HWMON_T_INPUT,
413 			   HWMON_T_INPUT
414 			   ),
415 	HWMON_CHANNEL_INFO(fan,
416 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
417 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
418 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
419 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
420 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
421 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
422 			   ),
423 	HWMON_CHANNEL_INFO(pwm,
424 			   HWMON_PWM_INPUT,
425 			   HWMON_PWM_INPUT,
426 			   HWMON_PWM_INPUT,
427 			   HWMON_PWM_INPUT,
428 			   HWMON_PWM_INPUT,
429 			   HWMON_PWM_INPUT
430 			   ),
431 	HWMON_CHANNEL_INFO(in,
432 			   HWMON_I_INPUT,
433 			   HWMON_I_INPUT,
434 			   HWMON_I_INPUT
435 			   ),
436 	NULL
437 };
438 
439 static const struct hwmon_chip_info ccp_chip_info = {
440 	.ops = &ccp_hwmon_ops,
441 	.info = ccp_info,
442 };
443 
444 /* read fan connection status and set labels */
get_fan_cnct(struct ccp_device * ccp)445 static int get_fan_cnct(struct ccp_device *ccp)
446 {
447 	int channel;
448 	int mode;
449 	int ret;
450 
451 	ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
452 	if (ret)
453 		return ret;
454 
455 	for (channel = 0; channel < NUM_FANS; channel++) {
456 		mode = ccp->buffer[channel + 1];
457 		if (mode == 0)
458 			continue;
459 
460 		set_bit(channel, ccp->fan_cnct);
461 		ccp->target[channel] = -ENODATA;
462 
463 		switch (mode) {
464 		case 1:
465 			scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
466 				  "fan%d 3pin", channel + 1);
467 			break;
468 		case 2:
469 			scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
470 				  "fan%d 4pin", channel + 1);
471 			break;
472 		default:
473 			scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
474 				  "fan%d other", channel + 1);
475 			break;
476 		}
477 	}
478 
479 	return 0;
480 }
481 
482 /* read temp sensor connection status */
get_temp_cnct(struct ccp_device * ccp)483 static int get_temp_cnct(struct ccp_device *ccp)
484 {
485 	int channel;
486 	int mode;
487 	int ret;
488 
489 	ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
490 	if (ret)
491 		return ret;
492 
493 	for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
494 		mode = ccp->buffer[channel + 1];
495 		if (mode == 0)
496 			continue;
497 
498 		set_bit(channel, ccp->temp_cnct);
499 	}
500 
501 	return 0;
502 }
503 
ccp_probe(struct hid_device * hdev,const struct hid_device_id * id)504 static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
505 {
506 	struct ccp_device *ccp;
507 	int ret;
508 
509 	ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
510 	if (!ccp)
511 		return -ENOMEM;
512 
513 	ccp->cmd_buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
514 	if (!ccp->cmd_buffer)
515 		return -ENOMEM;
516 
517 	ccp->buffer = devm_kmalloc(&hdev->dev, IN_BUFFER_SIZE, GFP_KERNEL);
518 	if (!ccp->buffer)
519 		return -ENOMEM;
520 
521 	ret = hid_parse(hdev);
522 	if (ret)
523 		return ret;
524 
525 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
526 	if (ret)
527 		return ret;
528 
529 	ret = hid_hw_open(hdev);
530 	if (ret)
531 		goto out_hw_stop;
532 
533 	ccp->hdev = hdev;
534 	hid_set_drvdata(hdev, ccp);
535 
536 	mutex_init(&ccp->mutex);
537 	spin_lock_init(&ccp->wait_input_report_lock);
538 	init_completion(&ccp->wait_input_report);
539 
540 	hid_device_io_start(hdev);
541 
542 	/* temp and fan connection status only updates when device is powered on */
543 	ret = get_temp_cnct(ccp);
544 	if (ret)
545 		goto out_hw_close;
546 
547 	ret = get_fan_cnct(ccp);
548 	if (ret)
549 		goto out_hw_close;
550 	ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
551 							 ccp, &ccp_chip_info, 0);
552 	if (IS_ERR(ccp->hwmon_dev)) {
553 		ret = PTR_ERR(ccp->hwmon_dev);
554 		goto out_hw_close;
555 	}
556 
557 	return 0;
558 
559 out_hw_close:
560 	hid_hw_close(hdev);
561 out_hw_stop:
562 	hid_hw_stop(hdev);
563 	return ret;
564 }
565 
ccp_remove(struct hid_device * hdev)566 static void ccp_remove(struct hid_device *hdev)
567 {
568 	struct ccp_device *ccp = hid_get_drvdata(hdev);
569 
570 	hwmon_device_unregister(ccp->hwmon_dev);
571 	hid_hw_close(hdev);
572 	hid_hw_stop(hdev);
573 }
574 
575 static const struct hid_device_id ccp_devices[] = {
576 	{ HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
577 	{ HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
578 	{ }
579 };
580 
581 static struct hid_driver ccp_driver = {
582 	.name = "corsair-cpro",
583 	.id_table = ccp_devices,
584 	.probe = ccp_probe,
585 	.remove = ccp_remove,
586 	.raw_event = ccp_raw_event,
587 };
588 
589 MODULE_DEVICE_TABLE(hid, ccp_devices);
590 MODULE_LICENSE("GPL");
591 
ccp_init(void)592 static int __init ccp_init(void)
593 {
594 	return hid_register_driver(&ccp_driver);
595 }
596 
ccp_exit(void)597 static void __exit ccp_exit(void)
598 {
599 	hid_unregister_driver(&ccp_driver);
600 }
601 
602 /*
603  * When compiling this driver as built-in, hwmon initcalls will get called before the
604  * hid driver and this driver would fail to register. late_initcall solves this.
605  */
606 late_initcall(ccp_init);
607 module_exit(ccp_exit);
608