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
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;
28 struct canchip_t *chip;
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 /* mark memory as allocated from RTL memory pool */
56 qends->ends_flags|=CAN_ENDSF_MEM_RTL;
57 canuser->qends = qends;
59 can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
60 list_add(&canuser->peers, &obj->obj_users);
61 can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
63 if(canqueue_connect_edge(edge=canque_new_edge_rtl(MAX_BUF_LENGTH),
64 canuser->qends, obj->qends)<0) goto no_tx_qedge;
66 if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_rtl(MAX_BUF_LENGTH),
67 obj->qends, canuser->qends)<0) goto no_rx_qedge;
68 /*FIXME: more generic model should be used there*/
69 canque_edge_decref(canuser->rx_edge0);
70 canque_edge_decref(edge);
72 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,50))
73 MOD_INC_USE_COUNT; /*is this enough for RT-Linux context ?*/
79 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
80 canque_edge_decref(edge);
82 list_del(&canuser->peers);
83 canuser->qends = NULL;
84 canqueue_ends_dispose_kern(qends, 1);
92 int can_open_rtl_posix(struct rtl_file *fptr)
96 struct canchip_t *chip;
97 struct canuser_t *canuser;
98 int minor_nr = RTL_MINOR_FROM_FILEPTR(fptr);
100 if(minor_nr>=MAX_TOT_MSGOBJS)
103 if ( ((obj=objects_p[minor_nr]) == NULL) ||
104 ((chip=objects_p[minor_nr]->hostchip) == NULL) ) {
105 CANMSG("There is no hardware support for the device file with minor nr.: %d\n",minor_nr);
109 atomic_inc(&obj->obj_used);
110 DEBUGMSG("Device %d opened %d times.\n", minor_nr, atomic_read(&obj->obj_used));
112 canuser = (struct canuser_t *)rt_malloc(sizeof(struct canuser_t));
117 canuser->flags=CANUSER_RTL_CLIENT | CANUSER_RTL_MEM;
118 canuser->userinfo.rtlinfo.file = fptr;
119 canuser->msgobj = obj;
120 canuser->magic = CAN_USER_MAGIC;
122 /*next line would solve many problems, but RT-Linux lacks this fundamental field */
123 /*fptr->private_data = canuser;*/
124 /*to test code I am adding this terible hack*/
125 can_set_rtl_file_private_data(fptr,canuser);
127 ret=can_open_rtl_common(canuser, fptr->f_flags);
128 if(ret>=0) return ret;
133 atomic_dec(&obj->obj_used);
138 #endif /*CAN_WITH_RTL*/