1 /**************************************************************************/
2 /* File: open.c - systemcall to open new user connection/handle */
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 /**************************************************************************/
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"
41 #define __NO_VERSION__
42 #include <linux/module.h>
44 int can_open(struct inode *inode, struct file *file)
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
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);
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);
65 if (chip->flags & CHIP_CONFIGURED)
66 DEBUGMSG("Device is already configured.\n");
68 if (chip->chipspecops->chip_config(chip))
69 CANMSG("Error configuring chip.\n");
71 chip->flags |= CHIP_CONFIGURED;
72 } /* End of chip configuration */
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
81 if (chip->chipspecops->pre_read_config(chip,obj)<0)
82 CANMSG("Error initializing chip for receiving\n");
86 canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
87 if(canuser == NULL) goto no_canuser;
89 canuser->userinfo.fileinfo.file = file;
90 canuser->msgobj = obj;
91 canuser->magic = CAN_USER_MAGIC;
92 file->private_data = canuser;
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;
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);
104 if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
105 canuser->qends, obj->qends)<0) goto no_tx_qedge;
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);
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
120 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
121 canque_edge_decref(edge);
123 list_del(&canuser->peers);
124 canuser->qends = NULL;
125 canqueue_ends_dispose_kern(qends, 1);
131 atomic_dec(&obj->obj_used);