1 /**************************************************************************/
2 /* File: open_rtl.c - RT-Linux open systemcall variant */
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> */
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. */
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: */
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 /**************************************************************************/
37 #include "../include/can.h"
38 #include "../include/can_sysdep.h"
39 #include "../include/main.h"
41 #include <rtl_malloc.h>
42 #include <rtl_posixio.h>
43 #include "../include/can_iortl.h"
45 #define __NO_VERSION__
46 #include <linux/module.h>
50 int can_open_rtl_common(struct canuser_t *canuser, int open_flags)
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;
58 if(!obj) return -ENODEV;
60 can_msgobj_set_fl(obj,OPENED);
64 if (!(chip->flags & CHIP_CONFIGURED)) {
65 if (chip->chipspecops->chip_config(chip))
66 CANMSG("Error configuring chip.\n");
68 chip->flags |= CHIP_CONFIGURED;
70 if (chip->chipspecops->pre_read_config(chip,obj)<0)
71 CANMSG("Error initializing chip for receiving\n");
74 } /* End of chip configuration */
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;
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);
88 if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
89 canuser->qends, obj->qends)<0) goto no_tx_qedge;
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);
97 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
98 MOD_INC_USE_COUNT; /*is this enough for RT-Linux context ?*/
104 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
105 canque_edge_decref(edge);
107 list_del(&canuser->peers);
108 canuser->qends = NULL;
109 canqueue_ends_dispose_kern(qends, 1);
117 int can_open_rtl_posix(struct rtl_file *fptr)
120 struct msgobj_t *obj;
121 struct canchip_t *chip;
122 struct canuser_t *canuser;
123 int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
125 if(minor_nr>=MAX_TOT_MSGOBJS)
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);
134 atomic_inc(&obj->obj_used);
135 DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
137 canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
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;
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);
152 ret=can_open_rtl_common(canuser, fptr->f_flags);
153 if(ret>=0) return ret;
158 atomic_dec(&obj->obj_used);
163 #endif /*CAN_WITH_RTL*/