Structured comments updated.
[lincan.git] / lincan / src / close_rtl.c
1 /* close.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */
9
10 #ifdef CAN_WITH_RTL
11
12 #include "../include/can.h"
13 #include "../include/can_sysdep.h"
14 #include "../include/main.h"
15
16 #include <rtl_malloc.h>
17 #include <rtl_posixio.h>
18 #include "../include/can_iortl.h"
19
20 #define __NO_VERSION__
21 #include <linux/module.h>
22
23 static inline
24 int can_release_rtl_common(struct canuser_t *canuser, int file_flags)
25 {
26         struct canque_ends_t *qends;
27         struct msgobj_t *obj;
28         can_spin_irqflags_t iflags;
29
30         obj = canuser->msgobj;
31         qends = canuser->qends;
32
33         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
34         list_del(&canuser->peers);
35         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
36         canuser->qends = NULL;
37         canqueue_ends_dispose_rtl(qends, file_flags & O_SYNC);
38
39         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
40         if(atomic_dec_and_test(&obj->obj_used)){
41                 can_msgobj_clear_fl(obj,OPENED);
42         };
43         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
44
45     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
46         MOD_DEC_USE_COUNT;
47     #endif
48
49         return 0;
50 }
51
52
53 int can_release_rtl_posix(struct rtl_file *fptr)
54 {
55         struct canuser_t *canuser =
56                 (struct canuser_t *)can_get_rtl_file_private_data(fptr);
57         int ret;
58         
59         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
60                 CANMSG("can_release_rtl_posix: bad canuser magic\n");
61                 return -ENODEV;
62         }
63
64         ret=can_release_rtl_common(canuser, fptr->f_flags);
65
66         rt_free(canuser);
67         
68         return ret;
69 }
70
71 #endif /*CAN_WITH_RTL*/
72