505
566
if (num_fingers >= 2) {
506
set_slot(dev, 0, true, min(a->x, b->x), min(a->y, b->y));
507
set_slot(dev, 1, true, max(a->x, b->x), max(a->y, b->y));
567
synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
569
synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
508
571
} else if (num_fingers == 1) {
509
set_slot(dev, 0, true, a->x, a->y);
510
set_slot(dev, 1, false, 0, 0);
572
synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
573
synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
512
set_slot(dev, 0, false, 0, 0);
513
set_slot(dev, 1, false, 0, 0);
575
synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
576
synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
580
static void synaptics_report_buttons(struct psmouse *psmouse,
581
const struct synaptics_hw_state *hw)
583
struct input_dev *dev = psmouse->dev;
584
struct synaptics_data *priv = psmouse->private;
587
input_report_key(dev, BTN_LEFT, hw->left);
588
input_report_key(dev, BTN_RIGHT, hw->right);
590
if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
591
input_report_key(dev, BTN_MIDDLE, hw->middle);
593
if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
594
input_report_key(dev, BTN_FORWARD, hw->up);
595
input_report_key(dev, BTN_BACK, hw->down);
598
for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
599
input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
602
static void synaptics_report_slot(struct input_dev *dev, int slot,
603
const struct synaptics_hw_state *hw)
605
input_mt_slot(dev, slot);
606
input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
610
input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
611
input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
612
input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
615
static void synaptics_report_mt_data(struct psmouse *psmouse,
616
struct synaptics_mt_state *mt_state,
617
const struct synaptics_hw_state *sgm)
619
struct input_dev *dev = psmouse->dev;
620
struct synaptics_data *priv = psmouse->private;
621
struct synaptics_hw_state *agm = &priv->agm;
622
struct synaptics_mt_state *old = &priv->mt_state;
624
switch (mt_state->count) {
626
synaptics_report_slot(dev, 0, NULL);
627
synaptics_report_slot(dev, 1, NULL);
630
if (mt_state->sgm == -1) {
631
synaptics_report_slot(dev, 0, NULL);
632
synaptics_report_slot(dev, 1, NULL);
633
} else if (mt_state->sgm == 0) {
634
synaptics_report_slot(dev, 0, sgm);
635
synaptics_report_slot(dev, 1, NULL);
637
synaptics_report_slot(dev, 0, NULL);
638
synaptics_report_slot(dev, 1, sgm);
643
* If the finger slot contained in SGM is valid, and either
644
* hasn't changed, or is new, then report SGM in MTB slot 0.
645
* Otherwise, empty MTB slot 0.
647
if (mt_state->sgm != -1 &&
648
(mt_state->sgm == old->sgm || old->sgm == -1))
649
synaptics_report_slot(dev, 0, sgm);
651
synaptics_report_slot(dev, 0, NULL);
654
* If the finger slot contained in AGM is valid, and either
655
* hasn't changed, or is new, then report AGM in MTB slot 1.
656
* Otherwise, empty MTB slot 1.
658
if (mt_state->agm != -1 &&
659
(mt_state->agm == old->agm || old->agm == -1))
660
synaptics_report_slot(dev, 1, agm);
662
synaptics_report_slot(dev, 1, NULL);
666
/* Don't use active slot count to generate BTN_TOOL events. */
667
input_mt_report_pointer_emulation(dev, false);
669
/* Send the number of fingers reported by touchpad itself. */
670
input_mt_report_finger_count(dev, mt_state->count);
672
synaptics_report_buttons(psmouse, sgm);
677
/* Handle case where mt_state->count = 0 */
678
static void synaptics_image_sensor_0f(struct synaptics_data *priv,
679
struct synaptics_mt_state *mt_state)
681
synaptics_mt_state_set(mt_state, 0, -1, -1);
682
priv->mt_state_lost = false;
685
/* Handle case where mt_state->count = 1 */
686
static void synaptics_image_sensor_1f(struct synaptics_data *priv,
687
struct synaptics_mt_state *mt_state)
689
struct synaptics_hw_state *agm = &priv->agm;
690
struct synaptics_mt_state *old = &priv->mt_state;
693
* If the last AGM was (0,0,0), and there is only one finger left,
694
* then we absolutely know that SGM contains slot 0, and all other
695
* fingers have been removed.
697
if (priv->agm_pending && agm->z == 0) {
698
synaptics_mt_state_set(mt_state, 1, 0, -1);
699
priv->mt_state_lost = false;
703
switch (old->count) {
705
synaptics_mt_state_set(mt_state, 1, 0, -1);
709
* If mt_state_lost, then the previous transition was 3->1,
710
* and SGM now contains either slot 0 or 1, but we don't know
711
* which. So, we just assume that the SGM now contains slot 1.
713
* If pending AGM and either:
714
* (a) the previous SGM slot contains slot 0, or
715
* (b) there was no SGM slot
716
* then, the SGM now contains slot 1
718
* Case (a) happens with very rapid "drum roll" gestures, where
719
* slot 0 finger is lifted and a new slot 1 finger touches
720
* within one reporting interval.
722
* Case (b) happens if initially two or more fingers tap
723
* briefly, and all but one lift before the end of the first
724
* reporting interval.
726
* (In both these cases, slot 0 will becomes empty, so SGM
727
* contains slot 1 with the new finger)
729
* Else, if there was no previous SGM, it now contains slot 0.
731
* Otherwise, SGM still contains the same slot.
733
if (priv->mt_state_lost ||
734
(priv->agm_pending && old->sgm <= 0))
735
synaptics_mt_state_set(mt_state, 1, 1, -1);
736
else if (old->sgm == -1)
737
synaptics_mt_state_set(mt_state, 1, 0, -1);
741
* If mt_state_lost, we don't know which finger SGM contains.
743
* So, report 1 finger, but with both slots empty.
744
* We will use slot 1 on subsequent 1->1
746
if (priv->mt_state_lost) {
747
synaptics_mt_state_set(mt_state, 1, -1, -1);
751
* Since the last AGM was NOT (0,0,0), it was the finger in
752
* slot 0 that has been removed.
753
* So, SGM now contains previous AGM's slot, and AGM is now
756
synaptics_mt_state_set(mt_state, 1, old->agm, -1);
760
* Since last AGM was not (0,0,0), we don't know which finger
763
* So, report 1 finger, but with both slots empty.
764
* We will use slot 1 on subsequent 1->1
766
synaptics_mt_state_set(mt_state, 1, -1, -1);
767
priv->mt_state_lost = true;
771
/* mt_state was updated by AGM-CONTACT packet */
776
/* Handle case where mt_state->count = 2 */
777
static void synaptics_image_sensor_2f(struct synaptics_data *priv,
778
struct synaptics_mt_state *mt_state)
780
struct synaptics_mt_state *old = &priv->mt_state;
782
switch (old->count) {
784
synaptics_mt_state_set(mt_state, 2, 0, 1);
788
* If previous SGM contained slot 1 or higher, SGM now contains
789
* slot 0 (the newly touching finger) and AGM contains SGM's
792
* Otherwise, SGM still contains slot 0 and AGM now contains
796
synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
798
synaptics_mt_state_set(mt_state, 2, 0, 1);
802
* If mt_state_lost, SGM now contains either finger 1 or 2, but
803
* we don't know which.
804
* So, we just assume that the SGM contains slot 0 and AGM 1.
806
if (priv->mt_state_lost)
807
synaptics_mt_state_set(mt_state, 2, 0, 1);
809
* Otherwise, use the same mt_state, since it either hasn't
810
* changed, or was updated by a recently received AGM-CONTACT
816
* 3->2 transitions have two unsolvable problems:
817
* 1) no indication is given which finger was removed
818
* 2) no way to tell if agm packet was for finger 3
819
* before 3->2, or finger 2 after 3->2.
821
* So, report 2 fingers, but empty all slots.
822
* We will guess slots [0,1] on subsequent 2->2.
824
synaptics_mt_state_set(mt_state, 2, -1, -1);
825
priv->mt_state_lost = true;
829
/* mt_state was updated by AGM-CONTACT packet */
834
/* Handle case where mt_state->count = 3 */
835
static void synaptics_image_sensor_3f(struct synaptics_data *priv,
836
struct synaptics_mt_state *mt_state)
838
struct synaptics_mt_state *old = &priv->mt_state;
840
switch (old->count) {
842
synaptics_mt_state_set(mt_state, 3, 0, 2);
846
* If previous SGM contained slot 2 or higher, SGM now contains
847
* slot 0 (one of the newly touching fingers) and AGM contains
848
* SGM's previous slot.
850
* Otherwise, SGM now contains slot 0 and AGM contains slot 2.
853
synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
855
synaptics_mt_state_set(mt_state, 3, 0, 2);
859
* If the AGM previously contained slot 3 or higher, then the
860
* newly touching finger is in the lowest available slot.
862
* If SGM was previously 1 or higher, then the new SGM is
863
* now slot 0 (with a new finger), otherwise, the new finger
864
* is now in a hidden slot between 0 and AGM's slot.
866
* In all such cases, the SGM now contains slot 0, and the AGM
867
* continues to contain the same slot as before.
870
synaptics_mt_state_set(mt_state, 3, 0, old->agm);
875
* After some 3->1 and all 3->2 transitions, we lose track
876
* of which slot is reported by SGM and AGM.
878
* For 2->3 in this state, report 3 fingers, but empty all
879
* slots, and we will guess (0,2) on a subsequent 0->3.
881
* To userspace, the resulting transition will look like:
882
* 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
884
if (priv->mt_state_lost) {
885
synaptics_mt_state_set(mt_state, 3, -1, -1);
890
* If the (SGM,AGM) really previously contained slots (0, 1),
891
* then we cannot know what slot was just reported by the AGM,
892
* because the 2->3 transition can occur either before or after
893
* the AGM packet. Thus, this most recent AGM could contain
894
* either the same old slot 1 or the new slot 2.
895
* Subsequent AGMs will be reporting slot 2.
897
* To userspace, the resulting transition will look like:
898
* 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
900
synaptics_mt_state_set(mt_state, 3, 0, -1);
904
* If, for whatever reason, the previous agm was invalid,
905
* Assume SGM now contains slot 0, AGM now contains slot 2.
908
synaptics_mt_state_set(mt_state, 3, 0, 2);
910
* mt_state either hasn't changed, or was updated by a recently
911
* received AGM-CONTACT packet.
917
/* mt_state was updated by AGM-CONTACT packet */
922
/* Handle case where mt_state->count = 4, or = 5 */
923
static void synaptics_image_sensor_45f(struct synaptics_data *priv,
924
struct synaptics_mt_state *mt_state)
926
/* mt_state was updated correctly by AGM-CONTACT packet */
927
priv->mt_state_lost = false;
930
static void synaptics_image_sensor_process(struct psmouse *psmouse,
931
struct synaptics_hw_state *sgm)
933
struct synaptics_data *priv = psmouse->private;
934
struct synaptics_hw_state *agm = &priv->agm;
935
struct synaptics_mt_state mt_state;
937
/* Initialize using current mt_state (as updated by last agm) */
938
mt_state = agm->mt_state;
941
* Update mt_state using the new finger count and current mt_state.
944
synaptics_image_sensor_0f(priv, &mt_state);
945
else if (sgm->w >= 4)
946
synaptics_image_sensor_1f(priv, &mt_state);
947
else if (sgm->w == 0)
948
synaptics_image_sensor_2f(priv, &mt_state);
949
else if (sgm->w == 1 && mt_state.count <= 3)
950
synaptics_image_sensor_3f(priv, &mt_state);
952
synaptics_image_sensor_45f(priv, &mt_state);
954
/* Send resulting input events to user space */
955
synaptics_report_mt_data(psmouse, &mt_state, sgm);
957
/* Store updated mt_state */
958
priv->mt_state = agm->mt_state = mt_state;
959
priv->agm_pending = false;