]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/close_rtl.c
b72b3d881861727f303428e377f7de7ba5f70c0a
[lincan.git] / lincan / src / close_rtl.c
1 /**************************************************************************/
2 /* File: close_rtl.c - RT-Linux variant of the close systemcall           */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifdef CAN_WITH_RTL
36
37 #include "../include/can.h"
38 #include "../include/can_sysdep.h"
39 #include "../include/main.h"
40
41 #include <rtl_malloc.h>
42 #include <rtl_posixio.h>
43 #include "../include/can_iortl.h"
44
45 #define __NO_VERSION__
46 #include <linux/module.h>
47
48 static inline
49 int can_release_rtl_common(struct canuser_t *canuser, int file_flags)
50 {
51         struct canque_ends_t *qends;
52         struct msgobj_t *obj;
53         can_spin_irqflags_t iflags;
54
55         obj = canuser->msgobj;
56         qends = canuser->qends;
57
58         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
59         list_del(&canuser->peers);
60         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
61         canuser->qends = NULL;
62         canqueue_ends_dispose_rtl(qends, file_flags & O_SYNC);
63
64         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
65         if(atomic_dec_and_test(&obj->obj_used)){
66                 can_msgobj_clear_fl(obj,OPENED);
67         };
68         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
69
70     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
71         MOD_DEC_USE_COUNT;
72     #endif
73
74         return 0;
75 }
76
77
78 int can_release_rtl_posix(struct rtl_file *fptr)
79 {
80         struct canuser_t *canuser =
81                 (struct canuser_t *)can_get_rtl_file_private_data(fptr);
82         int ret;
83         
84         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
85                 CANMSG("can_release_rtl_posix: bad canuser magic\n");
86                 return -ENODEV;
87         }
88
89         ret=can_release_rtl_common(canuser, fptr->f_flags);
90
91         rt_free(canuser);
92         
93         return ret;
94 }
95
96 #endif /*CAN_WITH_RTL*/
97