summaryrefslogtreecommitdiff
path: root/drivers/hid/hid-nvidia-blake.c
blob: 9bbc74501d8800d9d2ebf5e19e0b1fea55ebd972 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 * HID driver for NVIDIA Shield Wireless Joystick
 *
 * Copyright (c) 2013-2014, NVIDIA Corporation. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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/device.h>
#include <linux/input.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/kernel.h>

#include "hid-ids.h"

#define JOYSTICK_FUZZ 64
#define TRIGGER_FUZZ 64
#define JOYSTICK_FLAT 64
#define TRIGGER_FLAT 0

#define MAX_CHAR 255

#define TOUCHPAD_DEFAULT_X 128
#define TOUCHPAD_DEFAULT_Y 128
#define TOUCH_HID_REPORT_SIZE 4
#define TOUCH_ACTION_MASK 0x8
#define TOUCH_ACTION_SHFT 3
#define TOUCH_ACTION_UP 0
#define TOUCH_ACTION_DOWN 1
#define TOUCH_REPORT_ID 2

#define MAX_SPEED 10
#define DEFAULT_SPEED 7

#define SCALE_LEN 6

#define MOUSE_MODE_STR "mouse"
#define GESTURE_MODE_STR "gesture"
#define ABSOLUTE_MODE_STR "absolute"
#define DISABLED_MODE_STR "disabled"
#define UNKNOW_MODE_STR "unknown"

#define MAX_REL 255
#define MAX_ABS 65535

#define MAX_DPAD_MOVE 4

#define XSCALE 1
#define YSCALE 0

struct nvidia_tp_loc {
	u8 x;
	u8 y;
	u8 action;
	u8 speed;		/* Not used for now but keep as an parameter */
	u8 mode;		/* Trackpad mode */
	u8 release;
	u8 tp_size;
	struct hid_report *ff_report;
};

enum {
	MOUSE_MODE = 0,
	GESTURE_MODE,
	ABSOLUTE_MODE,
	DISABLED_MODE,
};

/*
 * Scale mapping is a table based on quadratic function
 */
static const s8 blake_touch_scale_table_x[SCALE_LEN] = {
	0, 1, 4, 8, 13, 15};

static const s8 blake_touch_scale_table_y[SCALE_LEN] = {
	0, 3, 10, 17, 22, 25};

static signed short scale_rel(u8 rel, u8 coeff, u8 xy)
{
	s8 s_rel = (s8)rel;
	s8 sign = (s_rel >= 0) ? 1 : -1;
	s8 abs_rel = s_rel * sign;

	if (abs_rel >= SCALE_LEN)
		abs_rel = SCALE_LEN - 1;

	/* If Direction is X or Y */
	if (xy) {
		return (signed short)((s_rel +
			sign*blake_touch_scale_table_x[abs_rel]));
	} else {
		return (signed short)((s_rel +
			sign*blake_touch_scale_table_y[abs_rel]));
	}
}

static __s32 scale_rel_to_abs(__s32 rel)
{
	__s32 val;

	if (rel < 0)
		val = MAX_REL + rel;
	else
		val = rel;

	val = val * MAX_ABS / MAX_REL;
	return val;
}


static __u8 *nv_report_fixup(struct hid_device *hdev, __u8 *rdesc,
		unsigned int *rsize)
{
	/*
	 * This driver converts from absolute to relative, so lets make
	 * it register as a relative mouse
	 */
	if (*rsize == 232 && rdesc[185] == 0x02) {
		rdesc[185] = 0x06;
	}
	return rdesc;
}

