]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open.c
CAN driver infrastructure redesign to LinCAN-0.2 version
[lincan.git] / lincan / src / open.c
1 /* open.c
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.2  9 Jul 2003
8  */
9
10 #define __NO_VERSION__
11 #include <linux/module.h> 
12
13 #include <linux/autoconf.h>
14
15 #include <linux/fs.h>
16 #include <linux/version.h>
17 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
18 #include <linux/malloc.h>
19 #else
20 #include <linux/slab.h>
21 #endif
22
23 #include "../include/main.h"
24 #include "../include/open.h"
25 #include "../include/i82527.h"
26 #include "../include/setup.h"
27
28 int can_open(struct inode *inode, struct file *file)
29 {
30         struct msgobj_t *obj;
31         struct chip_t *chip;
32         struct canuser_t *canuser;
33         struct canque_ends_t *qends;
34
35         if ( ((obj=objects_p[MINOR_NR]) == NULL) || 
36                         ((chip=objects_p[MINOR_NR]->hostchip) == NULL) ) {
37                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",MINOR_NR);
38                 return -ENODEV;
39         }
40
41         atomic_inc(&obj->obj_used);
42         DEBUGMSG("Device %d opened %d times.\n", MINOR_NR, atomic_read(&obj->obj_used));
43         obj->flags |= OBJ_OPENED;
44
45         if (chip->flags & CHIP_CONFIGURED) 
46                 DEBUGMSG("Device is already configured.\n");
47         else {
48                 if (chip->chipspecops->chip_config(chip))
49                         CANMSG("Error configuring chip.\n");
50                 else
51                         chip->flags |= CHIP_CONFIGURED; 
52
53                 if (chip->chipspecops->pre_read_config(chip,obj)<0)
54                         CANMSG("Error initializing chip for receiving\n");
55
56                 /* chip->flags |= OBJ_BUFFERS_ALLOCATED; */
57                 
58         } /* End of chip configuration */
59
60         canuser = (struct canuser_t *)kmalloc(sizeof(struct canuser_t), GFP_KERNEL);
61         if(canuser == NULL) goto no_canuser;
62         canuser->file = file;
63         canuser->msgobj = obj;
64         canuser->magic = CAN_USER_MAGIC;
65         file->private_data = canuser;
66
67         qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
68         if(qends == NULL) goto no_qends;
69         canqueue_ends_init_kern(qends);
70         canuser->qends = qends;
71         
72         list_add(&canuser->peers, &obj->obj_users);
73
74         if(canqueue_connect_edge(canque_new_edge_kern(MAX_BUF_LENGTH),
75                 canuser->qends, obj->qends)<0) goto no_qedge;
76
77         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
78                 obj->qends, canuser->qends)<0) goto no_qedge;
79
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
81         MOD_INC_USE_COUNT;
82 #endif  
83
84         return 0;
85         
86     no_qedge:
87         list_del(&canuser->peers);
88         canqueue_ends_done_kern(qends, 1);
89         canuser->qends = NULL;
90         kfree(qends);
91         obj->qends = NULL;
92
93     no_qends:
94         kfree(canuser);
95
96     no_canuser:
97         atomic_dec(&obj->obj_used);
98         return -ENOMEM;
99 }