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 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
53 net_warn_ratelimited("%s(), Unable to allocate object!\n",
58 obj->magic = IAS_OBJECT_MAGIC;
59 obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC);
61 net_warn_ratelimited("%s(), Unable to allocate name!\n",
68 /* Locking notes : the attrib spinlock has lower precendence
69 * than the objects spinlock. Never grap the objects spinlock
70 * while holding any attrib spinlock (risk of deadlock). Jean II */
71 obj->attribs = hashbin_new(HB_LOCK);
73 if (obj->attribs == NULL) {
74 net_warn_ratelimited("%s(), Unable to allocate attribs!\n",
83 EXPORT_SYMBOL(irias_new_object);
86 * Function irias_delete_attrib (attrib)
88 * Delete given attribute and deallocate all its memory
91 static void __irias_delete_attrib(struct ias_attrib *attrib)
93 IRDA_ASSERT(attrib != NULL, return;);
94 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
98 irias_delete_value(attrib->value);
99 attrib->magic = ~IAS_ATTRIB_MAGIC;
104 void __irias_delete_object(struct ias_object *obj)
106 IRDA_ASSERT(obj != NULL, return;);
107 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
111 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
113 obj->magic = ~IAS_OBJECT_MAGIC;
119 * Function irias_delete_object (obj)
121 * Remove object from hashbin and deallocate all attributes associated with
122 * with this object and the object itself
125 int irias_delete_object(struct ias_object *obj)
127 struct ias_object *node;
129 IRDA_ASSERT(obj != NULL, return -1;);
130 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
132 /* Remove from list */
133 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
135 pr_debug("%s(), object already removed!\n",
139 __irias_delete_object(obj);
143 EXPORT_SYMBOL(irias_delete_object);
146 * Function irias_delete_attrib (obj)
148 * Remove attribute from hashbin and, if it was the last attribute of
149 * the object, remove the object as well.
152 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
155 struct ias_attrib *node;
157 IRDA_ASSERT(obj != NULL, return -1;);
158 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
159 IRDA_ASSERT(attrib != NULL, return -1;);
161 /* Remove attribute from object */
162 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
164 return 0; /* Already removed or non-existent */
166 /* Deallocate attribute */
167 __irias_delete_attrib(node);
169 /* Check if object has still some attributes, destroy it if none.
170 * At first glance, this look dangerous, as the kernel reference
171 * various IAS objects. However, we only use this function on
172 * user attributes, not kernel attributes, so there is no risk
173 * of deleting a kernel object this way. Jean II */
174 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
175 if (cleanobject && !node)
176 irias_delete_object(obj);
182 * Function irias_insert_object (obj)
184 * Insert an object into the LM-IAS database
187 void irias_insert_object(struct ias_object *obj)
189 IRDA_ASSERT(obj != NULL, return;);
190 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
192 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
194 EXPORT_SYMBOL(irias_insert_object);
197 * Function irias_find_object (name)
199 * Find object with given name
202 struct ias_object *irias_find_object(char *name)
204 IRDA_ASSERT(name != NULL, return NULL;);
206 /* Unsafe (locking), object might change */
207 return hashbin_lock_find(irias_objects, 0, name);
209 EXPORT_SYMBOL(irias_find_object);
212 * Function irias_find_attrib (obj, name)
214 * Find named attribute in object
217 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
219 struct ias_attrib *attrib;
221 IRDA_ASSERT(obj != NULL, return NULL;);
222 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
223 IRDA_ASSERT(name != NULL, return NULL;);
225 attrib = hashbin_lock_find(obj->attribs, 0, name);
229 /* Unsafe (locking), attrib might change */
234 * Function irias_add_attribute (obj, attrib)
236 * Add attribute to object
239 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
242 IRDA_ASSERT(obj != NULL, return;);
243 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
245 IRDA_ASSERT(attrib != NULL, return;);
246 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
248 /* Set if attrib is owned by kernel or user space */
249 attrib->value->owner = owner;
251 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
255 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
257 * Change the value of an objects attribute.
260 int irias_object_change_attribute(char *obj_name, char *attrib_name,
261 struct ias_value *new_value)
263 struct ias_object *obj;
264 struct ias_attrib *attrib;
268 obj = hashbin_lock_find(irias_objects, 0, obj_name);
270 net_warn_ratelimited("%s: Unable to find object: %s\n",
275 /* Slightly unsafe (obj might get removed under us) */
276 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
279 attrib = hashbin_find(obj->attribs, 0, attrib_name);
280 if (attrib == NULL) {
281 net_warn_ratelimited("%s: Unable to find attribute: %s\n",
282 __func__, attrib_name);
283 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
287 if ( attrib->value->type != new_value->type) {
288 pr_debug("%s(), changing value type not allowed!\n",
290 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
294 /* Delete old value */
295 irias_delete_value(attrib->value);
297 /* Insert new value */
298 attrib->value = new_value;
301 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
304 EXPORT_SYMBOL(irias_object_change_attribute);
307 * Function irias_object_add_integer_attrib (obj, name, value)
309 * Add an integer attribute to an LM-IAS object
312 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
315 struct ias_attrib *attrib;
317 IRDA_ASSERT(obj != NULL, return;);
318 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
319 IRDA_ASSERT(name != NULL, return;);
321 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
322 if (attrib == NULL) {
323 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
328 attrib->magic = IAS_ATTRIB_MAGIC;
329 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
332 attrib->value = irias_new_integer_value(value);
333 if (!attrib->name || !attrib->value) {
334 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
337 irias_delete_value(attrib->value);
343 irias_add_attrib(obj, attrib, owner);
345 EXPORT_SYMBOL(irias_add_integer_attrib);
348 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
350 * Add a octet sequence attribute to an LM-IAS object
354 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
357 struct ias_attrib *attrib;
359 IRDA_ASSERT(obj != NULL, return;);
360 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
362 IRDA_ASSERT(name != NULL, return;);
363 IRDA_ASSERT(octets != NULL, return;);
365 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
366 if (attrib == NULL) {
367 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
372 attrib->magic = IAS_ATTRIB_MAGIC;
373 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
375 attrib->value = irias_new_octseq_value( octets, len);
376 if (!attrib->name || !attrib->value) {
377 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
380 irias_delete_value(attrib->value);
386 irias_add_attrib(obj, attrib, owner);
388 EXPORT_SYMBOL(irias_add_octseq_attrib);
391 * Function irias_object_add_string_attrib (obj, string)
393 * Add a string attribute to an LM-IAS object
396 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
399 struct ias_attrib *attrib;
401 IRDA_ASSERT(obj != NULL, return;);
402 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
404 IRDA_ASSERT(name != NULL, return;);
405 IRDA_ASSERT(value != NULL, return;);
407 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
408 if (attrib == NULL) {
409 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
414 attrib->magic = IAS_ATTRIB_MAGIC;
415 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
417 attrib->value = irias_new_string_value(value);
418 if (!attrib->name || !attrib->value) {
419 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
422 irias_delete_value(attrib->value);
428 irias_add_attrib(obj, attrib, owner);
430 EXPORT_SYMBOL(irias_add_string_attrib);
433 * Function irias_new_integer_value (integer)
435 * Create new IAS integer value
438 struct ias_value *irias_new_integer_value(int integer)
440 struct ias_value *value;
442 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
446 value->type = IAS_INTEGER;
448 value->t.integer = integer;
452 EXPORT_SYMBOL(irias_new_integer_value);
455 * Function irias_new_string_value (string)
457 * Create new IAS string value
459 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
461 struct ias_value *irias_new_string_value(char *string)
463 struct ias_value *value;
465 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
469 value->type = IAS_STRING;
470 value->charset = CS_ASCII;
471 value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC);
472 if (!value->t.string) {
473 net_warn_ratelimited("%s: Unable to kmalloc!\n", __func__);
478 value->len = strlen(value->t.string);
484 * Function irias_new_octseq_value (octets, len)
486 * Create new IAS octet-sequence value
488 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
490 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
492 struct ias_value *value;
494 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
498 value->type = IAS_OCT_SEQ;
500 if(len > IAS_MAX_OCTET_STRING)
501 len = IAS_MAX_OCTET_STRING;
504 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
505 if (value->t.oct_seq == NULL){
506 net_warn_ratelimited("%s: Unable to kmalloc!\n", __func__);
513 struct ias_value *irias_new_missing_value(void)
515 struct ias_value *value;
517 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
521 value->type = IAS_MISSING;
527 * Function irias_delete_value (value)
532 void irias_delete_value(struct ias_value *value)
534 IRDA_ASSERT(value != NULL, return;);
536 switch (value->type) {
537 case IAS_INTEGER: /* Fallthrough */
539 /* No need to deallocate */
542 /* Deallocate string */
543 kfree(value->t.string);
546 /* Deallocate byte stream */
547 kfree(value->t.oct_seq);
550 pr_debug("%s(), Unknown value type!\n", __func__);
555 EXPORT_SYMBOL(irias_delete_value);