Merge tag 'v3.10.105' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / irda / iriap.c
CommitLineData
1da177e4
LT
1/*********************************************************************
2 *
3 * Filename: iriap.c
4 * Version: 0.8
5 * Description: Information Access Protocol (IAP)
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Thu Aug 21 00:02:07 1997
9 * Modified at: Sat Dec 25 16:42:42 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 *
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
96de0e25 21 * Neither Dag Brattli nor University of Tromsø admit liability nor
1da177e4
LT
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
24 *
25 ********************************************************************/
26
1da177e4
LT
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/skbuff.h>
d7fe0f24 30#include <linux/fs.h>
1da177e4
LT
31#include <linux/string.h>
32#include <linux/init.h>
33#include <linux/seq_file.h>
5a0e3ad6 34#include <linux/slab.h>
1da177e4
LT
35
36#include <asm/byteorder.h>
37#include <asm/unaligned.h>
38
39#include <net/irda/irda.h>
40#include <net/irda/irttp.h>
41#include <net/irda/irlmp.h>
42#include <net/irda/irias_object.h>
43#include <net/irda/iriap_event.h>
44#include <net/irda/iriap.h>
45
46#ifdef CONFIG_IRDA_DEBUG
47/* FIXME: This one should go in irlmp.c */
36cbd3dc 48static const char *const ias_charset_types[] = {
1da177e4
LT
49 "CS_ASCII",
50 "CS_ISO_8859_1",
51 "CS_ISO_8859_2",
52 "CS_ISO_8859_3",
53 "CS_ISO_8859_4",
54 "CS_ISO_8859_5",
55 "CS_ISO_8859_6",
56 "CS_ISO_8859_7",
57 "CS_ISO_8859_8",
58 "CS_ISO_8859_9",
59 "CS_UNICODE"
60};
61#endif /* CONFIG_IRDA_DEBUG */
62
63static hashbin_t *iriap = NULL;
64static void *service_handle;
65
66static void __iriap_close(struct iriap_cb *self);
67static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode);
68static void iriap_disconnect_indication(void *instance, void *sap,
69 LM_REASON reason, struct sk_buff *skb);
70static void iriap_connect_indication(void *instance, void *sap,
71 struct qos_info *qos, __u32 max_sdu_size,
72 __u8 max_header_size,
73 struct sk_buff *skb);
74static void iriap_connect_confirm(void *instance, void *sap,
75 struct qos_info *qos,
76 __u32 max_sdu_size, __u8 max_header_size,
77 struct sk_buff *skb);
78static int iriap_data_indication(void *instance, void *sap,
79 struct sk_buff *skb);
80
81static void iriap_watchdog_timer_expired(void *data);
82
6819bc2e
YH
83static inline void iriap_start_watchdog_timer(struct iriap_cb *self,
84 int timeout)
1da177e4 85{
6819bc2e 86 irda_start_timer(&self->watchdog_timer, timeout, self,
1da177e4
LT
87 iriap_watchdog_timer_expired);
88}
89
79b38915
DM
90static struct lock_class_key irias_objects_key;
91
1da177e4
LT
92/*
93 * Function iriap_init (void)
94 *
95 * Initializes the IrIAP layer, called by the module initialization code
96 * in irmod.c
97 */
98int __init iriap_init(void)
99{
100 struct ias_object *obj;
101 struct iriap_cb *server;
102 __u8 oct_seq[6];
103 __u16 hints;
104
105 /* Allocate master array */
106 iriap = hashbin_new(HB_LOCK);
107 if (!iriap)
108 return -ENOMEM;
109
110 /* Object repository - defined in irias_object.c */
111 irias_objects = hashbin_new(HB_LOCK);
112 if (!irias_objects) {
113 IRDA_WARNING("%s: Can't allocate irias_objects hashbin!\n",
0dc47877 114 __func__);
1da177e4
LT
115 hashbin_delete(iriap, NULL);
116 return -ENOMEM;
117 }
118
79b38915
DM
119 lockdep_set_class_and_name(&irias_objects->hb_spinlock, &irias_objects_key,
120 "irias_objects");
121
1da177e4
LT
122 /*
123 * Register some default services for IrLMP
124 */
125 hints = irlmp_service_to_hint(S_COMPUTER);
126 service_handle = irlmp_register_service(hints);
127
128 /* Register the Device object with LM-IAS */
129 obj = irias_new_object("Device", IAS_DEVICE_ID);
130 irias_add_string_attrib(obj, "DeviceName", "Linux", IAS_KERNEL_ATTR);
131
132 oct_seq[0] = 0x01; /* Version 1 */
133 oct_seq[1] = 0x00; /* IAS support bits */
134 oct_seq[2] = 0x00; /* LM-MUX support bits */
135#ifdef CONFIG_IRDA_ULTRA
136 oct_seq[2] |= 0x04; /* Connectionless Data support */
137#endif
138 irias_add_octseq_attrib(obj, "IrLMPSupport", oct_seq, 3,
139 IAS_KERNEL_ATTR);
140 irias_insert_object(obj);
141
142 /*
143 * Register server support with IrLMP so we can accept incoming
144 * connections
145 */
146 server = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
147 if (!server) {
0dc47877 148 IRDA_DEBUG(0, "%s(), unable to open server\n", __func__);
1da177e4
LT
149 return -1;
150 }
151 iriap_register_lsap(server, LSAP_IAS, IAS_SERVER);
152
153 return 0;
154}
155
156/*
157 * Function iriap_cleanup (void)
158 *
159 * Initializes the IrIAP layer, called by the module cleanup code in
160 * irmod.c
161 */
75a69ac6 162void iriap_cleanup(void)
1da177e4
LT
163{
164 irlmp_unregister_service(service_handle);
165
166 hashbin_delete(iriap, (FREE_FUNC) __iriap_close);
167 hashbin_delete(irias_objects, (FREE_FUNC) __irias_delete_object);
168}
169
170/*
171 * Function iriap_open (void)
172 *
173 * Opens an instance of the IrIAP layer, and registers with IrLMP
174 */
175struct iriap_cb *iriap_open(__u8 slsap_sel, int mode, void *priv,
176 CONFIRM_CALLBACK callback)
177{
178 struct iriap_cb *self;
179
0dc47877 180 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4 181
b3ab09f9 182 self = kzalloc(sizeof(*self), GFP_ATOMIC);
1da177e4 183 if (!self) {
0dc47877 184 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
1da177e4
LT
185 return NULL;
186 }
187
188 /*
189 * Initialize instance
190 */
1da177e4
LT
191
192 self->magic = IAS_MAGIC;
193 self->mode = mode;
942878ee
VN
194 if (mode == IAS_CLIENT) {
195 if (iriap_register_lsap(self, slsap_sel, mode)) {
196 kfree(self);
197 return NULL;
198 }
199 }
1da177e4
LT
200
201 self->confirm = callback;
202 self->priv = priv;
203
204 /* iriap_getvaluebyclass_request() will construct packets before
205 * we connect, so this must have a sane value... Jean II */
206 self->max_header_size = LMP_MAX_HEADER;
207
208 init_timer(&self->watchdog_timer);
209
210 hashbin_insert(iriap, (irda_queue_t *) self, (long) self, NULL);
211
212 /* Initialize state machines */
213 iriap_next_client_state(self, S_DISCONNECT);
214 iriap_next_call_state(self, S_MAKE_CALL);
215 iriap_next_server_state(self, R_DISCONNECT);
216 iriap_next_r_connect_state(self, R_WAITING);
217
218 return self;
219}
220EXPORT_SYMBOL(iriap_open);
221
222/*
223 * Function __iriap_close (self)
224 *
225 * Removes (deallocates) the IrIAP instance
226 *
227 */
228static void __iriap_close(struct iriap_cb *self)
229{
0dc47877 230 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
231
232 IRDA_ASSERT(self != NULL, return;);
233 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
234
235 del_timer(&self->watchdog_timer);
236
237 if (self->request_skb)
238 dev_kfree_skb(self->request_skb);
239
240 self->magic = 0;
241
242 kfree(self);
243}
244
245/*
246 * Function iriap_close (void)
247 *
248 * Closes IrIAP and deregisters with IrLMP
249 */
250void iriap_close(struct iriap_cb *self)
251{
252 struct iriap_cb *entry;
253
0dc47877 254 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
255
256 IRDA_ASSERT(self != NULL, return;);
257 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
258
259 if (self->lsap) {
260 irlmp_close_lsap(self->lsap);
261 self->lsap = NULL;
262 }
263
264 entry = (struct iriap_cb *) hashbin_remove(iriap, (long) self, NULL);
265 IRDA_ASSERT(entry == self, return;);
266
267 __iriap_close(self);
268}
269EXPORT_SYMBOL(iriap_close);
270
271static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode)
272{
273 notify_t notify;
274
0dc47877 275 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
276
277 irda_notify_init(&notify);
278 notify.connect_confirm = iriap_connect_confirm;
279 notify.connect_indication = iriap_connect_indication;
280 notify.disconnect_indication = iriap_disconnect_indication;
281 notify.data_indication = iriap_data_indication;
282 notify.instance = self;
283 if (mode == IAS_CLIENT)
284 strcpy(notify.name, "IrIAS cli");
285 else
286 strcpy(notify.name, "IrIAS srv");
287
288 self->lsap = irlmp_open_lsap(slsap_sel, &notify, 0);
289 if (self->lsap == NULL) {
0dc47877 290 IRDA_ERROR("%s: Unable to allocated LSAP!\n", __func__);
1da177e4
LT
291 return -1;
292 }
293 self->slsap_sel = self->lsap->slsap_sel;
294
295 return 0;
296}
297
298/*
299 * Function iriap_disconnect_indication (handle, reason)
300 *
301 * Got disconnect, so clean up everything associated with this connection
302 *
303 */
304static void iriap_disconnect_indication(void *instance, void *sap,
305 LM_REASON reason,
306 struct sk_buff *skb)
307{
308 struct iriap_cb *self;
309
e15465e1
DC
310 IRDA_DEBUG(4, "%s(), reason=%s [%d]\n", __func__,
311 irlmp_reason_str(reason), reason);
1da177e4 312
ea110733 313 self = instance;
1da177e4
LT
314
315 IRDA_ASSERT(self != NULL, return;);
316 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
317
318 IRDA_ASSERT(iriap != NULL, return;);
319
320 del_timer(&self->watchdog_timer);
321
322 /* Not needed */
323 if (skb)
324 dev_kfree_skb(skb);
325
326 if (self->mode == IAS_CLIENT) {
0dc47877 327 IRDA_DEBUG(4, "%s(), disconnect as client\n", __func__);
1da177e4
LT
328
329
330 iriap_do_client_event(self, IAP_LM_DISCONNECT_INDICATION,
331 NULL);
332 /*
333 * Inform service user that the request failed by sending
334 * it a NULL value. Warning, the client might close us, so
335 * remember no to use self anymore after calling confirm
336 */
337 if (self->confirm)
338 self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
339 } else {
0dc47877 340 IRDA_DEBUG(4, "%s(), disconnect as server\n", __func__);
1da177e4
LT
341 iriap_do_server_event(self, IAP_LM_DISCONNECT_INDICATION,
342 NULL);
343 iriap_close(self);
344 }
345}
346
347/*
348 * Function iriap_disconnect_request (handle)
349 */
350static void iriap_disconnect_request(struct iriap_cb *self)
351{
352 struct sk_buff *tx_skb;
353
0dc47877 354 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
355
356 IRDA_ASSERT(self != NULL, return;);
357 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
358
1b0fee7d 359 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
1da177e4 360 if (tx_skb == NULL) {
1b0fee7d
SO
361 IRDA_DEBUG(0,
362 "%s(), Could not allocate an sk_buff of length %d\n",
0dc47877 363 __func__, LMP_MAX_HEADER);
1da177e4
LT
364 return;
365 }
366
367 /*
368 * Reserve space for MUX control and LAP header
369 */
370 skb_reserve(tx_skb, LMP_MAX_HEADER);
371
372 irlmp_disconnect_request(self->lsap, tx_skb);
373}
374
375/*
376 * Function iriap_getvaluebyclass (addr, name, attr)
377 *
4a4efbde 378 * Retrieve all values from attribute in all objects with given class
1da177e4
LT
379 * name
380 */
381int iriap_getvaluebyclass_request(struct iriap_cb *self,
382 __u32 saddr, __u32 daddr,
383 char *name, char *attr)
384{
385 struct sk_buff *tx_skb;
386 int name_len, attr_len, skb_len;
387 __u8 *frame;
388
389 IRDA_ASSERT(self != NULL, return -1;);
390 IRDA_ASSERT(self->magic == IAS_MAGIC, return -1;);
391
392 /* Client must supply the destination device address */
393 if (!daddr)
394 return -1;
395
396 self->daddr = daddr;
397 self->saddr = saddr;
398
399 /*
400 * Save operation, so we know what the later indication is about
401 */
402 self->operation = GET_VALUE_BY_CLASS;
403
404 /* Give ourselves 10 secs to finish this operation */
405 iriap_start_watchdog_timer(self, 10*HZ);
406
407 name_len = strlen(name); /* Up to IAS_MAX_CLASSNAME = 60 */
408 attr_len = strlen(attr); /* Up to IAS_MAX_ATTRIBNAME = 60 */
409
410 skb_len = self->max_header_size+2+name_len+1+attr_len+4;
485fb2c9 411 tx_skb = alloc_skb(skb_len, GFP_ATOMIC);
1da177e4
LT
412 if (!tx_skb)
413 return -ENOMEM;
414
415 /* Reserve space for MUX and LAP header */
416 skb_reserve(tx_skb, self->max_header_size);
417 skb_put(tx_skb, 3+name_len+attr_len);
418 frame = tx_skb->data;
419
420 /* Build frame */
421 frame[0] = IAP_LST | GET_VALUE_BY_CLASS;
422 frame[1] = name_len; /* Insert length of name */
423 memcpy(frame+2, name, name_len); /* Insert name */
424 frame[2+name_len] = attr_len; /* Insert length of attr */
425 memcpy(frame+3+name_len, attr, attr_len); /* Insert attr */
426
427 iriap_do_client_event(self, IAP_CALL_REQUEST_GVBC, tx_skb);
428
429 /* Drop reference count - see state_s_disconnect(). */
430 dev_kfree_skb(tx_skb);
431
432 return 0;
433}
434EXPORT_SYMBOL(iriap_getvaluebyclass_request);
435
436/*
437 * Function iriap_getvaluebyclass_confirm (self, skb)
438 *
439 * Got result from GetValueByClass command. Parse it and return result
440 * to service user.
441 *
442 */
443static void iriap_getvaluebyclass_confirm(struct iriap_cb *self,
444 struct sk_buff *skb)
445{
446 struct ias_value *value;
447 int charset;
448 __u32 value_len;
449 __u32 tmp_cpu32;
450 __u16 obj_id;
451 __u16 len;
452 __u8 type;
453 __u8 *fp;
454 int n;
455
456 IRDA_ASSERT(self != NULL, return;);
457 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
458 IRDA_ASSERT(skb != NULL, return;);
459
460 /* Initialize variables */
461 fp = skb->data;
462 n = 2;
463
464 /* Get length, MSB first */
260ffeed
HH
465 len = get_unaligned_be16(fp + n);
466 n += 2;
1da177e4 467
0dc47877 468 IRDA_DEBUG(4, "%s(), len=%d\n", __func__, len);
1da177e4
LT
469
470 /* Get object ID, MSB first */
260ffeed
HH
471 obj_id = get_unaligned_be16(fp + n);
472 n += 2;
1da177e4
LT
473
474 type = fp[n++];
0dc47877 475 IRDA_DEBUG(4, "%s(), Value type = %d\n", __func__, type);
1da177e4
LT
476
477 switch (type) {
478 case IAS_INTEGER:
479 memcpy(&tmp_cpu32, fp+n, 4); n += 4;
480 be32_to_cpus(&tmp_cpu32);
481 value = irias_new_integer_value(tmp_cpu32);
482
483 /* Legal values restricted to 0x01-0x6f, page 15 irttp */
0dc47877 484 IRDA_DEBUG(4, "%s(), lsap=%d\n", __func__, value->t.integer);
1da177e4
LT
485 break;
486 case IAS_STRING:
487 charset = fp[n++];
488
489 switch (charset) {
490 case CS_ASCII:
491 break;
492/* case CS_ISO_8859_1: */
493/* case CS_ISO_8859_2: */
494/* case CS_ISO_8859_3: */
495/* case CS_ISO_8859_4: */
496/* case CS_ISO_8859_5: */
497/* case CS_ISO_8859_6: */
498/* case CS_ISO_8859_7: */
499/* case CS_ISO_8859_8: */
500/* case CS_ISO_8859_9: */
501/* case CS_UNICODE: */
502 default:
90c7881e
DC
503 IRDA_DEBUG(0, "%s(), charset [%d] %s, not supported\n",
504 __func__, charset,
505 charset < ARRAY_SIZE(ias_charset_types) ?
506 ias_charset_types[charset] :
507 "(unknown)");
1da177e4
LT
508
509 /* Aborting, close connection! */
510 iriap_disconnect_request(self);
511 return;
512 /* break; */
513 }
514 value_len = fp[n++];
0dc47877 515 IRDA_DEBUG(4, "%s(), strlen=%d\n", __func__, value_len);
1da177e4
LT
516
517 /* Make sure the string is null-terminated */
37f9fc45
SO
518 if (n + value_len < skb->len)
519 fp[n + value_len] = 0x00;
1da177e4
LT
520 IRDA_DEBUG(4, "Got string %s\n", fp+n);
521
522 /* Will truncate to IAS_MAX_STRING bytes */
523 value = irias_new_string_value(fp+n);
524 break;
525 case IAS_OCT_SEQ:
260ffeed 526 value_len = get_unaligned_be16(fp + n);
1da177e4
LT
527 n += 2;
528
529 /* Will truncate to IAS_MAX_OCTET_STRING bytes */
530 value = irias_new_octseq_value(fp+n, value_len);
531 break;
532 default:
533 value = irias_new_missing_value();
534 break;
535 }
536
537 /* Finished, close connection! */
538 iriap_disconnect_request(self);
539
540 /* Warning, the client might close us, so remember no to use self
541 * anymore after calling confirm
542 */
543 if (self->confirm)
544 self->confirm(IAS_SUCCESS, obj_id, value, self->priv);
545 else {
0dc47877 546 IRDA_DEBUG(0, "%s(), missing handler!\n", __func__);
1da177e4
LT
547 irias_delete_value(value);
548 }
549}
550
551/*
552 * Function iriap_getvaluebyclass_response ()
553 *
554 * Send answer back to remote LM-IAS
555 *
556 */
557static void iriap_getvaluebyclass_response(struct iriap_cb *self,
558 __u16 obj_id,
559 __u8 ret_code,
560 struct ias_value *value)
561{
562 struct sk_buff *tx_skb;
563 int n;
448c31aa 564 __be32 tmp_be32;
405a42c5 565 __be16 tmp_be16;
1da177e4
LT
566 __u8 *fp;
567
0dc47877 568 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
569
570 IRDA_ASSERT(self != NULL, return;);
571 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
572 IRDA_ASSERT(value != NULL, return;);
573 IRDA_ASSERT(value->len <= 1024, return;);
574
575 /* Initialize variables */
576 n = 0;
577
578 /*
579 * We must adjust the size of the response after the length of the
580 * value. We add 32 bytes because of the 6 bytes for the frame and
581 * max 5 bytes for the value coding.
582 */
485fb2c9
SO
583 tx_skb = alloc_skb(value->len + self->max_header_size + 32,
584 GFP_ATOMIC);
1da177e4
LT
585 if (!tx_skb)
586 return;
587
588 /* Reserve space for MUX and LAP header */
589 skb_reserve(tx_skb, self->max_header_size);
590 skb_put(tx_skb, 6);
591
592 fp = tx_skb->data;
593
594 /* Build frame */
595 fp[n++] = GET_VALUE_BY_CLASS | IAP_LST;
596 fp[n++] = ret_code;
597
598 /* Insert list length (MSB first) */
6d82de9e 599 tmp_be16 = htons(0x0001);
1da177e4
LT
600 memcpy(fp+n, &tmp_be16, 2); n += 2;
601
602 /* Insert object identifier ( MSB first) */
603 tmp_be16 = cpu_to_be16(obj_id);
604 memcpy(fp+n, &tmp_be16, 2); n += 2;
605
606 switch (value->type) {
607 case IAS_STRING:
608 skb_put(tx_skb, 3 + value->len);
609 fp[n++] = value->type;
610 fp[n++] = 0; /* ASCII */
611 fp[n++] = (__u8) value->len;
612 memcpy(fp+n, value->t.string, value->len); n+=value->len;
613 break;
614 case IAS_INTEGER:
615 skb_put(tx_skb, 5);
616 fp[n++] = value->type;
617
618 tmp_be32 = cpu_to_be32(value->t.integer);
619 memcpy(fp+n, &tmp_be32, 4); n += 4;
620 break;
621 case IAS_OCT_SEQ:
622 skb_put(tx_skb, 3 + value->len);
623 fp[n++] = value->type;
624
625 tmp_be16 = cpu_to_be16(value->len);
626 memcpy(fp+n, &tmp_be16, 2); n += 2;
627 memcpy(fp+n, value->t.oct_seq, value->len); n+=value->len;
628 break;
629 case IAS_MISSING:
0dc47877 630 IRDA_DEBUG( 3, "%s: sending IAS_MISSING\n", __func__);
1da177e4
LT
631 skb_put(tx_skb, 1);
632 fp[n++] = value->type;
633 break;
634 default:
0dc47877 635 IRDA_DEBUG(0, "%s(), type not implemented!\n", __func__);
1da177e4
LT
636 break;
637 }
638 iriap_do_r_connect_event(self, IAP_CALL_RESPONSE, tx_skb);
639
640 /* Drop reference count - see state_r_execute(). */
641 dev_kfree_skb(tx_skb);
642}
643
644/*
645 * Function iriap_getvaluebyclass_indication (self, skb)
646 *
647 * getvaluebyclass is requested from peer LM-IAS
648 *
649 */
650static void iriap_getvaluebyclass_indication(struct iriap_cb *self,
651 struct sk_buff *skb)
652{
653 struct ias_object *obj;
654 struct ias_attrib *attrib;
655 int name_len;
656 int attr_len;
657 char name[IAS_MAX_CLASSNAME + 1]; /* 60 bytes */
658 char attr[IAS_MAX_ATTRIBNAME + 1]; /* 60 bytes */
659 __u8 *fp;
660 int n;
661
0dc47877 662 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
663
664 IRDA_ASSERT(self != NULL, return;);
665 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
666 IRDA_ASSERT(skb != NULL, return;);
667
668 fp = skb->data;
669 n = 1;
670
671 name_len = fp[n++];
d370af0e
DR
672
673 IRDA_ASSERT(name_len < IAS_MAX_CLASSNAME + 1, return;);
674
1da177e4
LT
675 memcpy(name, fp+n, name_len); n+=name_len;
676 name[name_len] = '\0';
677
678 attr_len = fp[n++];
d370af0e
DR
679
680 IRDA_ASSERT(attr_len < IAS_MAX_ATTRIBNAME + 1, return;);
681
1da177e4
LT
682 memcpy(attr, fp+n, attr_len); n+=attr_len;
683 attr[attr_len] = '\0';
684
685 IRDA_DEBUG(4, "LM-IAS: Looking up %s: %s\n", name, attr);
686 obj = irias_find_object(name);
687
688 if (obj == NULL) {
689 IRDA_DEBUG(2, "LM-IAS: Object %s not found\n", name);
690 iriap_getvaluebyclass_response(self, 0x1235, IAS_CLASS_UNKNOWN,
691 &irias_missing);
692 return;
693 }
694 IRDA_DEBUG(4, "LM-IAS: found %s, id=%d\n", obj->name, obj->id);
695
696 attrib = irias_find_attrib(obj, attr);
697 if (attrib == NULL) {
698 IRDA_DEBUG(2, "LM-IAS: Attribute %s not found\n", attr);
699 iriap_getvaluebyclass_response(self, obj->id,
6819bc2e 700 IAS_ATTRIB_UNKNOWN,
1da177e4
LT
701 &irias_missing);
702 return;
703 }
704
705 /* We have a match; send the value. */
706 iriap_getvaluebyclass_response(self, obj->id, IAS_SUCCESS,
707 attrib->value);
1da177e4
LT
708}
709
710/*
711 * Function iriap_send_ack (void)
712 *
713 * Currently not used
714 *
715 */
716void iriap_send_ack(struct iriap_cb *self)
717{
718 struct sk_buff *tx_skb;
719 __u8 *frame;
720
0dc47877 721 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
722
723 IRDA_ASSERT(self != NULL, return;);
724 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
725
1b0fee7d 726 tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
1da177e4
LT
727 if (!tx_skb)
728 return;
729
730 /* Reserve space for MUX and LAP header */
731 skb_reserve(tx_skb, self->max_header_size);
732 skb_put(tx_skb, 1);
733 frame = tx_skb->data;
734
735 /* Build frame */
736 frame[0] = IAP_LST | IAP_ACK | self->operation;
737
738 irlmp_data_request(self->lsap, tx_skb);
739}
740
741void iriap_connect_request(struct iriap_cb *self)
742{
743 int ret;
744
745 IRDA_ASSERT(self != NULL, return;);
746 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
747
748 ret = irlmp_connect_request(self->lsap, LSAP_IAS,
749 self->saddr, self->daddr,
750 NULL, NULL);
751 if (ret < 0) {
0dc47877 752 IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
1da177e4
LT
753 self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
754 }
755}
756
757/*
758 * Function iriap_connect_confirm (handle, skb)
759 *
760 * LSAP connection confirmed!
761 *
762 */
763static void iriap_connect_confirm(void *instance, void *sap,
764 struct qos_info *qos, __u32 max_seg_size,
765 __u8 max_header_size,
766 struct sk_buff *skb)
767{
768 struct iriap_cb *self;
769
ea110733 770 self = instance;
1da177e4
LT
771
772 IRDA_ASSERT(self != NULL, return;);
773 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
774 IRDA_ASSERT(skb != NULL, return;);
775
776 self->max_data_size = max_seg_size;
777 self->max_header_size = max_header_size;
778
779 del_timer(&self->watchdog_timer);
780
781 iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
782
783 /* Drop reference count - see state_s_make_call(). */
784 dev_kfree_skb(skb);
785}
786
787/*
788 * Function iriap_connect_indication ( handle, skb)
789 *
790 * Remote LM-IAS is requesting connection
791 *
792 */
793static void iriap_connect_indication(void *instance, void *sap,
794 struct qos_info *qos, __u32 max_seg_size,
795 __u8 max_header_size,
796 struct sk_buff *skb)
797{
798 struct iriap_cb *self, *new;
799
0dc47877 800 IRDA_DEBUG(1, "%s()\n", __func__);
1da177e4 801
ea110733 802 self = instance;
1da177e4
LT
803
804 IRDA_ASSERT(skb != NULL, return;);
805 IRDA_ASSERT(self != NULL, goto out;);
806 IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
807
808 /* Start new server */
809 new = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
810 if (!new) {
0dc47877 811 IRDA_DEBUG(0, "%s(), open failed\n", __func__);
1da177e4
LT
812 goto out;
813 }
814
815 /* Now attach up the new "socket" */
816 new->lsap = irlmp_dup(self->lsap, new);
817 if (!new->lsap) {
0dc47877 818 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
1da177e4
LT
819 goto out;
820 }
821
822 new->max_data_size = max_seg_size;
823 new->max_header_size = max_header_size;
824
825 /* Clean up the original one to keep it in listen state */
826 irlmp_listen(self->lsap);
827
828 iriap_do_server_event(new, IAP_LM_CONNECT_INDICATION, skb);
829
830out:
831 /* Drop reference count - see state_r_disconnect(). */
832 dev_kfree_skb(skb);
833}
834
835/*
836 * Function iriap_data_indication (handle, skb)
837 *
838 * Receives data from connection identified by handle from IrLMP
839 *
840 */
841static int iriap_data_indication(void *instance, void *sap,
842 struct sk_buff *skb)
843{
844 struct iriap_cb *self;
845 __u8 *frame;
846 __u8 opcode;
847
0dc47877 848 IRDA_DEBUG(3, "%s()\n", __func__);
1da177e4 849
ea110733 850 self = instance;
1da177e4
LT
851
852 IRDA_ASSERT(skb != NULL, return 0;);
853 IRDA_ASSERT(self != NULL, goto out;);
854 IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
855
856 frame = skb->data;
857
858 if (self->mode == IAS_SERVER) {
859 /* Call server */
0dc47877 860 IRDA_DEBUG(4, "%s(), Calling server!\n", __func__);
1da177e4
LT
861 iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
862 goto out;
863 }
864 opcode = frame[0];
865 if (~opcode & IAP_LST) {
866 IRDA_WARNING("%s:, IrIAS multiframe commands or "
867 "results is not implemented yet!\n",
0dc47877 868 __func__);
1da177e4
LT
869 goto out;
870 }
871
872 /* Check for ack frames since they don't contain any data */
873 if (opcode & IAP_ACK) {
0dc47877 874 IRDA_DEBUG(0, "%s() Got ack frame!\n", __func__);
1da177e4
LT
875 goto out;
876 }
877
878 opcode &= ~IAP_LST; /* Mask away LST bit */
879
880 switch (opcode) {
881 case GET_INFO_BASE:
882 IRDA_DEBUG(0, "IrLMP GetInfoBaseDetails not implemented!\n");
883 break;
884 case GET_VALUE_BY_CLASS:
885 iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
886
887 switch (frame[1]) {
888 case IAS_SUCCESS:
889 iriap_getvaluebyclass_confirm(self, skb);
890 break;
891 case IAS_CLASS_UNKNOWN:
0dc47877 892 IRDA_DEBUG(1, "%s(), No such class!\n", __func__);
1da177e4
LT
893 /* Finished, close connection! */
894 iriap_disconnect_request(self);
895
896 /*
897 * Warning, the client might close us, so remember
898 * no to use self anymore after calling confirm
899 */
900 if (self->confirm)
901 self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
902 self->priv);
903 break;
904 case IAS_ATTRIB_UNKNOWN:
0dc47877 905 IRDA_DEBUG(1, "%s(), No such attribute!\n", __func__);
1da177e4
LT
906 /* Finished, close connection! */
907 iriap_disconnect_request(self);
908
909 /*
910 * Warning, the client might close us, so remember
911 * no to use self anymore after calling confirm
912 */
913 if (self->confirm)
914 self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
915 self->priv);
916 break;
917 }
918 break;
919 default:
0dc47877 920 IRDA_DEBUG(0, "%s(), Unknown op-code: %02x\n", __func__,
1da177e4
LT
921 opcode);
922 break;
923 }
924
925out:
926 /* Cleanup - sub-calls will have done skb_get() as needed. */
927 dev_kfree_skb(skb);
928 return 0;
929}
930
931/*
932 * Function iriap_call_indication (self, skb)
933 *
934 * Received call to server from peer LM-IAS
935 *
936 */
937void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
938{
939 __u8 *fp;
940 __u8 opcode;
941
0dc47877 942 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
943
944 IRDA_ASSERT(self != NULL, return;);
945 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
946 IRDA_ASSERT(skb != NULL, return;);
947
948 fp = skb->data;
949
950 opcode = fp[0];
951 if (~opcode & 0x80) {
a572da43 952 IRDA_WARNING("%s: IrIAS multiframe commands or results "
0dc47877 953 "is not implemented yet!\n", __func__);
1da177e4
LT
954 return;
955 }
956 opcode &= 0x7f; /* Mask away LST bit */
957
958 switch (opcode) {
959 case GET_INFO_BASE:
960 IRDA_WARNING("%s: GetInfoBaseDetails not implemented yet!\n",
0dc47877 961 __func__);
1da177e4
LT
962 break;
963 case GET_VALUE_BY_CLASS:
964 iriap_getvaluebyclass_indication(self, skb);
965 break;
966 }
967 /* skb will be cleaned up in iriap_data_indication */
968}
969
970/*
971 * Function iriap_watchdog_timer_expired (data)
972 *
973 * Query has taken too long time, so abort
974 *
975 */
976static void iriap_watchdog_timer_expired(void *data)
977{
978 struct iriap_cb *self = (struct iriap_cb *) data;
979
980 IRDA_ASSERT(self != NULL, return;);
981 IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
982
983 /* iriap_close(self); */
984}
985
986#ifdef CONFIG_PROC_FS
987
36cbd3dc 988static const char *const ias_value_types[] = {
1da177e4
LT
989 "IAS_MISSING",
990 "IAS_INTEGER",
991 "IAS_OCT_SEQ",
992 "IAS_STRING"
993};
994
6819bc2e 995static inline struct ias_object *irias_seq_idx(loff_t pos)
1da177e4
LT
996{
997 struct ias_object *obj;
998
999 for (obj = (struct ias_object *) hashbin_get_first(irias_objects);
1000 obj; obj = (struct ias_object *) hashbin_get_next(irias_objects)) {
1001 if (pos-- == 0)
1002 break;
1003 }
6819bc2e 1004
1da177e4
LT
1005 return obj;
1006}
1007
1008static void *irias_seq_start(struct seq_file *seq, loff_t *pos)
1009{
1010 spin_lock_irq(&irias_objects->hb_spinlock);
1011
1012 return *pos ? irias_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1013}
1014
1015static void *irias_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1016{
1017 ++*pos;
1018
6819bc2e 1019 return (v == SEQ_START_TOKEN)
1da177e4
LT
1020 ? (void *) hashbin_get_first(irias_objects)
1021 : (void *) hashbin_get_next(irias_objects);
1022}
1023
1024static void irias_seq_stop(struct seq_file *seq, void *v)
1025{
1026 spin_unlock_irq(&irias_objects->hb_spinlock);
1027}
1028
1029static int irias_seq_show(struct seq_file *seq, void *v)
1030{
1031 if (v == SEQ_START_TOKEN)
1032 seq_puts(seq, "LM-IAS Objects:\n");
1033 else {
1034 struct ias_object *obj = v;
1035 struct ias_attrib *attrib;
1036
1037 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -EINVAL;);
1038
1039 seq_printf(seq, "name: %s, id=%d\n",
1040 obj->name, obj->id);
1041
1042 /* Careful for priority inversions here !
1043 * All other uses of attrib spinlock are independent of
1044 * the object spinlock, so we are safe. Jean II */
1045 spin_lock(&obj->attribs->hb_spinlock);
1046
1047 /* List all attributes for this object */
1048 for (attrib = (struct ias_attrib *) hashbin_get_first(obj->attribs);
1049 attrib != NULL;
1050 attrib = (struct ias_attrib *) hashbin_get_next(obj->attribs)) {
6819bc2e 1051
1da177e4
LT
1052 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC,
1053 goto outloop; );
1054
1055 seq_printf(seq, " - Attribute name: \"%s\", ",
1056 attrib->name);
1057 seq_printf(seq, "value[%s]: ",
1058 ias_value_types[attrib->value->type]);
1059
1060 switch (attrib->value->type) {
1061 case IAS_INTEGER:
1062 seq_printf(seq, "%d\n",
1063 attrib->value->t.integer);
1064 break;
1065 case IAS_STRING:
1066 seq_printf(seq, "\"%s\"\n",
1067 attrib->value->t.string);
1068 break;
1069 case IAS_OCT_SEQ:
6819bc2e 1070 seq_printf(seq, "octet sequence (%d bytes)\n",
1da177e4
LT
1071 attrib->value->len);
1072 break;
1073 case IAS_MISSING:
1074 seq_puts(seq, "missing\n");
1075 break;
1076 default:
6819bc2e 1077 seq_printf(seq, "type %d?\n",
1da177e4
LT
1078 attrib->value->type);
1079 }
1080 seq_putc(seq, '\n');
1081
1082 }
1083 IRDA_ASSERT_LABEL(outloop:)
1084 spin_unlock(&obj->attribs->hb_spinlock);
1085 }
1086
1087 return 0;
1088}
1089
56b3d975 1090static const struct seq_operations irias_seq_ops = {
1da177e4
LT
1091 .start = irias_seq_start,
1092 .next = irias_seq_next,
1093 .stop = irias_seq_stop,
1094 .show = irias_seq_show,
1095};
1096
1097static int irias_seq_open(struct inode *inode, struct file *file)
1098{
1099 IRDA_ASSERT( irias_objects != NULL, return -EINVAL;);
1100
1101 return seq_open(file, &irias_seq_ops);
1102}
1103
da7071d7 1104const struct file_operations irias_seq_fops = {
1da177e4
LT
1105 .owner = THIS_MODULE,
1106 .open = irias_seq_open,
1107 .read = seq_read,
1108 .llseek = seq_lseek,
1109 .release = seq_release,
1110};
1111
1112#endif /* PROC_FS */