]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ioctl.c
63aaea10188e1495f1024a8787c60d916d82b38a
[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  * 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.3  17 Jun 2004
8  */
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/ioctl.h"
14 #include "../include/i82527.h"
15
16 int can_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
17 {
18         int i=0;
19         unsigned short channel=0;
20         unsigned btr0=0, btr1=0;
21         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
22         struct msgobj_t *obj;
23         struct canchip_t *chip;
24         struct canque_ends_t *qends;
25         
26         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
27                 CANMSG("can_ioctl: bad canuser magic\n");
28                 return -ENODEV;
29         }
30         
31         obj = canuser->msgobj;
32         if (obj == NULL) {
33                 CANMSG("Could not assign buffer structure\n");
34                 return -1;
35         }
36
37         qends = canuser->qends;
38         chip = obj->hostchip;
39         if (chip == 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 CAN_DRV_QUERY: {
47                         return can_ioctl_query(canuser, arg);
48                 }
49                 case STAT: {
50                         for (i=0x0; i<0x100; i++)
51                                 CANMSG("0x%x is 0x%x\n",i,can_read_reg(chip,i));
52                         break;
53                 }
54                 case CMD_START: {
55                         if (chip->chipspecops->start_chip(chip))
56                                 return -1;
57                         break;
58                 }
59                 case CMD_STOP: {
60                         if (chip->chipspecops->stop_chip(chip))
61                                 return -1;
62                         break;
63                 }
64                 case CANQUE_FLUSH: {
65                         canque_flush(canuser->rx_edge0);
66                         break;
67                 }
68                 case CONF_FILTER: {
69
70                         /* In- and output buffer re-initialization */
71                         
72                         if(canuser->rx_edge0){
73                                 canque_set_filt(canuser->rx_edge0, arg, ~0, 0);
74                         }
75
76                         break;
77                 }
78                 
79                 case CANQUE_FILTER: {
80                         struct canfilt_t canfilt;
81                         copy_from_user(&canfilt, (void*)arg, sizeof(struct canfilt_t));
82                         if(canuser->rx_edge0){
83                                 canque_set_filt(canuser->rx_edge0, canfilt.id, canfilt.mask, canfilt.flags);
84                         }
85                         break;
86                 }
87                 
88                 case CANRTR_READ: {
89                         int ret;
90                         struct canmsg_t rtr_msg;
91                         
92                         copy_from_user(&rtr_msg, (void*)arg, sizeof(struct canmsg_t));
93                         ret = can_ioctl_remote_read(canuser, &rtr_msg, rtr_msg.id, 0);
94                         if(ret<0) return ret;
95                         copy_to_user((void*)arg, &rtr_msg, sizeof(struct canmsg_t));
96                 }
97
98                 case CONF_BAUD: {
99                         channel = arg & 0xff;
100                         btr0 = (arg >> 8) & 0xff;
101                         btr1 = (arg >> 16) & 0xff;
102
103                         if (chip->chipspecops->set_btregs(chip, btr0, btr1)) {
104                                 CANMSG("Error setting bit timing registers\n");
105                                 return -1;
106                         }
107                         break;
108                 }
109                 
110                 case CONF_BAUDPARAMS: {
111                         struct can_baudparams_t params;
112                         copy_from_user(&params, (void*)arg, sizeof(struct can_baudparams_t));
113                         if(params.flags == -1) params.flags = 0;
114                         if(params.baudrate == -1) params.baudrate = chip->baudrate;
115                         if(params.sjw == -1) params.sjw = 0;
116                         if(params.sample_pt == -1) params.sample_pt = 75;
117                         i=chip->chipspecops->baud_rate(chip, params.baudrate, chip->clock, params.sjw,
118                                                         params.sample_pt, params.flags);
119                         if(i>=0) chip->baudrate = params.baudrate;
120                         else {
121                                 CANMSG("Error setting baud parameters\n");
122                                 return -1;
123                         }
124                         break;
125                 }
126
127
128                 default: {
129                         CANMSG("Not a valid ioctl command\n");
130                         return -EINVAL;
131                 }
132                 
133         }
134
135         return 0;
136 }