]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open_rtl.c
The LinCAN driver license unified according to DCE FEE CTU head and superiors request.
[lincan.git] / lincan / src / open_rtl.c
1 /**************************************************************************/
2 /* File: open_rtl.c - RT-Linux open systemcall variant                    */
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
49 static inline
50 int can_open_rtl_common(struct canuser_t *canuser, int open_flags)
51 {
52         struct msgobj_t *obj=canuser->msgobj;
53         struct canchip_t *chip;
54         struct canque_ends_t *qends;
55         struct canque_edge_t *edge;
56         can_spin_irqflags_t iflags;
57
58         if(!obj) return -ENODEV;
59         
60         can_msgobj_set_fl(obj,OPENED);
61         
62         chip=obj->hostchip;
63         if (chip) {
64                 if (!(chip->flags & CHIP_CONFIGURED)) {
65                         if (chip->chipspecops->chip_config(chip))
66                                 CANMSG("Error configuring chip.\n");
67                         else
68                                 chip->flags |= CHIP_CONFIGURED; 
69
70                         if (chip->chipspecops->pre_read_config(chip,obj)<0)
71                                 CANMSG("Error initializing chip for receiving\n");
72
73                 }
74         } /* End of chip configuration */
75
76
77         qends = (struct canque_ends_t *)rt_malloc(sizeof(struct canque_ends_t));
78         if(qends == NULL) goto no_qends;
79         canqueue_ends_init_rtl(qends);
80         /* mark memory as allocated from RTL memory pool */
81         qends->ends_flags|=CAN_ENDSF_MEM_RTL;
82         canuser->qends = qends;
83         
84         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
85         list_add(&canuser->peers, &obj->obj_users);
86         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
87
88         if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
89                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
90
91         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_rtl(MAX_BUF_LENGTH),
92                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
93         /*FIXME: more generic model should be used there*/
94         canque_edge_decref(canuser->rx_edge0);
95         canque_edge_decref(edge);
96
97 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
98         MOD_INC_USE_COUNT;      /*is this enough for RT-Linux context ?*/
99 #endif  
100
101         return 0;
102         
103     no_rx_qedge:
104         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
105         canque_edge_decref(edge);
106     no_tx_qedge:
107         list_del(&canuser->peers);
108         canuser->qends = NULL;
109         canqueue_ends_dispose_kern(qends, 1);
110
111     no_qends:
112
113         return -ENOMEM;
114 }
115
116
117 int can_open_rtl_posix(struct rtl_file *fptr)
118 {
119         int ret;
120         struct msgobj_t *obj;
121         struct canchip_t *chip;
122         struct canuser_t *canuser;
123         int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
124         
125         if(minor_nr>=MAX_TOT_MSGOBJS)
126                 return -ENODEV;
127
128         if ( ((obj=objects_p[minor_nr]) == NULL) || 
129                         ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
130                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",minor_nr);
131                 return -ENODEV;
132         }
133
134         atomic_inc(&obj->obj_used);
135         DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
136
137         canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
138         if(canuser == NULL){
139                 ret=-ENOMEM;
140                 goto no_canuser;
141         }
142         canuser->flags=CANUSER_RTL_CLIENT | CANUSER_RTL_MEM;
143         canuser->userinfo.rtlinfo.file = fptr;
144         canuser->msgobj = obj;
145         canuser->magic = CAN_USER_MAGIC;
146
147         /*next line would solve many problems, but RT-Linux lacks this fundamental field */
148         /*fptr->private_data = canuser;*/
149         /*to test code I am adding this terible hack*/
150         can_set_rtl_file_private_data(fptr,canuser);
151
152         ret=can_open_rtl_common(canuser, fptr->f_flags);
153         if(ret>=0) return ret;
154
155         rt_free(canuser);
156
157     no_canuser:
158         atomic_dec(&obj->obj_used);
159         
160         return ret;
161 }
162
163 #endif /*CAN_WITH_RTL*/