amd64_edac: cleanup/complete NB MCE decoding
[linux/fpc-iii.git] / net / irda / irias_object.c
blob99ebb96f13869868bba080779354390c3284f5a9
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}};
41 * Function ias_new_object (name, id)
43 * Create a new IAS object
46 struct ias_object *irias_new_object( char *name, int id)
48 struct ias_object *obj;
50 IRDA_DEBUG( 4, "%s()\n", __func__);
52 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
53 if (obj == NULL) {
54 IRDA_WARNING("%s(), Unable to allocate object!\n",
55 __func__);
56 return NULL;
59 obj->magic = IAS_OBJECT_MAGIC;
60 obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC);
61 if (!obj->name) {
62 IRDA_WARNING("%s(), Unable to allocate name!\n",
63 __func__);
64 kfree(obj);
65 return NULL;
67 obj->id = id;
69 /* Locking notes : the attrib spinlock has lower precendence
70 * than the objects spinlock. Never grap the objects spinlock
71 * while holding any attrib spinlock (risk of deadlock). Jean II */
72 obj->attribs = hashbin_new(HB_LOCK);
74 if (obj->attribs == NULL) {
75 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
76 __func__);
77 kfree(obj->name);
78 kfree(obj);
79 return NULL;
82 return obj;
84 EXPORT_SYMBOL(irias_new_object);
87 * Function irias_delete_attrib (attrib)
89 * Delete given attribute and deallocate all its memory
92 static void __irias_delete_attrib(struct ias_attrib *attrib)
94 IRDA_ASSERT(attrib != NULL, return;);
95 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
97 kfree(attrib->name);
99 irias_delete_value(attrib->value);
100 attrib->magic = ~IAS_ATTRIB_MAGIC;
102 kfree(attrib);
105 void __irias_delete_object(struct ias_object *obj)
107 IRDA_ASSERT(obj != NULL, return;);
108 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
110 kfree(obj->name);
112 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
114 obj->magic = ~IAS_OBJECT_MAGIC;
116 kfree(obj);
120 * Function irias_delete_object (obj)
122 * Remove object from hashbin and deallocate all attributes associated with
123 * with this object and the object itself
126 int irias_delete_object(struct ias_object *obj)
128 struct ias_object *node;
130 IRDA_ASSERT(obj != NULL, return -1;);
131 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
133 /* Remove from list */
134 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
135 if (!node)
136 IRDA_DEBUG( 0, "%s(), object already removed!\n",
137 __func__);
139 /* Destroy */
140 __irias_delete_object(obj);
142 return 0;
144 EXPORT_SYMBOL(irias_delete_object);
147 * Function irias_delete_attrib (obj)
149 * Remove attribute from hashbin and, if it was the last attribute of
150 * the object, remove the object as well.
153 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
154 int cleanobject)
156 struct ias_attrib *node;
158 IRDA_ASSERT(obj != NULL, return -1;);
159 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
160 IRDA_ASSERT(attrib != NULL, return -1;);
162 /* Remove attribute from object */
163 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
164 if (!node)
165 return 0; /* Already removed or non-existent */
167 /* Deallocate attribute */
168 __irias_delete_attrib(node);
170 /* Check if object has still some attributes, destroy it if none.
171 * At first glance, this look dangerous, as the kernel reference
172 * various IAS objects. However, we only use this function on
173 * user attributes, not kernel attributes, so there is no risk
174 * of deleting a kernel object this way. Jean II */
175 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
176 if (cleanobject && !node)
177 irias_delete_object(obj);
179 return 0;
183 * Function irias_insert_object (obj)
185 * Insert an object into the LM-IAS database
188 void irias_insert_object(struct ias_object *obj)
190 IRDA_ASSERT(obj != NULL, return;);
191 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
193 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
195 EXPORT_SYMBOL(irias_insert_object);
198 * Function irias_find_object (name)
200 * Find object with given name
203 struct ias_object *irias_find_object(char *name)
205 IRDA_ASSERT(name != NULL, return NULL;);
207 /* Unsafe (locking), object might change */
208 return hashbin_lock_find(irias_objects, 0, name);
210 EXPORT_SYMBOL(irias_find_object);
213 * Function irias_find_attrib (obj, name)
215 * Find named attribute in object
218 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
220 struct ias_attrib *attrib;
222 IRDA_ASSERT(obj != NULL, return NULL;);
223 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
224 IRDA_ASSERT(name != NULL, return NULL;);
226 attrib = hashbin_lock_find(obj->attribs, 0, name);
227 if (attrib == NULL)
228 return NULL;
230 /* Unsafe (locking), attrib might change */
231 return attrib;
235 * Function irias_add_attribute (obj, attrib)
237 * Add attribute to object
240 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
241 int owner)
243 IRDA_ASSERT(obj != NULL, return;);
244 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
246 IRDA_ASSERT(attrib != NULL, return;);
247 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
249 /* Set if attrib is owned by kernel or user space */
250 attrib->value->owner = owner;
252 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
256 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
258 * Change the value of an objects attribute.
261 int irias_object_change_attribute(char *obj_name, char *attrib_name,
262 struct ias_value *new_value)
264 struct ias_object *obj;
265 struct ias_attrib *attrib;
266 unsigned long flags;
268 /* Find object */
269 obj = hashbin_lock_find(irias_objects, 0, obj_name);
270 if (obj == NULL) {
271 IRDA_WARNING("%s: Unable to find object: %s\n", __func__,
272 obj_name);
273 return -1;
276 /* Slightly unsafe (obj might get removed under us) */
277 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
279 /* Find attribute */
280 attrib = hashbin_find(obj->attribs, 0, attrib_name);
281 if (attrib == NULL) {
282 IRDA_WARNING("%s: Unable to find attribute: %s\n",
283 __func__, attrib_name);
284 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
285 return -1;
288 if ( attrib->value->type != new_value->type) {
289 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
290 __func__);
291 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
292 return -1;
295 /* Delete old value */
296 irias_delete_value(attrib->value);
298 /* Insert new value */
299 attrib->value = new_value;
301 /* Success */
302 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
303 return 0;
305 EXPORT_SYMBOL(irias_object_change_attribute);
308 * Function irias_object_add_integer_attrib (obj, name, value)
310 * Add an integer attribute to an LM-IAS object
313 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
314 int owner)
316 struct ias_attrib *attrib;
318 IRDA_ASSERT(obj != NULL, return;);
319 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
320 IRDA_ASSERT(name != NULL, return;);
322 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
323 if (attrib == NULL) {
324 IRDA_WARNING("%s: Unable to allocate attribute!\n",
325 __func__);
326 return;
329 attrib->magic = IAS_ATTRIB_MAGIC;
330 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
332 /* Insert value */
333 attrib->value = irias_new_integer_value(value);
334 if (!attrib->name || !attrib->value) {
335 IRDA_WARNING("%s: Unable to allocate attribute!\n",
336 __func__);
337 if (attrib->value)
338 irias_delete_value(attrib->value);
339 kfree(attrib->name);
340 kfree(attrib);
341 return;
344 irias_add_attrib(obj, attrib, owner);
346 EXPORT_SYMBOL(irias_add_integer_attrib);
349 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
351 * Add a octet sequence attribute to an LM-IAS object
355 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
356 int len, int owner)
358 struct ias_attrib *attrib;
360 IRDA_ASSERT(obj != NULL, return;);
361 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
363 IRDA_ASSERT(name != NULL, return;);
364 IRDA_ASSERT(octets != NULL, return;);
366 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
367 if (attrib == NULL) {
368 IRDA_WARNING("%s: Unable to allocate attribute!\n",
369 __func__);
370 return;
373 attrib->magic = IAS_ATTRIB_MAGIC;
374 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
376 attrib->value = irias_new_octseq_value( octets, len);
377 if (!attrib->name || !attrib->value) {
378 IRDA_WARNING("%s: Unable to allocate attribute!\n",
379 __func__);
380 if (attrib->value)
381 irias_delete_value(attrib->value);
382 kfree(attrib->name);
383 kfree(attrib);
384 return;
387 irias_add_attrib(obj, attrib, owner);
389 EXPORT_SYMBOL(irias_add_octseq_attrib);
392 * Function irias_object_add_string_attrib (obj, string)
394 * Add a string attribute to an LM-IAS object
397 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
398 int owner)
400 struct ias_attrib *attrib;
402 IRDA_ASSERT(obj != NULL, return;);
403 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
405 IRDA_ASSERT(name != NULL, return;);
406 IRDA_ASSERT(value != NULL, return;);
408 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
409 if (attrib == NULL) {
410 IRDA_WARNING("%s: Unable to allocate attribute!\n",
411 __func__);
412 return;
415 attrib->magic = IAS_ATTRIB_MAGIC;
416 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
418 attrib->value = irias_new_string_value(value);
419 if (!attrib->name || !attrib->value) {
420 IRDA_WARNING("%s: Unable to allocate attribute!\n",
421 __func__);
422 if (attrib->value)
423 irias_delete_value(attrib->value);
424 kfree(attrib->name);
425 kfree(attrib);
426 return;
429 irias_add_attrib(obj, attrib, owner);
431 EXPORT_SYMBOL(irias_add_string_attrib);
434 * Function irias_new_integer_value (integer)
436 * Create new IAS integer value
439 struct ias_value *irias_new_integer_value(int integer)
441 struct ias_value *value;
443 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
444 if (value == NULL) {
445 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
446 return NULL;
449 value->type = IAS_INTEGER;
450 value->len = 4;
451 value->t.integer = integer;
453 return value;
455 EXPORT_SYMBOL(irias_new_integer_value);
458 * Function irias_new_string_value (string)
460 * Create new IAS string value
462 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
464 struct ias_value *irias_new_string_value(char *string)
466 struct ias_value *value;
468 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
469 if (value == NULL) {
470 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
471 return NULL;
474 value->type = IAS_STRING;
475 value->charset = CS_ASCII;
476 value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC);
477 if (!value->t.string) {
478 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
479 kfree(value);
480 return NULL;
483 value->len = strlen(value->t.string);
485 return value;
489 * Function irias_new_octseq_value (octets, len)
491 * Create new IAS octet-sequence value
493 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
495 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
497 struct ias_value *value;
499 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
500 if (value == NULL) {
501 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
502 return NULL;
505 value->type = IAS_OCT_SEQ;
506 /* Check length */
507 if(len > IAS_MAX_OCTET_STRING)
508 len = IAS_MAX_OCTET_STRING;
509 value->len = len;
511 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
512 if (value->t.oct_seq == NULL){
513 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
514 kfree(value);
515 return NULL;
517 return value;
520 struct ias_value *irias_new_missing_value(void)
522 struct ias_value *value;
524 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
525 if (value == NULL) {
526 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
527 return NULL;
530 value->type = IAS_MISSING;
532 return value;
536 * Function irias_delete_value (value)
538 * Delete IAS value
541 void irias_delete_value(struct ias_value *value)
543 IRDA_DEBUG(4, "%s()\n", __func__);
545 IRDA_ASSERT(value != NULL, return;);
547 switch (value->type) {
548 case IAS_INTEGER: /* Fallthrough */
549 case IAS_MISSING:
550 /* No need to deallocate */
551 break;
552 case IAS_STRING:
553 /* Deallocate string */
554 kfree(value->t.string);
555 break;
556 case IAS_OCT_SEQ:
557 /* Deallocate byte stream */
558 kfree(value->t.oct_seq);
559 break;
560 default:
561 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __func__);
562 break;
564 kfree(value);
566 EXPORT_SYMBOL(irias_delete_value);