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