dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/can.h>
  24. #include <linux/can/dev.h>
  25. #include <linux/can/skb.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. const struct can_bittiming_const *btc)
  92. {
  93. struct can_priv *priv = netdev_priv(dev);
  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. /* Use CIA recommended sample points */
  101. if (bt->sample_point) {
  102. sampl_pt = bt->sample_point;
  103. } else {
  104. if (bt->bitrate > 800000)
  105. sampl_pt = 750;
  106. else if (bt->bitrate > 500000)
  107. sampl_pt = 800;
  108. else
  109. sampl_pt = 875;
  110. }
  111. /* tseg even = round down, odd = round up */
  112. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  113. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  114. tsegall = 1 + tseg / 2;
  115. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  116. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  117. /* chose brp step which is possible in system */
  118. brp = (brp / btc->brp_inc) * btc->brp_inc;
  119. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  120. continue;
  121. rate = priv->clock.freq / (brp * tsegall);
  122. error = bt->bitrate - rate;
  123. /* tseg brp biterror */
  124. if (error < 0)
  125. error = -error;
  126. if (error > best_error)
  127. continue;
  128. best_error = error;
  129. if (error == 0) {
  130. spt = can_update_spt(btc, sampl_pt, tseg / 2,
  131. &tseg1, &tseg2);
  132. error = sampl_pt - spt;
  133. if (error < 0)
  134. error = -error;
  135. if (error > spt_error)
  136. continue;
  137. spt_error = error;
  138. }
  139. best_tseg = tseg / 2;
  140. best_brp = brp;
  141. best_rate = rate;
  142. if (error == 0)
  143. break;
  144. }
  145. if (best_error) {
  146. /* Error in one-tenth of a percent */
  147. error = (best_error * 1000) / bt->bitrate;
  148. if (error > CAN_CALC_MAX_ERROR) {
  149. netdev_err(dev,
  150. "bitrate error %ld.%ld%% too high\n",
  151. error / 10, error % 10);
  152. return -EDOM;
  153. } else {
  154. netdev_warn(dev, "bitrate error %ld.%ld%%\n",
  155. error / 10, error % 10);
  156. }
  157. }
  158. /* real sample point */
  159. bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
  160. &tseg1, &tseg2);
  161. v64 = (u64)best_brp * 1000000000UL;
  162. do_div(v64, priv->clock.freq);
  163. bt->tq = (u32)v64;
  164. bt->prop_seg = tseg1 / 2;
  165. bt->phase_seg1 = tseg1 - bt->prop_seg;
  166. bt->phase_seg2 = tseg2;
  167. /* check for sjw user settings */
  168. if (!bt->sjw || !btc->sjw_max)
  169. bt->sjw = 1;
  170. else {
  171. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  172. if (bt->sjw > btc->sjw_max)
  173. bt->sjw = btc->sjw_max;
  174. /* bt->sjw must not be higher than tseg2 */
  175. if (tseg2 < bt->sjw)
  176. bt->sjw = tseg2;
  177. }
  178. bt->brp = best_brp;
  179. /* real bit-rate */
  180. bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
  181. return 0;
  182. }
  183. #else /* !CONFIG_CAN_CALC_BITTIMING */
  184. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  185. const struct can_bittiming_const *btc)
  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. const struct can_bittiming_const *btc)
  199. {
  200. struct can_priv *priv = netdev_priv(dev);
  201. int tseg1, alltseg;
  202. u64 brp64;
  203. tseg1 = bt->prop_seg + bt->phase_seg1;
  204. if (!bt->sjw)
  205. bt->sjw = 1;
  206. if (bt->sjw > btc->sjw_max ||
  207. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  208. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  209. return -ERANGE;
  210. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  211. if (btc->brp_inc > 1)
  212. do_div(brp64, btc->brp_inc);
  213. brp64 += 500000000UL - 1;
  214. do_div(brp64, 1000000000UL); /* the practicable BRP */
  215. if (btc->brp_inc > 1)
  216. brp64 *= btc->brp_inc;
  217. bt->brp = (u32)brp64;
  218. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  219. return -EINVAL;
  220. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  221. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  222. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  223. return 0;
  224. }
  225. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  226. const struct can_bittiming_const *btc)
  227. {
  228. int err;
  229. /* Check if the CAN device has bit-timing parameters */
  230. if (!btc)
  231. return -EOPNOTSUPP;
  232. /*
  233. * Depending on the given can_bittiming parameter structure the CAN
  234. * timing parameters are calculated based on the provided bitrate OR
  235. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  236. * provided directly which are then checked and fixed up.
  237. */
  238. if (!bt->tq && bt->bitrate)
  239. err = can_calc_bittiming(dev, bt, btc);
  240. else if (bt->tq && !bt->bitrate)
  241. err = can_fixup_bittiming(dev, bt, btc);
  242. else
  243. err = -EINVAL;
  244. return err;
  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. (skb->protocol != htons(ETH_P_CAN) &&
  285. skb->protocol != htons(ETH_P_CANFD))) {
  286. kfree_skb(skb);
  287. return;
  288. }
  289. if (!priv->echo_skb[idx]) {
  290. skb = can_create_echo_skb(skb);
  291. if (!skb)
  292. return;
  293. /* make settings for echo to reduce code in irq context */
  294. skb->pkt_type = PACKET_BROADCAST;
  295. skb->ip_summed = CHECKSUM_UNNECESSARY;
  296. skb->dev = dev;
  297. /* save this skb for tx interrupt echo handling */
  298. priv->echo_skb[idx] = skb;
  299. } else {
  300. /* locking problem with netif_stop_queue() ?? */
  301. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  302. kfree_skb(skb);
  303. }
  304. }
  305. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  306. /*
  307. * Get the skb from the stack and loop it back locally
  308. *
  309. * The function is typically called when the TX done interrupt
  310. * is handled in the device driver. The driver must protect
  311. * access to priv->echo_skb, if necessary.
  312. */
  313. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  314. {
  315. struct can_priv *priv = netdev_priv(dev);
  316. BUG_ON(idx >= priv->echo_skb_max);
  317. if (priv->echo_skb[idx]) {
  318. struct sk_buff *skb = priv->echo_skb[idx];
  319. struct can_frame *cf = (struct can_frame *)skb->data;
  320. u8 dlc = cf->can_dlc;
  321. netif_rx(priv->echo_skb[idx]);
  322. priv->echo_skb[idx] = NULL;
  323. return dlc;
  324. }
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  328. /*
  329. * Remove the skb from the stack and free it.
  330. *
  331. * The function is typically called when TX failed.
  332. */
  333. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  334. {
  335. struct can_priv *priv = netdev_priv(dev);
  336. BUG_ON(idx >= priv->echo_skb_max);
  337. if (priv->echo_skb[idx]) {
  338. kfree_skb(priv->echo_skb[idx]);
  339. priv->echo_skb[idx] = NULL;
  340. }
  341. }
  342. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  343. /*
  344. * CAN device restart for bus-off recovery
  345. */
  346. static void can_restart(unsigned long data)
  347. {
  348. struct net_device *dev = (struct net_device *)data;
  349. struct can_priv *priv = netdev_priv(dev);
  350. struct net_device_stats *stats = &dev->stats;
  351. struct sk_buff *skb;
  352. struct can_frame *cf;
  353. int err;
  354. BUG_ON(netif_carrier_ok(dev));
  355. /*
  356. * No synchronization needed because the device is bus-off and
  357. * no messages can come in or go out.
  358. */
  359. can_flush_echo_skb(dev);
  360. /* send restart message upstream */
  361. skb = alloc_can_err_skb(dev, &cf);
  362. if (skb == NULL) {
  363. err = -ENOMEM;
  364. goto restart;
  365. }
  366. cf->can_id |= CAN_ERR_RESTARTED;
  367. netif_rx(skb);
  368. stats->rx_packets++;
  369. stats->rx_bytes += cf->can_dlc;
  370. restart:
  371. netdev_dbg(dev, "restarted\n");
  372. priv->can_stats.restarts++;
  373. /* Now restart the device */
  374. err = priv->do_set_mode(dev, CAN_MODE_START);
  375. netif_carrier_on(dev);
  376. if (err)
  377. netdev_err(dev, "Error %d during restart", err);
  378. }
  379. int can_restart_now(struct net_device *dev)
  380. {
  381. struct can_priv *priv = netdev_priv(dev);
  382. /*
  383. * A manual restart is only permitted if automatic restart is
  384. * disabled and the device is in the bus-off state
  385. */
  386. if (priv->restart_ms)
  387. return -EINVAL;
  388. if (priv->state != CAN_STATE_BUS_OFF)
  389. return -EBUSY;
  390. /* Runs as soon as possible in the timer context */
  391. mod_timer(&priv->restart_timer, jiffies);
  392. return 0;
  393. }
  394. /*
  395. * CAN bus-off
  396. *
  397. * This functions should be called when the device goes bus-off to
  398. * tell the netif layer that no more packets can be sent or received.
  399. * If enabled, a timer is started to trigger bus-off recovery.
  400. */
  401. void can_bus_off(struct net_device *dev)
  402. {
  403. struct can_priv *priv = netdev_priv(dev);
  404. netdev_dbg(dev, "bus-off\n");
  405. netif_carrier_off(dev);
  406. priv->can_stats.bus_off++;
  407. if (priv->restart_ms)
  408. mod_timer(&priv->restart_timer,
  409. jiffies + (priv->restart_ms * HZ) / 1000);
  410. }
  411. EXPORT_SYMBOL_GPL(can_bus_off);
  412. static void can_setup(struct net_device *dev)
  413. {
  414. dev->type = ARPHRD_CAN;
  415. dev->mtu = CAN_MTU;
  416. dev->hard_header_len = 0;
  417. dev->addr_len = 0;
  418. dev->tx_queue_len = 10;
  419. /* New-style flags. */
  420. dev->flags = IFF_NOARP;
  421. dev->features = NETIF_F_HW_CSUM;
  422. }
  423. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  424. {
  425. struct sk_buff *skb;
  426. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  427. sizeof(struct can_frame));
  428. if (unlikely(!skb))
  429. return NULL;
  430. skb->protocol = htons(ETH_P_CAN);
  431. skb->pkt_type = PACKET_BROADCAST;
  432. skb->ip_summed = CHECKSUM_UNNECESSARY;
  433. can_skb_reserve(skb);
  434. can_skb_prv(skb)->ifindex = dev->ifindex;
  435. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  436. memset(*cf, 0, sizeof(struct can_frame));
  437. return skb;
  438. }
  439. EXPORT_SYMBOL_GPL(alloc_can_skb);
  440. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  441. struct canfd_frame **cfd)
  442. {
  443. struct sk_buff *skb;
  444. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  445. sizeof(struct canfd_frame));
  446. if (unlikely(!skb))
  447. return NULL;
  448. skb->protocol = htons(ETH_P_CANFD);
  449. skb->pkt_type = PACKET_BROADCAST;
  450. skb->ip_summed = CHECKSUM_UNNECESSARY;
  451. can_skb_reserve(skb);
  452. can_skb_prv(skb)->ifindex = dev->ifindex;
  453. *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
  454. memset(*cfd, 0, sizeof(struct canfd_frame));
  455. return skb;
  456. }
  457. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  458. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  459. {
  460. struct sk_buff *skb;
  461. skb = alloc_can_skb(dev, cf);
  462. if (unlikely(!skb))
  463. return NULL;
  464. (*cf)->can_id = CAN_ERR_FLAG;
  465. (*cf)->can_dlc = CAN_ERR_DLC;
  466. return skb;
  467. }
  468. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  469. /*
  470. * Allocate and setup space for the CAN network device
  471. */
  472. struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
  473. {
  474. struct net_device *dev;
  475. struct can_priv *priv;
  476. int size;
  477. if (echo_skb_max)
  478. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  479. echo_skb_max * sizeof(struct sk_buff *);
  480. else
  481. size = sizeof_priv;
  482. dev = alloc_netdev(size, "can%d", can_setup);
  483. if (!dev)
  484. return NULL;
  485. priv = netdev_priv(dev);
  486. if (echo_skb_max) {
  487. priv->echo_skb_max = echo_skb_max;
  488. priv->echo_skb = (void *)priv +
  489. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  490. }
  491. priv->state = CAN_STATE_STOPPED;
  492. init_timer(&priv->restart_timer);
  493. return dev;
  494. }
  495. EXPORT_SYMBOL_GPL(alloc_candev);
  496. /*
  497. * Free space of the CAN network device
  498. */
  499. void free_candev(struct net_device *dev)
  500. {
  501. free_netdev(dev);
  502. }
  503. EXPORT_SYMBOL_GPL(free_candev);
  504. /*
  505. * changing MTU and control mode for CAN/CANFD devices
  506. */
  507. int can_change_mtu(struct net_device *dev, int new_mtu)
  508. {
  509. struct can_priv *priv = netdev_priv(dev);
  510. /* Do not allow changing the MTU while running */
  511. if (dev->flags & IFF_UP)
  512. return -EBUSY;
  513. /* allow change of MTU according to the CANFD ability of the device */
  514. switch (new_mtu) {
  515. case CAN_MTU:
  516. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  517. break;
  518. case CANFD_MTU:
  519. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD))
  520. return -EINVAL;
  521. priv->ctrlmode |= CAN_CTRLMODE_FD;
  522. break;
  523. default:
  524. return -EINVAL;
  525. }
  526. dev->mtu = new_mtu;
  527. return 0;
  528. }
  529. EXPORT_SYMBOL_GPL(can_change_mtu);
  530. /*
  531. * Common open function when the device gets opened.
  532. *
  533. * This function should be called in the open function of the device
  534. * driver.
  535. */
  536. int open_candev(struct net_device *dev)
  537. {
  538. struct can_priv *priv = netdev_priv(dev);
  539. if (!priv->bittiming.bitrate) {
  540. netdev_err(dev, "bit-timing not yet defined\n");
  541. return -EINVAL;
  542. }
  543. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  544. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  545. (!priv->data_bittiming.bitrate ||
  546. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  547. netdev_err(dev, "incorrect/missing data bit-timing\n");
  548. return -EINVAL;
  549. }
  550. /* Switch carrier on if device was stopped while in bus-off state */
  551. if (!netif_carrier_ok(dev))
  552. netif_carrier_on(dev);
  553. setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
  554. return 0;
  555. }
  556. EXPORT_SYMBOL_GPL(open_candev);
  557. /*
  558. * Common close function for cleanup before the device gets closed.
  559. *
  560. * This function should be called in the close function of the device
  561. * driver.
  562. */
  563. void close_candev(struct net_device *dev)
  564. {
  565. struct can_priv *priv = netdev_priv(dev);
  566. del_timer_sync(&priv->restart_timer);
  567. can_flush_echo_skb(dev);
  568. }
  569. EXPORT_SYMBOL_GPL(close_candev);
  570. /*
  571. * CAN netlink interface
  572. */
  573. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  574. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  575. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  576. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  577. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  578. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  579. [IFLA_CAN_BITTIMING_CONST]
  580. = { .len = sizeof(struct can_bittiming_const) },
  581. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  582. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  583. [IFLA_CAN_DATA_BITTIMING]
  584. = { .len = sizeof(struct can_bittiming) },
  585. [IFLA_CAN_DATA_BITTIMING_CONST]
  586. = { .len = sizeof(struct can_bittiming_const) },
  587. };
  588. static int can_changelink(struct net_device *dev,
  589. struct nlattr *tb[], struct nlattr *data[])
  590. {
  591. struct can_priv *priv = netdev_priv(dev);
  592. int err;
  593. /* We need synchronization with dev->stop() */
  594. ASSERT_RTNL();
  595. if (data[IFLA_CAN_BITTIMING]) {
  596. struct can_bittiming bt;
  597. /* Do not allow changing bittiming while running */
  598. if (dev->flags & IFF_UP)
  599. return -EBUSY;
  600. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  601. err = can_get_bittiming(dev, &bt, priv->bittiming_const);
  602. if (err)
  603. return err;
  604. memcpy(&priv->bittiming, &bt, sizeof(bt));
  605. if (priv->do_set_bittiming) {
  606. /* Finally, set the bit-timing registers */
  607. err = priv->do_set_bittiming(dev);
  608. if (err)
  609. return err;
  610. }
  611. }
  612. if (data[IFLA_CAN_CTRLMODE]) {
  613. struct can_ctrlmode *cm;
  614. /* Do not allow changing controller mode while running */
  615. if (dev->flags & IFF_UP)
  616. return -EBUSY;
  617. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  618. if (cm->flags & ~priv->ctrlmode_supported)
  619. return -EOPNOTSUPP;
  620. priv->ctrlmode &= ~cm->mask;
  621. priv->ctrlmode |= cm->flags;
  622. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  623. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  624. dev->mtu = CANFD_MTU;
  625. else
  626. dev->mtu = CAN_MTU;
  627. }
  628. if (data[IFLA_CAN_RESTART_MS]) {
  629. /* Do not allow changing restart delay while running */
  630. if (dev->flags & IFF_UP)
  631. return -EBUSY;
  632. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  633. }
  634. if (data[IFLA_CAN_RESTART]) {
  635. /* Do not allow a restart while not running */
  636. if (!(dev->flags & IFF_UP))
  637. return -EINVAL;
  638. err = can_restart_now(dev);
  639. if (err)
  640. return err;
  641. }
  642. if (data[IFLA_CAN_DATA_BITTIMING]) {
  643. struct can_bittiming dbt;
  644. /* Do not allow changing bittiming while running */
  645. if (dev->flags & IFF_UP)
  646. return -EBUSY;
  647. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  648. sizeof(dbt));
  649. err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const);
  650. if (err)
  651. return err;
  652. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  653. if (priv->do_set_data_bittiming) {
  654. /* Finally, set the bit-timing registers */
  655. err = priv->do_set_data_bittiming(dev);
  656. if (err)
  657. return err;
  658. }
  659. }
  660. return 0;
  661. }
  662. static size_t can_get_size(const struct net_device *dev)
  663. {
  664. struct can_priv *priv = netdev_priv(dev);
  665. size_t size = 0;
  666. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  667. size += nla_total_size(sizeof(struct can_bittiming));
  668. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  669. size += nla_total_size(sizeof(struct can_bittiming_const));
  670. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  671. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  672. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  673. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  674. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  675. size += nla_total_size(sizeof(struct can_berr_counter));
  676. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  677. size += nla_total_size(sizeof(struct can_bittiming));
  678. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  679. size += nla_total_size(sizeof(struct can_bittiming_const));
  680. return size;
  681. }
  682. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  683. {
  684. struct can_priv *priv = netdev_priv(dev);
  685. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  686. struct can_berr_counter bec;
  687. enum can_state state = priv->state;
  688. if (priv->do_get_state)
  689. priv->do_get_state(dev, &state);
  690. if ((priv->bittiming.bitrate &&
  691. nla_put(skb, IFLA_CAN_BITTIMING,
  692. sizeof(priv->bittiming), &priv->bittiming)) ||
  693. (priv->bittiming_const &&
  694. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  695. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  696. nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
  697. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  698. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  699. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  700. (priv->do_get_berr_counter &&
  701. !priv->do_get_berr_counter(dev, &bec) &&
  702. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  703. (priv->data_bittiming.bitrate &&
  704. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  705. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  706. (priv->data_bittiming_const &&
  707. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  708. sizeof(*priv->data_bittiming_const),
  709. priv->data_bittiming_const)))
  710. return -EMSGSIZE;
  711. return 0;
  712. }
  713. static size_t can_get_xstats_size(const struct net_device *dev)
  714. {
  715. return sizeof(struct can_device_stats);
  716. }
  717. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  718. {
  719. struct can_priv *priv = netdev_priv(dev);
  720. if (nla_put(skb, IFLA_INFO_XSTATS,
  721. sizeof(priv->can_stats), &priv->can_stats))
  722. goto nla_put_failure;
  723. return 0;
  724. nla_put_failure:
  725. return -EMSGSIZE;
  726. }
  727. static int can_newlink(struct net *src_net, struct net_device *dev,
  728. struct nlattr *tb[], struct nlattr *data[])
  729. {
  730. return -EOPNOTSUPP;
  731. }
  732. static struct rtnl_link_ops can_link_ops __read_mostly = {
  733. .kind = "can",
  734. .maxtype = IFLA_CAN_MAX,
  735. .policy = can_policy,
  736. .setup = can_setup,
  737. .newlink = can_newlink,
  738. .changelink = can_changelink,
  739. .get_size = can_get_size,
  740. .fill_info = can_fill_info,
  741. .get_xstats_size = can_get_xstats_size,
  742. .fill_xstats = can_fill_xstats,
  743. };
  744. /*
  745. * Register the CAN network device
  746. */
  747. int register_candev(struct net_device *dev)
  748. {
  749. dev->rtnl_link_ops = &can_link_ops;
  750. return register_netdev(dev);
  751. }
  752. EXPORT_SYMBOL_GPL(register_candev);
  753. /*
  754. * Unregister the CAN network device
  755. */
  756. void unregister_candev(struct net_device *dev)
  757. {
  758. unregister_netdev(dev);
  759. }
  760. EXPORT_SYMBOL_GPL(unregister_candev);
  761. /*
  762. * Test if a network device is a candev based device
  763. * and return the can_priv* if so.
  764. */
  765. struct can_priv *safe_candev_priv(struct net_device *dev)
  766. {
  767. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  768. return NULL;
  769. return netdev_priv(dev);
  770. }
  771. EXPORT_SYMBOL_GPL(safe_candev_priv);
  772. static __init int can_dev_init(void)
  773. {
  774. int err;
  775. can_led_notifier_init();
  776. err = rtnl_link_register(&can_link_ops);
  777. if (!err)
  778. printk(KERN_INFO MOD_DESC "\n");
  779. return err;
  780. }
  781. module_init(can_dev_init);
  782. static __exit void can_dev_exit(void)
  783. {
  784. rtnl_link_unregister(&can_link_ops);
  785. can_led_notifier_exit();
  786. }
  787. module_exit(can_dev_exit);
  788. MODULE_ALIAS_RTNL_LINK("can");