1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/slab.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/isdn/capilli.h>
7
8 #define DBG(format, arg...) do { \
9 printk(KERN_DEBUG "%s: " format "\n" , __func__ , ## arg); \
10 } while (0)
11
12 struct capilib_msgidqueue {
13 struct capilib_msgidqueue *next;
14 u16 msgid;
15 };
16
17 struct capilib_ncci {
18 struct list_head list;
19 u16 applid;
20 u32 ncci;
21 u32 winsize;
22 int nmsg;
23 struct capilib_msgidqueue *msgidqueue;
24 struct capilib_msgidqueue *msgidlast;
25 struct capilib_msgidqueue *msgidfree;
26 struct capilib_msgidqueue msgidpool[CAPI_MAXDATAWINDOW];
27 };
28
29 // ---------------------------------------------------------------------------
30 // NCCI Handling
31
mq_init(struct capilib_ncci * np)32 static inline void mq_init(struct capilib_ncci *np)
33 {
34 u_int i;
35 np->msgidqueue = NULL;
36 np->msgidlast = NULL;
37 np->nmsg = 0;
38 memset(np->msgidpool, 0, sizeof(np->msgidpool));
39 np->msgidfree = &np->msgidpool[0];
40 for (i = 1; i < np->winsize; i++) {
41 np->msgidpool[i].next = np->msgidfree;
42 np->msgidfree = &np->msgidpool[i];
43 }
44 }
45
mq_enqueue(struct capilib_ncci * np,u16 msgid)46 static inline int mq_enqueue(struct capilib_ncci *np, u16 msgid)
47 {
48 struct capilib_msgidqueue *mq;
49 if ((mq = np->msgidfree) == NULL)
50 return 0;
51 np->msgidfree = mq->next;
52 mq->msgid = msgid;
53 mq->next = NULL;
54 if (np->msgidlast)
55 np->msgidlast->next = mq;
56 np->msgidlast = mq;
57 if (!np->msgidqueue)
58 np->msgidqueue = mq;
59 np->nmsg++;
60 return 1;
61 }
62
mq_dequeue(struct capilib_ncci * np,u16 msgid)63 static inline int mq_dequeue(struct capilib_ncci *np, u16 msgid)
64 {
65 struct capilib_msgidqueue **pp;
66 for (pp = &np->msgidqueue; *pp; pp = &(*pp)->next) {
67 if ((*pp)->msgid == msgid) {
68 struct capilib_msgidqueue *mq = *pp;
69 *pp = mq->next;
70 if (mq == np->msgidlast)
71 np->msgidlast = NULL;
72 mq->next = np->msgidfree;
73 np->msgidfree = mq;
74 np->nmsg--;
75 return 1;
76 }
77 }
78 return 0;
79 }
80
capilib_new_ncci(struct list_head * head,u16 applid,u32 ncci,u32 winsize)81 void capilib_new_ncci(struct list_head *head, u16 applid, u32 ncci, u32 winsize)
82 {
83 struct capilib_ncci *np;
84
85 np = kmalloc(sizeof(*np), GFP_ATOMIC);
86 if (!np) {
87 printk(KERN_WARNING "capilib_new_ncci: no memory.\n");
88 return;
89 }
90 if (winsize > CAPI_MAXDATAWINDOW) {
91 printk(KERN_ERR "capi_new_ncci: winsize %d too big\n",
92 winsize);
93 winsize = CAPI_MAXDATAWINDOW;
94 }
95 np->applid = applid;
96 np->ncci = ncci;
97 np->winsize = winsize;
98 mq_init(np);
99 list_add_tail(&np->list, head);
100 DBG("kcapi: appl %d ncci 0x%x up", applid, ncci);
101 }
102
103 EXPORT_SYMBOL(capilib_new_ncci);
104
capilib_free_ncci(struct list_head * head,u16 applid,u32 ncci)105 void capilib_free_ncci(struct list_head *head, u16 applid, u32 ncci)
106 {
107 struct list_head *l;
108 struct capilib_ncci *np;
109
110 list_for_each(l, head) {
111 np = list_entry(l, struct capilib_ncci, list);
112 if (np->applid != applid)
113 continue;
114 if (np->ncci != ncci)
115 continue;
116 printk(KERN_INFO "kcapi: appl %d ncci 0x%x down\n", applid, ncci);
117 list_del(&np->list);
118 kfree(np);
119 return;
120 }
121 printk(KERN_ERR "capilib_free_ncci: ncci 0x%x not found\n", ncci);
122 }
123
124 EXPORT_SYMBOL(capilib_free_ncci);
125
capilib_release_appl(struct list_head * head,u16 applid)126 void capilib_release_appl(struct list_head *head, u16 applid)
127 {
128 struct list_head *l, *n;
129 struct capilib_ncci *np;
130
131 list_for_each_safe(l, n, head) {
132 np = list_entry(l, struct capilib_ncci, list);
133 if (np->applid != applid)
134 continue;
135 printk(KERN_INFO "kcapi: appl %d ncci 0x%x forced down\n", applid, np->ncci);
136 list_del(&np->list);
137 kfree(np);
138 }
139 }
140
141 EXPORT_SYMBOL(capilib_release_appl);
142
capilib_release(struct list_head * head)143 void capilib_release(struct list_head *head)
144 {
145 struct list_head *l, *n;
146 struct capilib_ncci *np;
147
148 list_for_each_safe(l, n, head) {
149 np = list_entry(l, struct capilib_ncci, list);
150 printk(KERN_INFO "kcapi: appl %d ncci 0x%x forced down\n", np->applid, np->ncci);
151 list_del(&np->list);
152 kfree(np);
153 }
154 }
155
156 EXPORT_SYMBOL(capilib_release);
157
capilib_data_b3_req(struct list_head * head,u16 applid,u32 ncci,u16 msgid)158 u16 capilib_data_b3_req(struct list_head *head, u16 applid, u32 ncci, u16 msgid)
159 {
160 struct list_head *l;
161 struct capilib_ncci *np;
162
163 list_for_each(l, head) {
164 np = list_entry(l, struct capilib_ncci, list);
165 if (np->applid != applid)
166 continue;
167 if (np->ncci != ncci)
168 continue;
169
170 if (mq_enqueue(np, msgid) == 0)
171 return CAPI_SENDQUEUEFULL;
172
173 return CAPI_NOERROR;
174 }
175 printk(KERN_ERR "capilib_data_b3_req: ncci 0x%x not found\n", ncci);
176 return CAPI_NOERROR;
177 }
178
179 EXPORT_SYMBOL(capilib_data_b3_req);
180
capilib_data_b3_conf(struct list_head * head,u16 applid,u32 ncci,u16 msgid)181 void capilib_data_b3_conf(struct list_head *head, u16 applid, u32 ncci, u16 msgid)
182 {
183 struct list_head *l;
184 struct capilib_ncci *np;
185
186 list_for_each(l, head) {
187 np = list_entry(l, struct capilib_ncci, list);
188 if (np->applid != applid)
189 continue;
190 if (np->ncci != ncci)
191 continue;
192
193 if (mq_dequeue(np, msgid) == 0) {
194 printk(KERN_ERR "kcapi: msgid %hu ncci 0x%x not on queue\n",
195 msgid, ncci);
196 }
197 return;
198 }
199 printk(KERN_ERR "capilib_data_b3_conf: ncci 0x%x not found\n", ncci);
200 }
201
202 EXPORT_SYMBOL(capilib_data_b3_conf);
203