1 // SPDX-License-Identifier: GPL-2.0-only
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
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/workqueue.h>
13 #include <linux/can.h>
14 #include <linux/can/can-ml.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/skb.h>
17 #include <linux/can/led.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/of.h>
20
21 #define MOD_DESC "CAN device driver interface"
22
23 MODULE_DESCRIPTION(MOD_DESC);
24 MODULE_LICENSE("GPL v2");
25 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
26
can_update_state_error_stats(struct net_device * dev,enum can_state new_state)27 static void can_update_state_error_stats(struct net_device *dev,
28 enum can_state new_state)
29 {
30 struct can_priv *priv = netdev_priv(dev);
31
32 if (new_state <= priv->state)
33 return;
34
35 switch (new_state) {
36 case CAN_STATE_ERROR_WARNING:
37 priv->can_stats.error_warning++;
38 break;
39 case CAN_STATE_ERROR_PASSIVE:
40 priv->can_stats.error_passive++;
41 break;
42 case CAN_STATE_BUS_OFF:
43 priv->can_stats.bus_off++;
44 break;
45 default:
46 break;
47 }
48 }
49
can_tx_state_to_frame(struct net_device * dev,enum can_state state)50 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
51 {
52 switch (state) {
53 case CAN_STATE_ERROR_ACTIVE:
54 return CAN_ERR_CRTL_ACTIVE;
55 case CAN_STATE_ERROR_WARNING:
56 return CAN_ERR_CRTL_TX_WARNING;
57 case CAN_STATE_ERROR_PASSIVE:
58 return CAN_ERR_CRTL_TX_PASSIVE;
59 default:
60 return 0;
61 }
62 }
63
can_rx_state_to_frame(struct net_device * dev,enum can_state state)64 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
65 {
66 switch (state) {
67 case CAN_STATE_ERROR_ACTIVE:
68 return CAN_ERR_CRTL_ACTIVE;
69 case CAN_STATE_ERROR_WARNING:
70 return CAN_ERR_CRTL_RX_WARNING;
71 case CAN_STATE_ERROR_PASSIVE:
72 return CAN_ERR_CRTL_RX_PASSIVE;
73 default:
74 return 0;
75 }
76 }
77
can_get_state_str(const enum can_state state)78 const char *can_get_state_str(const enum can_state state)
79 {
80 switch (state) {
81 case CAN_STATE_ERROR_ACTIVE:
82 return "Error Active";
83 case CAN_STATE_ERROR_WARNING:
84 return "Error Warning";
85 case CAN_STATE_ERROR_PASSIVE:
86 return "Error Passive";
87 case CAN_STATE_BUS_OFF:
88 return "Bus Off";
89 case CAN_STATE_STOPPED:
90 return "Stopped";
91 case CAN_STATE_SLEEPING:
92 return "Sleeping";
93 default:
94 return "<unknown>";
95 }
96
97 return "<unknown>";
98 }
99 EXPORT_SYMBOL_GPL(can_get_state_str);
100
can_change_state(struct net_device * dev,struct can_frame * cf,enum can_state tx_state,enum can_state rx_state)101 void can_change_state(struct net_device *dev, struct can_frame *cf,
102 enum can_state tx_state, enum can_state rx_state)
103 {
104 struct can_priv *priv = netdev_priv(dev);
105 enum can_state new_state = max(tx_state, rx_state);
106
107 if (unlikely(new_state == priv->state)) {
108 netdev_warn(dev, "%s: oops, state did not change", __func__);
109 return;
110 }
111
112 netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
113 can_get_state_str(priv->state), priv->state,
114 can_get_state_str(new_state), new_state);
115
116 can_update_state_error_stats(dev, new_state);
117 priv->state = new_state;
118
119 if (!cf)
120 return;
121
122 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
123 cf->can_id |= CAN_ERR_BUSOFF;
124 return;
125 }
126
127 cf->can_id |= CAN_ERR_CRTL;
128 cf->data[1] |= tx_state >= rx_state ?
129 can_tx_state_to_frame(dev, tx_state) : 0;
130 cf->data[1] |= tx_state <= rx_state ?
131 can_rx_state_to_frame(dev, rx_state) : 0;
132 }
133 EXPORT_SYMBOL_GPL(can_change_state);
134
135 /* CAN device restart for bus-off recovery */
can_restart(struct net_device * dev)136 static void can_restart(struct net_device *dev)
137 {
138 struct can_priv *priv = netdev_priv(dev);
139 struct net_device_stats *stats = &dev->stats;
140 struct sk_buff *skb;
141 struct can_frame *cf;
142 int err;
143
144 BUG_ON(netif_carrier_ok(dev));
145
146 /* No synchronization needed because the device is bus-off and
147 * no messages can come in or go out.
148 */
149 can_flush_echo_skb(dev);
150
151 /* send restart message upstream */
152 skb = alloc_can_err_skb(dev, &cf);
153 if (!skb)
154 goto restart;
155
156 cf->can_id |= CAN_ERR_RESTARTED;
157
158 stats->rx_packets++;
159 stats->rx_bytes += cf->len;
160
161 netif_rx_ni(skb);
162
163 restart:
164 netdev_dbg(dev, "restarted\n");
165 priv->can_stats.restarts++;
166
167 /* Now restart the device */
168 err = priv->do_set_mode(dev, CAN_MODE_START);
169
170 netif_carrier_on(dev);
171 if (err)
172 netdev_err(dev, "Error %d during restart", err);
173 }
174
can_restart_work(struct work_struct * work)175 static void can_restart_work(struct work_struct *work)
176 {
177 struct delayed_work *dwork = to_delayed_work(work);
178 struct can_priv *priv = container_of(dwork, struct can_priv,
179 restart_work);
180
181 can_restart(priv->dev);
182 }
183
can_restart_now(struct net_device * dev)184 int can_restart_now(struct net_device *dev)
185 {
186 struct can_priv *priv = netdev_priv(dev);
187
188 /* A manual restart is only permitted if automatic restart is
189 * disabled and the device is in the bus-off state
190 */
191 if (priv->restart_ms)
192 return -EINVAL;
193 if (priv->state != CAN_STATE_BUS_OFF)
194 return -EBUSY;
195
196 cancel_delayed_work_sync(&priv->restart_work);
197 can_restart(dev);
198
199 return 0;
200 }
201
202 /* CAN bus-off
203 *
204 * This functions should be called when the device goes bus-off to
205 * tell the netif layer that no more packets can be sent or received.
206 * If enabled, a timer is started to trigger bus-off recovery.
207 */
can_bus_off(struct net_device * dev)208 void can_bus_off(struct net_device *dev)
209 {
210 struct can_priv *priv = netdev_priv(dev);
211
212 if (priv->restart_ms)
213 netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
214 priv->restart_ms);
215 else
216 netdev_info(dev, "bus-off\n");
217
218 netif_carrier_off(dev);
219
220 if (priv->restart_ms)
221 schedule_delayed_work(&priv->restart_work,
222 msecs_to_jiffies(priv->restart_ms));
223 }
224 EXPORT_SYMBOL_GPL(can_bus_off);
225
can_setup(struct net_device * dev)226 void can_setup(struct net_device *dev)
227 {
228 dev->type = ARPHRD_CAN;
229 dev->mtu = CAN_MTU;
230 dev->hard_header_len = 0;
231 dev->addr_len = 0;
232 dev->tx_queue_len = 10;
233
234 /* New-style flags. */
235 dev->flags = IFF_NOARP;
236 dev->features = NETIF_F_HW_CSUM;
237 }
238
239 /* Allocate and setup space for the CAN network device */
alloc_candev_mqs(int sizeof_priv,unsigned int echo_skb_max,unsigned int txqs,unsigned int rxqs)240 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
241 unsigned int txqs, unsigned int rxqs)
242 {
243 struct can_ml_priv *can_ml;
244 struct net_device *dev;
245 struct can_priv *priv;
246 int size;
247
248 /* We put the driver's priv, the CAN mid layer priv and the
249 * echo skb into the netdevice's priv. The memory layout for
250 * the netdev_priv is like this:
251 *
252 * +-------------------------+
253 * | driver's priv |
254 * +-------------------------+
255 * | struct can_ml_priv |
256 * +-------------------------+
257 * | array of struct sk_buff |
258 * +-------------------------+
259 */
260
261 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
262
263 if (echo_skb_max)
264 size = ALIGN(size, sizeof(struct sk_buff *)) +
265 echo_skb_max * sizeof(struct sk_buff *);
266
267 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
268 txqs, rxqs);
269 if (!dev)
270 return NULL;
271
272 priv = netdev_priv(dev);
273 priv->dev = dev;
274
275 can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
276 can_set_ml_priv(dev, can_ml);
277
278 if (echo_skb_max) {
279 priv->echo_skb_max = echo_skb_max;
280 priv->echo_skb = (void *)priv +
281 (size - echo_skb_max * sizeof(struct sk_buff *));
282 }
283
284 priv->state = CAN_STATE_STOPPED;
285
286 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
287
288 return dev;
289 }
290 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
291
292 /* Free space of the CAN network device */
free_candev(struct net_device * dev)293 void free_candev(struct net_device *dev)
294 {
295 free_netdev(dev);
296 }
297 EXPORT_SYMBOL_GPL(free_candev);
298
299 /* changing MTU and control mode for CAN/CANFD devices */
can_change_mtu(struct net_device * dev,int new_mtu)300 int can_change_mtu(struct net_device *dev, int new_mtu)
301 {
302 struct can_priv *priv = netdev_priv(dev);
303
304 /* Do not allow changing the MTU while running */
305 if (dev->flags & IFF_UP)
306 return -EBUSY;
307
308 /* allow change of MTU according to the CANFD ability of the device */
309 switch (new_mtu) {
310 case CAN_MTU:
311 /* 'CANFD-only' controllers can not switch to CAN_MTU */
312 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
313 return -EINVAL;
314
315 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
316 break;
317
318 case CANFD_MTU:
319 /* check for potential CANFD ability */
320 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
321 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
322 return -EINVAL;
323
324 priv->ctrlmode |= CAN_CTRLMODE_FD;
325 break;
326
327 default:
328 return -EINVAL;
329 }
330
331 dev->mtu = new_mtu;
332 return 0;
333 }
334 EXPORT_SYMBOL_GPL(can_change_mtu);
335
336 /* Common open function when the device gets opened.
337 *
338 * This function should be called in the open function of the device
339 * driver.
340 */
open_candev(struct net_device * dev)341 int open_candev(struct net_device *dev)
342 {
343 struct can_priv *priv = netdev_priv(dev);
344
345 if (!priv->bittiming.bitrate) {
346 netdev_err(dev, "bit-timing not yet defined\n");
347 return -EINVAL;
348 }
349
350 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
351 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
352 (!priv->data_bittiming.bitrate ||
353 priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
354 netdev_err(dev, "incorrect/missing data bit-timing\n");
355 return -EINVAL;
356 }
357
358 /* Switch carrier on if device was stopped while in bus-off state */
359 if (!netif_carrier_ok(dev))
360 netif_carrier_on(dev);
361
362 return 0;
363 }
364 EXPORT_SYMBOL_GPL(open_candev);
365
366 #ifdef CONFIG_OF
367 /* Common function that can be used to understand the limitation of
368 * a transceiver when it provides no means to determine these limitations
369 * at runtime.
370 */
of_can_transceiver(struct net_device * dev)371 void of_can_transceiver(struct net_device *dev)
372 {
373 struct device_node *dn;
374 struct can_priv *priv = netdev_priv(dev);
375 struct device_node *np = dev->dev.parent->of_node;
376 int ret;
377
378 dn = of_get_child_by_name(np, "can-transceiver");
379 if (!dn)
380 return;
381
382 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
383 of_node_put(dn);
384 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
385 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
386 }
387 EXPORT_SYMBOL_GPL(of_can_transceiver);
388 #endif
389
390 /* Common close function for cleanup before the device gets closed.
391 *
392 * This function should be called in the close function of the device
393 * driver.
394 */
close_candev(struct net_device * dev)395 void close_candev(struct net_device *dev)
396 {
397 struct can_priv *priv = netdev_priv(dev);
398
399 cancel_delayed_work_sync(&priv->restart_work);
400 can_flush_echo_skb(dev);
401 }
402 EXPORT_SYMBOL_GPL(close_candev);
403
can_set_termination(struct net_device * ndev,u16 term)404 static int can_set_termination(struct net_device *ndev, u16 term)
405 {
406 struct can_priv *priv = netdev_priv(ndev);
407 int set;
408
409 if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
410 set = 1;
411 else
412 set = 0;
413
414 gpiod_set_value(priv->termination_gpio, set);
415
416 return 0;
417 }
418
can_get_termination(struct net_device * ndev)419 static int can_get_termination(struct net_device *ndev)
420 {
421 struct can_priv *priv = netdev_priv(ndev);
422 struct device *dev = ndev->dev.parent;
423 struct gpio_desc *gpio;
424 u32 term;
425 int ret;
426
427 /* Disabling termination by default is the safe choice: Else if many
428 * bus participants enable it, no communication is possible at all.
429 */
430 gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
431 if (IS_ERR(gpio))
432 return dev_err_probe(dev, PTR_ERR(gpio),
433 "Cannot get termination-gpios\n");
434
435 if (!gpio)
436 return 0;
437
438 ret = device_property_read_u32(dev, "termination-ohms", &term);
439 if (ret) {
440 netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
441 ERR_PTR(ret));
442 return ret;
443 }
444
445 if (term > U16_MAX) {
446 netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
447 term, U16_MAX);
448 return -EINVAL;
449 }
450
451 priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
452 priv->termination_const = priv->termination_gpio_ohms;
453 priv->termination_gpio = gpio;
454 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
455 CAN_TERMINATION_DISABLED;
456 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
457 priv->do_set_termination = can_set_termination;
458
459 return 0;
460 }
461
462 /* Register the CAN network device */
register_candev(struct net_device * dev)463 int register_candev(struct net_device *dev)
464 {
465 struct can_priv *priv = netdev_priv(dev);
466 int err;
467
468 /* Ensure termination_const, termination_const_cnt and
469 * do_set_termination consistency. All must be either set or
470 * unset.
471 */
472 if ((!priv->termination_const != !priv->termination_const_cnt) ||
473 (!priv->termination_const != !priv->do_set_termination))
474 return -EINVAL;
475
476 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
477 return -EINVAL;
478
479 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
480 return -EINVAL;
481
482 if (!priv->termination_const) {
483 err = can_get_termination(dev);
484 if (err)
485 return err;
486 }
487
488 dev->rtnl_link_ops = &can_link_ops;
489 netif_carrier_off(dev);
490
491 return register_netdev(dev);
492 }
493 EXPORT_SYMBOL_GPL(register_candev);
494
495 /* Unregister the CAN network device */
unregister_candev(struct net_device * dev)496 void unregister_candev(struct net_device *dev)
497 {
498 unregister_netdev(dev);
499 }
500 EXPORT_SYMBOL_GPL(unregister_candev);
501
502 /* Test if a network device is a candev based device
503 * and return the can_priv* if so.
504 */
safe_candev_priv(struct net_device * dev)505 struct can_priv *safe_candev_priv(struct net_device *dev)
506 {
507 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
508 return NULL;
509
510 return netdev_priv(dev);
511 }
512 EXPORT_SYMBOL_GPL(safe_candev_priv);
513
can_dev_init(void)514 static __init int can_dev_init(void)
515 {
516 int err;
517
518 can_led_notifier_init();
519
520 err = can_netlink_register();
521 if (!err)
522 pr_info(MOD_DESC "\n");
523
524 return err;
525 }
526 module_init(can_dev_init);
527
can_dev_exit(void)528 static __exit void can_dev_exit(void)
529 {
530 can_netlink_unregister();
531
532 can_led_notifier_exit();
533 }
534 module_exit(can_dev_exit);
535
536 MODULE_ALIAS_RTNL_LINK("can");
537