1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
8 * This file contains the implementation of generic input mapper operations
9 * for input mapper management.
16 #include <linux/slab.h>
18 int input_mapper_add(struct list_head
*mappers
, struct imapper
*entry
,
19 int (*map_op
)(void *, struct imapper
*), void *data
)
21 struct list_head
*pos
, *pre
, *head
;
22 struct imapper
*pre_ent
, *pos_ent
;
26 if (list_empty(head
)) {
27 entry
->next
= entry
->addr
;
29 list_add(&entry
->list
, head
);
33 list_for_each(pos
, head
) {
34 pos_ent
= list_entry(pos
, struct imapper
, list
);
35 if (pos_ent
->slot
> entry
->slot
) {
36 /* found a position in list */
46 __list_add(&entry
->list
, pos
->prev
, pos
);
50 list_add_tail(&entry
->list
, head
);
53 pre_ent
= list_entry(pre
, struct imapper
, list
);
54 pos_ent
= list_entry(pos
, struct imapper
, list
);
56 entry
->next
= pos_ent
->addr
;
58 pre_ent
->next
= entry
->addr
;
59 map_op(data
, pre_ent
);
64 int input_mapper_delete(struct list_head
*mappers
, struct imapper
*entry
,
65 int (*map_op
)(void *, struct imapper
*), void *data
)
67 struct list_head
*next
, *pre
, *head
;
68 struct imapper
*pre_ent
, *next_ent
;
75 pre
= (entry
->list
.prev
== head
) ? head
->prev
: entry
->list
.prev
;
76 next
= (entry
->list
.next
== head
) ? head
->next
: entry
->list
.next
;
78 if (pre
== &entry
->list
) {
79 /* entry is the only one node in mappers list */
80 entry
->next
= entry
->addr
= entry
->user
= entry
->slot
= 0;
82 list_del(&entry
->list
);
86 pre_ent
= list_entry(pre
, struct imapper
, list
);
87 next_ent
= list_entry(next
, struct imapper
, list
);
89 pre_ent
->next
= next_ent
->addr
;
90 map_op(data
, pre_ent
);
91 list_del(&entry
->list
);
96 void free_input_mapper_list(struct list_head
*head
)
98 struct imapper
*entry
;
99 struct list_head
*pos
;
101 while (!list_empty(head
)) {
104 entry
= list_entry(pos
, struct imapper
, list
);