static int nvidia_raw_event(struct hid_device *hdev,
		struct hid_report *report, u8 *data, int size) {

	unsigned id;
	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);
	u8 action;
	u16 x, y;
	int press = 0;
	int release = 0;
	signed short relx, rely;
	signed short relx_raw, rely_raw;

	if (!report)
		return 1;
	id = report->id;

	if (!loc)
		return 1;

	/* If not valid touch events, let generic driver to handle this */
	if (id != TOUCH_REPORT_ID)
		return 0;

	/* If driver is in disabled mode,
	 * don't report anything to generic
	 * driver
	 */
	if (loc->mode == DISABLED_MODE)
		return 1;

	action = (data[1] & TOUCH_ACTION_MASK) >> TOUCH_ACTION_SHFT;
	x = data[2];
	y = data[4];



	if (!loc->action && action)
		press = 1;
	else if (loc->action && !action)
		release = 1;
	else if (!loc->action && !action)
		return 1;/* Double release, don't do anything */

	relx_raw = x - loc->x;
	rely_raw = y - loc->y;

	relx = scale_rel(relx_raw, loc->speed, XSCALE);
	rely = scale_rel(rely_raw, (loc->speed), YSCALE);

	loc->action = action;

	dbg_hid("%u %u %u rel %d %d\n", action, x, y, (s8)relx, (s8)rely);

	loc->x = x;
	loc->y = y;
	if (!press) {
		/*
		 * Not a press event, we
		 * need to report it to input subsystem
		 *
		 * If driver is in absolute mode, report
		 * raw absolute data to generic driver
		 *
		 * If driver is in gesture mode, report
		 * raw relative data to generic driver
		 */
		if (loc->mode == ABSOLUTE_MODE) {
			if (loc->tp_size == 8) {
				data[2] = x & 0xff;
				data[3] = y & 0xff;
			}
			/* otherwise we don't need to do anything */
			return 0;
		} else if (loc->mode == GESTURE_MODE) {
			if (loc->tp_size == 8) {
				data[2] = relx_raw & 0xff;
				data[3] = rely_raw & 0xff;
			} else {
				data[2] = relx_raw & 0xff;
				data[3] = relx_raw >> 8;
				data[4] = rely_raw & 0xff;
				data[5] = rely_raw >> 8;
			}
			if (release)
				loc->release = 1;
			else
				loc->release = 0;
			return 0;
		} else {
			if (loc->tp_size == 8) {
				data[2] = relx & 0xff;
				data[3] = rely & 0xff;
			} else {
				data[2] = relx & 0xff;
				data[3] = relx >> 8;
				data[4] = rely & 0xff;
				data[5] = rely >> 8;
			}
			return 0;
		}
	} else {
		/*
		 * if it's a press event,
		 * don't report.
		 */
		return 1;
	}
}

static int nvidia_event(struct hid_device *hdev, struct hid_field *field,
		struct hid_usage *usage, __s32 value) {

	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);
	__u16 keycode = ABS_HAT0X;

	if (!loc)
		return 0;

	/* If not in absolute mode or gesture mode, we pass as it is */
	if (loc->mode != ABSOLUTE_MODE && loc->mode != GESTURE_MODE)
		return 0;

	/* If not mouse event, we pass as it is */
	if (field->physical != HID_GD_MOUSE
			&& field->application != HID_GD_MOUSE)
		return 0;

	/* If not relative event, we pass as it is */
	if (usage->type != EV_REL)
		return 0;

	if (loc->mode == ABSOLUTE_MODE) {
		value = scale_rel_to_abs(value);
		input_event(field->hidinput->input, EV_ABS, usage->code,
				value);
		return 1;
	} else {

		value = (value > 1) ? 1 : ((value < -1) ? -1 : 0);
		if (usage->code == REL_X)
			keycode = ABS_HAT0X;
		else if (usage->code == REL_Y)
			keycode = ABS_HAT0Y;

		if (!loc->release)
			input_event(field->hidinput->input, EV_ABS, keycode,
				value);
		else
			input_event(field->hidinput->input, EV_ABS, keycode,
				0);
		return 1;
	}
}

static ssize_t blake_show_mode(struct device *dev,
		struct device_attribute *attr,
		char *buf) {

	struct hid_device *hdev =
		container_of(dev, struct hid_device, dev);

	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);

	if (!loc)
		return snprintf(buf, MAX_CHAR, UNKNOW_MODE_STR);

	switch (loc->mode) {
	case MOUSE_MODE:
		return snprintf(buf, MAX_CHAR, MOUSE_MODE_STR);
	case GESTURE_MODE:
		return snprintf(buf, MAX_CHAR, GESTURE_MODE_STR);
	case ABSOLUTE_MODE:
		return snprintf(buf, MAX_CHAR, ABSOLUTE_MODE_STR);
	case DISABLED_MODE:
		return snprintf(buf, MAX_CHAR, DISABLED_MODE_STR);
	default:
		return snprintf(buf, MAX_CHAR, UNKNOW_MODE_STR);
	}
}

