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
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/read.h"
15 /* This is the 'Normal' read handler for normal transmission messages */
16 ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
18 struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
19 struct canque_ends_t *qends;
21 struct canque_edge_t *qedge;
22 struct canque_slot_t *slot;
25 if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
26 CANMSG("can_read: bad canuser magic\n");
30 if (length < sizeof(struct canmsg_t)) {
31 DEBUGMSG("Trying to read less bytes than a CAN message, \n");
32 DEBUGMSG("this will always return zero.\n");
36 qends = canuser->qends;
38 ret=canque_test_outslot(qends, &qedge, &slot);
40 if (file->f_flags & O_NONBLOCK) {
43 ret=canque_get_outslot_wait_kern(qends, &qedge, &slot);
45 if (signal_pending(current)) {
46 DEBUGMSG("Rx interrupted\n");
50 DEBUGMSG("no data received\n");
57 ret = copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
58 canque_free_outslot(qends, qedge, slot);
59 buffer += sizeof(struct canmsg_t);
60 bytes_to_copy = length-sizeof(struct canmsg_t);
61 if(ret) return -EFAULT;
63 while (bytes_to_copy > 0) {
64 ret=canque_test_outslot(qends, &qedge, &slot);
67 ret = copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
68 canque_free_outslot(qends, qedge, slot);
69 buffer += sizeof(struct canmsg_t);
70 bytes_to_copy -= sizeof(struct canmsg_t);
71 if(ret) return -EFAULT;
74 return length-bytes_to_copy;