dev.c 21 KB

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