]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open.c
f785e2c17efe5212b1680d1b2ababec2512122c9
[lincan.git] / lincan / src / open.c
1 /**************************************************************************/
2 /* File: open.c - systemcall to open new user connection/handle           */
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 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/open.h"
39 #include "../include/setup.h"
40
41 #define __NO_VERSION__
42 #include <linux/module.h>
43
44 int can_open(struct inode *inode, struct file *file)
45 {
46         struct msgobj_t *obj;
47         struct canchip_t *chip;
48         struct canuser_t *canuser;
49         struct canque_ends_t *qends;
50         struct canque_edge_t *edge;
51         can_spin_irqflags_t iflags;
52         char openflag;          // Martin Petera: Object already opened
53
54         if ( ((obj=objects_p[MINOR_NR]) == NULL) ||
55                         ((chip=objects_p[MINOR_NR]->hostchip) == NULL) ) {
56                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",MINOR_NR);
57                 return -ENODEV;
58         }
59
60         atomic_inc(&obj->obj_used);
61         DEBUGMSG("Device %d opened %d times.\n", MINOR_NR, atomic_read(&obj->obj_used));
62         openflag = can_msgobj_test_fl(obj,OPENED);      // Martin Petera: store previous status
63         can_msgobj_set_fl(obj,OPENED);
64
65         if (chip->flags & CHIP_CONFIGURED)
66                 DEBUGMSG("Device is already configured.\n");
67         else {
68                 if (chip->chipspecops->chip_config(chip))
69                         CANMSG("Error configuring chip.\n");
70                 else
71                         chip->flags |= CHIP_CONFIGURED;
72         } /* End of chip configuration */
73
74
75         /* Martin Petera: Fix for HCAN2
76          * pre_read was called only once -> Opening second MSG object from userspace
77          * didn't call function to configure MSG object for receive.
78          * FIX: Call pre_read once for each MSG object
79          **/
80         if (!openflag) {
81                 if (chip->chipspecops->pre_read_config(chip,obj)<0)
82                         CANMSG("Error initializing chip for receiving\n");
83         }
84
85
86         canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
87         if(canuser == NULL) goto no_canuser;
88         canuser->flags=0;
89         canuser->userinfo.fileinfo.file = file;
90         canuser->msgobj = obj;
91         canuser->magic = CAN_USER_MAGIC;
92         file->private_data = canuser;
93
94         qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
95         if(qends == NULL) goto no_qends;
96         canqueue_ends_init_kern(qends);
97         canuser->qends = qends;
98
99         /*required to synchronize with RT-Linux context*/
100         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
101         list_add(&canuser->peers, &obj->obj_users);
102         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
103
104         if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
105                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
106
107         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
108                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
109         /*FIXME: more generic model should be used there*/
110         canque_edge_decref(canuser->rx_edge0);
111         canque_edge_decref(edge);
112
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
114         MOD_INC_USE_COUNT;
115 #endif
116
117         return 0;
118
119     no_rx_qedge:
120         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
121         canque_edge_decref(edge);
122     no_tx_qedge:
123         list_del(&canuser->peers);
124         canuser->qends = NULL;
125         canqueue_ends_dispose_kern(qends, 1);
126
127     no_qends:
128         kfree(canuser);
129
130     no_canuser:
131         atomic_dec(&obj->obj_used);
132         return -ENOMEM;
133 }