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#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/fs.h>
36#include <linux/seq_file.h>
37#include <linux/slab.h>
38#include <linux/export.h>
39
40#include <net/irda/irda.h>
41#include <net/irda/irlmp.h>
42
43#include <net/irda/discovery.h>
44
45#include <asm/unaligned.h>
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
63{
64 discovery_t *discovery, *node;
65 unsigned long flags;
66
67
68 new->firststamp = new->timestamp;
69
70 spin_lock_irqsave(&cachelog->hb_spinlock, flags);
71
72
73
74
75
76
77
78 discovery = (discovery_t *) hashbin_get_first(cachelog);
79 while (discovery != NULL ) {
80 node = discovery;
81
82
83 discovery = (discovery_t *) hashbin_get_next(cachelog);
84
85 if ((node->data.saddr == new->data.saddr) &&
86 ((node->data.daddr == new->data.daddr) ||
87 (strcmp(node->data.info, new->data.info) == 0)))
88 {
89
90
91
92 hashbin_remove_this(cachelog, (irda_queue_t *) node);
93
94 if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
95
96 new->firststamp = node->firststamp;
97 kfree(node);
98 }
99 }
100
101
102 hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
103
104 spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
105}
106
107
108
109
110
111
112
113void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
114{
115 discovery_t *discovery;
116
117 IRDA_DEBUG(4, "%s()\n", __func__);
118
119
120
121
122
123
124
125 if (log == NULL) {
126
127 return;
128 }
129
130
131
132
133
134
135 discovery = (discovery_t *) hashbin_remove_first(log);
136 while (discovery != NULL) {
137 irlmp_add_discovery(cachelog, discovery);
138
139 discovery = (discovery_t *) hashbin_remove_first(log);
140 }
141
142
143 hashbin_delete(log, (FREE_FUNC) kfree);
144}
145
146
147
148
149
150
151
152
153
154void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
155{
156 discovery_t * discovery;
157 discovery_t * curr;
158 unsigned long flags;
159 discinfo_t * buffer = NULL;
160 int n;
161 int i = 0;
162
163 IRDA_ASSERT(log != NULL, return;);
164 IRDA_DEBUG(4, "%s()\n", __func__);
165
166 spin_lock_irqsave(&log->hb_spinlock, flags);
167
168 discovery = (discovery_t *) hashbin_get_first(log);
169 while (discovery != NULL) {
170
171 curr = discovery;
172 discovery = (discovery_t *) hashbin_get_next(log);
173
174
175 if ((curr->data.saddr == saddr) &&
176 (force ||
177 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
178 {
179
180
181
182
183
184 if(buffer == NULL) {
185
186 n = HASHBIN_GET_SIZE(log);
187 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
188 if (buffer == NULL) {
189 spin_unlock_irqrestore(&log->hb_spinlock, flags);
190 return;
191 }
192
193 }
194
195
196 memcpy(&(buffer[i]), &(curr->data),
197 sizeof(discinfo_t));
198 i++;
199
200
201 curr = hashbin_remove_this(log, (irda_queue_t *) curr);
202 kfree(curr);
203 }
204 }
205
206
207
208
209
210 spin_unlock_irqrestore(&log->hb_spinlock, flags);
211
212 if(buffer == NULL)
213 return;
214
215
216 irlmp_discovery_expiry(buffer, i);
217
218
219 kfree(buffer);
220}
221
222#if 0
223
224
225
226
227
228
229void irlmp_dump_discoveries(hashbin_t *log)
230{
231 discovery_t *discovery;
232
233 IRDA_ASSERT(log != NULL, return;);
234
235 discovery = (discovery_t *) hashbin_get_first(log);
236 while (discovery != NULL) {
237 IRDA_DEBUG(0, "Discovery:\n");
238 IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr);
239 IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr);
240 IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info);
241
242 discovery = (discovery_t *) hashbin_get_next(log);
243 }
244}
245#endif
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
268 __u16 mask, int old_entries)
269{
270 discovery_t * discovery;
271 unsigned long flags;
272 discinfo_t * buffer = NULL;
273 int j_timeout = (sysctl_discovery_timeout * HZ);
274 int n;
275 int i = 0;
276
277 IRDA_ASSERT(pn != NULL, return NULL;);
278 IRDA_ASSERT(log != NULL, return NULL;);
279
280
281 spin_lock_irqsave(&log->hb_spinlock, flags);
282
283 discovery = (discovery_t *) hashbin_get_first(log);
284 while (discovery != NULL) {
285
286
287
288 if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
289 ((old_entries) ||
290 ((jiffies - discovery->firststamp) < j_timeout))) {
291
292
293
294
295
296 if(buffer == NULL) {
297
298 n = HASHBIN_GET_SIZE(log);
299 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
300 if (buffer == NULL) {
301 spin_unlock_irqrestore(&log->hb_spinlock, flags);
302 return NULL;
303 }
304
305 }
306
307
308 memcpy(&(buffer[i]), &(discovery->data),
309 sizeof(discinfo_t));
310 i++;
311 }
312 discovery = (discovery_t *) hashbin_get_next(log);
313 }
314
315 spin_unlock_irqrestore(&log->hb_spinlock, flags);
316
317
318 *pn = i;
319 return buffer;
320}
321
322#ifdef CONFIG_PROC_FS
323static inline discovery_t *discovery_seq_idx(loff_t pos)
324
325{
326 discovery_t *discovery;
327
328 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
329 discovery != NULL;
330 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
331 if (pos-- == 0)
332 break;
333 }
334
335 return discovery;
336}
337
338static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
339{
340 spin_lock_irq(&irlmp->cachelog->hb_spinlock);
341 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
342}
343
344static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
345{
346 ++*pos;
347 return (v == SEQ_START_TOKEN)
348 ? (void *) hashbin_get_first(irlmp->cachelog)
349 : (void *) hashbin_get_next(irlmp->cachelog);
350}
351
352static void discovery_seq_stop(struct seq_file *seq, void *v)
353{
354 spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
355}
356
357static int discovery_seq_show(struct seq_file *seq, void *v)
358{
359 if (v == SEQ_START_TOKEN)
360 seq_puts(seq, "IrLMP: Discovery log:\n\n");
361 else {
362 const discovery_t *discovery = v;
363
364 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
365 discovery->data.info,
366 discovery->data.hints[0],
367 discovery->data.hints[1]);
368#if 0
369 if ( discovery->data.hints[0] & HINT_PNP)
370 seq_puts(seq, "PnP Compatible ");
371 if ( discovery->data.hints[0] & HINT_PDA)
372 seq_puts(seq, "PDA/Palmtop ");
373 if ( discovery->data.hints[0] & HINT_COMPUTER)
374 seq_puts(seq, "Computer ");
375 if ( discovery->data.hints[0] & HINT_PRINTER)
376 seq_puts(seq, "Printer ");
377 if ( discovery->data.hints[0] & HINT_MODEM)
378 seq_puts(seq, "Modem ");
379 if ( discovery->data.hints[0] & HINT_FAX)
380 seq_puts(seq, "Fax ");
381 if ( discovery->data.hints[0] & HINT_LAN)
382 seq_puts(seq, "LAN Access ");
383
384 if ( discovery->data.hints[1] & HINT_TELEPHONY)
385 seq_puts(seq, "Telephony ");
386 if ( discovery->data.hints[1] & HINT_FILE_SERVER)
387 seq_puts(seq, "File Server ");
388 if ( discovery->data.hints[1] & HINT_COMM)
389 seq_puts(seq, "IrCOMM ");
390 if ( discovery->data.hints[1] & HINT_OBEX)
391 seq_puts(seq, "IrOBEX ");
392#endif
393 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
394 discovery->data.saddr,
395 discovery->data.daddr);
396
397 seq_putc(seq, '\n');
398 }
399 return 0;
400}
401
402static const struct seq_operations discovery_seq_ops = {
403 .start = discovery_seq_start,
404 .next = discovery_seq_next,
405 .stop = discovery_seq_stop,
406 .show = discovery_seq_show,
407};
408
409static int discovery_seq_open(struct inode *inode, struct file *file)
410{
411 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
412
413 return seq_open(file, &discovery_seq_ops);
414}
415
416const struct file_operations discovery_seq_fops = {
417 .owner = THIS_MODULE,
418 .open = discovery_seq_open,
419 .read = seq_read,
420 .llseek = seq_lseek,
421 .release = seq_release,
422};
423#endif
424