bfin: remove inline keyword
[xenomai-head.git] / include / rtdm / rtcan.h
blob3ff76515d086506aba6f98033f267641779f41d8
1 /**
2 * @file
3 * Real-Time Driver Model for RT-Socket-CAN, CAN device profile header
5 * @note Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
7 * @note Copyright (C) 2005, 2006 Sebastian Smolorz
8 * <Sebastian.Smolorz@stud.uni-hannover.de>
10 * This RTDM CAN device profile header is based on:
12 * include/linux/can.h, include/linux/socket.h, net/can/pf_can.h in
13 * linux-can.patch, a CAN socket framework for Linux
15 * Copyright (C) 2004, 2005,
16 * Robert Schwebel, Benedikt Spranger, Marc Kleine-Budde, Pengutronix
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software Foundation,
31 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 * @ingroup rtcan
36 /*!
37 * @ingroup profiles
38 * @defgroup rtcan CAN Devices
40 * This is the common interface a RTDM-compliant CAN device has to provide.
41 * Feel free to report bugs and comments on this profile to the "Socketcan"
42 * mailing list (Socketcan-core@lists.berlios.de) or directly to the authors
43 * (wg@grandegger.com or Sebastian.Smolorz@stud.uni-hannover.de).
45 * @b Profile @b Revision: 2
46 * @n
47 * @n
48 * @par Device Characteristics
49 * @n
50 * @ref rtdm_device.device_flags "Device Flags": @c RTDM_PROTOCOL_DEVICE @n
51 * @n
52 * @ref rtdm_device.protocol_family "Protocol Family": @c PF_CAN @n
53 * @n
54 * @ref rtdm_device.socket_type "Socket Type": @c SOCK_RAW @n
55 * @n
56 * @ref rtdm_device.device_class "Device Class": @c RTDM_CLASS_CAN @n
57 * @n
59 * @par Supported Operations
60 * @n
61 * @b Socket @n
62 * Environments: non-RT (RT optional, deprecated)@n
63 * @n
64 * Specific return values:
65 * - -EPROTONOSUPPORT (Protocol is not supported by the driver.
66 * See @ref CAN_PROTO "CAN protocols"
67 * for possible protocols.)
68 * .
69 * @n
70 * @n
71 * @b Close @n
72 * Blocking calls to any of the @ref Send or @ref Recv "Receive" functions
73 * will be unblocked when the socket is closed and return with an error. @n
74 * @n
75 * Environments: non-RT (RT optional, deprecated)@n
76 * @n
77 * Specific return values: none @n
78 * @n
79 * @n
80 * @b IOCTL @n
81 * Mandatory Environments: see @ref CANIOCTLs "below" @n
82 * Specific return values: see @ref CANIOCTLs "below" @n
83 * @n
84 * @n
85 * @anchor Bind
86 * @b Bind @n
87 * Binds a socket to one or all CAN devices (see struct sockaddr_can). If
88 * a filter list has been defined with setsockopt (see @ref Sockopts),
89 * it will be used upon reception of CAN frames to decide whether the
90 * bound socket will receive a frame. If no filter has been defined, the
91 * socket will receive @b all CAN frames on the specified interface(s). @n
92 * @n
93 * Binding to special interface index @c 0 will make the socket receive
94 * CAN frames from all CAN interfaces. @n
95 * @n
96 * Binding to an interface index is also relevant for the @ref Send functions
97 * because they will transmit a message over the interface the socket is
98 * bound to when no socket address is given to them. @n
99 * @n
100 * @n
101 * Environments: non-RT (RT optional)@n
102 * @n
103 * Specific return values:
104 * - -EFAULT (It was not possible to access user space memory area at the
105 * specified address.)
106 * - -ENOMEM (Not enough memory to fulfill the operation)
107 * - -EINVAL (Invalid address family, or invalid length of address structure)
108 * - -ENODEV (Invalid CAN interface index)
109 * - -ENOSPC (No enough space for filter list)
110 * - -EBADF (Socket is about to be closed)
111 * - -EAGAIN (Too many receivers. Old binding (if any) is still active.
112 * Close some sockets and try again.)
114 * @n
115 * @n
116 * @anchor Sockopts
117 * <b>Setsockopt, Getsockopt</b>@n
118 * These functions allow to set and get various socket options. Currently,
119 * only CAN raw sockets are supported. @n
120 * @n
121 * Supported Levels and Options:
122 * @n
123 * - Level @b SOL_CAN_RAW : CAN RAW protocol (see @ref CAN_RAW)
124 * - Option @ref CAN_RAW_FILTER : CAN filter list
125 * - Option @ref CAN_RAW_ERR_FILTER : CAN error mask
126 * - Option @ref CAN_RAW_LOOPBACK : CAN TX loopback to local sockets
129 * @n
130 * Environments: non-RT (RT optional)@n
131 * Specific return values: see links to options above. @n
132 * @n
133 * @n
134 * @anchor Recv
135 * <b>Recv, Recvfrom, Recvmsg</b> @n
136 * These functions receive CAN messages from a socket. Only one
137 * message per call can be received, so only one buffer with the correct length
138 * must be passed. For @c SOCK_RAW, this is the size of struct can_frame. @n
139 * @n
140 * Unlike a call to one of the @ref Send functions, a Recv function will not
141 * return with an error if an interface is down (due to bus-off or setting
142 * of stop mode) or in sleep mode. Moreover, in such a case there may still
143 * be some CAN messages in the socket buffer which could be read out
144 * successfully. @n
145 * @n
146 * It is possible to receive a high precision timestamp with every CAN
147 * message. The condition is a former instruction to the socket via
148 * @ref RTCAN_RTIOC_TAKE_TIMESTAMP. The timestamp will be copied to the
149 * @c msg_control buffer of <TT>struct user_msghdr</TT> if it points to a valid
150 * memory location with size of @ref nanosecs_abs_t. If this
151 * is a NULL pointer the timestamp will be discarded silently. @n
152 * @n
153 * @b Note: A @c msg_controllen of @c 0 upon completion of the function call
154 * indicates that no timestamp is available for that message.
155 * @n
156 * @n
157 * Supported Flags [in]:
158 * - MSG_DONTWAIT (By setting this flag the operation will only succeed if
159 * it would not block, i.e. if there is a message in the
160 * socket buffer. This flag takes precedence over a timeout
161 * specified by @ref RTCAN_RTIOC_RCV_TIMEOUT.)
162 * - MSG_PEEK (Receive a message but leave it in the socket buffer. The
163 * next receive operation will get that message again.)
165 * @n
166 * Supported Flags [out]: none @n
167 * @n
168 * Environments: RT (non-RT optional)@n
169 * @n
170 * Specific return values:
171 * - Non-negative value (Indicating the successful reception of a CAN message.
172 * For @c SOCK_RAW, this is the size of struct can_frame regardless of
173 * the actual size of the payload.)
174 * - -EFAULT (It was not possible to access user space memory area at one
175 * of the specified addresses.)
176 * - -EINVAL (Unsupported flag detected, or invalid length of socket address
177 * buffer, or invalid length of message control buffer)
178 * - -EMSGSIZE (Zero or more than one iovec buffer passed, or buffer too
179 * small)
180 * - -EAGAIN (No data available in non-blocking mode)
181 * - -EBADF (Socket was closed.)
182 * - -EINTR (Operation was interrupted explicitly or by signal.)
183 * - -ETIMEDOUT (Timeout)
185 * @n
186 * @n
187 * @anchor Send
188 * <b>Send, Sendto, Sendmsg</b> @n
189 * These functions send out CAN messages. Only one message per call can
190 * be transmitted, so only one buffer with the correct length must be passed.
191 * For @c SOCK_RAW, this is the size of struct can_frame. @n
192 * @n
193 * The following only applies to @c SOCK_RAW: If a socket address of
194 * struct sockaddr_can is given, only @c can_ifindex is used. It is also
195 * possible to omit the socket address. Then the interface the socket is
196 * bound to will be used for sending messages. @n
197 * @n
198 * If an interface goes down (due to bus-off or setting of stop mode) all
199 * senders that were blocked on this interface will be woken up. @n
200 * @n
201 * @n
202 * Supported Flags:
203 * - MSG_DONTWAIT (By setting this flag the transmit operation will only
204 * succeed if it would not block. This flag takes precedence
205 * over a timeout specified by @ref RTCAN_RTIOC_SND_TIMEOUT.)
207 * Environments: RT (non-RT optional)@n
208 * @n
209 * Specific return values:
210 * - Non-negative value equal to given buffer size (Indicating the
211 * successful completion of the function call. See also note.)
212 * - -EOPNOTSUPP (MSG_OOB flag is not supported.)
213 * - -EINVAL (Unsupported flag detected @e or: Invalid length of socket
214 * address @e or: Invalid address family @e or: Data length code
215 * of CAN frame not between 0 and 15 @e or: CAN standard frame has
216 * got an ID not between 0 and 2031)
217 * - -EMSGSIZE (Zero or more than one buffer passed or invalid size of buffer)
218 * - -EFAULT (It was not possible to access user space memory area at one
219 * of the specified addresses.)
220 * - -ENXIO (Invalid CAN interface index - @c 0 is not allowed here - or
221 * socket not bound or rather bound to all interfaces.)
222 * - -ENETDOWN (Controller is bus-off or in stopped state.)
223 * - -ECOMM (Controller is sleeping)
224 * - -EAGAIN (Cannot transmit without blocking but a non-blocking
225 * call was requested.)
226 * - -EINTR (Operation was interrupted explicitly or by signal)
227 * - -EBADF (Socket was closed.)
228 * - -ETIMEDOUT (Timeout)
230 * @b Note: A successful completion of the function call does not implicate a
231 * successful transmission of the message.
233 * @{
236 #ifndef _RTCAN_H
237 #define _RTCAN_H
239 #ifdef __KERNEL__
241 #include <linux/net.h>
242 #include <linux/socket.h>
243 #include <linux/if.h>
245 #else /* !__KERNEL__ */
247 #include <net/if.h>
249 #endif /* !__KERNEL__ */
251 #include <rtdm/rtdm.h>
253 #define RTCAN_PROFILE_VER 2
255 #ifndef AF_CAN
257 /** CAN address family */
258 #define AF_CAN 29
260 /** CAN protocol family */
261 #define PF_CAN AF_CAN
263 #endif
265 /** CAN socket levels
267 * Used for @ref Sockopts for the particular protocols.
269 #define SOL_CAN_RAW 103
271 /** Type of CAN id (see @ref CAN_xxx_MASK and @ref CAN_xxx_FLAG) */
272 typedef uint32_t can_id_t;
273 typedef uint32_t canid_t;
275 /** Type of CAN error mask */
276 typedef can_id_t can_err_mask_t;
279 * @anchor CAN_xxx_MASK @name CAN ID masks
280 * Bit masks for masking CAN IDs
281 * @{ */
283 /** Bit mask for extended CAN IDs */
284 #define CAN_EFF_MASK 0x1FFFFFFF
286 /** Bit mask for standard CAN IDs */
287 #define CAN_SFF_MASK 0x000007FF
289 /** @} */
292 * @anchor CAN_xxx_FLAG @name CAN ID flags
293 * Flags within a CAN ID indicating special CAN frame attributes
294 * @{ */
295 /** Extended frame */
296 #define CAN_EFF_FLAG 0x80000000
297 /** Remote transmission frame */
298 #define CAN_RTR_FLAG 0x40000000
299 /** Error frame (see @ref Errors), not valid in struct can_filter */
300 #define CAN_ERR_FLAG 0x20000000
301 /** Invert CAN filter definition, only valid in struct can_filter */
302 #define CAN_INV_FILTER CAN_ERR_FLAG
304 /** @} */
307 * @anchor CAN_PROTO @name Particular CAN protocols
308 * Possible protocols for the PF_CAN protocol family
310 * Currently only the RAW protocol is supported.
311 * @{ */
312 /** Raw protocol of @c PF_CAN, applicable to socket type @c SOCK_RAW */
313 #define CAN_RAW 1
314 /** @} */
316 #define CAN_BAUDRATE_UNKNOWN ((uint32_t)-1)
317 #define CAN_BAUDRATE_UNCONFIGURED 0
320 * Baudrate definition in bits per second
322 typedef uint32_t can_baudrate_t;
325 * Supported CAN bit-time types
327 enum CAN_BITTIME_TYPE {
328 /** Standard bit-time definition according to Bosch */
329 CAN_BITTIME_STD,
330 /** Hardware-specific BTR bit-time definition */
331 CAN_BITTIME_BTR
335 * See @ref CAN_BITTIME_TYPE
337 typedef enum CAN_BITTIME_TYPE can_bittime_type_t;
340 * Standard bit-time parameters according to Bosch
342 struct can_bittime_std {
343 uint32_t brp; /**< Baud rate prescaler */
344 uint8_t prop_seg; /**< from 1 to 8 */
345 uint8_t phase_seg1; /**< from 1 to 8 */
346 uint8_t phase_seg2; /**< from 1 to 8 */
347 uint8_t sjw:7; /**< from 1 to 4 */
348 uint8_t sam:1; /**< 1 - enable triple sampling */
352 * Hardware-specific BTR bit-times
354 struct can_bittime_btr {
356 uint8_t btr0; /**< Bus timing register 0 */
357 uint8_t btr1; /**< Bus timing register 1 */
361 * Custom CAN bit-time definition
363 struct can_bittime {
364 /** Type of bit-time definition */
365 can_bittime_type_t type;
367 union {
368 /** Standard bit-time */
369 struct can_bittime_std std;
370 /** Hardware-spcific BTR bit-time */
371 struct can_bittime_btr btr;
376 * @anchor CAN_MODE @name CAN operation modes
377 * Modes into which CAN controllers can be set
378 * @{ */
379 enum CAN_MODE {
380 /*! Set controller in Stop mode (no reception / transmission possible) */
381 CAN_MODE_STOP = 0,
383 /*! Set controller into normal operation. @n
384 * Coming from stopped mode or bus off, the controller begins with no
385 * errors in @ref CAN_STATE_ACTIVE. */
386 CAN_MODE_START,
388 /*! Set controller into Sleep mode. @n
389 * This is only possible if the controller is not stopped or bus-off. @n
390 * Notice that sleep mode will only be entered when there is no bus
391 * activity. If the controller detects bus activity while "sleeping"
392 * it will go into operating mode again. @n
393 * To actively leave sleep mode again trigger @c CAN_MODE_START. */
394 CAN_MODE_SLEEP
396 /** @} */
398 /** See @ref CAN_MODE */
399 typedef enum CAN_MODE can_mode_t;
402 * @anchor CAN_CTRLMODE @name CAN controller modes
403 * Special CAN controllers modes, which can be or'ed together.
405 * @note These modes are hardware-dependent. Please consult the hardware
406 * manual of the CAN controller for more detailed information.
408 * @{ */
410 /*! Listen-Only mode
412 * In this mode the CAN controller would give no acknowledge to the CAN-bus,
413 * even if a message is received successfully and messages would not be
414 * transmitted. This mode might be useful for bus-monitoring, hot-plugging
415 * or throughput analysis. */
416 #define CAN_CTRLMODE_LISTENONLY 0x1
418 /*! Loopback mode
420 * In this mode the CAN controller does an internal loop-back, a message is
421 * transmitted and simultaneously received. That mode can be used for self
422 * test operation. */
423 #define CAN_CTRLMODE_LOOPBACK 0x2
425 /*! Triple sampling mode
427 * In this mode the CAN controller uses Triple sampling. */
428 #define CAN_CTRLMODE_3_SAMPLES 0x4
430 /** @} */
432 /** See @ref CAN_CTRLMODE */
433 typedef int can_ctrlmode_t;
436 * @anchor CAN_STATE @name CAN controller states
437 * States a CAN controller can be in.
438 * @{ */
439 enum CAN_STATE {
440 /** CAN controller is error active */
441 CAN_STATE_ERROR_ACTIVE = 0,
442 CAN_STATE_ACTIVE = 0,
444 /** CAN controller is error active, warning level is reached */
445 CAN_STATE_ERROR_WARNING = 1,
446 CAN_STATE_BUS_WARNING = 1,
448 /** CAN controller is error passive */
449 CAN_STATE_ERROR_PASSIVE = 2,
450 CAN_STATE_BUS_PASSIVE = 2,
452 /** CAN controller went into Bus Off */
453 CAN_STATE_BUS_OFF,
455 /** CAN controller is scanning to get the baudrate */
456 CAN_STATE_SCANNING_BAUDRATE,
458 /** CAN controller is in stopped mode */
459 CAN_STATE_STOPPED,
461 /** CAN controller is in Sleep mode */
462 CAN_STATE_SLEEPING,
464 /** @} */
466 /** See @ref CAN_STATE */
467 typedef enum CAN_STATE can_state_t;
469 #define CAN_STATE_OPERATING(state) ((state) < CAN_STATE_BUS_OFF)
472 * Filter for reception of CAN messages.
474 * This filter works as follows:
475 * A received CAN ID is AND'ed bitwise with @c can_mask and then compared to
476 * @c can_id. This also includes the @ref CAN_EFF_FLAG and @ref CAN_RTR_FLAG
477 * of @ref CAN_xxx_FLAG. If this comparison is true, the message will be
478 * received by the socket. The logic can be inverted with the @c can_id flag
479 * @ref CAN_INV_FILTER :
481 * @code
482 * if (can_id & CAN_INV_FILTER) {
483 * if ((received_can_id & can_mask) != (can_id & ~CAN_INV_FILTER))
484 * accept-message;
485 * } else {
486 * if ((received_can_id & can_mask) == can_id)
487 * accept-message;
489 * @endcode
491 * Multiple filters can be arranged in a filter list and set with
492 * @ref Sockopts. If one of these filters matches a CAN ID upon reception
493 * of a CAN frame, this frame is accepted.
496 typedef struct can_filter {
497 /** CAN ID which must match with incoming IDs after passing the mask.
498 * The filter logic can be inverted with the flag @ref CAN_INV_FILTER. */
499 uint32_t can_id;
501 /** Mask which is applied to incoming IDs. See @ref CAN_xxx_MASK
502 * "CAN ID masks" if exactly one CAN ID should come through. */
503 uint32_t can_mask;
504 } can_filter_t;
507 * Socket address structure for the CAN address family
509 struct sockaddr_can {
510 /** CAN address family, must be @c AF_CAN */
511 sa_family_t can_family;
513 /** Interface index of CAN controller. See @ref SIOCGIFINDEX. */
514 int can_ifindex;
518 * Raw CAN frame
520 * Central structure for receiving and sending CAN frames.
522 typedef struct can_frame {
523 /** CAN ID of the frame
525 * See @ref CAN_xxx_FLAG "CAN ID flags" for special bits.
527 can_id_t can_id;
529 /** Size of the payload in bytes */
530 uint8_t can_dlc;
532 /** Payload data bytes */
533 uint8_t data[8] __attribute__ ((aligned(8)));
534 } can_frame_t;
537 * @anchor RTCAN_TIMESTAMPS @name Timestamp switches
538 * Arguments to pass to @ref RTCAN_RTIOC_TAKE_TIMESTAMP
539 * @{ */
540 #define RTCAN_TAKE_NO_TIMESTAMPS 0 /**< Switch off taking timestamps */
541 #define RTCAN_TAKE_TIMESTAMPS 1 /**< Do take timestamps */
542 /** @} */
544 #define RTIOC_TYPE_CAN RTDM_CLASS_CAN
547 * @anchor Rawsockopts @name RAW socket options
548 * Setting and getting CAN RAW socket options.
549 * @{ */
552 * CAN filter definition
554 * A CAN raw filter list with elements of struct can_filter can be installed
555 * with @c setsockopt. This list is used upon reception of CAN frames to
556 * decide whether the bound socket will receive a frame. An empty filter list
557 * can also be defined using optlen = 0, which is recommanded for write-only
558 * sockets.
559 * @n
560 * If the socket was already bound with @ref Bind, the old filter list
561 * gets replaced with the new one. Be aware that already received, but
562 * not read out CAN frames may stay in the socket buffer.
563 * @n
564 * @n
565 * @param [in] level @b SOL_CAN_RAW
567 * @param [in] optname @b CAN_RAW_FILTER
569 * @param [in] optval Pointer to array of struct can_filter.
571 * @param [in] optlen Size of filter list: count * sizeof( struct can_filter).
572 * @n
573 * Environments: non-RT (RT optional)@n
574 * @n
575 * Specific return values:
576 * - -EFAULT (It was not possible to access user space memory area at the
577 * specified address.)
578 * - -ENOMEM (Not enough memory to fulfill the operation)
579 * - -EINVAL (Invalid length "optlen")
580 * - -ENOSPC (No space to store filter list, check RT-Socket-CAN kernel
581 * parameters)
584 #define CAN_RAW_FILTER 0x1
587 * CAN error mask
589 * A CAN error mask (see @ref Errors) can be set with @c setsockopt. This
590 * mask is then used to decide if error frames are delivered to this socket
591 * in case of error condidtions. The error frames are marked with the
592 * @ref CAN_ERR_FLAG of @ref CAN_xxx_FLAG and must be handled by the
593 * application properly. A detailed description of the errors can be
594 * found in the @c can_id and the @c data fields of struct can_frame
595 * (see @ref Errors for futher details).
597 * @n
598 * @param [in] level @b SOL_CAN_RAW
600 * @param [in] optname @b CAN_RAW_ERR_FILTER
602 * @param [in] optval Pointer to error mask of type can_err_mask_t.
604 * @param [in] optlen Size of error mask: sizeof(can_err_mask_t).
606 * Environments: non-RT (RT optional)@n
607 * @n
608 * Specific return values:
609 * - -EFAULT (It was not possible to access user space memory area at the
610 * specified address.)
611 * - -EINVAL (Invalid length "optlen")
614 #define CAN_RAW_ERR_FILTER 0x2
617 * CAN TX loopback
619 * The TX loopback to other local sockets can be selected with this
620 * @c setsockopt.
622 * @note The TX loopback feature must be enabled in the kernel and then
623 * the loopback to other local TX sockets is enabled by default.
625 * @n
626 * @param [in] level @b SOL_CAN_RAW
628 * @param [in] optname @b CAN_RAW_LOOPBACK
630 * @param [in] optval Pointer to integer value.
632 * @param [in] optlen Size of int: sizeof(int).
634 * Environments: non-RT (RT optional)@n
635 * @n
636 * Specific return values:
637 * - -EFAULT (It was not possible to access user space memory area at the
638 * specified address.)
639 * - -EINVAL (Invalid length "optlen")
640 * - -EOPNOTSUPP (not supported, check RT-Socket-CAN kernel parameters).
642 #define CAN_RAW_LOOPBACK 0x3
645 * CAN receive own messages
647 * Not supported by RT-Socket-CAN, but defined for compatibility with
648 * Socket-CAN.
650 #define CAN_RAW_RECV_OWN_MSGS 0x4
652 /** @} */
655 * @anchor CANIOCTLs @name IOCTLs
656 * CAN device IOCTLs
657 * @{ */
660 * Get CAN interface index by name
662 * @param [in,out] arg Pointer to interface request structure buffer
663 * (<TT>struct ifreq</TT> from linux/if.h). If
664 * <TT>ifr_name</TT> holds a valid CAN interface name
665 * <TT>ifr_ifindex</TT> will be filled with the
666 * corresponding interface index.
668 * @return 0 on success, otherwise:
669 * - -EFAULT: It was not possible to access user space memory area at the
670 * specified address.
671 * - -ENODEV: No device with specified name exists.
673 * Environments:
675 * This service can be called from:
677 * - Kernel module initialization/cleanup code
678 * - Kernel-based task
679 * - User-space task (RT, non-RT)
681 * Rescheduling: never.
684 #ifdef DOXYGEN_CPP /* For Doxygen only, already defined by kernel headers */
685 #define SIOCGIFINDEX defined_by_kernel_header_file
686 #endif
689 * Set baud rate
691 * The baudrate must be specified in bits per second. The driver will
692 * try to calculate resonable CAN bit-timing parameters. You can use
693 * @ref SIOCSCANCUSTOMBITTIME to set custom bit-timing.
695 * @param [in] arg Pointer to interface request structure buffer
696 * (<TT>struct ifreq</TT> from linux/if.h).
697 * <TT>ifr_name</TT> must hold a valid CAN interface name,
698 * <TT>ifr_ifru</TT> must be filled with an instance of
699 * @ref can_baudrate_t.
701 * @return 0 on success, otherwise:
702 * - -EFAULT: It was not possible to access user space memory area at the
703 * specified address.
704 * - -ENODEV: No device with specified name exists.
705 * - -EINVAL: No valid baud rate, see @ref can_baudrate_t.
706 * - -EDOM : Baud rate not possible.
707 * - -EAGAIN: Request could not be successully fulfilled. Try again.
709 * Environments:
711 * This service can be called from:
713 * - Kernel module initialization/cleanup code
714 * - Kernel-based task
715 * - User-space task (RT, non-RT)
717 * @note Setting the baud rate is a configuration task. It should
718 * be done deliberately or otherwise CAN messages will likely be lost.
720 * Rescheduling: possible.
722 #define SIOCSCANBAUDRATE _IOW(RTIOC_TYPE_CAN, 0x01, struct ifreq)
725 * Get baud rate
727 * @param [in,out] arg Pointer to interface request structure buffer
728 * (<TT>struct ifreq</TT> from linux/if.h).
729 * <TT>ifr_name</TT> must hold a valid CAN interface name,
730 * <TT>ifr_ifru</TT> will be filled with an instance of
731 * @ref can_baudrate_t.
733 * @return 0 on success, otherwise:
734 * - -EFAULT: It was not possible to access user space memory area at the
735 * specified address.
736 * - -ENODEV: No device with specified name exists.
737 * - -EINVAL: No baud rate was set yet.
739 * Environments:
741 * This service can be called from:
743 * - Kernel module initialization/cleanup code
744 * - Kernel-based task
745 * - User-space task (RT, non-RT)
747 * Rescheduling: never.
749 #define SIOCGCANBAUDRATE _IOWR(RTIOC_TYPE_CAN, 0x02, struct ifreq)
752 * Set custom bit time parameter
754 * Custem-bit time could be defined in various formats (see
755 * struct can_bittime).
757 * @param [in] arg Pointer to interface request structure buffer
758 * (<TT>struct ifreq</TT> from linux/if.h).
759 * <TT>ifr_name</TT> must hold a valid CAN interface name,
760 * <TT>ifr_ifru</TT> must be filled with an instance of
761 * struct can_bittime.
763 * @return 0 on success, otherwise:
764 * - -EFAULT: It was not possible to access user space memory area at the
765 * specified address.
766 * - -ENODEV: No device with specified name exists.
767 * - -EINVAL: No valid baud rate, see @ref can_baudrate_t.
768 * - -EAGAIN: Request could not be successully fulfilled. Try again.
770 * Environments:
772 * This service can be called from:
774 * - Kernel module initialization/cleanup code
775 * - Kernel-based task
776 * - User-space task (RT, non-RT)
778 * @note Setting the bit-time is a configuration task. It should
779 * be done deliberately or otherwise CAN messages will likely be lost.
781 * Rescheduling: possible.
783 #define SIOCSCANCUSTOMBITTIME _IOW(RTIOC_TYPE_CAN, 0x03, struct ifreq)
786 * Get custum bit-time parameters
788 * @param [in,out] arg Pointer to interface request structure buffer
789 * (<TT>struct ifreq</TT> from linux/if.h).
790 * <TT>ifr_name</TT> must hold a valid CAN interface name,
791 * <TT>ifr_ifru</TT> will be filled with an instance of
792 * struct can_bittime.
794 * @return 0 on success, otherwise:
795 * - -EFAULT: It was not possible to access user space memory area at the
796 * specified address.
797 * - -ENODEV: No device with specified name exists.
798 * - -EINVAL: No baud rate was set yet.
800 * Environments:
802 * This service can be called from:
804 * - Kernel module initialization/cleanup code
805 * - Kernel-based task
806 * - User-space task (RT, non-RT)
808 * Rescheduling: never.
810 #define SIOCGCANCUSTOMBITTIME _IOWR(RTIOC_TYPE_CAN, 0x04, struct ifreq)
813 * Set operation mode of CAN controller
815 * See @ref CAN_MODE "CAN controller modes" for available modes.
817 * @param [in] arg Pointer to interface request structure buffer
818 * (<TT>struct ifreq</TT> from linux/if.h).
819 * <TT>ifr_name</TT> must hold a valid CAN interface name,
820 * <TT>ifr_ifru</TT> must be filled with an instance of
821 * @ref can_mode_t.
823 * @return 0 on success, otherwise:
824 * - -EFAULT: It was not possible to access user space memory area at the
825 * specified address.
826 * - -ENODEV: No device with specified name exists.
827 * - -EAGAIN: (@ref CAN_MODE_START, @ref CAN_MODE_STOP) Could not successfully
828 * set mode, hardware is busy. Try again.
829 * - -EINVAL: (@ref CAN_MODE_START) Cannot start controller,
830 * set baud rate first.
831 * - -ENETDOWN: (@ref CAN_MODE_SLEEP) Cannot go into sleep mode because
832 controller is stopped or bus off.
833 * - -EOPNOTSUPP: unknown mode
835 * Environments:
837 * This service can be called from:
839 * - Kernel module initialization/cleanup code
840 * - Kernel-based task
841 * - User-space task (RT, non-RT)
843 * @note Setting a CAN controller into normal operation after a bus-off can
844 * take some time (128 occurrences of 11 consecutive recessive bits).
845 * In such a case, although this IOCTL will return immediately with success
846 * and @ref SIOCGCANSTATE will report @ref CAN_STATE_ACTIVE,
847 * bus-off recovery may still be in progress. @n
848 * If a controller is bus-off, setting it into stop mode will return no error
849 * but the controller remains bus-off.
851 * Rescheduling: possible.
853 #define SIOCSCANMODE _IOW(RTIOC_TYPE_CAN, 0x05, struct ifreq)
856 * Get current state of CAN controller
858 * States are divided into main states and additional error indicators. A CAN
859 * controller is always in exactly one main state. CAN bus errors are
860 * registered by the CAN hardware and collected by the driver. There is one
861 * error indicator (bit) per error type. If this IOCTL is triggered the error
862 * types which occured since the last call of this IOCTL are reported and
863 * thereafter the error indicators are cleared. See also
864 * @ref CAN_STATE "CAN controller states".
866 * @param [in,out] arg Pointer to interface request structure buffer
867 * (<TT>struct ifreq</TT> from linux/if.h).
868 * <TT>ifr_name</TT> must hold a valid CAN interface name,
869 * <TT>ifr_ifru</TT> will be filled with an instance of
870 * @ref can_mode_t.
872 * @return 0 on success, otherwise:
873 * - -EFAULT: It was not possible to access user space memory area at the
874 * specified address.
875 * - -ENODEV: No device with specified name exists.
877 * Environments:
879 * This service can be called from:
881 * - Kernel module initialization/cleanup code
882 * - Kernel-based task
883 * - User-space task (RT, non-RT)
885 * Rescheduling: possible.
887 #define SIOCGCANSTATE _IOWR(RTIOC_TYPE_CAN, 0x06, struct ifreq)
890 * Set special controller modes
892 * Various special controller modes could be or'ed together (see
893 * @ref CAN_CTRLMODE for further information).
895 * @param [in] arg Pointer to interface request structure buffer
896 * (<TT>struct ifreq</TT> from linux/if.h).
897 * <TT>ifr_name</TT> must hold a valid CAN interface name,
898 * <TT>ifr_ifru</TT> must be filled with an instance of
899 * @ref can_ctrlmode_t.
901 * @return 0 on success, otherwise:
902 * - -EFAULT: It was not possible to access user space memory area at the
903 * specified address.
904 * - -ENODEV: No device with specified name exists.
905 * - -EINVAL: No valid baud rate, see @ref can_baudrate_t.
906 * - -EAGAIN: Request could not be successully fulfilled. Try again.
908 * Environments:
910 * This service can be called from:
912 * - Kernel module initialization/cleanup code
913 * - Kernel-based task
914 * - User-space task (RT, non-RT)
916 * @note Setting special controller modes is a configuration task. It should
917 * be done deliberately or otherwise CAN messages will likely be lost.
919 * Rescheduling: possible.
921 #define SIOCSCANCTRLMODE _IOW(RTIOC_TYPE_CAN, 0x07, struct ifreq)
924 * Get special controller modes
927 * @param [in] arg Pointer to interface request structure buffer
928 * (<TT>struct ifreq</TT> from linux/if.h).
929 * <TT>ifr_name</TT> must hold a valid CAN interface name,
930 * <TT>ifr_ifru</TT> must be filled with an instance of
931 * @ref can_ctrlmode_t.
933 * @return 0 on success, otherwise:
934 * - -EFAULT: It was not possible to access user space memory area at the
935 * specified address.
936 * - -ENODEV: No device with specified name exists.
937 * - -EINVAL: No baud rate was set yet.
939 * Environments:
941 * This service can be called from:
943 * - Kernel module initialization/cleanup code
944 * - Kernel-based task
945 * - User-space task (RT, non-RT)
947 * Rescheduling: possible.
949 #define SIOCGCANCTRLMODE _IOWR(RTIOC_TYPE_CAN, 0x08, struct ifreq)
952 * Enable or disable storing a high precision timestamp upon reception of
953 * a CAN frame.
955 * A newly created socket takes no timestamps by default.
957 * @param [in] arg int variable, see @ref RTCAN_TIMESTAMPS "Timestamp switches"
959 * @return 0 on success.
961 * Environments:
963 * This service can be called from:
965 * - Kernel module initialization/cleanup code
966 * - Kernel-based task
967 * - User-space task (RT, non-RT)
969 * @note Activating taking timestamps only has an effect on newly received
970 * CAN messages from the bus. Frames that already are in the socket buffer do
971 * not have timestamps if it was deactivated before. See @ref Recv "Receive"
972 * for more details.
974 * Rescheduling: never.
976 #define RTCAN_RTIOC_TAKE_TIMESTAMP _IOW(RTIOC_TYPE_CAN, 0x09, int)
979 * Specify a reception timeout for a socket
981 * Defines a timeout for all receive operations via a
982 * socket which will take effect when one of the @ref Recv "receive functions"
983 * is called without the @c MSG_DONTWAIT flag set.
985 * The default value for a newly created socket is an infinite timeout.
987 * @note The setting of the timeout value is not done atomically to avoid
988 * locks. Please set the value before receiving messages from the socket.
990 * @param [in] arg Pointer to @ref nanosecs_rel_t variable. The value is
991 * interpreted as relative timeout in nanoseconds in case
992 * of a positive value.
993 * See @ref RTDM_TIMEOUT_xxx "Timeouts" for special timeouts.
995 * @return 0 on success, otherwise:
996 * - -EFAULT: It was not possible to access user space memory area at the
997 * specified address.
999 * Environments:
1001 * This service can be called from:
1003 * - Kernel module initialization/cleanup code
1004 * - Kernel-based task
1005 * - User-space task (RT, non-RT)
1007 * Rescheduling: never.
1009 #define RTCAN_RTIOC_RCV_TIMEOUT _IOW(RTIOC_TYPE_CAN, 0x0A, nanosecs_rel_t)
1012 * Specify a transmission timeout for a socket
1014 * Defines a timeout for all send operations via a
1015 * socket which will take effect when one of the @ref Send "send functions"
1016 * is called without the @c MSG_DONTWAIT flag set.
1018 * The default value for a newly created socket is an infinite timeout.
1020 * @note The setting of the timeout value is not done atomically to avoid
1021 * locks. Please set the value before sending messages to the socket.
1023 * @param [in] arg Pointer to @ref nanosecs_rel_t variable. The value is
1024 * interpreted as relative timeout in nanoseconds in case
1025 * of a positive value.
1026 * See @ref RTDM_TIMEOUT_xxx "Timeouts" for special timeouts.
1028 * @return 0 on success, otherwise:
1029 * - -EFAULT: It was not possible to access user space memory area at the
1030 * specified address.
1032 * Environments:
1034 * This service can be called from:
1036 * - Kernel module initialization/cleanup code
1037 * - Kernel-based task
1038 * - User-space task (RT, non-RT)
1040 * Rescheduling: never.
1042 #define RTCAN_RTIOC_SND_TIMEOUT _IOW(RTIOC_TYPE_CAN, 0x0B, nanosecs_rel_t)
1043 /** @} */
1045 #define CAN_ERR_DLC 8 /* dlc for error frames */
1048 * @anchor Errors @name Error mask
1049 * Error class (mask) in @c can_id field of struct can_frame to
1050 * be used with @ref CAN_RAW_ERR_FILTER.
1052 * @b Note: Error reporting is hardware dependent and most CAN controllers
1053 * report less detailed error conditions than the SJA1000.
1055 * @b Note: In case of a bus-off error condition (@ref CAN_ERR_BUSOFF), the
1056 * CAN controller is @b not restarted automatically. It is the application's
1057 * responsibility to react appropriately, e.g. calling @ref CAN_MODE_START.
1059 * @b Note: Bus error interrupts (@ref CAN_ERR_BUSERROR) are enabled when an
1060 * application is calling a @ref Recv function on a socket listening
1061 * on bus errors (using @ref CAN_RAW_ERR_FILTER). After one bus error has
1062 * occured, the interrupt will be disabled to allow the application time for
1063 * error processing and to efficiently avoid bus error interrupt flooding.
1064 * @{ */
1066 /** TX timeout (netdevice driver) */
1067 #define CAN_ERR_TX_TIMEOUT 0x00000001U
1069 /** Lost arbitration (see @ref Error0 "data[0]") */
1070 #define CAN_ERR_LOSTARB 0x00000002U
1072 /** Controller problems (see @ref Error1 "data[1]") */
1073 #define CAN_ERR_CRTL 0x00000004U
1075 /** Protocol violations (see @ref Error2 "data[2]",
1076 @ref Error3 "data[3]") */
1077 #define CAN_ERR_PROT 0x00000008U
1079 /** Transceiver status (see @ref Error4 "data[4]") */
1080 #define CAN_ERR_TRX 0x00000010U
1082 /** Received no ACK on transmission */
1083 #define CAN_ERR_ACK 0x00000020U
1085 /** Bus off */
1086 #define CAN_ERR_BUSOFF 0x00000040U
1088 /** Bus error (may flood!) */
1089 #define CAN_ERR_BUSERROR 0x00000080U
1091 /** Controller restarted */
1092 #define CAN_ERR_RESTARTED 0x00000100U
1094 /** Omit EFF, RTR, ERR flags */
1095 #define CAN_ERR_MASK 0x1FFFFFFFU
1097 /** @} */
1100 * @anchor Error0 @name Arbitration lost error
1101 * Error in the data[0] field of struct can_frame.
1102 * @{ */
1103 /* arbitration lost in bit ... / data[0] */
1104 #define CAN_ERR_LOSTARB_UNSPEC 0x00 /**< unspecified */
1105 /**< else bit number in bitstream */
1106 /** @} */
1109 * @anchor Error1 @name Controller problems
1110 * Error in the data[1] field of struct can_frame.
1111 * @{ */
1112 /* error status of CAN-controller / data[1] */
1113 #define CAN_ERR_CRTL_UNSPEC 0x00 /**< unspecified */
1114 #define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /**< RX buffer overflow */
1115 #define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /**< TX buffer overflow */
1116 #define CAN_ERR_CRTL_RX_WARNING 0x04 /**< reached warning level for RX errors */
1117 #define CAN_ERR_CRTL_TX_WARNING 0x08 /**< reached warning level for TX errors */
1118 #define CAN_ERR_CRTL_RX_PASSIVE 0x10 /**< reached passive level for RX errors */
1119 #define CAN_ERR_CRTL_TX_PASSIVE 0x20 /**< reached passive level for TX errors */
1120 /** @} */
1123 * @anchor Error2 @name Protocol error type
1124 * Error in the data[2] field of struct can_frame.
1125 * @{ */
1126 /* error in CAN protocol (type) / data[2] */
1127 #define CAN_ERR_PROT_UNSPEC 0x00 /**< unspecified */
1128 #define CAN_ERR_PROT_BIT 0x01 /**< single bit error */
1129 #define CAN_ERR_PROT_FORM 0x02 /**< frame format error */
1130 #define CAN_ERR_PROT_STUFF 0x04 /**< bit stuffing error */
1131 #define CAN_ERR_PROT_BIT0 0x08 /**< unable to send dominant bit */
1132 #define CAN_ERR_PROT_BIT1 0x10 /**< unable to send recessive bit */
1133 #define CAN_ERR_PROT_OVERLOAD 0x20 /**< bus overload */
1134 #define CAN_ERR_PROT_ACTIVE 0x40 /**< active error announcement */
1135 #define CAN_ERR_PROT_TX 0x80 /**< error occured on transmission */
1136 /** @} */
1139 * @anchor Error3 @name Protocol error location
1140 * Error in the data[3] field of struct can_frame.
1141 * @{ */
1142 /* error in CAN protocol (location) / data[3] */
1143 #define CAN_ERR_PROT_LOC_UNSPEC 0x00 /**< unspecified */
1144 #define CAN_ERR_PROT_LOC_SOF 0x03 /**< start of frame */
1145 #define CAN_ERR_PROT_LOC_ID28_21 0x02 /**< ID bits 28 - 21 (SFF: 10 - 3) */
1146 #define CAN_ERR_PROT_LOC_ID20_18 0x06 /**< ID bits 20 - 18 (SFF: 2 - 0 )*/
1147 #define CAN_ERR_PROT_LOC_SRTR 0x04 /**< substitute RTR (SFF: RTR) */
1148 #define CAN_ERR_PROT_LOC_IDE 0x05 /**< identifier extension */
1149 #define CAN_ERR_PROT_LOC_ID17_13 0x07 /**< ID bits 17-13 */
1150 #define CAN_ERR_PROT_LOC_ID12_05 0x0F /**< ID bits 12-5 */
1151 #define CAN_ERR_PROT_LOC_ID04_00 0x0E /**< ID bits 4-0 */
1152 #define CAN_ERR_PROT_LOC_RTR 0x0C /**< RTR */
1153 #define CAN_ERR_PROT_LOC_RES1 0x0D /**< reserved bit 1 */
1154 #define CAN_ERR_PROT_LOC_RES0 0x09 /**< reserved bit 0 */
1155 #define CAN_ERR_PROT_LOC_DLC 0x0B /**< data length code */
1156 #define CAN_ERR_PROT_LOC_DATA 0x0A /**< data section */
1157 #define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /**< CRC sequence */
1158 #define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /**< CRC delimiter */
1159 #define CAN_ERR_PROT_LOC_ACK 0x19 /**< ACK slot */
1160 #define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /**< ACK delimiter */
1161 #define CAN_ERR_PROT_LOC_EOF 0x1A /**< end of frame */
1162 #define CAN_ERR_PROT_LOC_INTERM 0x12 /**< intermission */
1163 /** @} */
1166 * @anchor Error4 @name Protocol error location
1167 * Error in the data[4] field of struct can_frame.
1168 * @{ */
1169 /* error status of CAN-transceiver / data[4] */
1170 /* CANH CANL */
1171 #define CAN_ERR_TRX_UNSPEC 0x00 /**< 0000 0000 */
1172 #define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /**< 0000 0100 */
1173 #define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /**< 0000 0101 */
1174 #define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /**< 0000 0110 */
1175 #define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /**< 0000 0111 */
1176 #define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /**< 0100 0000 */
1177 #define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /**< 0101 0000 */
1178 #define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /**< 0110 0000 */
1179 #define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /**< 0111 0000 */
1180 #define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /**< 1000 0000 */
1181 /** @} */
1183 /* controller specific additional information / data[5..7] */
1185 /** @} */
1188 * @anchor CANutils @name CAN example and utility programs
1189 * @{ */
1190 /** @example rtcanconfig.c */
1191 /** @example rtcansend.c */
1192 /** @example rtcanrecv.c */
1193 /** @example rtcan_rtt.c */
1194 /** @} */
1196 /** @} */
1198 #endif /* _RTCAN_H */