dev.c 21 KB

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