static ssize_t blake_store_mode(struct device *dev,
	struct device_attribute *attr, const char *buf,
	size_t count) {

	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);
	size_t buflen;
	char blake_mode[MAX_CHAR];

	if (!loc)
		return count;

	blake_mode[sizeof(blake_mode) - 1] = '\0';
	strncpy(blake_mode, buf, sizeof(blake_mode) - 1);
	buflen = strlen(blake_mode);

	if (buflen && blake_mode[buflen - 1] == '\n')
		blake_mode[buflen - 1] = '\0';

	if (!strcmp(blake_mode, MOUSE_MODE_STR))
		loc->mode = MOUSE_MODE;
	else if (!strcmp(blake_mode, GESTURE_MODE_STR))
		loc->mode = GESTURE_MODE;
	else if (!strcmp(blake_mode, ABSOLUTE_MODE_STR))
		loc->mode = ABSOLUTE_MODE;
	else if (!strcmp(blake_mode, DISABLED_MODE_STR))
		loc->mode = DISABLED_MODE;
	return count;
}

static ssize_t blake_show_speed(struct device *dev,
		struct device_attribute *attr,
		char *buf) {

	struct hid_device *hdev =
		container_of(dev, struct hid_device, dev);

	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);

	return snprintf(buf, MAX_CHAR, "%d\n", loc->speed);
}

static ssize_t blake_store_speed(struct device *dev,
	struct device_attribute *attr, const char *buf,
	size_t count) {

	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
	struct nvidia_tp_loc *loc =
		(struct nvidia_tp_loc *)hid_get_drvdata(hdev);
	unsigned long speed_val;

	if (!loc)
		return count;

	if (!kstrtoul(buf, 10, &speed_val)) {
		if (speed_val > MAX_SPEED)
			return count;
		loc->speed = (u8)speed_val;
	}

	return count;
}


static int nvidia_ff_play(struct input_dev *dev, void *data,
			 struct ff_effect *effect)
{
	struct hid_device *hid = input_get_drvdata(dev);
	struct nvidia_tp_loc *loc = data;
	int left, right, length;

	left = effect->u.rumble.strong_magnitude;
	right = effect->u.rumble.weak_magnitude;
	length = effect->replay.length;

	dbg_hid("called with 0x%04x 0x%04x 0x%04x", left, right, length);

	length = length > 10000 ? 10000 : length;

	loc->ff_report->field[0]->value[0] = left;
	loc->ff_report->field[0]->value[1] = right;
	loc->ff_report->field[0]->value[2] = length;

	hid_hw_request(hid, loc->ff_report, HID_REQ_SET_REPORT);

	return 0;
}


static int nvidia_find_tp_len(struct hid_device *hdev, struct nvidia_tp_loc *loc)
{
	struct hid_report *report;
	struct list_head *report_list =
			&hdev->report_enum[HID_INPUT_REPORT].report_list;
	u8 i;

	if (list_empty(report_list)) {
		hid_err(hdev, "no output reports found\n");
		return -ENODEV;
	}

	/* set default */
	loc->tp_size = 8;
	list_for_each_entry(report, report_list, list) {
		if (report->id == TOUCH_REPORT_ID) {
			/* check if we are reporting u8 or u16 for x/y size, need to handle FW change */
			for (i = 0; i < report->maxfield; i++) {
				if (report->field[i]->application == HID_GD_MOUSE) {
					if (report->field[i]->report_size == 16) {
						loc->tp_size = 16;
						break;
					}
				}
			}
		}

	}
	return 0;
}

static int nvidia_init_ff(struct hid_device *hdev, struct nvidia_tp_loc *loc)
{
	struct hid_report *report;
	struct hid_input *hidinput = list_entry(hdev->inputs.next,
						struct hid_input, list);
	struct list_head *report_list =
			&hdev->report_enum[HID_OUTPUT_REPORT].report_list;
	struct input_dev *dev = hidinput->input;
	int error;

	if (list_empty(report_list)) {
		hid_err(hdev, "no output reports found\n");
		return -ENODEV;
	}

	list_for_each_entry(report, report_list, list) {
		if (report->maxfield < 1) {
			hid_warn(hdev, "no fields in the report\n");
			continue;
		}

		if (report->field[0]->application != (HID_UP_CUSTOM | 0x02)) {
			hid_warn(hdev, "application usage doesn't match expected\n");
			continue;
		}

		if (report->field[0]->report_count < 3) {
			hid_warn(hdev, "not enough values in the field\n");
			continue;
		}

		goto found_report;
	}
	hid_warn(hdev, "FF report not found\n");
	loc->ff_report = NULL;
	return -ENODEV;

	found_report:
	loc->ff_report = report;

	set_bit(FF_RUMBLE, dev->ffbit);
	error = input_ff_create_memless(dev, loc, nvidia_ff_play);

	if (error) {
		loc->ff_report = NULL;
		clear_bit(FF_RUMBLE, dev->ffbit);
		hid_warn(hdev, "Couldn't create FF device");
		return error;
	}

	hid_info(hdev, "Force feedback enabled\n");

	return 0;
}

