1 /*********************************************************************
3 * Filename: irias_object.c
5 * Description: IAS object database and functions
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Thu Oct 1 22:50:04 1998
9 * Modified at: Wed Dec 15 11:23:16 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * Neither Dag Brattli nor University of Tromsø admit liability nor
20 * provide warranty for any of this software. This material is
21 * provided "AS-IS" and at no charge.
23 ********************************************************************/
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/module.h>
30 #include <net/irda/irda.h>
31 #include <net/irda/irias_object.h>
33 hashbin_t
*irias_objects
;
36 * Used when a missing value needs to be returned
38 struct ias_value irias_missing
= { IAS_MISSING
, 0, 0, 0, {0}};
42 * Function ias_new_object (name, id)
44 * Create a new IAS object
47 struct ias_object
*irias_new_object( char *name
, int id
)
49 struct ias_object
*obj
;
51 IRDA_DEBUG( 4, "%s()\n", __func__
);
53 obj
= kzalloc(sizeof(struct ias_object
), GFP_ATOMIC
);
55 IRDA_WARNING("%s(), Unable to allocate object!\n",
60 obj
->magic
= IAS_OBJECT_MAGIC
;
61 obj
->name
= kstrndup(name
, IAS_MAX_CLASSNAME
, GFP_ATOMIC
);
63 IRDA_WARNING("%s(), Unable to allocate name!\n",
70 /* Locking notes : the attrib spinlock has lower precendence
71 * than the objects spinlock. Never grap the objects spinlock
72 * while holding any attrib spinlock (risk of deadlock). Jean II */
73 obj
->attribs
= hashbin_new(HB_LOCK
);
75 if (obj
->attribs
== NULL
) {
76 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
85 EXPORT_SYMBOL(irias_new_object
);
88 * Function irias_delete_attrib (attrib)
90 * Delete given attribute and deallocate all its memory
93 static void __irias_delete_attrib(struct ias_attrib
*attrib
)
95 IRDA_ASSERT(attrib
!= NULL
, return;);
96 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
100 irias_delete_value(attrib
->value
);
101 attrib
->magic
= ~IAS_ATTRIB_MAGIC
;
106 void __irias_delete_object(struct ias_object
*obj
)
108 IRDA_ASSERT(obj
!= NULL
, return;);
109 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
113 hashbin_delete(obj
->attribs
, (FREE_FUNC
) __irias_delete_attrib
);
115 obj
->magic
= ~IAS_OBJECT_MAGIC
;
121 * Function irias_delete_object (obj)
123 * Remove object from hashbin and deallocate all attributes associated with
124 * with this object and the object itself
127 int irias_delete_object(struct ias_object
*obj
)
129 struct ias_object
*node
;
131 IRDA_ASSERT(obj
!= NULL
, return -1;);
132 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
134 /* Remove from list */
135 node
= hashbin_remove_this(irias_objects
, (irda_queue_t
*) obj
);
137 IRDA_DEBUG( 0, "%s(), object already removed!\n",
141 __irias_delete_object(obj
);
145 EXPORT_SYMBOL(irias_delete_object
);
148 * Function irias_delete_attrib (obj)
150 * Remove attribute from hashbin and, if it was the last attribute of
151 * the object, remove the object as well.
154 int irias_delete_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
157 struct ias_attrib
*node
;
159 IRDA_ASSERT(obj
!= NULL
, return -1;);
160 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
161 IRDA_ASSERT(attrib
!= NULL
, return -1;);
163 /* Remove attribute from object */
164 node
= hashbin_remove_this(obj
->attribs
, (irda_queue_t
*) attrib
);
166 return 0; /* Already removed or non-existent */
168 /* Deallocate attribute */
169 __irias_delete_attrib(node
);
171 /* Check if object has still some attributes, destroy it if none.
172 * At first glance, this look dangerous, as the kernel reference
173 * various IAS objects. However, we only use this function on
174 * user attributes, not kernel attributes, so there is no risk
175 * of deleting a kernel object this way. Jean II */
176 node
= (struct ias_attrib
*) hashbin_get_first(obj
->attribs
);
177 if (cleanobject
&& !node
)
178 irias_delete_object(obj
);
184 * Function irias_insert_object (obj)
186 * Insert an object into the LM-IAS database
189 void irias_insert_object(struct ias_object
*obj
)
191 IRDA_ASSERT(obj
!= NULL
, return;);
192 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
194 hashbin_insert(irias_objects
, (irda_queue_t
*) obj
, 0, obj
->name
);
196 EXPORT_SYMBOL(irias_insert_object
);
199 * Function irias_find_object (name)
201 * Find object with given name
204 struct ias_object
*irias_find_object(char *name
)
206 IRDA_ASSERT(name
!= NULL
, return NULL
;);
208 /* Unsafe (locking), object might change */
209 return hashbin_lock_find(irias_objects
, 0, name
);
211 EXPORT_SYMBOL(irias_find_object
);
214 * Function irias_find_attrib (obj, name)
216 * Find named attribute in object
219 struct ias_attrib
*irias_find_attrib(struct ias_object
*obj
, char *name
)
221 struct ias_attrib
*attrib
;
223 IRDA_ASSERT(obj
!= NULL
, return NULL
;);
224 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return NULL
;);
225 IRDA_ASSERT(name
!= NULL
, return NULL
;);
227 attrib
= hashbin_lock_find(obj
->attribs
, 0, name
);
231 /* Unsafe (locking), attrib might change */
236 * Function irias_add_attribute (obj, attrib)
238 * Add attribute to object
241 static void irias_add_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
244 IRDA_ASSERT(obj
!= NULL
, return;);
245 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
247 IRDA_ASSERT(attrib
!= NULL
, return;);
248 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
250 /* Set if attrib is owned by kernel or user space */
251 attrib
->value
->owner
= owner
;
253 hashbin_insert(obj
->attribs
, (irda_queue_t
*) attrib
, 0, attrib
->name
);
257 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
259 * Change the value of an objects attribute.
262 int irias_object_change_attribute(char *obj_name
, char *attrib_name
,
263 struct ias_value
*new_value
)
265 struct ias_object
*obj
;
266 struct ias_attrib
*attrib
;
270 obj
= hashbin_lock_find(irias_objects
, 0, obj_name
);
272 IRDA_WARNING("%s: Unable to find object: %s\n", __func__
,
277 /* Slightly unsafe (obj might get removed under us) */
278 spin_lock_irqsave(&obj
->attribs
->hb_spinlock
, flags
);
281 attrib
= hashbin_find(obj
->attribs
, 0, attrib_name
);
282 if (attrib
== NULL
) {
283 IRDA_WARNING("%s: Unable to find attribute: %s\n",
284 __func__
, attrib_name
);
285 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
289 if ( attrib
->value
->type
!= new_value
->type
) {
290 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
292 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
296 /* Delete old value */
297 irias_delete_value(attrib
->value
);
299 /* Insert new value */
300 attrib
->value
= new_value
;
303 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
306 EXPORT_SYMBOL(irias_object_change_attribute
);
309 * Function irias_object_add_integer_attrib (obj, name, value)
311 * Add an integer attribute to an LM-IAS object
314 void irias_add_integer_attrib(struct ias_object
*obj
, char *name
, int value
,
317 struct ias_attrib
*attrib
;
319 IRDA_ASSERT(obj
!= NULL
, return;);
320 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
321 IRDA_ASSERT(name
!= NULL
, return;);
323 attrib
= kzalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
324 if (attrib
== NULL
) {
325 IRDA_WARNING("%s: Unable to allocate attribute!\n",
330 attrib
->magic
= IAS_ATTRIB_MAGIC
;
331 attrib
->name
= kstrndup(name
, IAS_MAX_ATTRIBNAME
, GFP_ATOMIC
);
334 attrib
->value
= irias_new_integer_value(value
);
335 if (!attrib
->name
|| !attrib
->value
) {
336 IRDA_WARNING("%s: Unable to allocate attribute!\n",
339 irias_delete_value(attrib
->value
);
345 irias_add_attrib(obj
, attrib
, owner
);
347 EXPORT_SYMBOL(irias_add_integer_attrib
);
350 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
352 * Add a octet sequence attribute to an LM-IAS object
356 void irias_add_octseq_attrib(struct ias_object
*obj
, char *name
, __u8
*octets
,
359 struct ias_attrib
*attrib
;
361 IRDA_ASSERT(obj
!= NULL
, return;);
362 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
364 IRDA_ASSERT(name
!= NULL
, return;);
365 IRDA_ASSERT(octets
!= NULL
, return;);
367 attrib
= kzalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
368 if (attrib
== NULL
) {
369 IRDA_WARNING("%s: Unable to allocate attribute!\n",
374 attrib
->magic
= IAS_ATTRIB_MAGIC
;
375 attrib
->name
= kstrndup(name
, IAS_MAX_ATTRIBNAME
, GFP_ATOMIC
);
377 attrib
->value
= irias_new_octseq_value( octets
, len
);
378 if (!attrib
->name
|| !attrib
->value
) {
379 IRDA_WARNING("%s: Unable to allocate attribute!\n",
382 irias_delete_value(attrib
->value
);
388 irias_add_attrib(obj
, attrib
, owner
);
390 EXPORT_SYMBOL(irias_add_octseq_attrib
);
393 * Function irias_object_add_string_attrib (obj, string)
395 * Add a string attribute to an LM-IAS object
398 void irias_add_string_attrib(struct ias_object
*obj
, char *name
, char *value
,
401 struct ias_attrib
*attrib
;
403 IRDA_ASSERT(obj
!= NULL
, return;);
404 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
406 IRDA_ASSERT(name
!= NULL
, return;);
407 IRDA_ASSERT(value
!= NULL
, return;);
409 attrib
= kzalloc(sizeof( struct ias_attrib
), GFP_ATOMIC
);
410 if (attrib
== NULL
) {
411 IRDA_WARNING("%s: Unable to allocate attribute!\n",
416 attrib
->magic
= IAS_ATTRIB_MAGIC
;
417 attrib
->name
= kstrndup(name
, IAS_MAX_ATTRIBNAME
, GFP_ATOMIC
);
419 attrib
->value
= irias_new_string_value(value
);
420 if (!attrib
->name
|| !attrib
->value
) {
421 IRDA_WARNING("%s: Unable to allocate attribute!\n",
424 irias_delete_value(attrib
->value
);
430 irias_add_attrib(obj
, attrib
, owner
);
432 EXPORT_SYMBOL(irias_add_string_attrib
);
435 * Function irias_new_integer_value (integer)
437 * Create new IAS integer value
440 struct ias_value
*irias_new_integer_value(int integer
)
442 struct ias_value
*value
;
444 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
446 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
450 value
->type
= IAS_INTEGER
;
452 value
->t
.integer
= integer
;
456 EXPORT_SYMBOL(irias_new_integer_value
);
459 * Function irias_new_string_value (string)
461 * Create new IAS string value
463 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
465 struct ias_value
*irias_new_string_value(char *string
)
467 struct ias_value
*value
;
469 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
471 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
475 value
->type
= IAS_STRING
;
476 value
->charset
= CS_ASCII
;
477 value
->t
.string
= kstrndup(string
, IAS_MAX_STRING
, GFP_ATOMIC
);
478 if (!value
->t
.string
) {
479 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
484 value
->len
= strlen(value
->t
.string
);
490 * Function irias_new_octseq_value (octets, len)
492 * Create new IAS octet-sequence value
494 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
496 struct ias_value
*irias_new_octseq_value(__u8
*octseq
, int len
)
498 struct ias_value
*value
;
500 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
502 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
506 value
->type
= IAS_OCT_SEQ
;
508 if(len
> IAS_MAX_OCTET_STRING
)
509 len
= IAS_MAX_OCTET_STRING
;
512 value
->t
.oct_seq
= kmemdup(octseq
, len
, GFP_ATOMIC
);
513 if (value
->t
.oct_seq
== NULL
){
514 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
521 struct ias_value
*irias_new_missing_value(void)
523 struct ias_value
*value
;
525 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
527 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__
);
531 value
->type
= IAS_MISSING
;
537 * Function irias_delete_value (value)
542 void irias_delete_value(struct ias_value
*value
)
544 IRDA_DEBUG(4, "%s()\n", __func__
);
546 IRDA_ASSERT(value
!= NULL
, return;);
548 switch (value
->type
) {
549 case IAS_INTEGER
: /* Fallthrough */
551 /* No need to deallocate */
554 /* Deallocate string */
555 kfree(value
->t
.string
);
558 /* Deallocate byte stream */
559 kfree(value
->t
.oct_seq
);
562 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __func__
);
567 EXPORT_SYMBOL(irias_delete_value
);