dev.c 21 KB

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