can.txt 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. ============================================================================
  2. can.txt
  3. Readme file for the Controller Area Network Protocol Family (aka Socket CAN)
  4. This file contains
  5. 1 Overview / What is Socket CAN
  6. 2 Motivation / Why using the socket API
  7. 3 Socket CAN concept
  8. 3.1 receive lists
  9. 3.2 local loopback of sent frames
  10. 3.3 network security issues (capabilities)
  11. 3.4 network problem notifications
  12. 4 How to use Socket CAN
  13. 4.1 RAW protocol sockets with can_filters (SOCK_RAW)
  14. 4.1.1 RAW socket option CAN_RAW_FILTER
  15. 4.1.2 RAW socket option CAN_RAW_ERR_FILTER
  16. 4.1.3 RAW socket option CAN_RAW_LOOPBACK
  17. 4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS
  18. 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM)
  19. 4.3 connected transport protocols (SOCK_SEQPACKET)
  20. 4.4 unconnected transport protocols (SOCK_DGRAM)
  21. 5 Socket CAN core module
  22. 5.1 can.ko module params
  23. 5.2 procfs content
  24. 5.3 writing own CAN protocol modules
  25. 6 CAN network drivers
  26. 6.1 general settings
  27. 6.2 local loopback of sent frames
  28. 6.3 CAN controller hardware filters
  29. 6.4 The virtual CAN driver (vcan)
  30. 6.5 currently supported CAN hardware
  31. 6.6 todo
  32. 7 Credits
  33. ============================================================================
  34. 1. Overview / What is Socket CAN
  35. --------------------------------
  36. The socketcan package is an implementation of CAN protocols
  37. (Controller Area Network) for Linux. CAN is a networking technology
  38. which has widespread use in automation, embedded devices, and
  39. automotive fields. While there have been other CAN implementations
  40. for Linux based on character devices, Socket CAN uses the Berkeley
  41. socket API, the Linux network stack and implements the CAN device
  42. drivers as network interfaces. The CAN socket API has been designed
  43. as similar as possible to the TCP/IP protocols to allow programmers,
  44. familiar with network programming, to easily learn how to use CAN
  45. sockets.
  46. 2. Motivation / Why using the socket API
  47. ----------------------------------------
  48. There have been CAN implementations for Linux before Socket CAN so the
  49. question arises, why we have started another project. Most existing
  50. implementations come as a device driver for some CAN hardware, they
  51. are based on character devices and provide comparatively little
  52. functionality. Usually, there is only a hardware-specific device
  53. driver which provides a character device interface to send and
  54. receive raw CAN frames, directly to/from the controller hardware.
  55. Queueing of frames and higher-level transport protocols like ISO-TP
  56. have to be implemented in user space applications. Also, most
  57. character-device implementations support only one single process to
  58. open the device at a time, similar to a serial interface. Exchanging
  59. the CAN controller requires employment of another device driver and
  60. often the need for adaption of large parts of the application to the
  61. new driver's API.
  62. Socket CAN was designed to overcome all of these limitations. A new
  63. protocol family has been implemented which provides a socket interface
  64. to user space applications and which builds upon the Linux network
  65. layer, so to use all of the provided queueing functionality. A device
  66. driver for CAN controller hardware registers itself with the Linux
  67. network layer as a network device, so that CAN frames from the
  68. controller can be passed up to the network layer and on to the CAN
  69. protocol family module and also vice-versa. Also, the protocol family
  70. module provides an API for transport protocol modules to register, so
  71. that any number of transport protocols can be loaded or unloaded
  72. dynamically. In fact, the can core module alone does not provide any
  73. protocol and cannot be used without loading at least one additional
  74. protocol module. Multiple sockets can be opened at the same time,
  75. on different or the same protocol module and they can listen/send
  76. frames on different or the same CAN IDs. Several sockets listening on
  77. the same interface for frames with the same CAN ID are all passed the
  78. same received matching CAN frames. An application wishing to
  79. communicate using a specific transport protocol, e.g. ISO-TP, just
  80. selects that protocol when opening the socket, and then can read and
  81. write application data byte streams, without having to deal with
  82. CAN-IDs, frames, etc.
  83. Similar functionality visible from user-space could be provided by a
  84. character device, too, but this would lead to a technically inelegant
  85. solution for a couple of reasons:
  86. * Intricate usage. Instead of passing a protocol argument to
  87. socket(2) and using bind(2) to select a CAN interface and CAN ID, an
  88. application would have to do all these operations using ioctl(2)s.
  89. * Code duplication. A character device cannot make use of the Linux
  90. network queueing code, so all that code would have to be duplicated
  91. for CAN networking.
  92. * Abstraction. In most existing character-device implementations, the
  93. hardware-specific device driver for a CAN controller directly
  94. provides the character device for the application to work with.
  95. This is at least very unusual in Unix systems for both, char and
  96. block devices. For example you don't have a character device for a
  97. certain UART of a serial interface, a certain sound chip in your
  98. computer, a SCSI or IDE controller providing access to your hard
  99. disk or tape streamer device. Instead, you have abstraction layers
  100. which provide a unified character or block device interface to the
  101. application on the one hand, and a interface for hardware-specific
  102. device drivers on the other hand. These abstractions are provided
  103. by subsystems like the tty layer, the audio subsystem or the SCSI
  104. and IDE subsystems for the devices mentioned above.
  105. The easiest way to implement a CAN device driver is as a character
  106. device without such a (complete) abstraction layer, as is done by most
  107. existing drivers. The right way, however, would be to add such a
  108. layer with all the functionality like registering for certain CAN
  109. IDs, supporting several open file descriptors and (de)multiplexing
  110. CAN frames between them, (sophisticated) queueing of CAN frames, and
  111. providing an API for device drivers to register with. However, then
  112. it would be no more difficult, or may be even easier, to use the
  113. networking framework provided by the Linux kernel, and this is what
  114. Socket CAN does.
  115. The use of the networking framework of the Linux kernel is just the
  116. natural and most appropriate way to implement CAN for Linux.
  117. 3. Socket CAN concept
  118. ---------------------
  119. As described in chapter 2 it is the main goal of Socket CAN to
  120. provide a socket interface to user space applications which builds
  121. upon the Linux network layer. In contrast to the commonly known
  122. TCP/IP and ethernet networking, the CAN bus is a broadcast-only(!)
  123. medium that has no MAC-layer addressing like ethernet. The CAN-identifier
  124. (can_id) is used for arbitration on the CAN-bus. Therefore the CAN-IDs
  125. have to be chosen uniquely on the bus. When designing a CAN-ECU
  126. network the CAN-IDs are mapped to be sent by a specific ECU.
  127. For this reason a CAN-ID can be treated best as a kind of source address.
  128. 3.1 receive lists
  129. The network transparent access of multiple applications leads to the
  130. problem that different applications may be interested in the same
  131. CAN-IDs from the same CAN network interface. The Socket CAN core
  132. module - which implements the protocol family CAN - provides several
  133. high efficient receive lists for this reason. If e.g. a user space
  134. application opens a CAN RAW socket, the raw protocol module itself
  135. requests the (range of) CAN-IDs from the Socket CAN core that are
  136. requested by the user. The subscription and unsubscription of
  137. CAN-IDs can be done for specific CAN interfaces or for all(!) known
  138. CAN interfaces with the can_rx_(un)register() functions provided to
  139. CAN protocol modules by the SocketCAN core (see chapter 5).
  140. To optimize the CPU usage at runtime the receive lists are split up
  141. into several specific lists per device that match the requested
  142. filter complexity for a given use-case.
  143. 3.2 local loopback of sent frames
  144. As known from other networking concepts the data exchanging
  145. applications may run on the same or different nodes without any
  146. change (except for the according addressing information):
  147. ___ ___ ___ _______ ___
  148. | _ | | _ | | _ | | _ _ | | _ |
  149. ||A|| ||B|| ||C|| ||A| |B|| ||C||
  150. |___| |___| |___| |_______| |___|
  151. | | | | |
  152. -----------------(1)- CAN bus -(2)---------------
  153. To ensure that application A receives the same information in the
  154. example (2) as it would receive in example (1) there is need for
  155. some kind of local loopback of the sent CAN frames on the appropriate
  156. node.
  157. The Linux network devices (by default) just can handle the
  158. transmission and reception of media dependent frames. Due to the
  159. arbitration on the CAN bus the transmission of a low prio CAN-ID
  160. may be delayed by the reception of a high prio CAN frame. To
  161. reflect the correct* traffic on the node the loopback of the sent
  162. data has to be performed right after a successful transmission. If
  163. the CAN network interface is not capable of performing the loopback for
  164. some reason the SocketCAN core can do this task as a fallback solution.
  165. See chapter 6.2 for details (recommended).
  166. The loopback functionality is enabled by default to reflect standard
  167. networking behaviour for CAN applications. Due to some requests from
  168. the RT-SocketCAN group the loopback optionally may be disabled for each
  169. separate socket. See sockopts from the CAN RAW sockets in chapter 4.1.
  170. * = you really like to have this when you're running analyser tools
  171. like 'candump' or 'cansniffer' on the (same) node.
  172. 3.3 network security issues (capabilities)
  173. The Controller Area Network is a local field bus transmitting only
  174. broadcast messages without any routing and security concepts.
  175. In the majority of cases the user application has to deal with
  176. raw CAN frames. Therefore it might be reasonable NOT to restrict
  177. the CAN access only to the user root, as known from other networks.
  178. Since the currently implemented CAN_RAW and CAN_BCM sockets can only
  179. send and receive frames to/from CAN interfaces it does not affect
  180. security of others networks to allow all users to access the CAN.
  181. To enable non-root users to access CAN_RAW and CAN_BCM protocol
  182. sockets the Kconfig options CAN_RAW_USER and/or CAN_BCM_USER may be
  183. selected at kernel compile time.
  184. 3.4 network problem notifications
  185. The use of the CAN bus may lead to several problems on the physical
  186. and media access control layer. Detecting and logging of these lower
  187. layer problems is a vital requirement for CAN users to identify
  188. hardware issues on the physical transceiver layer as well as
  189. arbitration problems and error frames caused by the different
  190. ECUs. The occurrence of detected errors are important for diagnosis
  191. and have to be logged together with the exact timestamp. For this
  192. reason the CAN interface driver can generate so called Error Frames
  193. that can optionally be passed to the user application in the same
  194. way as other CAN frames. Whenever an error on the physical layer
  195. or the MAC layer is detected (e.g. by the CAN controller) the driver
  196. creates an appropriate error frame. Error frames can be requested by
  197. the user application using the common CAN filter mechanisms. Inside
  198. this filter definition the (interested) type of errors may be
  199. selected. The reception of error frames is disabled by default.
  200. 4. How to use Socket CAN
  201. ------------------------
  202. Like TCP/IP, you first need to open a socket for communicating over a
  203. CAN network. Since Socket CAN implements a new protocol family, you
  204. need to pass PF_CAN as the first argument to the socket(2) system
  205. call. Currently, there are two CAN protocols to choose from, the raw
  206. socket protocol and the broadcast manager (BCM). So to open a socket,
  207. you would write
  208. s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  209. and
  210. s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
  211. respectively. After the successful creation of the socket, you would
  212. normally use the bind(2) system call to bind the socket to a CAN
  213. interface (which is different from TCP/IP due to different addressing
  214. - see chapter 3). After binding (CAN_RAW) or connecting (CAN_BCM)
  215. the socket, you can read(2) and write(2) from/to the socket or use
  216. send(2), sendto(2), sendmsg(2) and the recv* counterpart operations
  217. on the socket as usual. There are also CAN specific socket options
  218. described below.
  219. The basic CAN frame structure and the sockaddr structure are defined
  220. in include/linux/can.h:
  221. struct can_frame {
  222. canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
  223. __u8 can_dlc; /* data length code: 0 .. 8 */
  224. __u8 data[8] __attribute__((aligned(8)));
  225. };
  226. The alignment of the (linear) payload data[] to a 64bit boundary
  227. allows the user to define own structs and unions to easily access the
  228. CAN payload. There is no given byteorder on the CAN bus by
  229. default. A read(2) system call on a CAN_RAW socket transfers a
  230. struct can_frame to the user space.
  231. The sockaddr_can structure has an interface index like the
  232. PF_PACKET socket, that also binds to a specific interface:
  233. struct sockaddr_can {
  234. sa_family_t can_family;
  235. int can_ifindex;
  236. union {
  237. /* transport protocol class address info (e.g. ISOTP) */
  238. struct { canid_t rx_id, tx_id; } tp;
  239. /* reserved for future CAN protocols address information */
  240. } can_addr;
  241. };
  242. To determine the interface index an appropriate ioctl() has to
  243. be used (example for CAN_RAW sockets without error checking):
  244. int s;
  245. struct sockaddr_can addr;
  246. struct ifreq ifr;
  247. s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  248. strcpy(ifr.ifr_name, "can0" );
  249. ioctl(s, SIOCGIFINDEX, &ifr);
  250. addr.can_family = AF_CAN;
  251. addr.can_ifindex = ifr.ifr_ifindex;
  252. bind(s, (struct sockaddr *)&addr, sizeof(addr));
  253. (..)
  254. To bind a socket to all(!) CAN interfaces the interface index must
  255. be 0 (zero). In this case the socket receives CAN frames from every
  256. enabled CAN interface. To determine the originating CAN interface
  257. the system call recvfrom(2) may be used instead of read(2). To send
  258. on a socket that is bound to 'any' interface sendto(2) is needed to
  259. specify the outgoing interface.
  260. Reading CAN frames from a bound CAN_RAW socket (see above) consists
  261. of reading a struct can_frame:
  262. struct can_frame frame;
  263. nbytes = read(s, &frame, sizeof(struct can_frame));
  264. if (nbytes < 0) {
  265. perror("can raw socket read");
  266. return 1;
  267. }
  268. /* paraniod check ... */
  269. if (nbytes < sizeof(struct can_frame)) {
  270. fprintf(stderr, "read: incomplete CAN frame\n");
  271. return 1;
  272. }
  273. /* do something with the received CAN frame */
  274. Writing CAN frames can be done similarly, with the write(2) system call:
  275. nbytes = write(s, &frame, sizeof(struct can_frame));
  276. When the CAN interface is bound to 'any' existing CAN interface
  277. (addr.can_ifindex = 0) it is recommended to use recvfrom(2) if the
  278. information about the originating CAN interface is needed:
  279. struct sockaddr_can addr;
  280. struct ifreq ifr;
  281. socklen_t len = sizeof(addr);
  282. struct can_frame frame;
  283. nbytes = recvfrom(s, &frame, sizeof(struct can_frame),
  284. 0, (struct sockaddr*)&addr, &len);
  285. /* get interface name of the received CAN frame */
  286. ifr.ifr_ifindex = addr.can_ifindex;
  287. ioctl(s, SIOCGIFNAME, &ifr);
  288. printf("Received a CAN frame from interface %s", ifr.ifr_name);
  289. To write CAN frames on sockets bound to 'any' CAN interface the
  290. outgoing interface has to be defined certainly.
  291. strcpy(ifr.ifr_name, "can0");
  292. ioctl(s, SIOCGIFINDEX, &ifr);
  293. addr.can_ifindex = ifr.ifr_ifindex;
  294. addr.can_family = AF_CAN;
  295. nbytes = sendto(s, &frame, sizeof(struct can_frame),
  296. 0, (struct sockaddr*)&addr, sizeof(addr));
  297. 4.1 RAW protocol sockets with can_filters (SOCK_RAW)
  298. Using CAN_RAW sockets is extensively comparable to the commonly
  299. known access to CAN character devices. To meet the new possibilities
  300. provided by the multi user SocketCAN approach, some reasonable
  301. defaults are set at RAW socket binding time:
  302. - The filters are set to exactly one filter receiving everything
  303. - The socket only receives valid data frames (=> no error frames)
  304. - The loopback of sent CAN frames is enabled (see chapter 3.2)
  305. - The socket does not receive its own sent frames (in loopback mode)
  306. These default settings may be changed before or after binding the socket.
  307. To use the referenced definitions of the socket options for CAN_RAW
  308. sockets, include <linux/can/raw.h>.
  309. 4.1.1 RAW socket option CAN_RAW_FILTER
  310. The reception of CAN frames using CAN_RAW sockets can be controlled
  311. by defining 0 .. n filters with the CAN_RAW_FILTER socket option.
  312. The CAN filter structure is defined in include/linux/can.h:
  313. struct can_filter {
  314. canid_t can_id;
  315. canid_t can_mask;
  316. };
  317. A filter matches, when
  318. <received_can_id> & mask == can_id & mask
  319. which is analogous to known CAN controllers hardware filter semantics.
  320. The filter can be inverted in this semantic, when the CAN_INV_FILTER
  321. bit is set in can_id element of the can_filter structure. In
  322. contrast to CAN controller hardware filters the user may set 0 .. n
  323. receive filters for each open socket separately:
  324. struct can_filter rfilter[2];
  325. rfilter[0].can_id = 0x123;
  326. rfilter[0].can_mask = CAN_SFF_MASK;
  327. rfilter[1].can_id = 0x200;
  328. rfilter[1].can_mask = 0x700;
  329. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
  330. To disable the reception of CAN frames on the selected CAN_RAW socket:
  331. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  332. To set the filters to zero filters is quite obsolete as not read
  333. data causes the raw socket to discard the received CAN frames. But
  334. having this 'send only' use-case we may remove the receive list in the
  335. Kernel to save a little (really a very little!) CPU usage.
  336. 4.1.2 RAW socket option CAN_RAW_ERR_FILTER
  337. As described in chapter 3.4 the CAN interface driver can generate so
  338. called Error Frames that can optionally be passed to the user
  339. application in the same way as other CAN frames. The possible
  340. errors are divided into different error classes that may be filtered
  341. using the appropriate error mask. To register for every possible
  342. error condition CAN_ERR_MASK can be used as value for the error mask.
  343. The values for the error mask are defined in linux/can/error.h .
  344. can_err_mask_t err_mask = ( CAN_ERR_TX_TIMEOUT | CAN_ERR_BUSOFF );
  345. setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
  346. &err_mask, sizeof(err_mask));
  347. 4.1.3 RAW socket option CAN_RAW_LOOPBACK
  348. To meet multi user needs the local loopback is enabled by default
  349. (see chapter 3.2 for details). But in some embedded use-cases
  350. (e.g. when only one application uses the CAN bus) this loopback
  351. functionality can be disabled (separately for each socket):
  352. int loopback = 0; /* 0 = disabled, 1 = enabled (default) */
  353. setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback));
  354. 4.1.4 RAW socket option CAN_RAW_RECV_OWN_MSGS
  355. When the local loopback is enabled, all the sent CAN frames are
  356. looped back to the open CAN sockets that registered for the CAN
  357. frames' CAN-ID on this given interface to meet the multi user
  358. needs. The reception of the CAN frames on the same socket that was
  359. sending the CAN frame is assumed to be unwanted and therefore
  360. disabled by default. This default behaviour may be changed on
  361. demand:
  362. int recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
  363. setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
  364. &recv_own_msgs, sizeof(recv_own_msgs));
  365. 4.2 Broadcast Manager protocol sockets (SOCK_DGRAM)
  366. 4.3 connected transport protocols (SOCK_SEQPACKET)
  367. 4.4 unconnected transport protocols (SOCK_DGRAM)
  368. 5. Socket CAN core module
  369. -------------------------
  370. The Socket CAN core module implements the protocol family
  371. PF_CAN. CAN protocol modules are loaded by the core module at
  372. runtime. The core module provides an interface for CAN protocol
  373. modules to subscribe needed CAN IDs (see chapter 3.1).
  374. 5.1 can.ko module params
  375. - stats_timer: To calculate the Socket CAN core statistics
  376. (e.g. current/maximum frames per second) this 1 second timer is
  377. invoked at can.ko module start time by default. This timer can be
  378. disabled by using stattimer=0 on the module commandline.
  379. - debug: (removed since SocketCAN SVN r546)
  380. 5.2 procfs content
  381. As described in chapter 3.1 the Socket CAN core uses several filter
  382. lists to deliver received CAN frames to CAN protocol modules. These
  383. receive lists, their filters and the count of filter matches can be
  384. checked in the appropriate receive list. All entries contain the
  385. device and a protocol module identifier:
  386. foo@bar:~$ cat /proc/net/can/rcvlist_all
  387. receive list 'rx_all':
  388. (vcan3: no entry)
  389. (vcan2: no entry)
  390. (vcan1: no entry)
  391. device can_id can_mask function userdata matches ident
  392. vcan0 000 00000000 f88e6370 f6c6f400 0 raw
  393. (any: no entry)
  394. In this example an application requests any CAN traffic from vcan0.
  395. rcvlist_all - list for unfiltered entries (no filter operations)
  396. rcvlist_eff - list for single extended frame (EFF) entries
  397. rcvlist_err - list for error frames masks
  398. rcvlist_fil - list for mask/value filters
  399. rcvlist_inv - list for mask/value filters (inverse semantic)
  400. rcvlist_sff - list for single standard frame (SFF) entries
  401. Additional procfs files in /proc/net/can
  402. stats - Socket CAN core statistics (rx/tx frames, match ratios, ...)
  403. reset_stats - manual statistic reset
  404. version - prints the Socket CAN core version and the ABI version
  405. 5.3 writing own CAN protocol modules
  406. To implement a new protocol in the protocol family PF_CAN a new
  407. protocol has to be defined in include/linux/can.h .
  408. The prototypes and definitions to use the Socket CAN core can be
  409. accessed by including include/linux/can/core.h .
  410. In addition to functions that register the CAN protocol and the
  411. CAN device notifier chain there are functions to subscribe CAN
  412. frames received by CAN interfaces and to send CAN frames:
  413. can_rx_register - subscribe CAN frames from a specific interface
  414. can_rx_unregister - unsubscribe CAN frames from a specific interface
  415. can_send - transmit a CAN frame (optional with local loopback)
  416. For details see the kerneldoc documentation in net/can/af_can.c or
  417. the source code of net/can/raw.c or net/can/bcm.c .
  418. 6. CAN network drivers
  419. ----------------------
  420. Writing a CAN network device driver is much easier than writing a
  421. CAN character device driver. Similar to other known network device
  422. drivers you mainly have to deal with:
  423. - TX: Put the CAN frame from the socket buffer to the CAN controller.
  424. - RX: Put the CAN frame from the CAN controller to the socket buffer.
  425. See e.g. at Documentation/networking/netdevices.txt . The differences
  426. for writing CAN network device driver are described below:
  427. 6.1 general settings
  428. dev->type = ARPHRD_CAN; /* the netdevice hardware type */
  429. dev->flags = IFF_NOARP; /* CAN has no arp */
  430. dev->mtu = sizeof(struct can_frame);
  431. The struct can_frame is the payload of each socket buffer in the
  432. protocol family PF_CAN.
  433. 6.2 local loopback of sent frames
  434. As described in chapter 3.2 the CAN network device driver should
  435. support a local loopback functionality similar to the local echo
  436. e.g. of tty devices. In this case the driver flag IFF_ECHO has to be
  437. set to prevent the PF_CAN core from locally echoing sent frames
  438. (aka loopback) as fallback solution:
  439. dev->flags = (IFF_NOARP | IFF_ECHO);
  440. 6.3 CAN controller hardware filters
  441. To reduce the interrupt load on deep embedded systems some CAN
  442. controllers support the filtering of CAN IDs or ranges of CAN IDs.
  443. These hardware filter capabilities vary from controller to
  444. controller and have to be identified as not feasible in a multi-user
  445. networking approach. The use of the very controller specific
  446. hardware filters could make sense in a very dedicated use-case, as a
  447. filter on driver level would affect all users in the multi-user
  448. system. The high efficient filter sets inside the PF_CAN core allow
  449. to set different multiple filters for each socket separately.
  450. Therefore the use of hardware filters goes to the category 'handmade
  451. tuning on deep embedded systems'. The author is running a MPC603e
  452. @133MHz with four SJA1000 CAN controllers from 2002 under heavy bus
  453. load without any problems ...
  454. 6.4 The virtual CAN driver (vcan)
  455. Similar to the network loopback devices, vcan offers a virtual local
  456. CAN interface. A full qualified address on CAN consists of
  457. - a unique CAN Identifier (CAN ID)
  458. - the CAN bus this CAN ID is transmitted on (e.g. can0)
  459. so in common use cases more than one virtual CAN interface is needed.
  460. The virtual CAN interfaces allow the transmission and reception of CAN
  461. frames without real CAN controller hardware. Virtual CAN network
  462. devices are usually named 'vcanX', like vcan0 vcan1 vcan2 ...
  463. When compiled as a module the virtual CAN driver module is called vcan.ko
  464. Since Linux Kernel version 2.6.24 the vcan driver supports the Kernel
  465. netlink interface to create vcan network devices. The creation and
  466. removal of vcan network devices can be managed with the ip(8) tool:
  467. - Create a virtual CAN network interface:
  468. ip link add type vcan
  469. - Create a virtual CAN network interface with a specific name 'vcan42':
  470. ip link add dev vcan42 type vcan
  471. - Remove a (virtual CAN) network interface 'vcan42':
  472. ip link del vcan42
  473. The tool 'vcan' from the SocketCAN SVN repository on BerliOS is obsolete.
  474. Virtual CAN network device creation in older Kernels:
  475. In Linux Kernel versions < 2.6.24 the vcan driver creates 4 vcan
  476. netdevices at module load time by default. This value can be changed
  477. with the module parameter 'numdev'. E.g. 'modprobe vcan numdev=8'
  478. 6.5 currently supported CAN hardware
  479. On the project website http://developer.berlios.de/projects/socketcan
  480. there are different drivers available:
  481. vcan: Virtual CAN interface driver (if no real hardware is available)
  482. sja1000: Philips SJA1000 CAN controller (recommended)
  483. i82527: Intel i82527 CAN controller
  484. mscan: Motorola/Freescale CAN controller (e.g. inside SOC MPC5200)
  485. ccan: CCAN controller core (e.g. inside SOC h7202)
  486. slcan: For a bunch of CAN adaptors that are attached via a
  487. serial line ASCII protocol (for serial / USB adaptors)
  488. Additionally the different CAN adaptors (ISA/PCI/PCMCIA/USB/Parport)
  489. from PEAK Systemtechnik support the CAN netdevice driver model
  490. since Linux driver v6.0: http://www.peak-system.com/linux/index.htm
  491. Please check the Mailing Lists on the berlios OSS project website.
  492. 6.6 todo
  493. The configuration interface for CAN network drivers is still an open
  494. issue that has not been finalized in the socketcan project. Also the
  495. idea of having a library module (candev.ko) that holds functions
  496. that are needed by all CAN netdevices is not ready to ship.
  497. Your contribution is welcome.
  498. 7. Credits
  499. ----------
  500. Oliver Hartkopp (PF_CAN core, filters, drivers, bcm)
  501. Urs Thuermann (PF_CAN core, kernel integration, socket interfaces, raw, vcan)
  502. Jan Kizka (RT-SocketCAN core, Socket-API reconciliation)
  503. Wolfgang Grandegger (RT-SocketCAN core & drivers, Raw Socket-API reviews)
  504. Robert Schwebel (design reviews, PTXdist integration)
  505. Marc Kleine-Budde (design reviews, Kernel 2.6 cleanups, drivers)
  506. Benedikt Spranger (reviews)
  507. Thomas Gleixner (LKML reviews, coding style, posting hints)
  508. Andrey Volkov (kernel subtree structure, ioctls, mscan driver)
  509. Matthias Brukner (first SJA1000 CAN netdevice implementation Q2/2003)
  510. Klaus Hitschler (PEAK driver integration)
  511. Uwe Koppe (CAN netdevices with PF_PACKET approach)
  512. Michael Schulze (driver layer loopback requirement, RT CAN drivers review)