]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open_rtl.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[lincan.git] / lincan / src / open_rtl.c
1 /* open.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.3  17 Jun 2004
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
24 static inline
25 int can_open_rtl_common(struct canuser_t *canuser, int open_flags)
26 {
27         struct msgobj_t *obj=canuser->msgobj;
28         struct canchip_t *chip;
29         struct canque_ends_t *qends;
30         struct canque_edge_t *edge;
31         can_spin_irqflags_t iflags;
32
33         if(!obj) return -ENODEV;
34         
35         can_msgobj_set_fl(obj,OPENED);
36         
37         chip=obj->hostchip;
38         if (chip) {
39                 if (!(chip->flags & CHIP_CONFIGURED)) {
40                         if (chip->chipspecops->chip_config(chip))
41                                 CANMSG("Error configuring chip.\n");
42                         else
43                                 chip->flags |= CHIP_CONFIGURED; 
44
45                         if (chip->chipspecops->pre_read_config(chip,obj)<0)
46                                 CANMSG("Error initializing chip for receiving\n");
47
48                 }
49         } /* End of chip configuration */
50
51
52         qends = (struct canque_ends_t *)rt_malloc(sizeof(struct canque_ends_t));
53         if(qends == NULL) goto no_qends;
54         canqueue_ends_init_rtl(qends);
55         /* mark memory as allocated from RTL memory pool */
56         qends->ends_flags|=CAN_ENDSF_MEM_RTL;
57         canuser->qends = qends;
58         
59         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
60         list_add(&canuser->peers, &obj->obj_users);
61         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
62
63         if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
64                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
65
66         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_rtl(MAX_BUF_LENGTH),
67                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
68         /*FIXME: more generic model should be used there*/
69         canque_edge_decref(canuser->rx_edge0);
70         canque_edge_decref(edge);
71
72 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
73         MOD_INC_USE_COUNT;      /*is this enough for RT-Linux context ?*/
74 #endif  
75
76         return 0;
77         
78     no_rx_qedge:
79         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
80         canque_edge_decref(edge);
81     no_tx_qedge:
82         list_del(&canuser->peers);
83         canuser->qends = NULL;
84         canqueue_ends_dispose_kern(qends, 1);
85
86     no_qends:
87
88         return -ENOMEM;
89 }
90
91
92 int can_open_rtl_posix(struct rtl_file *fptr)
93 {
94         int ret;
95         struct msgobj_t *obj;
96         struct canchip_t *chip;
97         struct canuser_t *canuser;
98         int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
99         
100         if(minor_nr>=MAX_TOT_MSGOBJS)
101                 return -ENODEV;
102
103         if ( ((obj=objects_p[minor_nr]) == NULL) || 
104                         ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
105                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",minor_nr);
106                 return -ENODEV;
107         }
108
109         atomic_inc(&obj->obj_used);
110         DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
111
112         canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
113         if(canuser == NULL){
114                 ret=-ENOMEM;
115                 goto no_canuser;
116         }
117         canuser->flags=CANUSER_RTL_CLIENT | CANUSER_RTL_MEM;
118         canuser->userinfo.rtlinfo.file = fptr;
119         canuser->msgobj = obj;
120         canuser->magic = CAN_USER_MAGIC;
121
122         /*next line would solve many problems, but RT-Linux lacks this fundamental field */
123         /*fptr->private_data = canuser;*/
124         /*to test code I am adding this terible hack*/
125         can_set_rtl_file_private_data(fptr,canuser);
126
127         ret=can_open_rtl_common(canuser, fptr->f_flags);
128         if(ret>=0) return ret;
129
130         rt_free(canuser);
131
132     no_canuser:
133         atomic_dec(&obj->obj_used);
134         
135         return ret;
136 }
137
138 #endif /*CAN_WITH_RTL*/