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.2 9 Jul 2003
12 #include "../include/can.h"
13 #include "../include/can_sysdep.h"
14 #include "../include/main.h"
16 #include <rtl_malloc.h>
17 #include <rtl_posixio.h>
18 #include "../include/can_iortl.h"
20 #define __NO_VERSION__
21 #include <linux/module.h>
25 int can_open_rtl_common(struct canuser_t *canuser, int open_flags)
27 struct msgobj_t *obj=canuser->msgobj;
29 struct canque_ends_t *qends;
30 struct canque_edge_t *edge;
31 can_spin_irqflags_t iflags;
33 if(!obj) return -ENODEV;
35 can_msgobj_set_fl(obj,OPENED);
39 if (!(chip->flags & CHIP_CONFIGURED)) {
40 if (chip->chipspecops->chip_config(chip))
41 CANMSG("Error configuring chip.\n");
43 chip->flags |= CHIP_CONFIGURED;
45 if (chip->chipspecops->pre_read_config(chip,obj)<0)
46 CANMSG("Error initializing chip for receiving\n");
49 } /* End of chip configuration */
52 qends = (struct canque_ends_t *)rt_malloc(sizeof(struct canque_ends_t));
53 if(qends == NULL) goto no_qends;
54 canqueue_ends_init_rtl(qends);
55 canuser->qends = qends;
57 can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
58 list_add(&canuser->peers, &obj->obj_users);
59 can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
61 if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
62 canuser->qends, obj->qends)<0) goto no_tx_qedge;
64 if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_rtl(MAX_BUF_LENGTH),
65 obj->qends, canuser->qends)<0) goto no_rx_qedge;
66 /*FIXME: more generic model should be used there*/
67 canque_edge_decref(canuser->rx_edge0);
68 canque_edge_decref(edge);
70 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
71 MOD_INC_USE_COUNT; /*is this enough for RT-Linux context ?*/
77 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
78 canque_edge_decref(edge);
80 list_del(&canuser->peers);
81 canuser->qends = NULL;
82 canqueue_ends_dispose_kern(qends, 1);
90 int can_open_rtl_posix(struct rtl_file *fptr)
95 struct canuser_t *canuser;
96 int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
98 if(minor_nr>=MAX_TOT_MSGOBJS)
101 if ( ((obj=objects_p[minor_nr]) == NULL) ||
102 ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
103 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",minor_nr);
107 atomic_inc(&obj->obj_used);
108 DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
110 canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
115 canuser->flags=CANUSER_RTL_CLIENT | CANUSER_RTL_MEM;
116 canuser->userinfo.rtlinfo.file = fptr;
117 canuser->msgobj = obj;
118 canuser->magic = CAN_USER_MAGIC;
120 /*next line would solve many problems, but RT-Linux lacks this fundamental field */
121 /*fptr->private_data = canuser;*/
122 /*to test code I am adding this terible hack*/
123 can_set_rtl_file_private_data(fptr,canuser);
125 ret=can_open_rtl_common(canuser, fptr->f_flags);
126 if(ret>=0) return ret;
131 atomic_dec(&obj->obj_used);
136 #endif /*CAN_WITH_RTL*/