]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open_rtl.c
Added full RT-Linux POSIX interface to LinCAN driver, needs preparation of RT tests.
[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.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
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 chip_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         canuser->qends = qends;
56         
57         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
58         list_add(&canuser->peers, &obj->obj_users);
59         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
60
61         if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
62                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
63
64         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_rtl(MAX_BUF_LENGTH),
65                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
66         /*FIXME: more generic model should be used there*/
67         canque_edge_decref(canuser->rx_edge0);
68         canque_edge_decref(edge);
69
70 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
71         MOD_INC_USE_COUNT;      /*is this enough for RT-Linux context ?*/
72 #endif  
73
74         return 0;
75         
76     no_rx_qedge:
77         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
78         canque_edge_decref(edge);
79     no_tx_qedge:
80         list_del(&canuser->peers);
81         canuser->qends = NULL;
82         canqueue_ends_dispose_kern(qends, 1);
83
84     no_qends:
85
86         return -ENOMEM;
87 }
88
89
90 int can_open_rtl_posix(struct rtl_file *fptr)
91 {
92         int ret;
93         struct msgobj_t *obj;
94         struct chip_t *chip;
95         struct canuser_t *canuser;
96         int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
97         
98         if(minor_nr>=MAX_TOT_MSGOBJS)
99                 return -ENODEV;
100
101         if ( ((obj=objects_p[minor_nr]) == NULL) || 
102                         ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
103                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",minor_nr);
104                 return -ENODEV;
105         }
106
107         atomic_inc(&obj->obj_used);
108         DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
109
110         canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
111         if(canuser == NULL){
112                 ret=-ENOMEM;
113                 goto no_canuser;
114         }
115         canuser->flags=CANUSER_RTL_CLIENT | CANUSER_RTL_MEM;
116         canuser->userinfo.rtlinfo.file = fptr;
117         canuser->msgobj = obj;
118         canuser->magic = CAN_USER_MAGIC;
119
120         /*next line would solve many problems, but RT-Linux lacks this fundamental field */
121         /*fptr->private_data = canuser;*/
122         /*to test code I am adding this terible hack*/
123         can_set_rtl_file_private_data(fptr,canuser);
124
125         ret=can_open_rtl_common(canuser, fptr->f_flags);
126         if(ret>=0) return ret;
127
128         rt_free(canuser);
129
130     no_canuser:
131         atomic_dec(&obj->obj_used);
132         
133         return ret;
134 }
135
136 #endif /*CAN_WITH_RTL*/