]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[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  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */
7
8 #define __NO_VERSION__
9 #include <linux/module.h> 
10
11 #include <linux/autoconf.h>
12 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
13 #define MODVERSIONS
14 #endif
15
16 #if defined (MODVERSIONS)
17 #include <linux/modversions.h>
18 #endif
19
20 #include <linux/fs.h>
21 #include <linux/version.h>
22 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
23 #include <linux/malloc.h>
24 #else
25 #include <linux/slab.h>
26 #endif
27 #include <linux/version.h>
28
29 #include "../include/main.h"
30 #include "../include/open.h"
31 #include "../include/i82527.h"
32 #include "../include/setup.h"
33
34 int can_open(struct inode *inode, struct file *file)
35 {
36         struct msgobj_t *obj;
37         struct chip_t *chip;
38         struct canfifo_t *fifo;
39
40         if ( ((obj=objects_p[MINOR_NR]) == NULL) || 
41                         ((chip=objects_p[MINOR_NR]->hostchip) == NULL) ) {
42                 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",MINOR_NR);
43                 return -ENODEV;
44         }
45
46         if (objects_p[MINOR_NR]->flags & OPENED) {
47                 CANMSG("Sorry, only single open per device file.\n");
48                 return -EBUSY;
49         }
50         else
51                 objects_p[MINOR_NR]->flags |= OPENED;
52
53         if (chip->flags & CONFIGURED) 
54                 DEBUGMSG("Device is already configured.\n");
55         else {
56                 if (chip->chipspecops->chip_config(chip))
57                         CANMSG("Error configuring chip.\n");
58                 else
59                         chip->flags |= CONFIGURED; 
60         } /* End of chip configuration */
61
62         /* Allocate output buffer memory for the opened device */
63         fifo = objects_p[MINOR_NR]->fifo;
64         fifo->buf_tx_entry=(struct canmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct canmsg_t), GFP_KERNEL);
65         if (fifo->buf_tx_entry == NULL)
66                 return -ENOMEM;
67         else
68                 if ( add_mem_to_list(fifo->buf_tx_entry) )
69                         return -ENOMEM;
70         /* Allocate input buffer memory for the opened device */
71         fifo->buf_rx_entry=(struct canmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct canmsg_t), GFP_KERNEL);
72         if (fifo->buf_rx_entry == NULL)
73                 return -ENOMEM;
74         else
75                 if ( add_mem_to_list(fifo->buf_rx_entry) )
76                         return -ENOMEM;
77
78         /* In- and output buffer initialization */
79         fifo->tx_readp = fifo->buf_tx_entry;
80         fifo->tx_writep = fifo->buf_tx_entry;
81         fifo->rx_readp = fifo->buf_rx_entry;
82         fifo->rx_writep = fifo->buf_rx_entry;
83         fifo->rx_size = MAX_BUF_LENGTH * sizeof(struct canmsg_t);
84         fifo->tx_size = fifo->rx_size;
85
86 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
87         init_waitqueue(&fifo->readq);
88         init_waitqueue(&fifo->writeq);
89 #else
90         init_waitqueue_head(&fifo->readq);
91         init_waitqueue_head(&fifo->writeq);
92 #endif
93
94         fifo->rx_in_progress = 0;
95         fifo->tx_in_progress = 0;
96
97         chip->flags |= BUFFERS_ALLOCATED;
98
99         if (chip->chipspecops->pre_read_config(chip,obj)<0)
100                 CANMSG("Error initializing chip for receiving\n");
101                 
102 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
103         MOD_INC_USE_COUNT;
104 #endif  
105         return 0;
106 }