static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR,
	blake_show_speed, blake_store_speed);
static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
	blake_show_mode, blake_store_mode);

static int nvidia_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
	int ret;
	struct nvidia_tp_loc *loc;

	loc = (struct nvidia_tp_loc *)
		kmalloc(sizeof(struct nvidia_tp_loc *), GFP_KERNEL);

	if (!loc) {
		hid_err(hdev, "cannot alloc device touchpad state\n");
		return -ENOMEM;
	}

	loc->x = TOUCHPAD_DEFAULT_X;
	loc->y = TOUCHPAD_DEFAULT_Y;
	loc->action = 0;
	loc->speed = DEFAULT_SPEED;
	loc->mode = MOUSE_MODE;
	hid_set_drvdata(hdev, loc);

	/* Parse the HID report now */
	ret = hid_parse(hdev);
	if (ret) {
		hid_err(hdev, "parse failed\n");
		goto err_parse;
	}

	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
	if (ret)
		goto err_parse;

	ret = device_create_file(&hdev->dev, &dev_attr_speed);
	if (ret)
		hid_warn(hdev, "cannot create sysfs for speed\n");
	ret = device_create_file(&hdev->dev, &dev_attr_mode);
	if (ret)
		hid_warn(hdev, "cannot create sysfs for mode\n");
	return 0;


	nvidia_init_ff(hdev, loc);
	nvidia_find_tp_len(hdev, loc);

err_parse:
	kfree(loc);
	return ret;
}

static void nvidia_remove(struct hid_device *hdev)
{
	struct nvidia_tp_loc *loc = hid_get_drvdata(hdev);

	if (!loc)
		return;

	device_remove_file(&hdev->dev, &dev_attr_speed);
	device_remove_file(&hdev->dev, &dev_attr_mode);

	hid_hw_stop(hdev);
}

static int nvidia_input_mapped(struct hid_device *hdev, struct hid_input *hi,
				  struct hid_field *field, struct hid_usage *usage,
				  unsigned long **bit, int *max)
{
	int a = field->logical_minimum;
	int b = field->logical_maximum;
	int fuzz;
	int flat;

	if ((usage->type == EV_ABS) && (field->application == HID_GD_GAMEPAD
			|| field->application == HID_GD_JOYSTICK)) {
		switch (usage->hid) {
		case HID_GD_X:
		case HID_GD_Y:
		case HID_GD_RX:
		case HID_GD_RY:
			fuzz = JOYSTICK_FUZZ;
			flat = JOYSTICK_FLAT;
			break;
		case HID_GD_Z:
		case HID_GD_RZ:
			fuzz = TRIGGER_FUZZ;
			flat = TRIGGER_FLAT;
			break;
		default: return 0;/*Use generic mapping for HatX, HatY*/
		}
		set_bit(usage->type, hi->input->evbit);
		set_bit(usage->code, *bit);
		input_set_abs_params(hi->input, usage->code, a, b, fuzz, flat);
		input_abs_set_res(hi->input, usage->code,
			hidinput_calc_abs_res(field, usage->code));
		return -1;
	}
	return 0;
}

static const struct hid_device_id nvidia_devices[] = {
	{ HID_USB_DEVICE(USB_VENDOR_ID_NVIDIA, USB_DEVICE_ID_NVIDIA_BLAKE) },
	{ }
};
MODULE_DEVICE_TABLE(hid, nvidia_devices);

static struct hid_driver nvidia_driver = {
	.name = "hid-nvidia-blake",
	.id_table = nvidia_devices,
	.input_mapped = nvidia_input_mapped,
	.raw_event = nvidia_raw_event,
	.report_fixup = nv_report_fixup,
	.event = nvidia_event,
	.probe = nvidia_probe,
	.remove = nvidia_remove,
};
module_hid_driver(nvidia_driver);

MODULE_AUTHOR("Jun Yan <juyan@nvidia.com>");
MODULE_LICENSE("GPL");