-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-joypad.c
1592 lines (1319 loc) · 41.2 KB
/
open-joypad.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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Open Joypad Driver
*
* Supports:
* 1) No ADC mode (GPIO-only)
* 2) Multi-ADC mode (multiple iio channels)
* 3) Single-ADC + analog multiplexer mode
* 4) Miyoo serial mode
*
* The device tree property "joypad-mode" must be one of:
* - "none" -> No ADC, only GPIO
* - "multiadc" -> Multi-ADC approach
* - "singleadc" -> Single-ADC with analog multiplexer
* - "miyooserial" -> Miyoo serial-based joystick input
*
* Example usage in device tree is provided at the end of this file.
*/
#include <linux/module.h>
#include <linux/input-polldev.h>
#include <linux/platform_device.h>
#include <linux/iio/consumer.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0))
#include <linux/of_gpio.h>
#else
#include <linux/of_gpio_legacy.h>
#endif
#include <linux/pwm.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/fs.h>
#include <linux/termios.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <linux/math64.h>
#include <linux/pm.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
/* Constants for IIO channels and typical voltage scaling. */
#define SARADC_CH_NUM 8
#define ADC_MAX_VOLTAGE 1800
#define ADC_TUNING_DEFAULT 180
#define DRV_NAME "open-joypad"
/* Macros to clamp and do percentage-based tuning. */
#define CLAMP(x, low, high) \
(((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define ADC_DATA_TUNING(x, p) ((x * p) / 100)
/* Miyoo-specific definitions. */
#define MIYOO_FRAME_SIZE 6
#define MIYOO_MAGIC_START 0xFF
#define MIYOO_MAGIC_END 0xFE
#define MIYOO_AXIS_MIN (-32760)
#define MIYOO_AXIS_MAX ( 32760)
#define MIYOO_CALIBRATION_FRAMES 50
#define MIYOO_SERIAL_INACTIVITY_TIMEOUT (5 * HZ)
/* Driver operating modes */
enum joypad_mode {
JOYPAD_MODE_NONE = 0,
JOYPAD_MODE_MULTIADC,
JOYPAD_MODE_SINGLEADC,
JOYPAD_MODE_MIYOOSERIAL,
};
/* Per-channel config for multi-ADC mode. */
struct bt_adc {
int value;
int report_type;
int max;
int min;
int cal;
int scale;
bool invert;
int channel;
int tuning_p;
int tuning_n;
};
/* Per-button GPIO config. */
struct bt_gpio {
const char *label;
int num;
int report_type;
int linux_code;
bool old_value;
bool active_level;
};
/* Single-ADC approach: analog multiplexer lines. */
struct analog_mux {
struct iio_channel *iio_ch;
int sel_a_gpio;
int sel_b_gpio;
int en_gpio;
};
/* Per-channel config for single-ADC approach. */
struct bt_adc_single {
int value;
int report_type;
int max;
int min;
int cal;
int scale;
bool invert;
int amux_ch;
int tuning_p;
int tuning_n;
};
/* Miyoo joystick calibration info. */
struct miyoo_cal {
int x_min;
int x_max;
int x_zero;
int y_min;
int y_max;
int y_zero;
};
/* Miyoo two-stick container. */
struct miyoo_serial_stick {
struct miyoo_cal left_cal;
struct miyoo_cal right_cal;
int left_x;
int left_y;
int right_x;
int right_y;
};
/* Main driver private data. */
struct joypad {
struct device *dev;
enum joypad_mode mode;
int poll_interval;
bool enable;
/* GPIO buttons */
int bt_gpio_count;
struct bt_gpio *gpios;
bool auto_repeat;
/* ADC / analog input common settings */
int bt_adc_fuzz;
int bt_adc_flat;
int bt_adc_scale;
int bt_adc_deadzone;
/* Inversion flags */
bool invert_absx;
bool invert_absy;
bool invert_absz;
bool invert_absrx;
bool invert_absry;
bool invert_absrz;
struct mutex lock;
struct input_polled_dev *poll_dev;
struct input_dev *input;
/* Multi-ADC fields */
struct iio_channel *adc_ch[SARADC_CH_NUM];
int chan_count;
struct bt_adc *adcs;
/* Single-ADC multiplexer fields */
struct analog_mux *amux;
int amux_count;
struct bt_adc_single *adcs_single;
/* Rumble (PWM) fields */
struct pwm_device *pwm;
struct work_struct play_work;
u16 level;
u16 boost_weak;
u16 boost_strong;
bool has_rumble;
bool rumble_enabled;
/* Miyoo serial fields */
struct miyoo_serial_stick miyoo;
struct task_struct *miyoo_thread;
struct file *miyoo_filp;
struct delayed_work miyoo_init_work;
unsigned long last_data_jiffies;
};
/* Global reference if needed externally. */
extern struct input_dev *joypad_input_g;
struct input_dev *joypad_input_g;
/* -------------------------------------------------------------------------- */
/* Rumble (PWM) Support */
/* -------------------------------------------------------------------------- */
static int pwm_vibrator_start(struct joypad *joypad)
{
struct pwm_state state;
int err;
pwm_get_state(joypad->pwm, &state);
pwm_set_relative_duty_cycle(&state, joypad->level, 0xffff);
state.enabled = true;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 11, 0))
err = pwm_apply_state(joypad->pwm, &state);
#else
err = pwm_apply_might_sleep(joypad->pwm, &state);
#endif
if (err)
dev_err(joypad->dev, "failed to apply PWM state: %d\n", err);
return err;
}
static void pwm_vibrator_stop(struct joypad *joypad)
{
pwm_disable(joypad->pwm);
}
static void pwm_vibrator_play_work(struct work_struct *work)
{
struct joypad *joypad = container_of(work, struct joypad, play_work);
mutex_lock(&joypad->lock);
if (!joypad->rumble_enabled) {
pwm_vibrator_stop(joypad);
mutex_unlock(&joypad->lock);
return;
}
if (joypad->level)
pwm_vibrator_start(joypad);
else
pwm_vibrator_stop(joypad);
mutex_unlock(&joypad->lock);
}
/* Force-Feedback (rumble) callback */
static int rumble_play_effect(struct input_dev *dev,
void *data, struct ff_effect *effect)
{
struct joypad *joypad = data;
u32 boosted_level;
if (effect->type != FF_RUMBLE)
return 0;
mutex_lock(&joypad->lock);
if (!joypad->rumble_enabled) {
mutex_unlock(&joypad->lock);
return 0;
}
if (effect->u.rumble.strong_magnitude)
boosted_level = effect->u.rumble.strong_magnitude + joypad->boost_strong;
else
boosted_level = effect->u.rumble.weak_magnitude + joypad->boost_weak;
joypad->level = (u16)CLAMP(boosted_level, 0, 0xffff);
mutex_unlock(&joypad->lock);
schedule_work(&joypad->play_work);
return 0;
}
/* Sysfs toggling for rumble */
static ssize_t joypad_store_rumble_enable(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
bool enable = simple_strtoul(buf, NULL, 10);
mutex_lock(&joypad->lock);
if (enable && !joypad->rumble_enabled) {
joypad->rumble_enabled = true;
} else if (!enable && joypad->rumble_enabled) {
joypad->rumble_enabled = false;
joypad->level = 0;
cancel_work_sync(&joypad->play_work);
pwm_vibrator_stop(joypad);
}
mutex_unlock(&joypad->lock);
return count;
}
static ssize_t joypad_show_rumble_enable(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", joypad->rumble_enabled ? 1 : 0);
}
static DEVICE_ATTR(rumble_enable, S_IWUSR | S_IRUGO,
joypad_show_rumble_enable,
joypad_store_rumble_enable);
static struct attribute *joypad_rumble_attrs[] = {
&dev_attr_rumble_enable.attr,
NULL,
};
static struct attribute_group joypad_rumble_attr_group = {
.attrs = joypad_rumble_attrs,
};
/* -------------------------------------------------------------------------- */
/* GPIO (Digital) Input Checks */
/* -------------------------------------------------------------------------- */
static void joypad_gpio_check(struct joypad *joypad)
{
int i;
for (i = 0; i < joypad->bt_gpio_count; i++) {
struct bt_gpio *gpio = &joypad->gpios[i];
int value;
if (gpio_get_value_cansleep(gpio->num) < 0) {
dev_err(joypad->dev, "failed to get gpio state\n");
continue;
}
value = gpio_get_value(gpio->num);
if (value != gpio->old_value) {
input_event(joypad->poll_dev->input,
gpio->report_type,
gpio->linux_code,
(value == gpio->active_level) ? 1 : 0);
gpio->old_value = value;
}
}
input_sync(joypad->poll_dev->input);
}
/* -------------------------------------------------------------------------- */
/* Multi-ADC Reading & Handling */
/* -------------------------------------------------------------------------- */
static int joypad_adc_read_multi(struct joypad *joypad, struct bt_adc *adc)
{
int value;
if (iio_read_channel_processed(joypad->adc_ch[adc->channel], &value) < 0)
return 0;
value *= adc->scale;
return value;
}
static void joypad_adc_check_multiadc(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int i;
/* We assume each pair is (X, Y) or (RX, RY), etc. */
for (i = 0; i < joypad->chan_count; i += 2) {
int mag;
struct bt_adc *adcx = &joypad->adcs[i];
struct bt_adc *adcy = &joypad->adcs[i + 1];
adcx->value = joypad_adc_read_multi(joypad, adcx);
adcy->value = joypad_adc_read_multi(joypad, adcy);
adcx->value -= adcx->cal;
adcy->value -= adcy->cal;
/* Apply radial deadzone if needed. */
mag = int_sqrt((adcx->value * (long)adcx->value)
+ (adcy->value * (long)adcy->value));
if (joypad->bt_adc_deadzone) {
if (mag <= joypad->bt_adc_deadzone) {
adcx->value = 0;
adcy->value = 0;
} else {
adcx->value = (((adcx->max * adcx->value) / mag)
* (mag - joypad->bt_adc_deadzone))
/ (adcx->max - joypad->bt_adc_deadzone);
adcy->value = (((adcy->max * adcy->value) / mag)
* (mag - joypad->bt_adc_deadzone))
/ (adcy->max - joypad->bt_adc_deadzone);
}
}
/* Per-axis tuning (positive/negative) */
if (adcx->tuning_n && adcx->value < 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_n);
if (adcx->tuning_p && adcx->value > 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_p);
if (adcy->tuning_n && adcy->value < 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_n);
if (adcy->tuning_p && adcy->value > 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_p);
/* Clamp and possibly invert. */
adcx->value = CLAMP(adcx->value, adcx->min, adcx->max);
adcy->value = CLAMP(adcy->value, adcy->min, adcy->max);
input_report_abs(poll_dev->input, adcx->report_type,
adcx->invert ? -adcx->value : adcx->value);
input_report_abs(poll_dev->input, adcy->report_type,
adcy->invert ? -adcy->value : adcy->value);
}
input_sync(poll_dev->input);
}
/* -------------------------------------------------------------------------- */
/* Single-ADC (Multiplexer) Handling */
/* -------------------------------------------------------------------------- */
static int joypad_amux_select(struct analog_mux *amux, int channel)
{
/* This function sets SEL_A / SEL_B lines, and optionally EN. */
gpio_set_value(amux->en_gpio, 0);
switch (channel) {
case 0:
gpio_set_value(amux->sel_a_gpio, 0);
gpio_set_value(amux->sel_b_gpio, 0);
break;
case 1:
gpio_set_value(amux->sel_a_gpio, 0);
gpio_set_value(amux->sel_b_gpio, 1);
break;
case 2:
gpio_set_value(amux->sel_a_gpio, 1);
gpio_set_value(amux->sel_b_gpio, 0);
break;
case 3:
gpio_set_value(amux->sel_a_gpio, 1);
gpio_set_value(amux->sel_b_gpio, 1);
break;
default:
/* Disable if an invalid channel is selected. */
gpio_set_value(amux->en_gpio, 1);
return -EINVAL;
}
usleep_range(10, 20);
return 0;
}
static int joypad_adc_read_single(struct analog_mux *amux, struct bt_adc_single *adc)
{
int value, ret;
ret = joypad_amux_select(amux, adc->amux_ch);
if (ret)
return 0;
/* For single ADC, we read raw. Optionally .processed could be used. */
iio_read_channel_raw(amux->iio_ch, &value);
value *= adc->scale;
return value;
}
static void joypad_adc_check_singleadc(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int i;
/* Similarly, we assume pairs of channels (X,Y) or (RY,RX)... */
for (i = 0; i < joypad->amux_count; i += 2) {
int mag;
struct bt_adc_single *adcx = &joypad->adcs_single[i];
struct bt_adc_single *adcy = &joypad->adcs_single[i + 1];
adcx->value = joypad_adc_read_single(joypad->amux, adcx) - adcx->cal;
adcy->value = joypad_adc_read_single(joypad->amux, adcy) - adcy->cal;
mag = int_sqrt((adcx->value * (long)adcx->value)
+ (adcy->value * (long)adcy->value));
if (joypad->bt_adc_deadzone) {
if (mag <= joypad->bt_adc_deadzone) {
adcx->value = 0;
adcy->value = 0;
} else {
adcx->value = (((adcx->max * adcx->value) / mag)
* (mag - joypad->bt_adc_deadzone))
/ (adcx->max - joypad->bt_adc_deadzone);
adcy->value = (((adcy->max * adcy->value) / mag)
* (mag - joypad->bt_adc_deadzone))
/ (adcy->max - joypad->bt_adc_deadzone);
}
}
/* Per-axis tuning (positive/negative) */
if (adcx->tuning_n && adcx->value < 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_n);
if (adcx->tuning_p && adcx->value > 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_p);
if (adcy->tuning_n && adcy->value < 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_n);
if (adcy->tuning_p && adcy->value > 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_p);
adcx->value = CLAMP(adcx->value, adcx->min, adcx->max);
adcy->value = CLAMP(adcy->value, adcy->min, adcy->max);
input_report_abs(poll_dev->input, adcx->report_type,
adcx->invert ? -adcx->value : adcx->value);
input_report_abs(poll_dev->input, adcy->report_type,
adcy->invert ? -adcy->value : adcy->value);
}
input_sync(poll_dev->input);
}
/* -------------------------------------------------------------------------- */
/* Miyoo Serial Thread & Helpers */
/* -------------------------------------------------------------------------- */
static int miyoo_calibrate_axis_x(const struct miyoo_cal *cal, int raw)
{
int rangePos = cal->x_max - cal->x_zero;
int rangeNeg = cal->x_zero - cal->x_min;
int result = 0;
if (raw > cal->x_zero) {
int diff = raw - cal->x_zero;
if (rangePos != 0) {
result = (diff * MIYOO_AXIS_MAX) / rangePos;
if (result > MIYOO_AXIS_MAX)
result = MIYOO_AXIS_MAX;
}
} else if (raw < cal->x_zero) {
int diff = cal->x_zero - raw;
if (rangeNeg != 0) {
result = -((diff * -MIYOO_AXIS_MIN) / rangeNeg);
if (result < MIYOO_AXIS_MIN)
result = MIYOO_AXIS_MIN;
}
}
return result;
}
static int miyoo_calibrate_axis_y(const struct miyoo_cal *cal, int raw)
{
int rangePos = cal->y_max - cal->y_zero;
int rangeNeg = cal->y_zero - cal->y_min;
int result = 0;
if (raw > cal->y_zero) {
int diff = raw - cal->y_zero;
if (rangePos != 0) {
result = (diff * MIYOO_AXIS_MAX) / rangePos;
if (result > MIYOO_AXIS_MAX)
result = MIYOO_AXIS_MAX;
}
} else if (raw < cal->y_zero) {
int diff = cal->y_zero - raw;
if (rangeNeg != 0) {
result = -((diff * -MIYOO_AXIS_MIN) / rangeNeg);
if (result < MIYOO_AXIS_MIN)
result = MIYOO_AXIS_MIN;
}
}
return result;
}
static void miyoo_apply_deadzone(int *px, int *py)
{
long long x = *px, y = *py;
long long magSq = x * x + y * y;
long long dead = (long long)(0.10f * MIYOO_AXIS_MAX); /* 10% deadzone */
long long deadSq = dead * dead;
if (magSq <= deadSq) {
*px = 0;
*py = 0;
}
}
/* Quickly auto-calibrate neutral from multiple frames. */
static int miyoo_autocal(struct joypad *joypad)
{
long sumYL = 0, sumXL = 0, sumYR = 0, sumXR = 0;
int framesRead = 0;
unsigned char buf[MIYOO_FRAME_SIZE];
while (framesRead < MIYOO_CALIBRATION_FRAMES) {
ssize_t n = kernel_read(joypad->miyoo_filp, buf, MIYOO_FRAME_SIZE,
&joypad->miyoo_filp->f_pos);
if (n < 0) {
dev_err(joypad->dev, "Auto-cal read error: %zd\n", n);
msleep(10);
continue;
}
if (n < MIYOO_FRAME_SIZE) {
msleep(10);
continue;
}
if (buf[0] == MIYOO_MAGIC_START && buf[5] == MIYOO_MAGIC_END) {
sumYL += buf[1];
sumXL += buf[2];
sumYR += buf[3];
sumXR += buf[4];
framesRead++;
}
}
if (!framesRead) {
dev_err(joypad->dev, "No frames read for calibration!\n");
return -EIO;
}
joypad->miyoo.left_cal.x_zero = sumXL / framesRead;
joypad->miyoo.left_cal.y_zero = sumYL / framesRead;
joypad->miyoo.right_cal.x_zero = sumXR / framesRead;
joypad->miyoo.right_cal.y_zero = sumYR / framesRead;
return 0;
}
/* Frame parser for the Miyoo data. */
static void miyoo_parse_frame(struct joypad *joypad, unsigned char *frame)
{
if (frame[0] != MIYOO_MAGIC_START || frame[5] != MIYOO_MAGIC_END)
return;
joypad->last_data_jiffies = jiffies;
{
int rawYL = frame[1];
int rawXL = frame[2];
int rawYR = frame[3];
int rawXR = frame[4];
int lx = miyoo_calibrate_axis_x(&joypad->miyoo.left_cal, rawXL);
int ly = miyoo_calibrate_axis_y(&joypad->miyoo.left_cal, rawYL);
int rx = miyoo_calibrate_axis_x(&joypad->miyoo.right_cal, rawXR);
int ry = miyoo_calibrate_axis_y(&joypad->miyoo.right_cal, rawYR);
miyoo_apply_deadzone(&lx, &ly);
miyoo_apply_deadzone(&rx, &ry);
joypad->miyoo.left_x = lx;
joypad->miyoo.left_y = ly;
joypad->miyoo.right_x = rx;
joypad->miyoo.right_y = ry;
}
}
static void set_tty_9600_8N1(struct file *filp)
{
struct tty_file_private *file_priv;
struct tty_struct *tty;
struct ktermios newt;
if (!filp || !filp->private_data)
return;
file_priv = filp->private_data;
if (!file_priv || !file_priv->tty)
return;
tty = file_priv->tty;
newt = tty->termios;
newt.c_iflag = 0;
newt.c_oflag = 0;
newt.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
newt.c_lflag = 0;
memset(newt.c_cc, 0, NCCS);
newt.c_cc[VMIN] = 1;
newt.c_cc[VTIME] = 0;
tty_set_termios(tty, &newt);
}
static int miyoo_serial_open(struct joypad *joypad)
{
struct file *filp;
filp = filp_open("/dev/ttyS1", O_RDWR | O_NOCTTY, 0);
if (IS_ERR(filp)) {
dev_err(joypad->dev, "ERROR: open /dev/ttyS1 failed: %ld\n", PTR_ERR(filp));
return PTR_ERR(filp);
}
joypad->miyoo_filp = filp;
set_tty_9600_8N1(filp);
return 0;
}
static void miyoo_serial_close(struct joypad *joypad)
{
if (joypad->miyoo_filp) {
filp_close(joypad->miyoo_filp, NULL);
joypad->miyoo_filp = NULL;
}
}
static int miyoo_serial_threadfn(void *data)
{
struct joypad *joypad = data;
while (!kthread_should_stop()) {
unsigned char frame[MIYOO_FRAME_SIZE];
ssize_t n;
if (!joypad->miyoo_filp) {
msleep(100);
continue;
}
n = kernel_read(joypad->miyoo_filp, frame, MIYOO_FRAME_SIZE,
&joypad->miyoo_filp->f_pos);
if (n < 0) {
dev_err(joypad->dev, "Serial read error: %zd\n", n);
msleep(10);
continue;
}
if (n < MIYOO_FRAME_SIZE) {
msleep(10);
continue;
}
miyoo_parse_frame(joypad, frame);
/* Re-open TTY if no data for 5 seconds. */
if (time_after(jiffies, joypad->last_data_jiffies + MIYOO_SERIAL_INACTIVITY_TIMEOUT)) {
dev_warn(joypad->dev, "No data for 5s; re-opening TTY.\n");
miyoo_serial_close(joypad);
if (!miyoo_serial_open(joypad))
joypad->last_data_jiffies = jiffies;
else
dev_err(joypad->dev, "Failed to re-open /dev/ttyS1.\n");
}
}
return 0;
}
static int miyoo_start_serial(struct joypad *joypad)
{
int ret;
ret = miyoo_serial_open(joypad);
if (ret < 0)
return ret;
ret = miyoo_autocal(joypad);
if (ret < 0)
dev_err(joypad->dev, "Auto-calibration failed: %d\n", ret);
joypad->miyoo_thread = kthread_run(miyoo_serial_threadfn, joypad,
"miyoo-serial-thread");
if (IS_ERR(joypad->miyoo_thread)) {
miyoo_serial_close(joypad);
return PTR_ERR(joypad->miyoo_thread);
}
joypad->last_data_jiffies = jiffies;
return 0;
}
static void miyoo_delayed_init_workfn(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct joypad *joypad = container_of(dwork, struct joypad, miyoo_init_work);
miyoo_start_serial(joypad);
}
/* -------------------------------------------------------------------------- */
/* Input Device Callbacks */
/* -------------------------------------------------------------------------- */
static void joypad_poll(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
if (!joypad->enable)
goto update_interval;
switch (joypad->mode) {
case JOYPAD_MODE_MULTIADC:
joypad_adc_check_multiadc(poll_dev);
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_SINGLEADC:
joypad_adc_check_singleadc(poll_dev);
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_MIYOOSERIAL:
/* Report the last known values from the Miyoo thread. */
input_report_abs(poll_dev->input, ABS_X, joypad->miyoo.left_x);
input_report_abs(poll_dev->input, ABS_Y, joypad->miyoo.left_y);
input_report_abs(poll_dev->input, ABS_RX, joypad->miyoo.right_x);
input_report_abs(poll_dev->input, ABS_RY, joypad->miyoo.right_y);
input_sync(poll_dev->input);
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_NONE:
default:
joypad_gpio_check(joypad);
break;
}
update_interval:
if (poll_dev->poll_interval != joypad->poll_interval) {
mutex_lock(&joypad->lock);
poll_dev->poll_interval = joypad->poll_interval;
mutex_unlock(&joypad->lock);
}
}
static void joypad_open(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int i;
/* Reset old_value for GPIOs. */
for (i = 0; i < joypad->bt_gpio_count; i++) {
struct bt_gpio *gpio = &joypad->gpios[i];
gpio->old_value = (gpio->active_level ? 0 : 1);
}
/* Perform initial ADC calibration reading. */
switch (joypad->mode) {
case JOYPAD_MODE_MULTIADC:
for (i = 0; i < joypad->chan_count; i++) {
int val;
struct bt_adc *adc = &joypad->adcs[i];
val = joypad_adc_read_multi(joypad, adc);
adc->cal = val; /* store initial offset */
}
joypad_adc_check_multiadc(poll_dev);
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_SINGLEADC:
for (i = 0; i < joypad->amux_count; i++) {
int val;
struct bt_adc_single *adc = &joypad->adcs_single[i];
val = joypad_adc_read_single(joypad->amux, adc);
adc->cal = val;
}
joypad_adc_check_singleadc(poll_dev);
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_MIYOOSERIAL:
joypad_gpio_check(joypad);
break;
case JOYPAD_MODE_NONE:
default:
joypad_gpio_check(joypad);
break;
}
mutex_lock(&joypad->lock);
joypad->enable = true;
mutex_unlock(&joypad->lock);
}
static void joypad_close(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
if (joypad->has_rumble) {
cancel_work_sync(&joypad->play_work);
pwm_vibrator_stop(joypad);
}
mutex_lock(&joypad->lock);
joypad->enable = false;
mutex_unlock(&joypad->lock);
}
/* -------------------------------------------------------------------------- */
/* Device Tree / Platform Setup */
/* -------------------------------------------------------------------------- */
static int joypad_rumble_setup(struct device *dev, struct joypad *joypad)
{
int error;
struct pwm_state state;
joypad->pwm = devm_pwm_get(dev, "enable");
if (IS_ERR(joypad->pwm)) {
dev_err(dev, "No valid PWM found for rumble\n");
return PTR_ERR(joypad->pwm);
}
INIT_WORK(&joypad->play_work, pwm_vibrator_play_work);
pwm_init_state(joypad->pwm, &state);
state.enabled = false;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 11, 0))
error = pwm_apply_state(joypad->pwm, &state);
#else
error = pwm_apply_might_sleep(joypad->pwm, &state);
#endif
if (error) {
dev_err(dev, "failed to apply initial PWM state: %d\n", error);
return error;
}
return 0;
}
static int joypad_iochannel_setup(struct device *dev, struct joypad *joypad)
{
enum iio_chan_type type;
const char *uname;
int i, ret;
for (i = 0; i < joypad->chan_count; i++) {
ret = of_property_read_string_index(dev->of_node, "io-channel-names", i, &uname);
if (ret < 0) {
dev_err(dev, "invalid channel name index[%d]\n", i);
return -EINVAL;
}
joypad->adc_ch[i] = devm_iio_channel_get(dev, uname);
if (IS_ERR(joypad->adc_ch[i])) {
dev_err(dev, "iio channel get error\n");
return PTR_ERR(joypad->adc_ch[i]);
}
if (!joypad->adc_ch[i]->indio_dev)
return -ENXIO;
if (iio_get_channel_type(joypad->adc_ch[i], &type))
return -EINVAL;
if (type != IIO_VOLTAGE) {
dev_err(dev, "Incompatible channel type %d\n", type);
return -EINVAL;
}
}
return 0;
}
static int joypad_adc_setup_multi(struct device *dev, struct joypad *joypad)
{
int i;
joypad->adcs = devm_kzalloc(dev,
joypad->chan_count * sizeof(struct bt_adc),
GFP_KERNEL);
if (!joypad->adcs)
return -ENOMEM;
for (i = 0; i < joypad->chan_count; i++) {
struct bt_adc *adc = &joypad->adcs[i];
adc->scale = joypad->bt_adc_scale;
adc->max = (ADC_MAX_VOLTAGE / 2);
adc->min = -adc->max;
if (adc->scale) {
adc->max *= adc->scale;
adc->min *= adc->scale;
}
adc->channel = i;
adc->invert = false; /* will override below if set */