Simplify kallsyms_lookup()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / irda / irias_object.c
blob4adaae242b9e44b422b46f1fe3b4fd8422db1b0d
1 /*********************************************************************
3 * Filename: irias_object.c
4 * Version: 0.3
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/string.h>
26 #include <linux/socket.h>
27 #include <linux/module.h>
29 #include <net/irda/irda.h>
30 #include <net/irda/irias_object.h>
32 hashbin_t *irias_objects;
35 * Used when a missing value needs to be returned
37 struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}};
40 * Function strndup (str, max)
42 * My own kernel version of strndup!
44 * Faster, check boundary... Jean II
46 static char *strndup(char *str, size_t max)
48 char *new_str;
49 int len;
51 /* Check string */
52 if (str == NULL)
53 return NULL;
54 /* Check length, truncate */
55 len = strlen(str);
56 if(len > max)
57 len = max;
59 /* Allocate new string */
60 new_str = kmalloc(len + 1, GFP_ATOMIC);
61 if (new_str == NULL) {
62 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
63 return NULL;
66 /* Copy and truncate */
67 memcpy(new_str, str, len);
68 new_str[len] = '\0';
70 return new_str;
74 * Function ias_new_object (name, id)
76 * Create a new IAS object
79 struct ias_object *irias_new_object( char *name, int id)
81 struct ias_object *obj;
83 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
85 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
86 if (obj == NULL) {
87 IRDA_WARNING("%s(), Unable to allocate object!\n",
88 __FUNCTION__);
89 return NULL;
92 obj->magic = IAS_OBJECT_MAGIC;
93 obj->name = strndup(name, IAS_MAX_CLASSNAME);
94 if (!obj->name) {
95 IRDA_WARNING("%s(), Unable to allocate name!\n",
96 __FUNCTION__);
97 kfree(obj);
98 return NULL;
100 obj->id = id;
102 /* Locking notes : the attrib spinlock has lower precendence
103 * than the objects spinlock. Never grap the objects spinlock
104 * while holding any attrib spinlock (risk of deadlock). Jean II */
105 obj->attribs = hashbin_new(HB_LOCK);
107 if (obj->attribs == NULL) {
108 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
109 __FUNCTION__);
110 kfree(obj->name);
111 kfree(obj);
112 return NULL;
115 return obj;
117 EXPORT_SYMBOL(irias_new_object);
120 * Function irias_delete_attrib (attrib)
122 * Delete given attribute and deallocate all its memory
125 static void __irias_delete_attrib(struct ias_attrib *attrib)
127 IRDA_ASSERT(attrib != NULL, return;);
128 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
130 kfree(attrib->name);
132 irias_delete_value(attrib->value);
133 attrib->magic = ~IAS_ATTRIB_MAGIC;
135 kfree(attrib);
138 void __irias_delete_object(struct ias_object *obj)
140 IRDA_ASSERT(obj != NULL, return;);
141 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
143 kfree(obj->name);
145 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
147 obj->magic = ~IAS_OBJECT_MAGIC;
149 kfree(obj);
153 * Function irias_delete_object (obj)
155 * Remove object from hashbin and deallocate all attributes associated with
156 * with this object and the object itself
159 int irias_delete_object(struct ias_object *obj)
161 struct ias_object *node;
163 IRDA_ASSERT(obj != NULL, return -1;);
164 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
166 /* Remove from list */
167 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
168 if (!node)
169 IRDA_DEBUG( 0, "%s(), object already removed!\n",
170 __FUNCTION__);
172 /* Destroy */
173 __irias_delete_object(obj);
175 return 0;
177 EXPORT_SYMBOL(irias_delete_object);
180 * Function irias_delete_attrib (obj)
182 * Remove attribute from hashbin and, if it was the last attribute of
183 * the object, remove the object as well.
186 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
187 int cleanobject)
189 struct ias_attrib *node;
191 IRDA_ASSERT(obj != NULL, return -1;);
192 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
193 IRDA_ASSERT(attrib != NULL, return -1;);
195 /* Remove attribute from object */
196 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
197 if (!node)
198 return 0; /* Already removed or non-existent */
200 /* Deallocate attribute */
201 __irias_delete_attrib(node);
203 /* Check if object has still some attributes, destroy it if none.
204 * At first glance, this look dangerous, as the kernel reference
205 * various IAS objects. However, we only use this function on
206 * user attributes, not kernel attributes, so there is no risk
207 * of deleting a kernel object this way. Jean II */
208 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
209 if (cleanobject && !node)
210 irias_delete_object(obj);
212 return 0;
216 * Function irias_insert_object (obj)
218 * Insert an object into the LM-IAS database
221 void irias_insert_object(struct ias_object *obj)
223 IRDA_ASSERT(obj != NULL, return;);
224 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
226 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
228 EXPORT_SYMBOL(irias_insert_object);
231 * Function irias_find_object (name)
233 * Find object with given name
236 struct ias_object *irias_find_object(char *name)
238 IRDA_ASSERT(name != NULL, return NULL;);
240 /* Unsafe (locking), object might change */
241 return hashbin_lock_find(irias_objects, 0, name);
243 EXPORT_SYMBOL(irias_find_object);
246 * Function irias_find_attrib (obj, name)
248 * Find named attribute in object
251 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
253 struct ias_attrib *attrib;
255 IRDA_ASSERT(obj != NULL, return NULL;);
256 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
257 IRDA_ASSERT(name != NULL, return NULL;);
259 attrib = hashbin_lock_find(obj->attribs, 0, name);
260 if (attrib == NULL)
261 return NULL;
263 /* Unsafe (locking), attrib might change */
264 return attrib;
268 * Function irias_add_attribute (obj, attrib)
270 * Add attribute to object
273 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
274 int owner)
276 IRDA_ASSERT(obj != NULL, return;);
277 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
279 IRDA_ASSERT(attrib != NULL, return;);
280 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
282 /* Set if attrib is owned by kernel or user space */
283 attrib->value->owner = owner;
285 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
289 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
291 * Change the value of an objects attribute.
294 int irias_object_change_attribute(char *obj_name, char *attrib_name,
295 struct ias_value *new_value)
297 struct ias_object *obj;
298 struct ias_attrib *attrib;
299 unsigned long flags;
301 /* Find object */
302 obj = hashbin_lock_find(irias_objects, 0, obj_name);
303 if (obj == NULL) {
304 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
305 obj_name);
306 return -1;
309 /* Slightly unsafe (obj might get removed under us) */
310 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
312 /* Find attribute */
313 attrib = hashbin_find(obj->attribs, 0, attrib_name);
314 if (attrib == NULL) {
315 IRDA_WARNING("%s: Unable to find attribute: %s\n",
316 __FUNCTION__, attrib_name);
317 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
318 return -1;
321 if ( attrib->value->type != new_value->type) {
322 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
323 __FUNCTION__);
324 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
325 return -1;
328 /* Delete old value */
329 irias_delete_value(attrib->value);
331 /* Insert new value */
332 attrib->value = new_value;
334 /* Success */
335 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
336 return 0;
338 EXPORT_SYMBOL(irias_object_change_attribute);
341 * Function irias_object_add_integer_attrib (obj, name, value)
343 * Add an integer attribute to an LM-IAS object
346 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
347 int owner)
349 struct ias_attrib *attrib;
351 IRDA_ASSERT(obj != NULL, return;);
352 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
353 IRDA_ASSERT(name != NULL, return;);
355 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
356 if (attrib == NULL) {
357 IRDA_WARNING("%s: Unable to allocate attribute!\n",
358 __FUNCTION__);
359 return;
362 attrib->magic = IAS_ATTRIB_MAGIC;
363 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
365 /* Insert value */
366 attrib->value = irias_new_integer_value(value);
367 if (!attrib->name || !attrib->value) {
368 IRDA_WARNING("%s: Unable to allocate attribute!\n",
369 __FUNCTION__);
370 if (attrib->value)
371 irias_delete_value(attrib->value);
372 kfree(attrib->name);
373 kfree(attrib);
374 return;
377 irias_add_attrib(obj, attrib, owner);
379 EXPORT_SYMBOL(irias_add_integer_attrib);
382 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
384 * Add a octet sequence attribute to an LM-IAS object
388 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
389 int len, int owner)
391 struct ias_attrib *attrib;
393 IRDA_ASSERT(obj != NULL, return;);
394 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
396 IRDA_ASSERT(name != NULL, return;);
397 IRDA_ASSERT(octets != NULL, return;);
399 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
400 if (attrib == NULL) {
401 IRDA_WARNING("%s: Unable to allocate attribute!\n",
402 __FUNCTION__);
403 return;
406 attrib->magic = IAS_ATTRIB_MAGIC;
407 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
409 attrib->value = irias_new_octseq_value( octets, len);
410 if (!attrib->name || !attrib->value) {
411 IRDA_WARNING("%s: Unable to allocate attribute!\n",
412 __FUNCTION__);
413 if (attrib->value)
414 irias_delete_value(attrib->value);
415 kfree(attrib->name);
416 kfree(attrib);
417 return;
420 irias_add_attrib(obj, attrib, owner);
422 EXPORT_SYMBOL(irias_add_octseq_attrib);
425 * Function irias_object_add_string_attrib (obj, string)
427 * Add a string attribute to an LM-IAS object
430 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
431 int owner)
433 struct ias_attrib *attrib;
435 IRDA_ASSERT(obj != NULL, return;);
436 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
438 IRDA_ASSERT(name != NULL, return;);
439 IRDA_ASSERT(value != NULL, return;);
441 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
442 if (attrib == NULL) {
443 IRDA_WARNING("%s: Unable to allocate attribute!\n",
444 __FUNCTION__);
445 return;
448 attrib->magic = IAS_ATTRIB_MAGIC;
449 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
451 attrib->value = irias_new_string_value(value);
452 if (!attrib->name || !attrib->value) {
453 IRDA_WARNING("%s: Unable to allocate attribute!\n",
454 __FUNCTION__);
455 if (attrib->value)
456 irias_delete_value(attrib->value);
457 kfree(attrib->name);
458 kfree(attrib);
459 return;
462 irias_add_attrib(obj, attrib, owner);
464 EXPORT_SYMBOL(irias_add_string_attrib);
467 * Function irias_new_integer_value (integer)
469 * Create new IAS integer value
472 struct ias_value *irias_new_integer_value(int integer)
474 struct ias_value *value;
476 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
477 if (value == NULL) {
478 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
479 return NULL;
482 value->type = IAS_INTEGER;
483 value->len = 4;
484 value->t.integer = integer;
486 return value;
488 EXPORT_SYMBOL(irias_new_integer_value);
491 * Function irias_new_string_value (string)
493 * Create new IAS string value
495 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
497 struct ias_value *irias_new_string_value(char *string)
499 struct ias_value *value;
501 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
502 if (value == NULL) {
503 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
504 return NULL;
507 value->type = IAS_STRING;
508 value->charset = CS_ASCII;
509 value->t.string = strndup(string, IAS_MAX_STRING);
510 if (!value->t.string) {
511 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
512 kfree(value);
513 return NULL;
516 value->len = strlen(value->t.string);
518 return value;
522 * Function irias_new_octseq_value (octets, len)
524 * Create new IAS octet-sequence value
526 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
528 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
530 struct ias_value *value;
532 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
533 if (value == NULL) {
534 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
535 return NULL;
538 value->type = IAS_OCT_SEQ;
539 /* Check length */
540 if(len > IAS_MAX_OCTET_STRING)
541 len = IAS_MAX_OCTET_STRING;
542 value->len = len;
544 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
545 if (value->t.oct_seq == NULL){
546 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
547 kfree(value);
548 return NULL;
550 return value;
553 struct ias_value *irias_new_missing_value(void)
555 struct ias_value *value;
557 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
558 if (value == NULL) {
559 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
560 return NULL;
563 value->type = IAS_MISSING;
565 return value;
569 * Function irias_delete_value (value)
571 * Delete IAS value
574 void irias_delete_value(struct ias_value *value)
576 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
578 IRDA_ASSERT(value != NULL, return;);
580 switch (value->type) {
581 case IAS_INTEGER: /* Fallthrough */
582 case IAS_MISSING:
583 /* No need to deallocate */
584 break;
585 case IAS_STRING:
586 /* Deallocate string */
587 kfree(value->t.string);
588 break;
589 case IAS_OCT_SEQ:
590 /* Deallocate byte stream */
591 kfree(value->t.oct_seq);
592 break;
593 default:
594 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
595 break;
597 kfree(value);
599 EXPORT_SYMBOL(irias_delete_value);