]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ioctl.c
2d410f19b531ffa4a1223719071049db70433135
[lincan.git] / lincan / src / ioctl.c
1 /**************************************************************************/
2 /* File: ioctl.c - CAN driver IOCTL functions support                     */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/ioctl.h"
39
40 int can_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
41 {
42         int i=0;
43         unsigned short channel=0;
44         unsigned btr0=0, btr1=0;
45         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
46         struct msgobj_t *obj;
47         struct canchip_t *chip;
48         struct canque_ends_t *qends;
49         
50         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
51                 CANMSG("can_ioctl: bad canuser magic\n");
52                 return -ENODEV;
53         }
54         
55         obj = canuser->msgobj;
56         if (obj == NULL) {
57                 CANMSG("Could not assign buffer structure\n");
58                 return -1;
59         }
60
61         qends = canuser->qends;
62         chip = obj->hostchip;
63         if (chip == NULL) {
64                 CANMSG("Device is not correctly configured.\n");
65                 CANMSG("Please reload the driver.\n");
66                 return -1;
67         }
68
69         switch (cmd) {
70                 case CAN_DRV_QUERY: {
71                         return can_ioctl_query(canuser, arg);
72                 }
73                 case STAT: {
74                         for (i=0x0; i<0x100; i++)
75                                 CANMSG("0x%x is 0x%x\n",i,can_read_reg(chip,i));
76                         break;
77                 }
78                 case CMD_START: {
79                         if (chip->chipspecops->start_chip(chip))
80                                 return -1;
81                         break;
82                 }
83                 case CMD_STOP: {
84                         if (chip->chipspecops->stop_chip(chip))
85                                 return -1;
86                         break;
87                 }
88                 case CANQUE_FLUSH: {
89                         canque_flush(canuser->rx_edge0);
90                         break;
91                 }
92                 case CONF_FILTER: {
93
94                         /* In- and output buffer re-initialization */
95                         
96                         if(canuser->rx_edge0){
97                                 canque_set_filt(canuser->rx_edge0, arg, ~0, 0);
98                         }
99
100                         break;
101                 }
102                 
103                 case CANQUE_FILTER: {
104                         struct canfilt_t canfilt;
105                         int ret;
106                         ret = copy_from_user(&canfilt, (void*)arg, sizeof(struct canfilt_t));
107                         if(ret) return -EFAULT;
108                         if(canuser->rx_edge0){
109                                 canque_set_filt(canuser->rx_edge0, canfilt.id, canfilt.mask, canfilt.flags);
110                         }
111                         break;
112                 }
113                 
114                 case CANRTR_READ: {
115                         int ret;
116                         struct canmsg_t rtr_msg;
117                         
118                         ret = copy_from_user(&rtr_msg, (void*)arg, sizeof(struct canmsg_t));
119                         if(ret) return -EFAULT;
120                         ret = can_ioctl_remote_read(canuser, &rtr_msg, rtr_msg.id, 0);
121                         if(ret<0) return ret;
122                         ret = copy_to_user((void*)arg, &rtr_msg, sizeof(struct canmsg_t));
123                         if(ret) return -EFAULT;
124                         break;
125                 }
126
127                 case CONF_BAUD: {
128                         channel = arg & 0xff;
129                         btr0 = (arg >> 8) & 0xff;
130                         btr1 = (arg >> 16) & 0xff;
131
132                         if (chip->chipspecops->set_btregs(chip, btr0, btr1)) {
133                                 CANMSG("Error setting bit timing registers\n");
134                                 return -1;
135                         }
136                         break;
137                 }
138                 
139                 case CONF_BAUDPARAMS: {
140                         struct can_baudparams_t params;
141                         int ret;
142                         
143                         ret = copy_from_user(&params, (void*)arg, sizeof(struct can_baudparams_t));
144                         if(ret) return -EFAULT;
145
146                         if(params.flags == -1) params.flags = 0;
147                         if(params.baudrate == -1) params.baudrate = chip->baudrate;
148                         if(params.sjw == -1) params.sjw = 0;
149                         if(params.sample_pt == -1) params.sample_pt = 75;
150                         i=chip->chipspecops->baud_rate(chip, params.baudrate, chip->clock, params.sjw,
151                                                         params.sample_pt, params.flags);
152                         if(i>=0) chip->baudrate = params.baudrate;
153                         else {
154                                 CANMSG("Error setting baud parameters\n");
155                                 return -1;
156                         }
157                         break;
158                 }
159
160
161                 default: {
162                         CANMSG("Not a valid ioctl command\n");
163                         return -EINVAL;
164                 }
165                 
166         }
167
168         return 0;
169 }