Structured comments updated.
[lincan.git] / lincan / src / close.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 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/close.h"
14 #include "../include/i82527.h"
15 #include "../include/setup.h"
16 #include "../include/fasync.h"
17
18 #define __NO_VERSION__
19 #include <linux/module.h>
20
21 int can_close(struct inode *inode, struct file *file)
22 {
23         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
24         struct canque_ends_t *qends;
25         struct msgobj_t *obj;
26         can_spin_irqflags_t iflags;
27         
28         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
29                 CANMSG("can_close: bad canuser magic\n");
30                 return -ENODEV;
31         }
32         
33         obj = canuser->msgobj;
34         qends = canuser->qends;
35         
36     #ifdef CAN_ENABLE_KERN_FASYNC
37
38         can_fasync(-1, file, 0);
39
40     #endif /*CAN_ENABLE_KERN_FASYNC*/
41
42         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
43         list_del(&canuser->peers);
44         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
45         canuser->qends = NULL;
46         canqueue_ends_dispose_kern(qends, file->f_flags & O_SYNC);
47
48         kfree(canuser);
49
50         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
51         if(atomic_dec_and_test(&obj->obj_used)){
52                 can_msgobj_clear_fl(obj,OPENED);
53         };
54         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
55         
56     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
57         MOD_DEC_USE_COUNT;
58     #endif
59         return 0;
60 }