dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/can.h>
  24. #include <linux/can/dev.h>
  25. #include <linux/can/netlink.h>
  26. #include <net/rtnetlink.h>
  27. #define MOD_DESC "CAN device driver interface"
  28. MODULE_DESCRIPTION(MOD_DESC);
  29. MODULE_LICENSE("GPL v2");
  30. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  31. #ifdef CONFIG_CAN_CALC_BITTIMING
  32. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  33. /*
  34. * Bit-timing calculation derived from:
  35. *
  36. * Code based on LinCAN sources and H8S2638 project
  37. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  38. * Copyright 2005 Stanislav Marek
  39. * email: pisa@cmp.felk.cvut.cz
  40. *
  41. * Calculates proper bit-timing parameters for a specified bit-rate
  42. * and sample-point, which can then be used to set the bit-timing
  43. * registers of the CAN controller. You can find more information
  44. * in the header file linux/can/netlink.h.
  45. */
  46. static int can_update_spt(const struct can_bittiming_const *btc,
  47. int sampl_pt, int tseg, int *tseg1, int *tseg2)
  48. {
  49. *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
  50. if (*tseg2 < btc->tseg2_min)
  51. *tseg2 = btc->tseg2_min;
  52. if (*tseg2 > btc->tseg2_max)
  53. *tseg2 = btc->tseg2_max;
  54. *tseg1 = tseg - *tseg2;
  55. if (*tseg1 > btc->tseg1_max) {
  56. *tseg1 = btc->tseg1_max;
  57. *tseg2 = tseg - *tseg1;
  58. }
  59. return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
  60. }
  61. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
  62. {
  63. struct can_priv *priv = netdev_priv(dev);
  64. const struct can_bittiming_const *btc = priv->bittiming_const;
  65. long rate, best_rate = 0;
  66. long best_error = 1000000000, error = 0;
  67. int best_tseg = 0, best_brp = 0, brp = 0;
  68. int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
  69. int spt_error = 1000, spt = 0, sampl_pt;
  70. u64 v64;
  71. if (!priv->bittiming_const)
  72. return -ENOTSUPP;
  73. /* Use CIA recommended sample points */
  74. if (bt->sample_point) {
  75. sampl_pt = bt->sample_point;
  76. } else {
  77. if (bt->bitrate > 800000)
  78. sampl_pt = 750;
  79. else if (bt->bitrate > 500000)
  80. sampl_pt = 800;
  81. else
  82. sampl_pt = 875;
  83. }
  84. /* tseg even = round down, odd = round up */
  85. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  86. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  87. tsegall = 1 + tseg / 2;
  88. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  89. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  90. /* chose brp step which is possible in system */
  91. brp = (brp / btc->brp_inc) * btc->brp_inc;
  92. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  93. continue;
  94. rate = priv->clock.freq / (brp * tsegall);
  95. error = bt->bitrate - rate;
  96. /* tseg brp biterror */
  97. if (error < 0)
  98. error = -error;
  99. if (error > best_error)
  100. continue;
  101. best_error = error;
  102. if (error == 0) {
  103. spt = can_update_spt(btc, sampl_pt, tseg / 2,
  104. &tseg1, &tseg2);
  105. error = sampl_pt - spt;
  106. if (error < 0)
  107. error = -error;
  108. if (error > spt_error)
  109. continue;
  110. spt_error = error;
  111. }
  112. best_tseg = tseg / 2;
  113. best_brp = brp;
  114. best_rate = rate;
  115. if (error == 0)
  116. break;
  117. }
  118. if (best_error) {
  119. /* Error in one-tenth of a percent */
  120. error = (best_error * 1000) / bt->bitrate;
  121. if (error > CAN_CALC_MAX_ERROR) {
  122. dev_err(dev->dev.parent,
  123. "bitrate error %ld.%ld%% too high\n",
  124. error / 10, error % 10);
  125. return -EDOM;
  126. } else {
  127. dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
  128. error / 10, error % 10);
  129. }
  130. }
  131. /* real sample point */
  132. bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
  133. &tseg1, &tseg2);
  134. v64 = (u64)best_brp * 1000000000UL;
  135. do_div(v64, priv->clock.freq);
  136. bt->tq = (u32)v64;
  137. bt->prop_seg = tseg1 / 2;
  138. bt->phase_seg1 = tseg1 - bt->prop_seg;
  139. bt->phase_seg2 = tseg2;
  140. bt->sjw = 1;
  141. bt->brp = best_brp;
  142. /* real bit-rate */
  143. bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
  144. return 0;
  145. }
  146. #else /* !CONFIG_CAN_CALC_BITTIMING */
  147. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
  148. {
  149. dev_err(dev->dev.parent, "bit-timing calculation not available\n");
  150. return -EINVAL;
  151. }
  152. #endif /* CONFIG_CAN_CALC_BITTIMING */
  153. /*
  154. * Checks the validity of the specified bit-timing parameters prop_seg,
  155. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  156. * prescaler value brp. You can find more information in the header
  157. * file linux/can/netlink.h.
  158. */
  159. static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
  160. {
  161. struct can_priv *priv = netdev_priv(dev);
  162. const struct can_bittiming_const *btc = priv->bittiming_const;
  163. int tseg1, alltseg;
  164. u64 brp64;
  165. if (!priv->bittiming_const)
  166. return -ENOTSUPP;
  167. tseg1 = bt->prop_seg + bt->phase_seg1;
  168. if (!bt->sjw)
  169. bt->sjw = 1;
  170. if (bt->sjw > btc->sjw_max ||
  171. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  172. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  173. return -ERANGE;
  174. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  175. if (btc->brp_inc > 1)
  176. do_div(brp64, btc->brp_inc);
  177. brp64 += 500000000UL - 1;
  178. do_div(brp64, 1000000000UL); /* the practicable BRP */
  179. if (btc->brp_inc > 1)
  180. brp64 *= btc->brp_inc;
  181. bt->brp = (u32)brp64;
  182. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  183. return -EINVAL;
  184. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  185. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  186. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  187. return 0;
  188. }
  189. int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
  190. {
  191. struct can_priv *priv = netdev_priv(dev);
  192. int err;
  193. /* Check if the CAN device has bit-timing parameters */
  194. if (priv->bittiming_const) {
  195. /* Non-expert mode? Check if the bitrate has been pre-defined */
  196. if (!bt->tq)
  197. /* Determine bit-timing parameters */
  198. err = can_calc_bittiming(dev, bt);
  199. else
  200. /* Check bit-timing params and calculate proper brp */
  201. err = can_fixup_bittiming(dev, bt);
  202. if (err)
  203. return err;
  204. }
  205. return 0;
  206. }
  207. /*
  208. * Local echo of CAN messages
  209. *
  210. * CAN network devices *should* support a local echo functionality
  211. * (see Documentation/networking/can.txt). To test the handling of CAN
  212. * interfaces that do not support the local echo both driver types are
  213. * implemented. In the case that the driver does not support the echo
  214. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  215. * to perform the echo as a fallback solution.
  216. */
  217. static void can_flush_echo_skb(struct net_device *dev)
  218. {
  219. struct can_priv *priv = netdev_priv(dev);
  220. struct net_device_stats *stats = &dev->stats;
  221. int i;
  222. for (i = 0; i < priv->echo_skb_max; i++) {
  223. if (priv->echo_skb[i]) {
  224. kfree_skb(priv->echo_skb[i]);
  225. priv->echo_skb[i] = NULL;
  226. stats->tx_dropped++;
  227. stats->tx_aborted_errors++;
  228. }
  229. }
  230. }
  231. /*
  232. * Put the skb on the stack to be looped backed locally lateron
  233. *
  234. * The function is typically called in the start_xmit function
  235. * of the device driver. The driver must protect access to
  236. * priv->echo_skb, if necessary.
  237. */
  238. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  239. unsigned int idx)
  240. {
  241. struct can_priv *priv = netdev_priv(dev);
  242. BUG_ON(idx >= priv->echo_skb_max);
  243. /* check flag whether this packet has to be looped back */
  244. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
  245. kfree_skb(skb);
  246. return;
  247. }
  248. if (!priv->echo_skb[idx]) {
  249. struct sock *srcsk = skb->sk;
  250. if (atomic_read(&skb->users) != 1) {
  251. struct sk_buff *old_skb = skb;
  252. skb = skb_clone(old_skb, GFP_ATOMIC);
  253. kfree_skb(old_skb);
  254. if (!skb)
  255. return;
  256. } else
  257. skb_orphan(skb);
  258. skb->sk = srcsk;
  259. /* make settings for echo to reduce code in irq context */
  260. skb->protocol = htons(ETH_P_CAN);
  261. skb->pkt_type = PACKET_BROADCAST;
  262. skb->ip_summed = CHECKSUM_UNNECESSARY;
  263. skb->dev = dev;
  264. /* save this skb for tx interrupt echo handling */
  265. priv->echo_skb[idx] = skb;
  266. } else {
  267. /* locking problem with netif_stop_queue() ?? */
  268. dev_err(dev->dev.parent, "%s: BUG! echo_skb is occupied!\n",
  269. __func__);
  270. kfree_skb(skb);
  271. }
  272. }
  273. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  274. /*
  275. * Get the skb from the stack and loop it back locally
  276. *
  277. * The function is typically called when the TX done interrupt
  278. * is handled in the device driver. The driver must protect
  279. * access to priv->echo_skb, if necessary.
  280. */
  281. void can_get_echo_skb(struct net_device *dev, unsigned int idx)
  282. {
  283. struct can_priv *priv = netdev_priv(dev);
  284. BUG_ON(idx >= priv->echo_skb_max);
  285. if (priv->echo_skb[idx]) {
  286. netif_rx(priv->echo_skb[idx]);
  287. priv->echo_skb[idx] = NULL;
  288. }
  289. }
  290. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  291. /*
  292. * Remove the skb from the stack and free it.
  293. *
  294. * The function is typically called when TX failed.
  295. */
  296. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  297. {
  298. struct can_priv *priv = netdev_priv(dev);
  299. BUG_ON(idx >= priv->echo_skb_max);
  300. if (priv->echo_skb[idx]) {
  301. kfree_skb(priv->echo_skb[idx]);
  302. priv->echo_skb[idx] = NULL;
  303. }
  304. }
  305. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  306. /*
  307. * CAN device restart for bus-off recovery
  308. */
  309. void can_restart(unsigned long data)
  310. {
  311. struct net_device *dev = (struct net_device *)data;
  312. struct can_priv *priv = netdev_priv(dev);
  313. struct net_device_stats *stats = &dev->stats;
  314. struct sk_buff *skb;
  315. struct can_frame *cf;
  316. int err;
  317. BUG_ON(netif_carrier_ok(dev));
  318. /*
  319. * No synchronization needed because the device is bus-off and
  320. * no messages can come in or go out.
  321. */
  322. can_flush_echo_skb(dev);
  323. /* send restart message upstream */
  324. skb = alloc_can_err_skb(dev, &cf);
  325. if (skb == NULL) {
  326. err = -ENOMEM;
  327. goto restart;
  328. }
  329. cf->can_id |= CAN_ERR_RESTARTED;
  330. netif_rx(skb);
  331. stats->rx_packets++;
  332. stats->rx_bytes += cf->can_dlc;
  333. restart:
  334. dev_dbg(dev->dev.parent, "restarted\n");
  335. priv->can_stats.restarts++;
  336. /* Now restart the device */
  337. err = priv->do_set_mode(dev, CAN_MODE_START);
  338. netif_carrier_on(dev);
  339. if (err)
  340. dev_err(dev->dev.parent, "Error %d during restart", err);
  341. }
  342. int can_restart_now(struct net_device *dev)
  343. {
  344. struct can_priv *priv = netdev_priv(dev);
  345. /*
  346. * A manual restart is only permitted if automatic restart is
  347. * disabled and the device is in the bus-off state
  348. */
  349. if (priv->restart_ms)
  350. return -EINVAL;
  351. if (priv->state != CAN_STATE_BUS_OFF)
  352. return -EBUSY;
  353. /* Runs as soon as possible in the timer context */
  354. mod_timer(&priv->restart_timer, jiffies);
  355. return 0;
  356. }
  357. /*
  358. * CAN bus-off
  359. *
  360. * This functions should be called when the device goes bus-off to
  361. * tell the netif layer that no more packets can be sent or received.
  362. * If enabled, a timer is started to trigger bus-off recovery.
  363. */
  364. void can_bus_off(struct net_device *dev)
  365. {
  366. struct can_priv *priv = netdev_priv(dev);
  367. dev_dbg(dev->dev.parent, "bus-off\n");
  368. netif_carrier_off(dev);
  369. priv->can_stats.bus_off++;
  370. if (priv->restart_ms)
  371. mod_timer(&priv->restart_timer,
  372. jiffies + (priv->restart_ms * HZ) / 1000);
  373. }
  374. EXPORT_SYMBOL_GPL(can_bus_off);
  375. static void can_setup(struct net_device *dev)
  376. {
  377. dev->type = ARPHRD_CAN;
  378. dev->mtu = sizeof(struct can_frame);
  379. dev->hard_header_len = 0;
  380. dev->addr_len = 0;
  381. dev->tx_queue_len = 10;
  382. /* New-style flags. */
  383. dev->flags = IFF_NOARP;
  384. dev->features = NETIF_F_NO_CSUM;
  385. }
  386. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  387. {
  388. struct sk_buff *skb;
  389. skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
  390. if (unlikely(!skb))
  391. return NULL;
  392. skb->protocol = htons(ETH_P_CAN);
  393. skb->pkt_type = PACKET_BROADCAST;
  394. skb->ip_summed = CHECKSUM_UNNECESSARY;
  395. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  396. memset(*cf, 0, sizeof(struct can_frame));
  397. return skb;
  398. }
  399. EXPORT_SYMBOL_GPL(alloc_can_skb);
  400. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  401. {
  402. struct sk_buff *skb;
  403. skb = alloc_can_skb(dev, cf);
  404. if (unlikely(!skb))
  405. return NULL;
  406. (*cf)->can_id = CAN_ERR_FLAG;
  407. (*cf)->can_dlc = CAN_ERR_DLC;
  408. return skb;
  409. }
  410. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  411. /*
  412. * Allocate and setup space for the CAN network device
  413. */
  414. struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
  415. {
  416. struct net_device *dev;
  417. struct can_priv *priv;
  418. int size;
  419. if (echo_skb_max)
  420. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  421. echo_skb_max * sizeof(struct sk_buff *);
  422. else
  423. size = sizeof_priv;
  424. dev = alloc_netdev(size, "can%d", can_setup);
  425. if (!dev)
  426. return NULL;
  427. priv = netdev_priv(dev);
  428. if (echo_skb_max) {
  429. priv->echo_skb_max = echo_skb_max;
  430. priv->echo_skb = (void *)priv +
  431. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  432. }
  433. priv->state = CAN_STATE_STOPPED;
  434. init_timer(&priv->restart_timer);
  435. return dev;
  436. }
  437. EXPORT_SYMBOL_GPL(alloc_candev);
  438. /*
  439. * Free space of the CAN network device
  440. */
  441. void free_candev(struct net_device *dev)
  442. {
  443. free_netdev(dev);
  444. }
  445. EXPORT_SYMBOL_GPL(free_candev);
  446. /*
  447. * Common open function when the device gets opened.
  448. *
  449. * This function should be called in the open function of the device
  450. * driver.
  451. */
  452. int open_candev(struct net_device *dev)
  453. {
  454. struct can_priv *priv = netdev_priv(dev);
  455. if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
  456. dev_err(dev->dev.parent, "bit-timing not yet defined\n");
  457. return -EINVAL;
  458. }
  459. /* Switch carrier on if device was stopped while in bus-off state */
  460. if (!netif_carrier_ok(dev))
  461. netif_carrier_on(dev);
  462. setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
  463. return 0;
  464. }
  465. EXPORT_SYMBOL_GPL(open_candev);
  466. /*
  467. * Common close function for cleanup before the device gets closed.
  468. *
  469. * This function should be called in the close function of the device
  470. * driver.
  471. */
  472. void close_candev(struct net_device *dev)
  473. {
  474. struct can_priv *priv = netdev_priv(dev);
  475. if (del_timer_sync(&priv->restart_timer))
  476. dev_put(dev);
  477. can_flush_echo_skb(dev);
  478. }
  479. EXPORT_SYMBOL_GPL(close_candev);
  480. /*
  481. * CAN netlink interface
  482. */
  483. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  484. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  485. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  486. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  487. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  488. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  489. [IFLA_CAN_BITTIMING_CONST]
  490. = { .len = sizeof(struct can_bittiming_const) },
  491. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  492. };
  493. static int can_changelink(struct net_device *dev,
  494. struct nlattr *tb[], struct nlattr *data[])
  495. {
  496. struct can_priv *priv = netdev_priv(dev);
  497. int err;
  498. /* We need synchronization with dev->stop() */
  499. ASSERT_RTNL();
  500. if (data[IFLA_CAN_CTRLMODE]) {
  501. struct can_ctrlmode *cm;
  502. /* Do not allow changing controller mode while running */
  503. if (dev->flags & IFF_UP)
  504. return -EBUSY;
  505. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  506. priv->ctrlmode &= ~cm->mask;
  507. priv->ctrlmode |= cm->flags;
  508. }
  509. if (data[IFLA_CAN_BITTIMING]) {
  510. struct can_bittiming bt;
  511. /* Do not allow changing bittiming while running */
  512. if (dev->flags & IFF_UP)
  513. return -EBUSY;
  514. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  515. if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
  516. return -EINVAL;
  517. err = can_get_bittiming(dev, &bt);
  518. if (err)
  519. return err;
  520. memcpy(&priv->bittiming, &bt, sizeof(bt));
  521. if (priv->do_set_bittiming) {
  522. /* Finally, set the bit-timing registers */
  523. err = priv->do_set_bittiming(dev);
  524. if (err)
  525. return err;
  526. }
  527. }
  528. if (data[IFLA_CAN_RESTART_MS]) {
  529. /* Do not allow changing restart delay while running */
  530. if (dev->flags & IFF_UP)
  531. return -EBUSY;
  532. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  533. }
  534. if (data[IFLA_CAN_RESTART]) {
  535. /* Do not allow a restart while not running */
  536. if (!(dev->flags & IFF_UP))
  537. return -EINVAL;
  538. err = can_restart_now(dev);
  539. if (err)
  540. return err;
  541. }
  542. return 0;
  543. }
  544. static size_t can_get_size(const struct net_device *dev)
  545. {
  546. struct can_priv *priv = netdev_priv(dev);
  547. size_t size;
  548. size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  549. size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */
  550. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  551. size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
  552. size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */
  553. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  554. size += sizeof(struct can_bittiming_const);
  555. return size;
  556. }
  557. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  558. {
  559. struct can_priv *priv = netdev_priv(dev);
  560. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  561. enum can_state state = priv->state;
  562. if (priv->do_get_state)
  563. priv->do_get_state(dev, &state);
  564. NLA_PUT_U32(skb, IFLA_CAN_STATE, state);
  565. NLA_PUT(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
  566. NLA_PUT_U32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms);
  567. NLA_PUT(skb, IFLA_CAN_BITTIMING,
  568. sizeof(priv->bittiming), &priv->bittiming);
  569. NLA_PUT(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock);
  570. if (priv->bittiming_const)
  571. NLA_PUT(skb, IFLA_CAN_BITTIMING_CONST,
  572. sizeof(*priv->bittiming_const), priv->bittiming_const);
  573. return 0;
  574. nla_put_failure:
  575. return -EMSGSIZE;
  576. }
  577. static size_t can_get_xstats_size(const struct net_device *dev)
  578. {
  579. return sizeof(struct can_device_stats);
  580. }
  581. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  582. {
  583. struct can_priv *priv = netdev_priv(dev);
  584. NLA_PUT(skb, IFLA_INFO_XSTATS,
  585. sizeof(priv->can_stats), &priv->can_stats);
  586. return 0;
  587. nla_put_failure:
  588. return -EMSGSIZE;
  589. }
  590. static int can_newlink(struct net *src_net, struct net_device *dev,
  591. struct nlattr *tb[], struct nlattr *data[])
  592. {
  593. return -EOPNOTSUPP;
  594. }
  595. static struct rtnl_link_ops can_link_ops __read_mostly = {
  596. .kind = "can",
  597. .maxtype = IFLA_CAN_MAX,
  598. .policy = can_policy,
  599. .setup = can_setup,
  600. .newlink = can_newlink,
  601. .changelink = can_changelink,
  602. .get_size = can_get_size,
  603. .fill_info = can_fill_info,
  604. .get_xstats_size = can_get_xstats_size,
  605. .fill_xstats = can_fill_xstats,
  606. };
  607. /*
  608. * Register the CAN network device
  609. */
  610. int register_candev(struct net_device *dev)
  611. {
  612. dev->rtnl_link_ops = &can_link_ops;
  613. return register_netdev(dev);
  614. }
  615. EXPORT_SYMBOL_GPL(register_candev);
  616. /*
  617. * Unregister the CAN network device
  618. */
  619. void unregister_candev(struct net_device *dev)
  620. {
  621. unregister_netdev(dev);
  622. }
  623. EXPORT_SYMBOL_GPL(unregister_candev);
  624. static __init int can_dev_init(void)
  625. {
  626. int err;
  627. err = rtnl_link_register(&can_link_ops);
  628. if (!err)
  629. printk(KERN_INFO MOD_DESC "\n");
  630. return err;
  631. }
  632. module_init(can_dev_init);
  633. static __exit void can_dev_exit(void)
  634. {
  635. rtnl_link_unregister(&can_link_ops);
  636. }
  637. module_exit(can_dev_exit);
  638. MODULE_ALIAS_RTNL_LINK("can");