Xenomai  3.0.8
rtcan_dev.h
1 /*
2  * Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
3  *
4  * Derived from RTnet project file stack/include/rtdev.h:
5  *
6  * Copyright (C) 1999 Lineo, Inc
7  * 1999, 2002 David A. Schleef <ds@schleef.org>
8  * 2003-2005 Jan Kiszka <jan.kiszka@web.de>
9  *
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 #ifndef __RTCAN_DEV_H_
27 #define __RTCAN_DEV_H_
28 
29 
30 #ifdef __KERNEL__
31 
32 #include <asm/atomic.h>
33 #include <linux/netdevice.h>
34 #include <linux/semaphore.h>
35 
36 #include "rtcan_list.h"
37 
38 
39 /* Number of MSCAN devices the driver can handle */
40 #define RTCAN_MAX_DEVICES CONFIG_XENO_DRIVERS_CAN_MAX_DEVICES
41 
42 /* Maximum number of single filters per controller which can be registered
43  * for reception at the same time using Bind */
44 #define RTCAN_MAX_RECEIVERS CONFIG_XENO_DRIVERS_CAN_MAX_RECEIVERS
45 
46 /* Suppress handling of refcount if module support is not enabled
47  * or modules cannot be unloaded */
48 
49 #if defined(CONFIG_MODULES) && defined(CONFIG_MODULE_UNLOAD)
50 #define RTCAN_USE_REFCOUNT
51 #endif
52 
53 /*
54  * CAN harware-dependent bit-timing constant
55  *
56  * Used for calculating and checking bit-timing parameters
57  */
58 struct can_bittiming_const {
59  char name[16]; /* Name of the CAN controller hardware */
60  __u32 tseg1_min; /* Time segement 1 = prop_seg + phase_seg1 */
61  __u32 tseg1_max;
62  __u32 tseg2_min; /* Time segement 2 = phase_seg2 */
63  __u32 tseg2_max;
64  __u32 sjw_max; /* Synchronisation jump width */
65  __u32 brp_min; /* Bit-rate prescaler */
66  __u32 brp_max;
67  __u32 brp_inc;
68 };
69 
70 struct rtcan_device {
71  unsigned int version;
72 
73  char name[IFNAMSIZ];
74 
75  char *ctrl_name; /* Name of CAN controller */
76  char *board_name;/* Name of CAN board */
77 
78  unsigned long base_addr; /* device I/O address */
79  rtdm_irq_t irq_handle; /* RTDM IRQ handle */
80 
81  int ifindex;
82 #ifdef RTCAN_USE_REFCOUNT
83  atomic_t refcount;
84 #endif
85 
86  void *priv; /* pointer to chip private data */
87 
88  void *board_priv;/* pointer to board private data*/
89 
90  struct semaphore nrt_lock; /* non-real-time locking */
91 
92  /* Spinlock for all devices (but not for all attributes) and also for HW
93  * access to all CAN controllers
94  */
95  rtdm_lock_t device_lock;
96 
97  /* Acts as a mutex allowing only one sender to write to the MSCAN
98  * simultaneously. Created when the controller goes into operating mode,
99  * destroyed if it goes into reset mode. */
100  rtdm_sem_t tx_sem;
101 
102  /* Baudrate of this device. Protected by device_lock in all device
103  * structures. */
104  unsigned int can_sys_clock;
105 
106 
107  /* Baudrate of this device. Protected by device_lock in all device
108  * structures. */
109  can_baudrate_t baudrate;
110 
111  struct can_bittime bit_time;
112  const struct can_bittiming_const *bittiming_const;
113 
114  /* State which the controller is in. Protected by device_lock in all
115  * device structures. */
116  can_state_t state;
117 
118  /* State which the controller was before sleeping. Protected by
119  * device_lock in all device structures. */
120  can_state_t state_before_sleep;
121 
122  /* Controller specific settings. Protected by device_lock in all
123  * device structures. */
124  can_ctrlmode_t ctrl_mode;
125 
126  /* Device operations */
127  int (*hard_start_xmit)(struct rtcan_device *dev,
128  struct can_frame *frame);
129  int (*do_set_mode)(struct rtcan_device *dev,
130  can_mode_t mode,
131  rtdm_lockctx_t *lock_ctx);
132  can_state_t (*do_get_state)(struct rtcan_device *dev);
133  int (*do_set_bit_time)(struct rtcan_device *dev,
134  struct can_bittime *bit_time,
135  rtdm_lockctx_t *lock_ctx);
136 #ifdef CONFIG_XENO_DRIVERS_CAN_BUS_ERR
137  void (*do_enable_bus_err)(struct rtcan_device *dev);
138 #endif
139 
140  /* Reception list head. This list contains all filters which have been
141  * registered via a bind call. */
142  struct rtcan_recv *recv_list;
143 
144  /* Empty list head. This list contains all empty entries not needed
145  * by the reception list and therefore is disjunctive with it. */
146  struct rtcan_recv *empty_list;
147 
148  /* Preallocated array for the list entries. To increase cache
149  * locality all list elements are kept in this array. */
150  struct rtcan_recv receivers[RTCAN_MAX_RECEIVERS];
151 
152  /* Indicates the length of the empty list */
153  int free_entries;
154 
155  /* A few statistics counters */
156  unsigned int tx_count;
157  unsigned int rx_count;
158  unsigned int err_count;
159 
160 #ifdef CONFIG_PROC_FS
161  struct proc_dir_entry *proc_root;
162 #endif
163 #ifdef CONFIG_XENO_DRIVERS_CAN_LOOPBACK
164  struct rtcan_skb tx_skb;
165  struct rtcan_socket *tx_socket;
166 #endif /* CONFIG_XENO_DRIVERS_CAN_LOOPBACK */
167 };
168 
169 
170 extern struct semaphore rtcan_devices_nrt_lock;
171 
172 
173 void rtcan_dev_free(struct rtcan_device *dev);
174 
175 int rtcan_dev_register(struct rtcan_device *dev);
176 int rtcan_dev_unregister(struct rtcan_device *dev);
177 
178 struct rtcan_device *rtcan_dev_alloc(int sizeof_priv, int sizeof_board_priv);
179 void rtcan_dev_alloc_name (struct rtcan_device *dev, const char *name_mask);
180 
181 struct rtcan_device *rtcan_dev_get_by_name(const char *if_name);
182 struct rtcan_device *rtcan_dev_get_by_index(int ifindex);
183 
184 #ifdef RTCAN_USE_REFCOUNT
185 #define rtcan_dev_reference(dev) atomic_inc(&(dev)->refcount)
186 #define rtcan_dev_dereference(dev) atomic_dec(&(dev)->refcount)
187 #else
188 #define rtcan_dev_reference(dev) do {} while(0)
189 #define rtcan_dev_dereference(dev) do {} while(0)
190 #endif
191 
192 #ifdef CONFIG_PROC_FS
193 int rtcan_dev_create_proc(struct rtcan_device* dev);
194 void rtcan_dev_remove_proc(struct rtcan_device* dev);
195 #else /* !CONFIG_PROC_FS */
196 static inline int rtcan_dev_create_proc(struct rtcan_device* dev)
197 {
198  return 0;
199 }
200 static inline void rtcan_dev_remove_proc(struct rtcan_device* dev) { }
201 #endif /* !CONFIG_PROC_FS */
202 
203 #endif /* __KERNEL__ */
204 
205 #endif /* __RTCAN_DEV_H_ */
ipipe_spinlock_t rtdm_lock_t
Lock variable.
Definition: driver.h:551
Custom CAN bit-time definition.
Definition: can.h:151
uint32_t can_baudrate_t
Baudrate definition in bits per second.
Definition: can.h:110
Raw CAN frame.
Definition: can.h:313
Copyright © 2011 Gilles Chanteperdrix gilles.chanteperdrix@xenomai.org.
Definition: atomic.h:24
int can_ctrlmode_t
See CAN_CTRLMODE.
Definition: can.h:221
enum CAN_STATE can_state_t
See CAN_STATE.
Definition: can.h:258
unsigned long rtdm_lockctx_t
Variable to save the context while holding a lock.
Definition: driver.h:554
enum CAN_MODE can_mode_t
See CAN_MODE.
Definition: can.h:187