2 * Linux CAN-bus device driver.
3 * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4 * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5 * email:pisa@cmp.felk.cvut.cz
6 * This software is released under the GPL-License.
7 * Version lincan-0.3 17 Jun 2004
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/open.h"
14 #include "../include/setup.h"
16 #define __NO_VERSION__
17 #include <linux/module.h>
19 int can_open(struct inode *inode, struct file *file)
22 struct canchip_t *chip;
23 struct canuser_t *canuser;
24 struct canque_ends_t *qends;
25 struct canque_edge_t *edge;
26 can_spin_irqflags_t iflags;
27 char openflag; // Martin Petera: Object already opened
29 if ( ((obj=objects_p[MINOR_NR]) == NULL) ||
30 ((chip=objects_p[MINOR_NR]->hostchip) == NULL) ) {
31 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",MINOR_NR);
35 atomic_inc(&obj->obj_used);
36 DEBUGMSG("Device %d opened %d times.\n", MINOR_NR, atomic_read(&obj->obj_used));
37 openflag = can_msgobj_test_fl(obj,OPENED); // Martin Petera: store previous status
38 can_msgobj_set_fl(obj,OPENED);
40 if (chip->flags & CHIP_CONFIGURED)
41 DEBUGMSG("Device is already configured.\n");
43 if (chip->chipspecops->chip_config(chip))
44 CANMSG("Error configuring chip.\n");
46 chip->flags |= CHIP_CONFIGURED;
47 } /* End of chip configuration */
50 /* Martin Petera: Fix for HCAN2
51 * pre_read was called only once -> Opening second MSG object from userspace
52 * didn't call function to configure MSG object for receive.
53 * FIX: Call pre_read once for each MSG object
56 if (chip->chipspecops->pre_read_config(chip,obj)<0)
57 CANMSG("Error initializing chip for receiving\n");
61 canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
62 if(canuser == NULL) goto no_canuser;
64 canuser->userinfo.fileinfo.file = file;
65 canuser->msgobj = obj;
66 canuser->magic = CAN_USER_MAGIC;
67 file->private_data = canuser;
69 qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
70 if(qends == NULL) goto no_qends;
71 canqueue_ends_init_kern(qends);
72 canuser->qends = qends;
74 /*required to synchronize with RT-Linux context*/
75 can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
76 list_add(&canuser->peers, &obj->obj_users);
77 can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
79 if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
80 canuser->qends, obj->qends)<0) goto no_tx_qedge;
82 if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
83 obj->qends, canuser->qends)<0) goto no_rx_qedge;
84 /*FIXME: more generic model should be used there*/
85 canque_edge_decref(canuser->rx_edge0);
86 canque_edge_decref(edge);
88 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
95 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
96 canque_edge_decref(edge);
98 list_del(&canuser->peers);
99 canuser->qends = NULL;
100 canqueue_ends_dispose_kern(qends, 1);
106 atomic_dec(&obj->obj_used);