]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/open.c
The original version of Arnaud Westenberg Linux CAN-bus driver
[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/malloc.h>
22 #include <linux/version.h>
23
24 #include "../include/main.h"
25 #include "../include/open.h"
26 #include "../include/i82527.h"
27 #include "../include/setup.h"
28
29 int can_open(struct inode *inode, struct file *file)
30 {
31         struct msgobj_t *obj;
32         struct chip_t *chip;
33         struct canfifo_t *fifo;
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         if (objects_p[MINOR_NR]->flags & OPENED) {
42                 CANMSG("Sorry, only single open per device file.\n");
43                 return -EBUSY;
44         }
45         else
46                 objects_p[MINOR_NR]->flags |= OPENED;
47
48         if (chip->flags & CONFIGURED) 
49                 DEBUGMSG("Device is already configured.\n");
50         else {
51                 if (chip->chipspecops->chip_config(chip))
52                         CANMSG("Error configuring chip.\n");
53                 else
54                         chip->flags |= CONFIGURED; 
55         } /* End of chip configuration */
56
57         /* Allocate output buffer memory for the opened device */
58         fifo = objects_p[MINOR_NR]->fifo;
59         fifo->buf_tx_entry=(struct canmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct canmsg_t), GFP_KERNEL);
60         if (fifo->buf_tx_entry == NULL)
61                 return -ENOMEM;
62         else
63                 if ( add_mem_to_list(fifo->buf_tx_entry) )
64                         return -ENOMEM;
65         /* Allocate input buffer memory for the opened device */
66         fifo->buf_rx_entry=(struct canmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct canmsg_t), GFP_KERNEL);
67         if (fifo->buf_rx_entry == NULL)
68                 return -ENOMEM;
69         else
70                 if ( add_mem_to_list(fifo->buf_rx_entry) )
71                         return -ENOMEM;
72
73         /* In- and output buffer initialization */
74         fifo->tx_readp = fifo->buf_tx_entry;
75         fifo->tx_writep = fifo->buf_tx_entry;
76         fifo->rx_readp = fifo->buf_rx_entry;
77         fifo->rx_writep = fifo->buf_rx_entry;
78         fifo->rx_size = MAX_BUF_LENGTH * sizeof(struct canmsg_t);
79         fifo->tx_size = fifo->rx_size;
80
81 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
82         init_waitqueue(&fifo->readq);
83         init_waitqueue(&fifo->writeq);
84 #else
85         init_waitqueue_head(&fifo->readq);
86         init_waitqueue_head(&fifo->writeq);
87 #endif
88
89         fifo->rx_in_progress = 0;
90         fifo->tx_in_progress = 0;
91
92         fifo->head = fifo->tail = 0; //TEMP!!
93
94         if (chip->chipspecops->pre_read_config(chip,obj)<0)
95                 CANMSG("Error initializing chip for receiving\n");
96                 
97         MOD_INC_USE_COUNT;
98         
99         return 0;
100 }