Linux Audio

Check our new training course

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