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