diff options
| author | Wolfgang Grandegger <wg@grandegger.com> | 2009-05-15 19:39:29 -0400 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2009-05-18 18:41:41 -0400 |
| commit | 39549eef3587f1c1e8c65c88a2400d10fd30ea17 (patch) | |
| tree | 58367320ce0e3541c8e4c15a0d76ca879d3c154d | |
| parent | 4261a2043f1bed16f226c507ea37015090600c0f (diff) | |
can: CAN Network device driver and Netlink interface
The CAN network device driver interface provides a generic interface to
setup, configure and monitor CAN network devices. It exports a set of
common data structures and functions, which all real CAN network device
drivers should use. Please have a look to the SJA1000 or MSCAN driver
to understand how to use them. The name of the module is can-dev.ko.
Furthermore, it adds a Netlink interface allowing to configure the CAN
device using the program "ip" from the iproute2 utility suite.
For further information please check "Documentation/networking/can.txt"
Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
| -rw-r--r-- | drivers/net/can/Kconfig | 23 | ||||
| -rw-r--r-- | drivers/net/can/Makefile | 5 | ||||
| -rw-r--r-- | drivers/net/can/dev.c | 657 | ||||
| -rw-r--r-- | include/linux/can/Kbuild | 1 | ||||
| -rw-r--r-- | include/linux/can/dev.h | 70 | ||||
| -rw-r--r-- | include/linux/can/netlink.h | 113 |
6 files changed, 869 insertions, 0 deletions
diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index 57def0d57371..77adb8ef6e4f 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig | |||
| @@ -12,6 +12,29 @@ config CAN_VCAN | |||
| 12 | This driver can also be built as a module. If so, the module | 12 | This driver can also be built as a module. If so, the module |
| 13 | will be called vcan. | 13 | will be called vcan. |
| 14 | 14 | ||
| 15 | config CAN_DEV | ||
| 16 | tristate "Platform CAN drivers with Netlink support" | ||
| 17 | depends on CAN | ||
| 18 | default Y | ||
| 19 | ---help--- | ||
| 20 | Enables the common framework for platform CAN drivers with Netlink | ||
| 21 | support. This is the standard library for CAN drivers. | ||
| 22 | If unsure, say Y. | ||
| 23 | |||
| 24 | config CAN_CALC_BITTIMING | ||
| 25 | bool "CAN bit-timing calculation" | ||
| 26 | depends on CAN_DEV | ||
| 27 | default Y | ||
| 28 | ---help--- | ||
| 29 | If enabled, CAN bit-timing parameters will be calculated for the | ||
| 30 | bit-rate specified via Netlink argument "bitrate" when the device | ||
| 31 | get started. This works fine for the most common CAN controllers | ||
| 32 | with standard bit-rates but may fail for exotic bit-rates or CAN | ||
| 33 | source clock frequencies. Disabling saves some space, but then the | ||
| 34 | bit-timing parameters must be specified directly using the Netlink | ||
| 35 | arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw". | ||
| 36 | If unsure, say Y. | ||
| 37 | |||
| 15 | config CAN_DEBUG_DEVICES | 38 | config CAN_DEBUG_DEVICES |
| 16 | bool "CAN devices debugging messages" | 39 | bool "CAN devices debugging messages" |
| 17 | depends on CAN | 40 | depends on CAN |
diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile index c4bead705cd9..6c865475cd88 100644 --- a/drivers/net/can/Makefile +++ b/drivers/net/can/Makefile | |||
| @@ -3,3 +3,8 @@ | |||
| 3 | # | 3 | # |
| 4 | 4 | ||
| 5 | obj-$(CONFIG_CAN_VCAN) += vcan.o | 5 | obj-$(CONFIG_CAN_VCAN) += vcan.o |
| 6 | |||
| 7 | obj-$(CONFIG_CAN_DEV) += can-dev.o | ||
| 8 | can-dev-y := dev.o | ||
| 9 | |||
| 10 | ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG | ||
diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c new file mode 100644 index 000000000000..52b0e7d8901d --- /dev/null +++ b/drivers/net/can/dev.c | |||
| @@ -0,0 +1,657 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix | ||
| 3 | * Copyright (C) 2006 Andrey Volkov, Varma Electronics | ||
| 4 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the version 2 of the GNU General Public License | ||
| 8 | * as published by the Free Software Foundation | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <linux/module.h> | ||
| 21 | #include <linux/kernel.h> | ||
| 22 | #include <linux/netdevice.h> | ||
| 23 | #include <linux/if_arp.h> | ||
| 24 | #include <linux/can.h> | ||
| 25 | #include <linux/can/dev.h> | ||
| 26 | #include <linux/can/netlink.h> | ||
| 27 | #include <net/rtnetlink.h> | ||
| 28 | |||
| 29 | #define MOD_DESC "CAN device driver interface" | ||
| 30 | |||
| 31 | MODULE_DESCRIPTION(MOD_DESC); | ||
| 32 | MODULE_LICENSE("GPL v2"); | ||
| 33 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); | ||
| 34 | |||
| 35 | #ifdef CONFIG_CAN_CALC_BITTIMING | ||
| 36 | #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ | ||
| 37 | |||
| 38 | /* | ||
| 39 | * Bit-timing calculation derived from: | ||
| 40 | * | ||
| 41 | * Code based on LinCAN sources and H8S2638 project | ||
| 42 | * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz | ||
| 43 | * Copyright 2005 Stanislav Marek | ||
| 44 | * email: pisa@cmp.felk.cvut.cz | ||
| 45 | * | ||
| 46 | * Calculates proper bit-timing parameters for a specified bit-rate | ||
| 47 | * and sample-point, which can then be used to set the bit-timing | ||
| 48 | * registers of the CAN controller. You can find more information | ||
| 49 | * in the header file linux/can/netlink.h. | ||
| 50 | */ | ||
| 51 | static int can_update_spt(const struct can_bittiming_const *btc, | ||
| 52 | int sampl_pt, int tseg, int *tseg1, int *tseg2) | ||
| 53 | { | ||
| 54 | *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000; | ||
| 55 | if (*tseg2 < btc->tseg2_min) | ||
| 56 | *tseg2 = btc->tseg2_min; | ||
| 57 | if (*tseg2 > btc->tseg2_max) | ||
| 58 | *tseg2 = btc->tseg2_max; | ||
| 59 | *tseg1 = tseg - *tseg2; | ||
| 60 | if (*tseg1 > btc->tseg1_max) { | ||
| 61 | *tseg1 = btc->tseg1_max; | ||
| 62 | *tseg2 = tseg - *tseg1; | ||
| 63 | } | ||
| 64 | return 1000 * (tseg + 1 - *tseg2) / (tseg + 1); | ||
| 65 | } | ||
| 66 | |||
| 67 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | ||
| 68 | { | ||
| 69 | struct can_priv *priv = netdev_priv(dev); | ||
| 70 | const struct can_bittiming_const *btc = priv->bittiming_const; | ||
| 71 | long rate, best_rate = 0; | ||
| 72 | long best_error = 1000000000, error = 0; | ||
| 73 | int best_tseg = 0, best_brp = 0, brp = 0; | ||
| 74 | int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0; | ||
| 75 | int spt_error = 1000, spt = 0, sampl_pt; | ||
| 76 | u64 v64; | ||
| 77 | |||
| 78 | if (!priv->bittiming_const) | ||
| 79 | return -ENOTSUPP; | ||
| 80 | |||
| 81 | /* Use CIA recommended sample points */ | ||
| 82 | if (bt->sample_point) { | ||
| 83 | sampl_pt = bt->sample_point; | ||
| 84 | } else { | ||
| 85 | if (bt->bitrate > 800000) | ||
| 86 | sampl_pt = 750; | ||
| 87 | else if (bt->bitrate > 500000) | ||
| 88 | sampl_pt = 800; | ||
| 89 | else | ||
| 90 | sampl_pt = 875; | ||
| 91 | } | ||
| 92 | |||
| 93 | /* tseg even = round down, odd = round up */ | ||
| 94 | for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; | ||
| 95 | tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { | ||
| 96 | tsegall = 1 + tseg / 2; | ||
| 97 | /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ | ||
| 98 | brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; | ||
| 99 | /* chose brp step which is possible in system */ | ||
| 100 | brp = (brp / btc->brp_inc) * btc->brp_inc; | ||
| 101 | if ((brp < btc->brp_min) || (brp > btc->brp_max)) | ||
| 102 | continue; | ||
| 103 | rate = priv->clock.freq / (brp * tsegall); | ||
| 104 | error = bt->bitrate - rate; | ||
| 105 | /* tseg brp biterror */ | ||
| 106 | if (error < 0) | ||
| 107 | error = -error; | ||
| 108 | if (error > best_error) | ||
| 109 | continue; | ||
| 110 | best_error = error; | ||
| 111 | if (error == 0) { | ||
| 112 | spt = can_update_spt(btc, sampl_pt, tseg / 2, | ||
| 113 | &tseg1, &tseg2); | ||
| 114 | error = sampl_pt - spt; | ||
| 115 | if (error < 0) | ||
| 116 | error = -error; | ||
| 117 | if (error > spt_error) | ||
| 118 | continue; | ||
| 119 | spt_error = error; | ||
| 120 | } | ||
| 121 | best_tseg = tseg / 2; | ||
| 122 | best_brp = brp; | ||
| 123 | best_rate = rate; | ||
| 124 | if (error == 0) | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | |||
| 128 | if (best_error) { | ||
| 129 | /* Error in one-tenth of a percent */ | ||
| 130 | error = (best_error * 1000) / bt->bitrate; | ||
| 131 | if (error > CAN_CALC_MAX_ERROR) { | ||
| 132 | dev_err(dev->dev.parent, | ||
| 133 | "bitrate error %ld.%ld%% too high\n", | ||
| 134 | error / 10, error % 10); | ||
| 135 | return -EDOM; | ||
| 136 | } else { | ||
| 137 | dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n", | ||
| 138 | error / 10, error % 10); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | /* real sample point */ | ||
| 143 | bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg, | ||
| 144 | &tseg1, &tseg2); | ||
| 145 | |||
| 146 | v64 = (u64)best_brp * 1000000000UL; | ||
| 147 | do_div(v64, priv->clock.freq); | ||
| 148 | bt->tq = (u32)v64; | ||
| 149 | bt->prop_seg = tseg1 / 2; | ||
| 150 | bt->phase_seg1 = tseg1 - bt->prop_seg; | ||
| 151 | bt->phase_seg2 = tseg2; | ||
| 152 | bt->sjw = 1; | ||
| 153 | bt->brp = best_brp; | ||
| 154 | /* real bit-rate */ | ||
| 155 | bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1)); | ||
| 156 | |||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | #else /* !CONFIG_CAN_CALC_BITTIMING */ | ||
| 160 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) | ||
| 161 | { | ||
| 162 | dev_err(dev->dev.parent, "bit-timing calculation not available\n"); | ||
| 163 | return -EINVAL; | ||
| 164 | } | ||
| 165 | #endif /* CONFIG_CAN_CALC_BITTIMING */ | ||
| 166 | |||
| 167 | /* | ||
| 168 | * Checks the validity of the specified bit-timing parameters prop_seg, | ||
| 169 | * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate | ||
| 170 | * prescaler value brp. You can find more information in the header | ||
| 171 | * file linux/can/netlink.h. | ||
| 172 | */ | ||
| 173 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt) | ||
| 174 | { | ||
| 175 | struct can_priv *priv = netdev_priv(dev); | ||
| 176 | const struct can_bittiming_const *btc = priv->bittiming_const; | ||
| 177 | int tseg1, alltseg; | ||
| 178 | u64 brp64; | ||
| 179 | |||
| 180 | if (!priv->bittiming_const) | ||
| 181 | return -ENOTSUPP; | ||
| 182 | |||
| 183 | tseg1 = bt->prop_seg + bt->phase_seg1; | ||
| 184 | if (!bt->sjw) | ||
| 185 | bt->sjw = 1; | ||
| 186 | if (bt->sjw > btc->sjw_max || | ||
| 187 | tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max || | ||
| 188 | bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max) | ||
| 189 | return -ERANGE; | ||
| 190 | |||
| 191 | brp64 = (u64)priv->clock.freq * (u64)bt->tq; | ||
| 192 | if (btc->brp_inc > 1) | ||
| 193 | do_div(brp64, btc->brp_inc); | ||
| 194 | brp64 += 500000000UL - 1; | ||
| 195 | do_div(brp64, 1000000000UL); /* the practicable BRP */ | ||
| 196 | if (btc->brp_inc > 1) | ||
| 197 | brp64 *= btc->brp_inc; | ||
| 198 | bt->brp = (u32)brp64; | ||
| 199 | |||
| 200 | if (bt->brp < btc->brp_min || bt->brp > btc->brp_max) | ||
| 201 | return -EINVAL; | ||
| 202 | |||
| 203 | alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1; | ||
| 204 | bt->bitrate = priv->clock.freq / (bt->brp * alltseg); | ||
| 205 | bt->sample_point = ((tseg1 + 1) * 1000) / alltseg; | ||
| 206 | |||
| 207 | return 0; | ||
| 208 | } | ||
| 209 | |||
| 210 | int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt) | ||
| 211 | { | ||
| 212 | struct can_priv *priv = netdev_priv(dev); | ||
| 213 | int err; | ||
| 214 | |||
| 215 | /* Check if the CAN device has bit-timing parameters */ | ||
| 216 | if (priv->bittiming_const) { | ||
| 217 | |||
| 218 | /* Non-expert mode? Check if the bitrate has been pre-defined */ | ||
| 219 | if (!bt->tq) | ||
| 220 | /* Determine bit-timing parameters */ | ||
| 221 | err = can_calc_bittiming(dev, bt); | ||
| 222 | else | ||
| 223 | /* Check bit-timing params and calculate proper brp */ | ||
| 224 | err = can_fixup_bittiming(dev, bt); | ||
| 225 | if (err) | ||
| 226 | return err; | ||
| 227 | } | ||
| 228 | |||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* | ||
| 233 | * Local echo of CAN messages | ||
| 234 | * | ||
| 235 | * CAN network devices *should* support a local echo functionality | ||
| 236 | * (see Documentation/networking/can.txt). To test the handling of CAN | ||
| 237 | * interfaces that do not support the local echo both driver types are | ||
| 238 | * implemented. In the case that the driver does not support the echo | ||
| 239 | * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core | ||
| 240 | * to perform the echo as a fallback solution. | ||
| 241 | */ | ||
| 242 | static void can_flush_echo_skb(struct net_device *dev) | ||
| 243 | { | ||
| 244 | struct can_priv *priv = netdev_priv(dev); | ||
| 245 | struct net_device_stats *stats = &dev->stats; | ||
| 246 | int i; | ||
| 247 | |||
| 248 | for (i = 0; i < CAN_ECHO_SKB_MAX; i++) { | ||
| 249 | if (priv->echo_skb[i]) { | ||
| 250 | kfree_skb(priv->echo_skb[i]); | ||
| 251 | priv->echo_skb[i] = NULL; | ||
| 252 | stats->tx_dropped++; | ||
| 253 | stats->tx_aborted_errors++; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | /* | ||
| 259 | * Put the skb on the stack to be looped backed locally lateron | ||
| 260 | * | ||
| 261 | * The function is typically called in the start_xmit function | ||
| 262 | * of the device driver. The driver must protect access to | ||
| 263 | * priv->echo_skb, if necessary. | ||
| 264 | */ | ||
| 265 | void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx) | ||
| 266 | { | ||
| 267 | struct can_priv *priv = netdev_priv(dev); | ||
| 268 | |||
| 269 | /* check flag whether this packet has to be looped back */ | ||
| 270 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) { | ||
| 271 | kfree_skb(skb); | ||
| 272 | return; | ||
| 273 | } | ||
| 274 | |||
| 275 | if (!priv->echo_skb[idx]) { | ||
| 276 | struct sock *srcsk = skb->sk; | ||
| 277 | |||
| 278 | if (atomic_read(&skb->users) != 1) { | ||
| 279 | struct sk_buff *old_skb = skb; | ||
| 280 | |||
| 281 | skb = skb_clone(old_skb, GFP_ATOMIC); | ||
| 282 | kfree_skb(old_skb); | ||
| 283 | if (!skb) | ||
| 284 | return; | ||
| 285 | } else | ||
| 286 | skb_orphan(skb); | ||
| 287 | |||
| 288 | skb->sk = srcsk; | ||
| 289 | |||
| 290 | /* make settings for echo to reduce code in irq context */ | ||
| 291 | skb->protocol = htons(ETH_P_CAN); | ||
| 292 | skb->pkt_type = PACKET_BROADCAST; | ||
| 293 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
| 294 | skb->dev = dev; | ||
| 295 | |||
| 296 | /* save this skb for tx interrupt echo handling */ | ||
| 297 | priv->echo_skb[idx] = skb; | ||
| 298 | } else { | ||
| 299 | /* locking problem with netif_stop_queue() ?? */ | ||
| 300 | dev_err(dev->dev.parent, "%s: BUG! echo_skb is occupied!\n", | ||
| 301 | __func__); | ||
| 302 | kfree_skb(skb); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | EXPORT_SYMBOL_GPL(can_put_echo_skb); | ||
| 306 | |||
| 307 | /* | ||
| 308 | * Get the skb from the stack and loop it back locally | ||
| 309 | * | ||
| 310 | * The function is typically called when the TX done interrupt | ||
| 311 | * is handled in the device driver. The driver must protect | ||
| 312 | * access to priv->echo_skb, if necessary. | ||
| 313 | */ | ||
| 314 | void can_get_echo_skb(struct net_device *dev, int idx) | ||
| 315 | { | ||
| 316 | struct can_priv *priv = netdev_priv(dev); | ||
| 317 | |||
| 318 | if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) { | ||
| 319 | netif_rx(priv->echo_skb[idx]); | ||
| 320 | priv->echo_skb[idx] = NULL; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | EXPORT_SYMBOL_GPL(can_get_echo_skb); | ||
| 324 | |||
| 325 | /* | ||
| 326 | * CAN device restart for bus-off recovery | ||
| 327 | */ | ||
| 328 | void can_restart(unsigned long data) | ||
| 329 | { | ||
| 330 | struct net_device *dev = (struct net_device *)data; | ||
| 331 | struct can_priv *priv = netdev_priv(dev); | ||
| 332 | struct net_device_stats *stats = &dev->stats; | ||
| 333 | struct sk_buff *skb; | ||
| 334 | struct can_frame *cf; | ||
| 335 | int err; | ||
| 336 | |||
| 337 | BUG_ON(netif_carrier_ok(dev)); | ||
| 338 | |||
| 339 | /* | ||
| 340 | * No synchronization needed because the device is bus-off and | ||
| 341 | * no messages can come in or go out. | ||
| 342 | */ | ||
| 343 | can_flush_echo_skb(dev); | ||
| 344 | |||
| 345 | /* send restart message upstream */ | ||
| 346 | skb = dev_alloc_skb(sizeof(struct can_frame)); | ||
| 347 | if (skb == NULL) { | ||
| 348 | err = -ENOMEM; | ||
| 349 | goto out; | ||
| 350 | } | ||
| 351 | skb->dev = dev; | ||
| 352 | skb->protocol = htons(ETH_P_CAN); | ||
| 353 | cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); | ||
| 354 | memset(cf, 0, sizeof(struct can_frame)); | ||
| 355 | cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED; | ||
| 356 | cf->can_dlc = CAN_ERR_DLC; | ||
| 357 | |||
| 358 | netif_rx(skb); | ||
| 359 | |||
| 360 | dev->last_rx = jiffies; | ||
| 361 | stats->rx_packets++; | ||
| 362 | stats->rx_bytes += cf->can_dlc; | ||
| 363 | |||
| 364 | dev_dbg(dev->dev.parent, "restarted\n"); | ||
| 365 | priv->can_stats.restarts++; | ||
| 366 | |||
| 367 | /* Now restart the device */ | ||
| 368 | err = priv->do_set_mode(dev, CAN_MODE_START); | ||
| 369 | |||
| 370 | out: | ||
| 371 | netif_carrier_on(dev); | ||
| 372 | if (err) | ||
| 373 | dev_err(dev->dev.parent, "Error %d during restart", err); | ||
| 374 | } | ||
| 375 | |||
| 376 | int can_restart_now(struct net_device *dev) | ||
| 377 | { | ||
| 378 | struct can_priv *priv = netdev_priv(dev); | ||
| 379 | |||
| 380 | /* | ||
| 381 | * A manual restart is only permitted if automatic restart is | ||
| 382 | * disabled and the device is in the bus-off state | ||
| 383 | */ | ||
| 384 | if (priv->restart_ms) | ||
| 385 | return -EINVAL; | ||
| 386 | if (priv->state != CAN_STATE_BUS_OFF) | ||
| 387 | return -EBUSY; | ||
| 388 | |||
| 389 | /* Runs as soon as possible in the timer context */ | ||
| 390 | mod_timer(&priv->restart_timer, jiffies); | ||
| 391 | |||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | |||
| 395 | /* | ||
| 396 | * CAN bus-off | ||
| 397 | * | ||
| 398 | * This functions should be called when the device goes bus-off to | ||
| 399 | * tell the netif layer that no more packets can be sent or received. | ||
| 400 | * If enabled, a timer is started to trigger bus-off recovery. | ||
| 401 | */ | ||
| 402 | void can_bus_off(struct net_device *dev) | ||
| 403 | { | ||
| 404 | struct can_priv *priv = netdev_priv(dev); | ||
| 405 | |||
| 406 | dev_dbg(dev->dev.parent, "bus-off\n"); | ||
| 407 | |||
| 408 | netif_carrier_off(dev); | ||
| 409 | priv->can_stats.bus_off++; | ||
| 410 | |||
| 411 | if (priv->restart_ms) | ||
| 412 | mod_timer(&priv->restart_timer, | ||
| 413 | jiffies + (priv->restart_ms * HZ) / 1000); | ||
| 414 | } | ||
| 415 | EXPORT_SYMBOL_GPL(can_bus_off); | ||
| 416 | |||
| 417 | static void can_setup(struct net_device *dev) | ||
| 418 | { | ||
| 419 | dev->type = ARPHRD_CAN; | ||
| 420 | dev->mtu = sizeof(struct can_frame); | ||
| 421 | dev->hard_header_len = 0; | ||
| 422 | dev->addr_len = 0; | ||
| 423 | dev->tx_queue_len = 10; | ||
| 424 | |||
| 425 | /* New-style flags. */ | ||
| 426 | dev->flags = IFF_NOARP; | ||
| 427 | dev->features = NETIF_F_NO_CSUM; | ||
| 428 | } | ||
| 429 | |||
| 430 | /* | ||
| 431 | * Allocate and setup space for the CAN network device | ||
| 432 | */ | ||
| 433 | struct net_device *alloc_candev(int sizeof_priv) | ||
| 434 | { | ||
| 435 | struct net_device *dev; | ||
| 436 | struct can_priv *priv; | ||
| 437 | |||
| 438 | dev = alloc_netdev(sizeof_priv, "can%d", can_setup); | ||
| 439 | if (!dev) | ||
| 440 | return NULL; | ||
| 441 | |||
| 442 | priv = netdev_priv(dev); | ||
| 443 | |||
| 444 | priv->state = CAN_STATE_STOPPED; | ||
| 445 | |||
| 446 | init_timer(&priv->restart_timer); | ||
| 447 | |||
| 448 | return dev; | ||
| 449 | } | ||
| 450 | EXPORT_SYMBOL_GPL(alloc_candev); | ||
| 451 | |||
| 452 | /* | ||
| 453 | * Free space of the CAN network device | ||
| 454 | */ | ||
| 455 | void free_candev(struct net_device *dev) | ||
| 456 | { | ||
| 457 | free_netdev(dev); | ||
| 458 | } | ||
| 459 | EXPORT_SYMBOL_GPL(free_candev); | ||
| 460 | |||
| 461 | /* | ||
| 462 | * Common open function when the device gets opened. | ||
| 463 | * | ||
| 464 | * This function should be called in the open function of the device | ||
| 465 | * driver. | ||
| 466 | */ | ||
| 467 | int open_candev(struct net_device *dev) | ||
| 468 | { | ||
| 469 | struct can_priv *priv = netdev_priv(dev); | ||
| 470 | |||
| 471 | if (!priv->bittiming.tq && !priv->bittiming.bitrate) { | ||
| 472 | dev_err(dev->dev.parent, "bit-timing not yet defined\n"); | ||
| 473 | return -EINVAL; | ||
| 474 | } | ||
| 475 | |||
| 476 | setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev); | ||
| 477 | |||
| 478 | return 0; | ||
| 479 | } | ||
| 480 | EXPORT_SYMBOL(open_candev); | ||
| 481 | |||
| 482 | /* | ||
| 483 | * Common close function for cleanup before the device gets closed. | ||
| 484 | * | ||
| 485 | * This function should be called in the close function of the device | ||
| 486 | * driver. | ||
| 487 | */ | ||
| 488 | void close_candev(struct net_device *dev) | ||
| 489 | { | ||
| 490 | struct can_priv *priv = netdev_priv(dev); | ||
| 491 | |||
| 492 | if (del_timer_sync(&priv->restart_timer)) | ||
| 493 | dev_put(dev); | ||
| 494 | can_flush_echo_skb(dev); | ||
| 495 | } | ||
| 496 | EXPORT_SYMBOL_GPL(close_candev); | ||
| 497 | |||
| 498 | /* | ||
| 499 | * CAN netlink interface | ||
| 500 | */ | ||
| 501 | static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { | ||
| 502 | [IFLA_CAN_STATE] = { .type = NLA_U32 }, | ||
| 503 | [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, | ||
| 504 | [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, | ||
| 505 | [IFLA_CAN_RESTART] = { .type = NLA_U32 }, | ||
| 506 | [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, | ||
| 507 | [IFLA_CAN_BITTIMING_CONST] | ||
| 508 | = { .len = sizeof(struct can_bittiming_const) }, | ||
| 509 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, | ||
| 510 | }; | ||
| 511 | |||
| 512 | static int can_changelink(struct net_device *dev, | ||
| 513 | struct nlattr *tb[], struct nlattr *data[]) | ||
| 514 | { | ||
| 515 | struct can_priv *priv = netdev_priv(dev); | ||
| 516 | int err; | ||
| 517 | |||
| 518 | /* We need synchronization with dev->stop() */ | ||
| 519 | ASSERT_RTNL(); | ||
| 520 | |||
| 521 | if (data[IFLA_CAN_CTRLMODE]) { | ||
| 522 | struct can_ctrlmode *cm; | ||
| 523 | |||
| 524 | /* Do not allow changing controller mode while running */ | ||
| 525 | if (dev->flags & IFF_UP) | ||
| 526 | return -EBUSY; | ||
| 527 | cm = nla_data(data[IFLA_CAN_CTRLMODE]); | ||
| 528 | priv->ctrlmode &= ~cm->mask; | ||
| 529 | priv->ctrlmode |= cm->flags; | ||
| 530 | } | ||
| 531 | |||
| 532 | if (data[IFLA_CAN_BITTIMING]) { | ||
| 533 | struct can_bittiming bt; | ||
| 534 | |||
| 535 | /* Do not allow changing bittiming while running */ | ||
| 536 | if (dev->flags & IFF_UP) | ||
| 537 | return -EBUSY; | ||
| 538 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); | ||
| 539 | if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq)) | ||
| 540 | return -EINVAL; | ||
| 541 | err = can_get_bittiming(dev, &bt); | ||
| 542 | if (err) | ||
| 543 | return err; | ||
| 544 | memcpy(&priv->bittiming, &bt, sizeof(bt)); | ||
| 545 | |||
| 546 | if (priv->do_set_bittiming) { | ||
| 547 | /* Finally, set the bit-timing registers */ | ||
| 548 | err = priv->do_set_bittiming(dev); | ||
| 549 | if (err) | ||
| 550 | return err; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | if (data[IFLA_CAN_RESTART_MS]) { | ||
| 555 | /* Do not allow changing restart delay while running */ | ||
| 556 | if (dev->flags & IFF_UP) | ||
| 557 | return -EBUSY; | ||
| 558 | priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); | ||
| 559 | } | ||
| 560 | |||
| 561 | if (data[IFLA_CAN_RESTART]) { | ||
| 562 | /* Do not allow a restart while not running */ | ||
| 563 | if (!(dev->flags & IFF_UP)) | ||
| 564 | return -EINVAL; | ||
| 565 | err = can_restart_now(dev); | ||
| 566 | if (err) | ||
| 567 | return err; | ||
| 568 | } | ||
| 569 | |||
| 570 | return 0; | ||
| 571 | } | ||
| 572 | |||
| 573 | static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) | ||
| 574 | { | ||
| 575 | struct can_priv *priv = netdev_priv(dev); | ||
| 576 | struct can_ctrlmode cm = {.flags = priv->ctrlmode}; | ||
| 577 | enum can_state state = priv->state; | ||
| 578 | |||
| 579 | if (priv->do_get_state) | ||
| 580 | priv->do_get_state(dev, &state); | ||
| 581 | NLA_PUT_U32(skb, IFLA_CAN_STATE, state); | ||
| 582 | NLA_PUT(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm); | ||
| 583 | NLA_PUT_U32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms); | ||
| 584 | NLA_PUT(skb, IFLA_CAN_BITTIMING, | ||
| 585 | sizeof(priv->bittiming), &priv->bittiming); | ||
| 586 | NLA_PUT(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock); | ||
| 587 | if (priv->bittiming_const) | ||
| 588 | NLA_PUT(skb, IFLA_CAN_BITTIMING_CONST, | ||
| 589 | sizeof(*priv->bittiming_const), priv->bittiming_const); | ||
| 590 | |||
| 591 | return 0; | ||
| 592 | |||
| 593 | nla_put_failure: | ||
| 594 | return -EMSGSIZE; | ||
| 595 | } | ||
| 596 | |||
| 597 | static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) | ||
| 598 | { | ||
| 599 | struct can_priv *priv = netdev_priv(dev); | ||
| 600 | |||
| 601 | NLA_PUT(skb, IFLA_INFO_XSTATS, | ||
| 602 | sizeof(priv->can_stats), &priv->can_stats); | ||
| 603 | |||
| 604 | return 0; | ||
| 605 | |||
| 606 | nla_put_failure: | ||
| 607 | return -EMSGSIZE; | ||
| 608 | } | ||
| 609 | |||
| 610 | static struct rtnl_link_ops can_link_ops __read_mostly = { | ||
| 611 | .kind = "can", | ||
| 612 | .maxtype = IFLA_CAN_MAX, | ||
| 613 | .policy = can_policy, | ||
| 614 | .setup = can_setup, | ||
| 615 | .changelink = can_changelink, | ||
| 616 | .fill_info = can_fill_info, | ||
| 617 | .fill_xstats = can_fill_xstats, | ||
| 618 | }; | ||
| 619 | |||
| 620 | /* | ||
| 621 | * Register the CAN network device | ||
| 622 | */ | ||
| 623 | int register_candev(struct net_device *dev) | ||
| 624 | { | ||
| 625 | dev->rtnl_link_ops = &can_link_ops; | ||
| 626 | return register_netdev(dev); | ||
| 627 | } | ||
| 628 | EXPORT_SYMBOL_GPL(register_candev); | ||
| 629 | |||
| 630 | /* | ||
| 631 | * Unregister the CAN network device | ||
| 632 | */ | ||
| 633 | void unregister_candev(struct net_device *dev) | ||
| 634 | { | ||
| 635 | unregister_netdev(dev); | ||
| 636 | } | ||
| 637 | EXPORT_SYMBOL_GPL(unregister_candev); | ||
| 638 | |||
| 639 | static __init int can_dev_init(void) | ||
| 640 | { | ||
| 641 | int err; | ||
| 642 | |||
| 643 | err = rtnl_link_register(&can_link_ops); | ||
| 644 | if (!err) | ||
| 645 | printk(KERN_INFO MOD_DESC "\n"); | ||
| 646 | |||
| 647 | return err; | ||
| 648 | } | ||
| 649 | module_init(can_dev_init); | ||
| 650 | |||
| 651 | static __exit void can_dev_exit(void) | ||
| 652 | { | ||
| 653 | rtnl_link_unregister(&can_link_ops); | ||
| 654 | } | ||
| 655 | module_exit(can_dev_exit); | ||
| 656 | |||
| 657 | MODULE_ALIAS_RTNL_LINK("can"); | ||
diff --git a/include/linux/can/Kbuild b/include/linux/can/Kbuild index eff898aac02b..8cb05aae661c 100644 --- a/include/linux/can/Kbuild +++ b/include/linux/can/Kbuild | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | header-y += raw.h | 1 | header-y += raw.h |
| 2 | header-y += bcm.h | 2 | header-y += bcm.h |
| 3 | header-y += error.h | 3 | header-y += error.h |
| 4 | header-y += netlink.h | ||
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h new file mode 100644 index 000000000000..4a37a56f6cdd --- /dev/null +++ b/include/linux/can/dev.h | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | /* | ||
| 2 | * linux/can/dev.h | ||
| 3 | * | ||
| 4 | * Definitions for the CAN network device driver interface | ||
| 5 | * | ||
| 6 | * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com> | ||
| 7 | * Varma Electronics Oy | ||
| 8 | * | ||
| 9 | * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com> | ||
| 10 | * | ||
| 11 | * Send feedback to <socketcan-users@lists.berlios.de> | ||
| 12 | */ | ||
| 13 | |||
| 14 | #ifndef CAN_DEV_H | ||
| 15 | #define CAN_DEV_H | ||
| 16 | |||
| 17 | #include <linux/can/netlink.h> | ||
| 18 | #include <linux/can/error.h> | ||
| 19 | |||
| 20 | /* | ||
| 21 | * CAN mode | ||
| 22 | */ | ||
| 23 | enum can_mode { | ||
| 24 | CAN_MODE_STOP = 0, | ||
| 25 | CAN_MODE_START, | ||
| 26 | CAN_MODE_SLEEP | ||
| 27 | }; | ||
| 28 | |||
| 29 | /* | ||
| 30 | * CAN common private data | ||
| 31 | */ | ||
| 32 | #define CAN_ECHO_SKB_MAX 4 | ||
| 33 | |||
| 34 | struct can_priv { | ||
| 35 | struct can_device_stats can_stats; | ||
| 36 | |||
| 37 | struct can_bittiming bittiming; | ||
| 38 | struct can_bittiming_const *bittiming_const; | ||
| 39 | struct can_clock clock; | ||
| 40 | |||
| 41 | enum can_state state; | ||
| 42 | u32 ctrlmode; | ||
| 43 | |||
| 44 | int restart_ms; | ||
| 45 | struct timer_list restart_timer; | ||
| 46 | |||
| 47 | struct sk_buff *echo_skb[CAN_ECHO_SKB_MAX]; | ||
| 48 | |||
| 49 | int (*do_set_bittiming)(struct net_device *dev); | ||
| 50 | int (*do_set_mode)(struct net_device *dev, enum can_mode mode); | ||
| 51 | int (*do_get_state)(const struct net_device *dev, | ||
| 52 | enum can_state *state); | ||
| 53 | }; | ||
| 54 | |||
| 55 | struct net_device *alloc_candev(int sizeof_priv); | ||
| 56 | void free_candev(struct net_device *dev); | ||
| 57 | |||
| 58 | int open_candev(struct net_device *dev); | ||
| 59 | void close_candev(struct net_device *dev); | ||
| 60 | |||
| 61 | int register_candev(struct net_device *dev); | ||
| 62 | void unregister_candev(struct net_device *dev); | ||
| 63 | |||
| 64 | int can_restart_now(struct net_device *dev); | ||
| 65 | void can_bus_off(struct net_device *dev); | ||
| 66 | |||
| 67 | void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx); | ||
| 68 | void can_get_echo_skb(struct net_device *dev, int idx); | ||
| 69 | |||
| 70 | #endif /* CAN_DEV_H */ | ||
diff --git a/include/linux/can/netlink.h b/include/linux/can/netlink.h new file mode 100644 index 000000000000..9ecbb7871c0e --- /dev/null +++ b/include/linux/can/netlink.h | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | /* | ||
| 2 | * linux/can/netlink.h | ||
| 3 | * | ||
| 4 | * Definitions for the CAN netlink interface | ||
| 5 | * | ||
| 6 | * Copyright (c) 2009 Wolfgang Grandegger <wg@grandegger.com> | ||
| 7 | * | ||
| 8 | * Send feedback to <socketcan-users@lists.berlios.de> | ||
| 9 | * | ||
| 10 | */ | ||
| 11 | |||
| 12 | #ifndef CAN_NETLINK_H | ||
| 13 | #define CAN_NETLINK_H | ||
| 14 | |||
| 15 | #include <linux/types.h> | ||
| 16 | |||
| 17 | /* | ||
| 18 | * CAN bit-timing parameters | ||
| 19 | * | ||
| 20 | * For futher information, please read chapter "8 BIT TIMING | ||
| 21 | * REQUIREMENTS" of the "Bosch CAN Specification version 2.0" | ||
| 22 | * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf. | ||
| 23 | */ | ||
| 24 | struct can_bittiming { | ||
| 25 | __u32 bitrate; /* Bit-rate in bits/second */ | ||
| 26 | __u32 sample_point; /* Sample point in one-tenth of a percent */ | ||
| 27 | __u32 tq; /* Time quanta (TQ) in nanoseconds */ | ||
| 28 | __u32 prop_seg; /* Propagation segment in TQs */ | ||
| 29 | __u32 phase_seg1; /* Phase buffer segment 1 in TQs */ | ||
| 30 | __u32 phase_seg2; /* Phase buffer segment 2 in TQs */ | ||
| 31 | __u32 sjw; /* Synchronisation jump width in TQs */ | ||
| 32 | __u32 brp; /* Bit-rate prescaler */ | ||
| 33 | }; | ||
| 34 | |||
| 35 | /* | ||
| 36 | * CAN harware-dependent bit-timing constant | ||
| 37 | * | ||
| 38 | * Used for calculating and checking bit-timing parameters | ||
| 39 | */ | ||
| 40 | struct can_bittiming_const { | ||
| 41 | char name[16]; /* Name of the CAN controller hardware */ | ||
| 42 | __u32 tseg1_min; /* Time segement 1 = prop_seg + phase_seg1 */ | ||
| 43 | __u32 tseg1_max; | ||
| 44 | __u32 tseg2_min; /* Time segement 2 = phase_seg2 */ | ||
| 45 | __u32 tseg2_max; | ||
| 46 | __u32 sjw_max; /* Synchronisation jump width */ | ||
| 47 | __u32 brp_min; /* Bit-rate prescaler */ | ||
| 48 | __u32 brp_max; | ||
| 49 | __u32 brp_inc; | ||
| 50 | }; | ||
| 51 | |||
| 52 | /* | ||
| 53 | * CAN clock parameters | ||
| 54 | */ | ||
| 55 | struct can_clock { | ||
| 56 | __u32 freq; /* CAN system clock frequency in Hz */ | ||
| 57 | }; | ||
| 58 | |||
| 59 | /* | ||
| 60 | * CAN operational and error states | ||
| 61 | */ | ||
| 62 | enum can_state { | ||
| 63 | CAN_STATE_ERROR_ACTIVE = 0, /* RX/TX error count < 96 */ | ||
| 64 | CAN_STATE_ERROR_WARNING, /* RX/TX error count < 128 */ | ||
| 65 | CAN_STATE_ERROR_PASSIVE, /* RX/TX error count < 256 */ | ||
| 66 | CAN_STATE_BUS_OFF, /* RX/TX error count >= 256 */ | ||
| 67 | CAN_STATE_STOPPED, /* Device is stopped */ | ||
| 68 | CAN_STATE_SLEEPING, /* Device is sleeping */ | ||
| 69 | CAN_STATE_MAX | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* | ||
| 73 | * CAN controller mode | ||
| 74 | */ | ||
| 75 | struct can_ctrlmode { | ||
| 76 | __u32 mask; | ||
| 77 | __u32 flags; | ||
| 78 | }; | ||
| 79 | |||
| 80 | #define CAN_CTRLMODE_LOOPBACK 0x1 /* Loopback mode */ | ||
| 81 | #define CAN_CTRLMODE_LISTENONLY 0x2 /* Listen-only mode */ | ||
| 82 | #define CAN_CTRLMODE_3_SAMPLES 0x4 /* Triple sampling mode */ | ||
| 83 | |||
| 84 | /* | ||
| 85 | * CAN device statistics | ||
| 86 | */ | ||
| 87 | struct can_device_stats { | ||
| 88 | __u32 bus_error; /* Bus errors */ | ||
| 89 | __u32 error_warning; /* Changes to error warning state */ | ||
| 90 | __u32 error_passive; /* Changes to error passive state */ | ||
| 91 | __u32 bus_off; /* Changes to bus off state */ | ||
| 92 | __u32 arbitration_lost; /* Arbitration lost errors */ | ||
| 93 | __u32 restarts; /* CAN controller re-starts */ | ||
| 94 | }; | ||
| 95 | |||
| 96 | /* | ||
| 97 | * CAN netlink interface | ||
| 98 | */ | ||
| 99 | enum { | ||
| 100 | IFLA_CAN_UNSPEC, | ||
| 101 | IFLA_CAN_BITTIMING, | ||
| 102 | IFLA_CAN_BITTIMING_CONST, | ||
| 103 | IFLA_CAN_CLOCK, | ||
| 104 | IFLA_CAN_STATE, | ||
| 105 | IFLA_CAN_CTRLMODE, | ||
| 106 | IFLA_CAN_RESTART_MS, | ||
| 107 | IFLA_CAN_RESTART, | ||
| 108 | __IFLA_CAN_MAX | ||
| 109 | }; | ||
| 110 | |||
| 111 | #define IFLA_CAN_MAX (__IFLA_CAN_MAX - 1) | ||
| 112 | |||
| 113 | #endif /* CAN_NETLINK_H */ | ||
