]> 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
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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
86         if (chip->hostdevice->hwspecops->release_device)
87                 kref_get(&chip->hostdevice->refcount);
88 #endif
89
90         canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
91         if(canuser == NULL) goto no_canuser;
92         canuser->flags=0;
93         canuser->userinfo.fileinfo.file = file;
94         canuser->msgobj = obj;
95         canuser->magic = CAN_USER_MAGIC;
96         file->private_data = canuser;
97
98         qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
99         if(qends == NULL) goto no_qends;
100         canqueue_ends_init_kern(qends);
101         canuser->qends = qends;
102
103         /*required to synchronize with RT-Linux context*/
104         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
105         list_add(&canuser->peers, &obj->obj_users);
106         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
107
108         if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
109                 canuser->qends, obj->qends)<0) goto no_tx_qedge;
110
111         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
112                 obj->qends, canuser->qends)<0) goto no_rx_qedge;
113         /*FIXME: more generic model should be used there*/
114         canque_edge_decref(canuser->rx_edge0);
115         canque_edge_decref(edge);
116
117 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
118         MOD_INC_USE_COUNT;
119 #endif
120
121         return 0;
122
123     no_rx_qedge:
124         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
125         canque_edge_decref(edge);
126     no_tx_qedge:
127         list_del(&canuser->peers);
128         canuser->qends = NULL;
129         canqueue_ends_dispose_kern(qends, 1);
130
131     no_qends:
132         kfree(canuser);
133
134     no_canuser:
135         atomic_dec(&obj->obj_used);
136         return -ENOMEM;
137 }