dev.c 20 KB

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