Separated normal read and RTR assisted read transfer.
[lincan.git] / lincan / src / ioctl_remote.c
1 /* ioctl_remote.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 Nov 2004
8  */
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/read.h"
14 #include "../include/ioctl.h"
15
16 /* This is the 'RTR' read handler for remote transmission request messages */
17 int can_ioctl_remote_read(struct canuser_t *canuser, struct canmsg_t *rtr_msg,
18                           unsigned long rtr_id, int options)
19 {
20         can_spin_irqflags_t flags;
21         struct rtr_id *rtr_current, *new_rtr_entry;
22         struct msgobj_t *obj;
23         struct canchip_t *chip;
24         /*struct canque_ends_t *qends;*/
25         
26         DEBUGMSG("Remote transmission request\n");
27
28         /*qends = canuser->qends;*/
29
30         /* Initialize hardware pointers */
31         obj = canuser->msgobj;
32         if (obj == NULL) {
33                 CANMSG("Could not assign buffer structure\n");
34                 return -ENODEV;
35         }
36
37         if ( (chip = obj->hostchip) == NULL) {
38                 CANMSG("Device is not correctly configured,\n");
39                 CANMSG("please reload the driver.\n");
40                 return -ENODEV;
41         }
42
43         can_spin_lock_irqsave(&hardware_p->rtr_lock, flags);
44         if (hardware_p->rtr_queue == NULL) { //No remote messages pending
45                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
46                 if (new_rtr_entry == NULL) {
47                         can_spin_unlock_irqrestore(&hardware_p->rtr_lock, 
48                                                                 flags);
49                         return -ENOMEM;
50                 }
51                 hardware_p->rtr_queue=new_rtr_entry;
52         }
53         else {
54                 rtr_current=hardware_p->rtr_queue;
55                 while (rtr_current->next != NULL)
56                         rtr_current=rtr_current->next;
57                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
58                 rtr_current->next=new_rtr_entry;
59         }
60         init_waitqueue_head(&new_rtr_entry->rtr_wq);
61         new_rtr_entry->id = rtr_id;
62         new_rtr_entry->rtr_message = rtr_msg;
63         new_rtr_entry->next=NULL;
64
65         can_spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
66
67         /* Send remote transmission request */
68         chip->chipspecops->remote_request(chip,obj);
69         obj->ret = 0;
70         interruptible_sleep_on(&new_rtr_entry->rtr_wq);
71
72         can_spin_lock_irqsave(&hardware_p->rtr_lock, flags);
73         if (hardware_p->rtr_queue == new_rtr_entry) {
74                 if (new_rtr_entry->next != NULL) 
75                         hardware_p->rtr_queue=new_rtr_entry->next;
76                 else
77                         hardware_p->rtr_queue=NULL;
78         }
79         else {
80                 rtr_current=hardware_p->rtr_queue;
81                 while (rtr_current->next != new_rtr_entry)
82                         rtr_current=rtr_current->next;
83                 if (new_rtr_entry->next != NULL)
84                         rtr_current->next=new_rtr_entry->next;
85                 else
86                         rtr_current->next=NULL;
87         }
88         can_spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
89         kfree(new_rtr_entry);
90
91         return obj->ret;
92 }
93