]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ioctl.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[lincan.git] / lincan / src / ioctl.c
1 /* ioctl.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 #include <linux/autoconf.h>
9 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
10 #define MODVERSIONS
11 #endif
12
13 #if defined (MODVERSIONS)
14 #include <linux/modversions.h>
15 #endif
16
17 #include <linux/fs.h>
18 #include <linux/version.h>
19 #include <linux/string.h>
20
21 #include "../include/main.h"
22 #include "../include/ioctl.h"
23 #include "../include/i82527.h"
24
25 int can_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
26 {
27         int i=0;
28         unsigned short channel=0;
29         unsigned btr0=0, btr1=0;
30         struct msgobj_t *obj;
31         struct chip_t *chip = objects_p[MINOR_NR]->hostchip;
32         struct canfifo_t *fifo = objects_p[MINOR_NR]->fifo;
33
34         /* Initialize hardware pointers */
35         if ( (obj = objects_p[MINOR_NR]) == NULL) {
36                 CANMSG("Could not assign buffer structure\n");
37                 return -1;
38         }
39         if ( (chip = obj->hostchip) == NULL) {
40                 CANMSG("Device is not correctly configured.\n");
41                 CANMSG("Please reload the driver.\n");
42                 return -1;
43         }
44
45         switch (cmd) {
46                 case STAT: {
47                         for (i=0x0; i<0x100; i++)
48                                 CANMSG("0x%x is 0x%x\n",i,can_read_reg(chip,i));
49                         break;
50                 }
51                 case CMD_START: {
52                         if (chips_p[arg]->chipspecops->start_chip(chip))
53                                 return -1;
54                         break;
55                 }
56                 case CMD_STOP: {
57                         if (chips_p[arg]->chipspecops->stop_chip(chip))
58                                 return -1;
59                         break;
60                 }
61                 case CONF_FILTER: {
62                         if (!strcmp(chip->chip_type,"i82527")) {
63                         
64                                 unsigned char id1, id0;
65                                 id1 = (unsigned char) (arg << 5);
66                                 id0 = (unsigned char) (arg >> 3);
67
68                                 DEBUGMSG("configuring ID=%lx in message object:
69                                                  %02x, %02x\n", arg, id0, id1);
70                                 can_write_reg(chip,id1,MSG_OFFSET(obj->object) +
71                                                                 iMSGID1);
72                                 can_write_reg(chip,id0,MSG_OFFSET(obj->object) +
73                                                                 iMSGID0);
74                         }
75
76                         /* In- and output buffer re-initialization */
77                         
78                         fifo->tx_readp = fifo->buf_tx_entry;
79                         fifo->tx_writep = fifo->buf_tx_entry;
80                         fifo->rx_readp = fifo->buf_rx_entry;
81                         fifo->rx_writep = fifo->buf_rx_entry;
82                         fifo->rx_size= MAX_BUF_LENGTH * sizeof(struct canmsg_t);
83                         fifo->tx_size = fifo->rx_size;
84
85                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
86                                 init_waitqueue(&fifo->readq);
87                                 init_waitqueue(&fifo->writeq);
88                         #else
89                                 init_waitqueue_head(&fifo->readq);
90                                 init_waitqueue_head(&fifo->writeq);
91                         #endif
92
93                         fifo->rx_in_progress = 0;
94                         fifo->tx_in_progress = 0;
95
96                         break;
97                 }
98
99                 case CONF_BAUD: {
100                         channel = arg & 0xff;
101                         btr0 = (arg >> 8) & 0xff;
102                         btr1 = (arg >> 16) & 0xff;
103
104                         if (chips_p[channel]->chipspecops->set_btregs(chip,
105                                                                 btr0, btr1)) {
106                                 CANMSG("Error setting bit timing registers\n");
107                                 return -1;
108                         }
109                         break;
110                 }
111                 default: {
112                         CANMSG("Not a valid ioctl command\n");
113                         return -EINVAL;
114                 }
115                 
116         }
117
118         return 0;
119 }