]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ioctl_remote.c
Merge branch 'master' into can-usb1
[lincan.git] / lincan / src / ioctl_remote.c
1 /**************************************************************************/
2 /* File: ioctl_remote.c - remote CAN messages support IOCTL systemcall    */
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/read.h"
39 #include "../include/ioctl.h"
40
41 /* This is the 'RTR' read handler for remote transmission request messages */
42 int can_ioctl_remote_read(struct canuser_t *canuser, struct canmsg_t *rtr_msg,
43                           unsigned long rtr_id, int options)
44 {
45         can_spin_irqflags_t flags;
46         struct rtr_id *rtr_current, *new_rtr_entry;
47         struct msgobj_t *obj;
48         struct canchip_t *chip;
49         int err;
50         /*struct canque_ends_t *qends;*/
51
52         DEBUGMSG("Remote transmission request\n");
53
54         /*qends = canuser->qends;*/
55
56         /* Initialize hardware pointers */
57         obj = canuser->msgobj;
58         if (obj == NULL) {
59                 CANMSG("Could not assign buffer structure\n");
60                 return -ENODEV;
61         }
62
63         if ( (chip = obj->hostchip) == NULL) {
64                 CANMSG("Device is not correctly configured,\n");
65                 CANMSG("please reload the driver.\n");
66                 return -ENODEV;
67         }
68
69         can_spin_lock_irqsave(&hardware_p->rtr_lock, flags);
70         if (hardware_p->rtr_queue == NULL) { //No remote messages pending
71                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
72                 if (new_rtr_entry == NULL) {
73                         can_spin_unlock_irqrestore(&hardware_p->rtr_lock,
74                                                                 flags);
75                         return -ENOMEM;
76                 }
77                 hardware_p->rtr_queue=new_rtr_entry;
78         }
79         else {
80                 rtr_current=hardware_p->rtr_queue;
81                 while (rtr_current->next != NULL)
82                         rtr_current=rtr_current->next;
83                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
84                 rtr_current->next=new_rtr_entry;
85         }
86         init_waitqueue_head(&new_rtr_entry->rtr_wq);
87         new_rtr_entry->id = rtr_id;
88         new_rtr_entry->ready_fl = 0;
89         new_rtr_entry->rtr_message = rtr_msg;
90         new_rtr_entry->next=NULL;
91
92         can_spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
93
94         /* Send remote transmission request */
95         chip->chipspecops->remote_request(chip,obj);
96         obj->ret = 0;
97
98         err = wait_event_interruptible(new_rtr_entry->rtr_wq, new_rtr_entry->ready_fl);
99
100         can_spin_lock_irqsave(&hardware_p->rtr_lock, flags);
101         if (hardware_p->rtr_queue == new_rtr_entry) {
102                 if (new_rtr_entry->next != NULL)
103                         hardware_p->rtr_queue=new_rtr_entry->next;
104                 else
105                         hardware_p->rtr_queue=NULL;
106         }
107         else {
108                 rtr_current=hardware_p->rtr_queue;
109                 while (rtr_current->next != new_rtr_entry)
110                         rtr_current=rtr_current->next;
111                 if (new_rtr_entry->next != NULL)
112                         rtr_current->next=new_rtr_entry->next;
113                 else
114                         rtr_current->next=NULL;
115         }
116         can_spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
117         kfree(new_rtr_entry);
118
119         if (err)
120                 return -EINTR;
121
122         return obj->ret;
123 }
124