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"
14 ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *offset)
16 struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
18 struct canmsg_t msg_buff;
19 struct canque_ends_t *qends;
20 struct canque_edge_t *qedge;
21 struct canque_slot_t *slot;
23 unsigned bytes_to_copy;
25 if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
26 CANMSG("can_write: bad canuser magic\n");
30 if (length < sizeof(struct canmsg_t)) {
31 DEBUGMSG("Trying to write less bytes than a CAN message,\n");
32 DEBUGMSG("this will always return 0 !\n");
35 if (length > INT_MAX) {
36 CANMSG("Trying to write more than is supported.\n");
39 if (length % sizeof(struct canmsg_t)) {
40 CANMSG("The number of bytes requested to be written is not a multiple of\n");
41 CANMSG("'sizeof(struct canmsg_t)', currently this is not allowed.\n");
45 /* Initialize hardware pointers */
46 obj = canuser->msgobj;
48 CANMSG("Could not assign buffer structure\n");
51 qends = canuser->qends;
54 /* Prepare first message */
55 copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
57 /* Automatic selection of extended format if ID>2047 */
58 if (msg_buff.id & ~0x7ffl & MSG_ID_MASK ) msg_buff.flags |= MSG_EXT;
59 /* has been dependent on "extended" option */
61 /* If the output buffer is full, return immediately in case O_NONBLOCK
62 * has been specified or loop until space becomes available.
64 if ((ret=canque_get_inslot4id(qends, &qedge, &slot,
65 0, msg_buff.id, 0))<0){
66 DEBUGMSG("Buffer is full\n");
70 if (file->f_flags & O_NONBLOCK)
73 ret=canque_get_inslot4id_wait_kern(qends, &qedge, &slot,
76 if (signal_pending(current))
83 canque_put_inslot(qends, qedge, slot);
84 buffer += sizeof(struct canmsg_t);
85 bytes_to_copy = length-sizeof(struct canmsg_t);
88 * Try to send more messages
90 while (bytes_to_copy >= sizeof(struct canmsg_t)) {
91 bytes_to_copy -= sizeof(struct canmsg_t);
92 /* Prepare first message */
93 copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
95 /* Automatic selection of extended format if ID>2047 */
96 if (msg_buff.id & ~0x7ffl & MSG_ID_MASK ) msg_buff.flags |= MSG_EXT;
97 /* has been dependent on "extended" option */
100 if(canque_get_inslot4id(qends, &qedge, &slot,
101 0, msg_buff.id, 0) < 0) break;
103 canque_put_inslot(qends, qedge, slot);
104 buffer += sizeof(struct canmsg_t);
107 if(file->f_flags & O_SYNC) {
108 canque_sync_wait_kern(qends, qedge);
110 return length-bytes_to_copy;