25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/input.h>
28 #include <linux/slab.h>
31 #include <linux/hid.h>
35 #define PID_EFFECTS_MAX 64
39 #define PID_SET_EFFECT 0
40 #define PID_EFFECT_OPERATION 1
41 #define PID_DEVICE_GAIN 2
43 #define PID_BLOCK_LOAD 4
44 #define PID_BLOCK_FREE 5
45 #define PID_DEVICE_CONTROL 6
46 #define PID_CREATE_NEW_EFFECT 7
48 #define PID_REQUIRED_REPORTS 7
50 #define PID_SET_ENVELOPE 8
51 #define PID_SET_CONDITION 9
52 #define PID_SET_PERIODIC 10
53 #define PID_SET_CONSTANT 11
54 #define PID_SET_RAMP 12
55 static const u8 pidff_reports[] = {
56 0x21, 0x77, 0x7d, 0x7f, 0x89, 0x90, 0x96, 0xab,
57 0x5a, 0x5f, 0x6e, 0x73, 0x74
65 #define PID_EFFECT_BLOCK_INDEX 0
67 #define PID_DURATION 1
69 #define PID_TRIGGER_BUTTON 3
70 #define PID_TRIGGER_REPEAT_INT 4
71 #define PID_DIRECTION_ENABLE 5
72 #define PID_START_DELAY 6
73 static const u8 pidff_set_effect[] = {
74 0x22, 0x50, 0x52, 0x53, 0x54, 0x56, 0xa7
77 #define PID_ATTACK_LEVEL 1
78 #define PID_ATTACK_TIME 2
79 #define PID_FADE_LEVEL 3
80 #define PID_FADE_TIME 4
81 static const u8 pidff_set_envelope[] = { 0x22, 0x5b, 0x5c, 0x5d, 0x5e };
83 #define PID_PARAM_BLOCK_OFFSET 1
84 #define PID_CP_OFFSET 2
85 #define PID_POS_COEFFICIENT 3
86 #define PID_NEG_COEFFICIENT 4
87 #define PID_POS_SATURATION 5
88 #define PID_NEG_SATURATION 6
89 #define PID_DEAD_BAND 7
90 static const u8 pidff_set_condition[] = {
91 0x22, 0x23, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65
94 #define PID_MAGNITUDE 1
98 static const u8 pidff_set_periodic[] = { 0x22, 0x70, 0x6f, 0x71, 0x72 };
99 static const u8 pidff_set_constant[] = { 0x22, 0x70 };
101 #define PID_RAMP_START 1
102 #define PID_RAMP_END 2
103 static const u8 pidff_set_ramp[] = { 0x22, 0x75, 0x76 };
105 #define PID_RAM_POOL_AVAILABLE 1
106 static const u8 pidff_block_load[] = { 0x22, 0xac };
108 #define PID_LOOP_COUNT 1
109 static const u8 pidff_effect_operation[] = { 0x22, 0x7c };
111 static const u8 pidff_block_free[] = { 0x22 };
113 #define PID_DEVICE_GAIN_FIELD 0
114 static const u8 pidff_device_gain[] = { 0x7e };
116 #define PID_RAM_POOL_SIZE 0
117 #define PID_SIMULTANEOUS_MAX 1
118 #define PID_DEVICE_MANAGED_POOL 2
119 static const u8 pidff_pool[] = { 0x80, 0x83, 0xa9 };
123 #define PID_ENABLE_ACTUATORS 0
125 static const u8 pidff_device_control[] = { 0x97, 0x9a };
127 #define PID_CONSTANT 0
131 #define PID_TRIANGLE 4
133 #define PID_SAW_DOWN 6
136 #define PID_INERTIA 9
137 #define PID_FRICTION 10
138 static const u8 pidff_effect_types[] = {
139 0x26, 0x27, 0x30, 0x31, 0x32, 0x33, 0x34,
140 0x40, 0x41, 0x42, 0x43
143 #define PID_BLOCK_LOAD_SUCCESS 0
144 #define PID_BLOCK_LOAD_FULL 1
145 static const u8 pidff_block_load_status[] = { 0x8c, 0x8d };
147 #define PID_EFFECT_START 0
148 #define PID_EFFECT_STOP 1
149 static const u8 pidff_effect_operation_status[] = { 0x79, 0x7b };
215 return i == 0 ? 0 : i >
222 usage->
value[0] = pidff_rescale(value, 0xffff, usage->
field);
223 pr_debug(
"calculated from %d to %d\n", value, usage->
value[0]);
228 if (usage->
field->logical_minimum < 0)
229 usage->
value[0] = pidff_rescale_signed(value, usage->
field);
233 pidff_rescale(-value, 0x8000, usage->
field);
236 pidff_rescale(value, 0x7fff, usage->
field);
238 pr_debug(
"calculated from %d to %d\n", value, usage->
value[0]);
244 static void pidff_set_envelope_report(
struct pidff_device *pidff,
256 0x7fff ? 0x7fff : envelope->
fade_level, 0x7fff,
273 static int pidff_needs_set_envelope(
struct ff_envelope *envelope,
285 static void pidff_set_constant_force_report(
struct pidff_device *pidff,
300 static int pidff_needs_set_constant(
struct ff_effect *effect,
309 static void pidff_set_effect_report(
struct pidff_device *pidff,
335 static int pidff_needs_set_effect(
struct ff_effect *effect,
348 static void pidff_set_periodic_report(
struct pidff_device *pidff,
368 static int pidff_needs_set_periodic(
struct ff_effect *effect,
380 static void pidff_set_condition_report(
struct pidff_device *pidff,
388 for (i = 0; i < 2; i++) {
410 static int pidff_needs_set_condition(
struct ff_effect *effect,
416 for (i = 0; i < 2; i++) {
434 static void pidff_set_ramp_force_report(
struct pidff_device *pidff,
440 effect->
u.
ramp.start_level);
442 effect->
u.
ramp.end_level);
452 return effect->
u.
ramp.start_level != old->
u.
ramp.start_level ||
453 effect->
u.
ramp.end_level != old->
u.
ramp.end_level;
463 static int pidff_request_effect_upload(
struct pidff_device *pidff,
int efnum)
470 hid_dbg(pidff->
hid,
"create_new_effect sent, type: %d\n", efnum);
476 for (j = 0; j < 60; j++) {
477 hid_dbg(pidff->
hid,
"pid_block_load requested\n");
483 hid_dbg(pidff->
hid,
"device reported free memory: %d bytes\n",
490 hid_dbg(pidff->
hid,
"not enough memory free: %d bytes\n",
496 hid_err(pidff->
hid,
"pid_block_load failed 60 times\n");
503 static void pidff_playback_pid(
struct pidff_device *pidff,
int pid_id,
int n)
523 static int pidff_playback(
struct input_dev *
dev,
int effect_id,
int value)
527 pidff_playback_pid(pidff, pidff->
pid_id[effect_id], value);
535 static void pidff_erase_pid(
struct pidff_device *pidff,
int pid_id)
545 static int pidff_erase_effect(
struct input_dev *
dev,
int effect_id)
548 int pid_id = pidff->
pid_id[effect_id];
550 hid_dbg(pidff->
hid,
"starting to erase %d/%d\n",
551 effect_id, pidff->
pid_id[effect_id]);
555 pidff_playback_pid(pidff, pid_id, 0);
556 pidff_erase_pid(pidff, pid_id);
564 static int pidff_upload_effect(
struct input_dev *
dev,
struct ff_effect *effect,
571 switch (effect->
type) {
574 error = pidff_request_effect_upload(pidff,
579 if (!old || pidff_needs_set_effect(effect, old))
580 pidff_set_effect_report(pidff, effect);
581 if (!old || pidff_needs_set_constant(effect, old))
582 pidff_set_constant_force_report(pidff, effect);
584 pidff_needs_set_envelope(&effect->
u.
constant.envelope,
586 pidff_set_envelope_report(pidff,
613 error = pidff_request_effect_upload(pidff,
618 if (!old || pidff_needs_set_effect(effect, old))
619 pidff_set_effect_report(pidff, effect);
620 if (!old || pidff_needs_set_periodic(effect, old))
621 pidff_set_periodic_report(pidff, effect);
623 pidff_needs_set_envelope(&effect->
u.
periodic.envelope,
625 pidff_set_envelope_report(pidff,
631 error = pidff_request_effect_upload(pidff,
636 if (!old || pidff_needs_set_effect(effect, old))
637 pidff_set_effect_report(pidff, effect);
638 if (!old || pidff_needs_set_ramp(effect, old))
639 pidff_set_ramp_force_report(pidff, effect);
641 pidff_needs_set_envelope(&effect->
u.
ramp.envelope,
642 &old->
u.
ramp.envelope))
643 pidff_set_envelope_report(pidff,
644 &effect->
u.
ramp.envelope);
649 error = pidff_request_effect_upload(pidff,
654 if (!old || pidff_needs_set_effect(effect, old))
655 pidff_set_effect_report(pidff, effect);
656 if (!old || pidff_needs_set_condition(effect, old))
657 pidff_set_condition_report(pidff, effect);
662 error = pidff_request_effect_upload(pidff,
667 if (!old || pidff_needs_set_effect(effect, old))
668 pidff_set_effect_report(pidff, effect);
669 if (!old || pidff_needs_set_condition(effect, old))
670 pidff_set_condition_report(pidff, effect);
675 error = pidff_request_effect_upload(pidff,
680 if (!old || pidff_needs_set_effect(effect, old))
681 pidff_set_effect_report(pidff, effect);
682 if (!old || pidff_needs_set_condition(effect, old))
683 pidff_set_condition_report(pidff, effect);
688 error = pidff_request_effect_upload(pidff,
693 if (!old || pidff_needs_set_effect(effect, old))
694 pidff_set_effect_report(pidff, effect);
695 if (!old || pidff_needs_set_condition(effect, old))
696 pidff_set_condition_report(pidff, effect);
716 static void pidff_set_gain(
struct input_dev *dev,
u16 gain)
725 static void pidff_autocenter(
struct pidff_device *pidff,
u16 magnitude)
754 static void pidff_set_autocenter(
struct input_dev *dev,
u16 magnitude)
758 pidff_autocenter(pidff, magnitude);
769 for (k = 0; k <
count; k++) {
771 for (i = 0; i < report->
maxfield; i++) {
772 if (report->
field[i]->maxusage !=
773 report->
field[i]->report_count) {
774 pr_debug(
"maxusage and report_count do not match, skipping\n");
777 for (j = 0; j < report->
field[
i]->maxusage; j++) {
778 if (report->
field[i]->usage[j].hid ==
792 if (!found && strict) {
793 pr_debug(
"failed to locate %d\n", k);
803 static int pidff_check_usage(
int usage)
807 for (i = 0; i <
sizeof(pidff_reports); i++)
818 static void pidff_find_reports(
struct hid_device *
hid,
int report_type,
828 ret = pidff_check_usage(report->
field[0]->logical);
830 hid_dbg(hid,
"found usage 0x%02x from field->logical\n",
843 i = report->
field[0]->usage[0].collection_index;
847 ret = pidff_check_usage(hid->
collection[i - 1].usage);
848 if (ret != -1 && !pidff->
reports[ret]) {
850 "found usage 0x%02x from collection array\n",
878 int usage,
int enforce_min)
882 for (i = 0; i < report->
maxfield; i++) {
884 report->
field[i]->report_count > 0) {
886 report->
field[i]->logical_minimum == 1)
889 pr_err(
"logical_minimum is not 1 as it should be\n");
900 static int pidff_find_special_keys(
int *keys,
struct hid_field *fld,
901 const u8 *usagetable,
int count)
907 for (i = 0; i <
count; i++) {
908 for (j = 0; j < fld->
maxusage; j++) {
919 #define PIDFF_FIND_SPECIAL_KEYS(keys, field, name) \
920 pidff_find_special_keys(pidff->keys, pidff->field, pidff_ ## name, \
921 sizeof(pidff_ ## name))
926 static int pidff_find_special_fields(
struct pidff_device *pidff)
928 hid_dbg(pidff->
hid,
"finding special fields\n");
952 hid_err(pidff->
hid,
"effect lists not found\n");
957 hid_err(pidff->
hid,
"direction field not found\n");
962 hid_err(pidff->
hid,
"device control field not found\n");
967 hid_err(pidff->
hid,
"block load status field not found\n");
972 hid_err(pidff->
hid,
"effect operation field not found\n");
977 pidff_device_control,
978 sizeof(pidff_device_control));
984 hid_err(pidff->
hid,
"no effect types found\n");
989 block_load_status) !=
990 sizeof(pidff_block_load_status)) {
992 "block load status identifiers not found\n");
997 effect_operation_status) !=
998 sizeof(pidff_effect_operation_status)) {
999 hid_err(pidff->
hid,
"effect operation identifiers not found\n");
1009 static int pidff_find_effects(
struct pidff_device *pidff,
1010 struct input_dev *dev)
1014 for (i = 0; i <
sizeof(pidff_effect_types); i++) {
1015 int pidff_type = pidff->
type_id[
i];
1019 "effect type number %d is invalid\n", i);
1061 #define PIDFF_FIND_FIELDS(name, report, strict) \
1062 pidff_find_fields(pidff->name, pidff_ ## name, \
1063 pidff->reports[report], \
1064 sizeof(pidff_ ## name), strict)
1069 static int pidff_init_fields(
struct pidff_device *pidff,
struct input_dev *dev)
1071 int envelope_ok = 0;
1074 hid_err(pidff->
hid,
"unknown set_effect report layout\n");
1080 hid_err(pidff->
hid,
"unknown pid_block_load report layout\n");
1085 hid_err(pidff->
hid,
"unknown effect_operation report layout\n");
1090 hid_err(pidff->
hid,
"unknown pid_block_free report layout\n");
1097 if (pidff_find_special_fields(pidff) || pidff_find_effects(pidff, dev))
1103 "has constant effect but no envelope\n");
1106 "has ramp effect but no envelope\n");
1110 "has periodic effect but no envelope\n");
1115 hid_warn(pidff->
hid,
"unknown constant effect layout\n");
1121 hid_warn(pidff->
hid,
"unknown ramp effect layout\n");
1130 hid_warn(pidff->
hid,
"unknown condition effect layout\n");
1139 hid_warn(pidff->
hid,
"unknown periodic effect layout\n");
1179 "device reports %d simultaneous effects\n",
1183 hid_dbg(pidff->
hid,
"pid_pool requested again\n");
1194 static int pidff_check_autocenter(
struct pidff_device *pidff,
1195 struct input_dev *dev)
1207 error = pidff_request_effect_upload(pidff, 1);
1209 hid_err(pidff->
hid,
"upload request failed\n");
1215 pidff_autocenter(pidff, 0xffff);
1219 "device has unknown autocenter control method\n");
1222 pidff_erase_pid(pidff,
1237 struct input_dev *dev = hidinput->
input;
1238 struct ff_device *ff;
1242 hid_dbg(hid,
"starting pid init\n");
1245 hid_dbg(hid,
"not a PID device, no output report\n");
1258 if (!pidff_reports_ok(pidff)) {
1259 hid_dbg(hid,
"reports not ok, aborting\n");
1264 error = pidff_init_fields(pidff, dev);
1276 error = pidff_check_autocenter(pidff, dev);
1284 hid_dbg(hid,
"max effects is %d\n", max_effects);
1290 hid_dbg(hid,
"max simultaneous effects is %d\n",
1294 hid_dbg(hid,
"device memory size is %d bytes\n",
1300 "device does not support device managed pool\n");
1304 error = input_ff_create(dev, max_effects);
1309 ff->private = pidff;
1310 ff->upload = pidff_upload_effect;
1311 ff->erase = pidff_erase_effect;
1312 ff->set_gain = pidff_set_gain;
1313 ff->set_autocenter = pidff_set_autocenter;
1314 ff->playback = pidff_playback;