]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open.c
Merge branch 'master' into can-usb1
[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         int minor_nr = INODE2MINOR_NR(inode);
54
55         if ((minor_nr < 0) || (minor_nr >= MAX_TOT_MSGOBJS)) {
56                 CANMSG("can_open: bad minor %d\n", minor_nr);
57                 return -ENODEV;
58         }
59
60         if ( ((obj=objects_p[minor_nr]) == NULL) ||
61             ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
62                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n", minor_nr);
63                 return -ENODEV;
64         }
65
66         atomic_inc(&obj->obj_used);
67         DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
68         openflag = can_msgobj_test_fl(obj,OPENED);      // Martin Petera: store previous status
69         can_msgobj_set_fl(obj,OPENED);
70
71         if (chip->flags & CHIP_CONFIGURED)
72                 DEBUGMSG("Device is already configured.\n");
73         else {
74                 if (chip->chipspecops->chip_config(chip))
75                         CANMSG("Error configuring chip.\n");
76                 else
77                         chip->flags |= CHIP_CONFIGURED;
78         } /* End of chip configuration */
79
80
81         /* Martin Petera: Fix for HCAN2
82          * pre_read was called only once -> Opening second MSG object from userspace
83          * didn't call function to configure MSG object for receive.
84          * FIX: Call pre_read once for each MSG object
85          **/
86         if (!openflag) {
87                 if (chip->chipspecops->pre_read_config(chip,obj)<0)
88                         CANMSG("Error initializing chip for receiving\n");
89         }
90
91 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
92         if (chip->hostdevice->hwspecops->release_device)
93                 kref_get(&chip->hostdevice->refcount);
94 #endif
95
96         canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
97         if(canuser == NULL) goto no_canuser;
98         canuser->flags=0;
99         canuser->userinfo.fileinfo.file = file;
100         canuser->msgobj = obj;
101         canuser->magic = CAN_USER_MAGIC;
102         file->private_data = canuser;
103
104         qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
105         if(qends == NULL) goto no_qends;
106         canqueue_ends_init_kern(qends);
107         canuser->qends = qends;
108
109         /*required to synchronize with RT-Linux context*/
110         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
111         list_add(&canuser->peers, &obj->obj_users);
112         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
113
114         if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
115                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
116
117         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
118                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
119         /*FIXME: more generic model should be used there*/
120         canque_edge_decref(canuser->rx_edge0);
121         canque_edge_decref(edge);
122
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
124         MOD_INC_USE_COUNT;
125 #endif
126
127         return 0;
128
129     no_rx_qedge:
130         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
131         canque_edge_decref(edge);
132     no_tx_qedge:
133         list_del(&canuser->peers);
134         canuser->qends = NULL;
135         canqueue_ends_dispose_kern(qends, 1);
136
137     no_qends:
138         kfree(canuser);
139
140     no_canuser:
141         atomic_dec(&obj->obj_used);
142         return -ENOMEM;
143 }