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 if (netif_carrier_ok(dev))
145 netdev_err(dev, "Attempt to restart for bus-off recovery, but carrier is OK?\n");
146
147 /* No synchronization needed because the device is bus-off and
148 * no messages can come in or go out.
149 */
150 can_flush_echo_skb(dev);
151
152 /* send restart message upstream */
153 skb = alloc_can_err_skb(dev, &cf);
154 if (!skb)
155 goto restart;
156
157 cf->can_id |= CAN_ERR_RESTARTED;
158
159 stats->rx_packets++;
160 stats->rx_bytes += cf->len;
161
162 netif_rx_ni(skb);
163
164 restart:
165 netdev_dbg(dev, "restarted\n");
166 priv->can_stats.restarts++;
167
168 /* Now restart the device */
169 netif_carrier_on(dev);
170 err = priv->do_set_mode(dev, CAN_MODE_START);
171 if (err) {
172 netdev_err(dev, "Error %d during restart", err);
173 netif_carrier_off(dev);
174 }
175 }
176
can_restart_work(struct work_struct * work)177 static void can_restart_work(struct work_struct *work)
178 {
179 struct delayed_work *dwork = to_delayed_work(work);
180 struct can_priv *priv = container_of(dwork, struct can_priv,
181 restart_work);
182
183 can_restart(priv->dev);
184 }
185
can_restart_now(struct net_device * dev)186 int can_restart_now(struct net_device *dev)
187 {
188 struct can_priv *priv = netdev_priv(dev);
189
190 /* A manual restart is only permitted if automatic restart is
191 * disabled and the device is in the bus-off state
192 */
193 if (priv->restart_ms)
194 return -EINVAL;
195 if (priv->state != CAN_STATE_BUS_OFF)
196 return -EBUSY;
197
198 cancel_delayed_work_sync(&priv->restart_work);
199 can_restart(dev);
200
201 return 0;
202 }
203
204 /* CAN bus-off
205 *
206 * This functions should be called when the device goes bus-off to
207 * tell the netif layer that no more packets can be sent or received.
208 * If enabled, a timer is started to trigger bus-off recovery.
209 */
can_bus_off(struct net_device * dev)210 void can_bus_off(struct net_device *dev)
211 {
212 struct can_priv *priv = netdev_priv(dev);
213
214 if (priv->restart_ms)
215 netdev_info(dev, "bus-off, scheduling restart in %d ms\n",
216 priv->restart_ms);
217 else
218 netdev_info(dev, "bus-off\n");
219
220 netif_carrier_off(dev);
221
222 if (priv->restart_ms)
223 schedule_delayed_work(&priv->restart_work,
224 msecs_to_jiffies(priv->restart_ms));
225 }
226 EXPORT_SYMBOL_GPL(can_bus_off);
227
can_setup(struct net_device * dev)228 void can_setup(struct net_device *dev)
229 {
230 dev->type = ARPHRD_CAN;
231 dev->mtu = CAN_MTU;
232 dev->hard_header_len = 0;
233 dev->addr_len = 0;
234 dev->tx_queue_len = 10;
235
236 /* New-style flags. */
237 dev->flags = IFF_NOARP;
238 dev->features = NETIF_F_HW_CSUM;
239 }
240
241 /* 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)242 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
243 unsigned int txqs, unsigned int rxqs)
244 {
245 struct can_ml_priv *can_ml;
246 struct net_device *dev;
247 struct can_priv *priv;
248 int size;
249
250 /* We put the driver's priv, the CAN mid layer priv and the
251 * echo skb into the netdevice's priv. The memory layout for
252 * the netdev_priv is like this:
253 *
254 * +-------------------------+
255 * | driver's priv |
256 * +-------------------------+
257 * | struct can_ml_priv |
258 * +-------------------------+
259 * | array of struct sk_buff |
260 * +-------------------------+
261 */
262
263 size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
264
265 if (echo_skb_max)
266 size = ALIGN(size, sizeof(struct sk_buff *)) +
267 echo_skb_max * sizeof(struct sk_buff *);
268
269 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
270 txqs, rxqs);
271 if (!dev)
272 return NULL;
273
274 priv = netdev_priv(dev);
275 priv->dev = dev;
276
277 can_ml = (void *)priv + ALIGN(sizeof_priv, NETDEV_ALIGN);
278 can_set_ml_priv(dev, can_ml);
279
280 if (echo_skb_max) {
281 priv->echo_skb_max = echo_skb_max;
282 priv->echo_skb = (void *)priv +
283 (size - echo_skb_max * sizeof(struct sk_buff *));
284 }
285
286 priv->state = CAN_STATE_STOPPED;
287
288 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
289
290 return dev;
291 }
292 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
293
294 /* Free space of the CAN network device */
free_candev(struct net_device * dev)295 void free_candev(struct net_device *dev)
296 {
297 free_netdev(dev);
298 }
299 EXPORT_SYMBOL_GPL(free_candev);
300
301 /* changing MTU and control mode for CAN/CANFD devices */
can_change_mtu(struct net_device * dev,int new_mtu)302 int can_change_mtu(struct net_device *dev, int new_mtu)
303 {
304 struct can_priv *priv = netdev_priv(dev);
305
306 /* Do not allow changing the MTU while running */
307 if (dev->flags & IFF_UP)
308 return -EBUSY;
309
310 /* allow change of MTU according to the CANFD ability of the device */
311 switch (new_mtu) {
312 case CAN_MTU:
313 /* 'CANFD-only' controllers can not switch to CAN_MTU */
314 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
315 return -EINVAL;
316
317 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
318 break;
319
320 case CANFD_MTU:
321 /* check for potential CANFD ability */
322 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
323 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
324 return -EINVAL;
325
326 priv->ctrlmode |= CAN_CTRLMODE_FD;
327 break;
328
329 default:
330 return -EINVAL;
331 }
332
333 dev->mtu = new_mtu;
334 return 0;
335 }
336 EXPORT_SYMBOL_GPL(can_change_mtu);
337
338 /* Common open function when the device gets opened.
339 *
340 * This function should be called in the open function of the device
341 * driver.
342 */
open_candev(struct net_device * dev)343 int open_candev(struct net_device *dev)
344 {
345 struct can_priv *priv = netdev_priv(dev);
346
347 if (!priv->bittiming.bitrate) {
348 netdev_err(dev, "bit-timing not yet defined\n");
349 return -EINVAL;
350 }
351
352 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
353 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
354 (!priv->data_bittiming.bitrate ||
355 priv->data_bittiming.bitrate < priv->bittiming.bitrate)) {
356 netdev_err(dev, "incorrect/missing data bit-timing\n");
357 return -EINVAL;
358 }
359
360 /* Switch carrier on if device was stopped while in bus-off state */
361 if (!netif_carrier_ok(dev))
362 netif_carrier_on(dev);
363
364 return 0;
365 }
366 EXPORT_SYMBOL_GPL(open_candev);
367
368 #ifdef CONFIG_OF
369 /* Common function that can be used to understand the limitation of
370 * a transceiver when it provides no means to determine these limitations
371 * at runtime.
372 */
of_can_transceiver(struct net_device * dev)373 void of_can_transceiver(struct net_device *dev)
374 {
375 struct device_node *dn;
376 struct can_priv *priv = netdev_priv(dev);
377 struct device_node *np = dev->dev.parent->of_node;
378 int ret;
379
380 dn = of_get_child_by_name(np, "can-transceiver");
381 if (!dn)
382 return;
383
384 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
385 of_node_put(dn);
386 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
387 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
388 }
389 EXPORT_SYMBOL_GPL(of_can_transceiver);
390 #endif
391
392 /* Common close function for cleanup before the device gets closed.
393 *
394 * This function should be called in the close function of the device
395 * driver.
396 */
close_candev(struct net_device * dev)397 void close_candev(struct net_device *dev)
398 {
399 struct can_priv *priv = netdev_priv(dev);
400
401 cancel_delayed_work_sync(&priv->restart_work);
402 can_flush_echo_skb(dev);
403 }
404 EXPORT_SYMBOL_GPL(close_candev);
405
can_set_termination(struct net_device * ndev,u16 term)406 static int can_set_termination(struct net_device *ndev, u16 term)
407 {
408 struct can_priv *priv = netdev_priv(ndev);
409 int set;
410
411 if (term == priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED])
412 set = 1;
413 else
414 set = 0;
415
416 gpiod_set_value(priv->termination_gpio, set);
417
418 return 0;
419 }
420
can_get_termination(struct net_device * ndev)421 static int can_get_termination(struct net_device *ndev)
422 {
423 struct can_priv *priv = netdev_priv(ndev);
424 struct device *dev = ndev->dev.parent;
425 struct gpio_desc *gpio;
426 u32 term;
427 int ret;
428
429 /* Disabling termination by default is the safe choice: Else if many
430 * bus participants enable it, no communication is possible at all.
431 */
432 gpio = devm_gpiod_get_optional(dev, "termination", GPIOD_OUT_LOW);
433 if (IS_ERR(gpio))
434 return dev_err_probe(dev, PTR_ERR(gpio),
435 "Cannot get termination-gpios\n");
436
437 if (!gpio)
438 return 0;
439
440 ret = device_property_read_u32(dev, "termination-ohms", &term);
441 if (ret) {
442 netdev_err(ndev, "Cannot get termination-ohms: %pe\n",
443 ERR_PTR(ret));
444 return ret;
445 }
446
447 if (term > U16_MAX) {
448 netdev_err(ndev, "Invalid termination-ohms value (%u > %u)\n",
449 term, U16_MAX);
450 return -EINVAL;
451 }
452
453 priv->termination_const_cnt = ARRAY_SIZE(priv->termination_gpio_ohms);
454 priv->termination_const = priv->termination_gpio_ohms;
455 priv->termination_gpio = gpio;
456 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_DISABLED] =
457 CAN_TERMINATION_DISABLED;
458 priv->termination_gpio_ohms[CAN_TERMINATION_GPIO_ENABLED] = term;
459 priv->do_set_termination = can_set_termination;
460
461 return 0;
462 }
463
464 /* Register the CAN network device */
register_candev(struct net_device * dev)465 int register_candev(struct net_device *dev)
466 {
467 struct can_priv *priv = netdev_priv(dev);
468 int err;
469
470 /* Ensure termination_const, termination_const_cnt and
471 * do_set_termination consistency. All must be either set or
472 * unset.
473 */
474 if ((!priv->termination_const != !priv->termination_const_cnt) ||
475 (!priv->termination_const != !priv->do_set_termination))
476 return -EINVAL;
477
478 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
479 return -EINVAL;
480
481 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
482 return -EINVAL;
483
484 if (!priv->termination_const) {
485 err = can_get_termination(dev);
486 if (err)
487 return err;
488 }
489
490 dev->rtnl_link_ops = &can_link_ops;
491 netif_carrier_off(dev);
492
493 return register_netdev(dev);
494 }
495 EXPORT_SYMBOL_GPL(register_candev);
496
497 /* Unregister the CAN network device */
unregister_candev(struct net_device * dev)498 void unregister_candev(struct net_device *dev)
499 {
500 unregister_netdev(dev);
501 }
502 EXPORT_SYMBOL_GPL(unregister_candev);
503
504 /* Test if a network device is a candev based device
505 * and return the can_priv* if so.
506 */
safe_candev_priv(struct net_device * dev)507 struct can_priv *safe_candev_priv(struct net_device *dev)
508 {
509 if (dev->type != ARPHRD_CAN || dev->rtnl_link_ops != &can_link_ops)
510 return NULL;
511
512 return netdev_priv(dev);
513 }
514 EXPORT_SYMBOL_GPL(safe_candev_priv);
515
can_dev_init(void)516 static __init int can_dev_init(void)
517 {
518 int err;
519
520 can_led_notifier_init();
521
522 err = can_netlink_register();
523 if (!err)
524 pr_info(MOD_DESC "\n");
525
526 return err;
527 }
528 module_init(can_dev_init);
529
can_dev_exit(void)530 static __exit void can_dev_exit(void)
531 {
532 can_netlink_unregister();
533
534 can_led_notifier_exit();
535 }
536 module_exit(can_dev_exit);
537
538 MODULE_ALIAS_RTNL_LINK("can");
539