can.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * lib/route/link/can.c CAN Link Info
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2012 Benedikt Spranger <b.spranger@linutronix.de>
  10. */
  11. /**
  12. * @ingroup link
  13. * @defgroup can CAN
  14. * Controller Area Network link module
  15. *
  16. * @details
  17. * \b Link Type Name: "can"
  18. *
  19. * @route_doc{link_can, CAN Documentation}
  20. *
  21. * @{
  22. */
  23. #include <netlink-private/netlink.h>
  24. #include <netlink/netlink.h>
  25. #include <netlink/attr.h>
  26. #include <netlink/utils.h>
  27. #include <netlink/object.h>
  28. #include <netlink/route/rtnl.h>
  29. #include <netlink-private/route/link/api.h>
  30. #include <netlink/route/link/can.h>
  31. #include <linux/can/netlink.h>
  32. /** @cond SKIP */
  33. #define CAN_HAS_BITTIMING (1<<0)
  34. #define CAN_HAS_BITTIMING_CONST (1<<1)
  35. #define CAN_HAS_CLOCK (1<<2)
  36. #define CAN_HAS_STATE (1<<3)
  37. #define CAN_HAS_CTRLMODE (1<<4)
  38. #define CAN_HAS_RESTART_MS (1<<5)
  39. #define CAN_HAS_RESTART (1<<6)
  40. #define CAN_HAS_BERR_COUNTER (1<<7)
  41. struct can_info {
  42. uint32_t ci_state;
  43. uint32_t ci_restart;
  44. uint32_t ci_restart_ms;
  45. struct can_ctrlmode ci_ctrlmode;
  46. struct can_bittiming ci_bittiming;
  47. struct can_bittiming_const ci_bittiming_const;
  48. struct can_clock ci_clock;
  49. struct can_berr_counter ci_berr_counter;
  50. uint32_t ci_mask;
  51. };
  52. /** @endcond */
  53. static struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  54. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  55. [IFLA_CAN_CTRLMODE] = { .minlen = sizeof(struct can_ctrlmode) },
  56. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  57. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  58. [IFLA_CAN_BITTIMING] = { .minlen = sizeof(struct can_bittiming) },
  59. [IFLA_CAN_BITTIMING_CONST]
  60. = { .minlen = sizeof(struct can_bittiming_const) },
  61. [IFLA_CAN_CLOCK] = { .minlen = sizeof(struct can_clock) },
  62. [IFLA_CAN_BERR_COUNTER] = { .minlen = sizeof(struct can_berr_counter) },
  63. };
  64. static int can_alloc(struct rtnl_link *link)
  65. {
  66. struct can_info *ci;
  67. ci = calloc(1, sizeof(*ci));
  68. if (!ci)
  69. return -NLE_NOMEM;
  70. link->l_info = ci;
  71. return 0;
  72. }
  73. static int can_parse(struct rtnl_link *link, struct nlattr *data,
  74. struct nlattr *xstats)
  75. {
  76. struct nlattr *tb[IFLA_CAN_MAX+1];
  77. struct can_info *ci;
  78. int err;
  79. NL_DBG(3, "Parsing CAN link info");
  80. if ((err = nla_parse_nested(tb, IFLA_CAN_MAX, data, can_policy)) < 0)
  81. goto errout;
  82. if ((err = can_alloc(link)) < 0)
  83. goto errout;
  84. ci = link->l_info;
  85. if (tb[IFLA_CAN_STATE]) {
  86. ci->ci_state = nla_get_u32(tb[IFLA_CAN_STATE]);
  87. ci->ci_mask |= CAN_HAS_STATE;
  88. }
  89. if (tb[IFLA_CAN_RESTART]) {
  90. ci->ci_restart = nla_get_u32(tb[IFLA_CAN_RESTART]);
  91. ci->ci_mask |= CAN_HAS_RESTART;
  92. }
  93. if (tb[IFLA_CAN_RESTART_MS]) {
  94. ci->ci_restart_ms = nla_get_u32(tb[IFLA_CAN_RESTART_MS]);
  95. ci->ci_mask |= CAN_HAS_RESTART_MS;
  96. }
  97. if (tb[IFLA_CAN_CTRLMODE]) {
  98. nla_memcpy(&ci->ci_ctrlmode, tb[IFLA_CAN_CTRLMODE],
  99. sizeof(ci->ci_ctrlmode));
  100. ci->ci_mask |= CAN_HAS_CTRLMODE;
  101. }
  102. if (tb[IFLA_CAN_BITTIMING]) {
  103. nla_memcpy(&ci->ci_bittiming, tb[IFLA_CAN_BITTIMING],
  104. sizeof(ci->ci_bittiming));
  105. ci->ci_mask |= CAN_HAS_BITTIMING;
  106. }
  107. if (tb[IFLA_CAN_BITTIMING_CONST]) {
  108. nla_memcpy(&ci->ci_bittiming_const,
  109. tb[IFLA_CAN_BITTIMING_CONST],
  110. sizeof(ci->ci_bittiming_const));
  111. ci->ci_mask |= CAN_HAS_BITTIMING_CONST;
  112. }
  113. if (tb[IFLA_CAN_CLOCK]) {
  114. nla_memcpy(&ci->ci_clock, tb[IFLA_CAN_CLOCK],
  115. sizeof(ci->ci_clock));
  116. ci->ci_mask |= CAN_HAS_CLOCK;
  117. }
  118. if (tb[IFLA_CAN_BERR_COUNTER]) {
  119. nla_memcpy(&ci->ci_berr_counter, tb[IFLA_CAN_BERR_COUNTER],
  120. sizeof(ci->ci_berr_counter));
  121. ci->ci_mask |= CAN_HAS_BERR_COUNTER;
  122. }
  123. err = 0;
  124. errout:
  125. return err;
  126. }
  127. static void can_free(struct rtnl_link *link)
  128. {
  129. struct can_info *ci = link->l_info;
  130. free(ci);
  131. link->l_info = NULL;
  132. }
  133. static char *print_can_state (uint32_t state)
  134. {
  135. char *text;
  136. switch (state)
  137. {
  138. case CAN_STATE_ERROR_ACTIVE:
  139. text = "error active";
  140. break;
  141. case CAN_STATE_ERROR_WARNING:
  142. text = "error warning";
  143. break;
  144. case CAN_STATE_ERROR_PASSIVE:
  145. text = "error passive";
  146. break;
  147. case CAN_STATE_BUS_OFF:
  148. text = "bus off";
  149. break;
  150. case CAN_STATE_STOPPED:
  151. text = "stopped";
  152. break;
  153. case CAN_STATE_SLEEPING:
  154. text = "sleeping";
  155. break;
  156. default:
  157. text = "unknown state";
  158. }
  159. return text;
  160. }
  161. static void can_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
  162. {
  163. struct can_info *ci = link->l_info;
  164. char buf [64];
  165. rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf, sizeof(buf));
  166. nl_dump(p, "bitrate %d %s <%s>",
  167. ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
  168. }
  169. static void can_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
  170. {
  171. struct can_info *ci = link->l_info;
  172. char buf [64];
  173. rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf, sizeof(buf));
  174. nl_dump(p, " bitrate %d %s <%s>",
  175. ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
  176. if (ci->ci_mask & CAN_HAS_RESTART) {
  177. if (ci->ci_restart)
  178. nl_dump_line(p," restarting\n");
  179. }
  180. if (ci->ci_mask & CAN_HAS_RESTART_MS) {
  181. nl_dump_line(p," restart interval %d ms\n",
  182. ci->ci_restart_ms);
  183. }
  184. if (ci->ci_mask & CAN_HAS_BITTIMING) {
  185. nl_dump_line(p," sample point %f %%\n",
  186. ((float) ci->ci_bittiming.sample_point)/10);
  187. nl_dump_line(p," time quanta %d ns\n",
  188. ci->ci_bittiming.tq);
  189. nl_dump_line(p," propagation segment %d tq\n",
  190. ci->ci_bittiming.prop_seg);
  191. nl_dump_line(p," phase buffer segment1 %d tq\n",
  192. ci->ci_bittiming.phase_seg1);
  193. nl_dump_line(p," phase buffer segment2 %d tq\n",
  194. ci->ci_bittiming.phase_seg2);
  195. nl_dump_line(p," synchronisation jump width %d tq\n",
  196. ci->ci_bittiming.sjw);
  197. nl_dump_line(p," bitrate prescaler %d\n",
  198. ci->ci_bittiming.brp);
  199. }
  200. if (ci->ci_mask & CAN_HAS_BITTIMING_CONST) {
  201. nl_dump_line(p," minimum tsig1 %d tq\n",
  202. ci->ci_bittiming_const.tseg1_min);
  203. nl_dump_line(p," maximum tsig1 %d tq\n",
  204. ci->ci_bittiming_const.tseg1_max);
  205. nl_dump_line(p," minimum tsig2 %d tq\n",
  206. ci->ci_bittiming_const.tseg2_min);
  207. nl_dump_line(p," maximum tsig2 %d tq\n",
  208. ci->ci_bittiming_const.tseg2_max);
  209. nl_dump_line(p," maximum sjw %d tq\n",
  210. ci->ci_bittiming_const.sjw_max);
  211. nl_dump_line(p," minimum brp %d\n",
  212. ci->ci_bittiming_const.brp_min);
  213. nl_dump_line(p," maximum brp %d\n",
  214. ci->ci_bittiming_const.brp_max);
  215. nl_dump_line(p," brp increment %d\n",
  216. ci->ci_bittiming_const.brp_inc);
  217. }
  218. if (ci->ci_mask & CAN_HAS_CLOCK) {
  219. nl_dump_line(p," base freq %d Hz\n", ci->ci_clock);
  220. }
  221. if (ci->ci_mask & CAN_HAS_BERR_COUNTER) {
  222. nl_dump_line(p," bus error RX %d\n",
  223. ci->ci_berr_counter.rxerr);
  224. nl_dump_line(p," bus error TX %d\n",
  225. ci->ci_berr_counter.txerr);
  226. }
  227. return;
  228. }
  229. static int can_clone(struct rtnl_link *dst, struct rtnl_link *src)
  230. {
  231. struct can_info *cdst, *csrc = src->l_info;
  232. int ret;
  233. dst->l_info = NULL;
  234. ret = rtnl_link_set_type(dst, "can");
  235. if (ret < 0)
  236. return ret;
  237. cdst = malloc(sizeof(*cdst));
  238. if (!cdst)
  239. return -NLE_NOMEM;
  240. *cdst = *csrc;
  241. dst->l_info = cdst;
  242. return 0;
  243. }
  244. static int can_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
  245. {
  246. struct can_info *ci = link->l_info;
  247. struct nlattr *data;
  248. data = nla_nest_start(msg, IFLA_INFO_DATA);
  249. if (!data)
  250. return -NLE_MSGSIZE;
  251. if (ci->ci_mask & CAN_HAS_RESTART)
  252. NLA_PUT_U32(msg, CAN_HAS_RESTART, ci->ci_restart);
  253. if (ci->ci_mask & CAN_HAS_RESTART_MS)
  254. NLA_PUT_U32(msg, CAN_HAS_RESTART_MS, ci->ci_restart_ms);
  255. if (ci->ci_mask & CAN_HAS_CTRLMODE)
  256. NLA_PUT(msg, CAN_HAS_CTRLMODE, sizeof(ci->ci_ctrlmode),
  257. &ci->ci_ctrlmode);
  258. if (ci->ci_mask & CAN_HAS_BITTIMING)
  259. NLA_PUT(msg, CAN_HAS_BITTIMING, sizeof(ci->ci_bittiming),
  260. &ci->ci_bittiming);
  261. if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
  262. NLA_PUT(msg, CAN_HAS_BITTIMING_CONST,
  263. sizeof(ci->ci_bittiming_const),
  264. &ci->ci_bittiming_const);
  265. if (ci->ci_mask & CAN_HAS_CLOCK)
  266. NLA_PUT(msg, CAN_HAS_CLOCK, sizeof(ci->ci_clock),
  267. &ci->ci_clock);
  268. nla_nest_end(msg, data);
  269. nla_put_failure:
  270. return 0;
  271. }
  272. static struct rtnl_link_info_ops can_info_ops = {
  273. .io_name = "can",
  274. .io_alloc = can_alloc,
  275. .io_parse = can_parse,
  276. .io_dump = {
  277. [NL_DUMP_LINE] = can_dump_line,
  278. [NL_DUMP_DETAILS] = can_dump_details,
  279. },
  280. .io_clone = can_clone,
  281. .io_put_attrs = can_put_attrs,
  282. .io_free = can_free,
  283. };
  284. /** @cond SKIP */
  285. #define IS_CAN_LINK_ASSERT(link) \
  286. if ((link)->l_info_ops != &can_info_ops) { \
  287. APPBUG("Link is not a CAN link. set type \"can\" first."); \
  288. return -NLE_OPNOTSUPP; \
  289. }
  290. /** @endcond */
  291. /**
  292. * @name CAN Object
  293. * @{
  294. */
  295. /**
  296. * Check if link is a CAN link
  297. * @arg link Link object
  298. *
  299. * @return True if link is a CAN link, otherwise false is returned.
  300. */
  301. int rtnl_link_is_can(struct rtnl_link *link)
  302. {
  303. return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "can");
  304. }
  305. /**
  306. * Restart CAN device
  307. * @arg link Link object
  308. *
  309. * @return 0 on success or a negative error code
  310. */
  311. int rtnl_link_can_restart(struct rtnl_link *link)
  312. {
  313. struct can_info *ci = link->l_info;
  314. IS_CAN_LINK_ASSERT(link);
  315. ci->ci_restart = 1;
  316. ci->ci_restart |= CAN_HAS_RESTART;
  317. return 0;
  318. }
  319. /**
  320. * Get CAN base frequency
  321. * @arg link Link object
  322. * @arg freq frequency in Hz
  323. *
  324. * @return 0 on success or a negative error code
  325. */
  326. int rtnl_link_can_freq(struct rtnl_link *link, uint32_t *freq)
  327. {
  328. struct can_info *ci = link->l_info;
  329. IS_CAN_LINK_ASSERT(link);
  330. if (!freq)
  331. return -NLE_INVAL;
  332. if (ci->ci_mask & CAN_HAS_CLOCK)
  333. *freq = ci->ci_clock.freq;
  334. else
  335. return -NLE_AGAIN;
  336. return 0;
  337. }
  338. /**
  339. * Get CAN state
  340. * @arg link Link object
  341. * @arg state CAN bus state
  342. * @return 0 on success or a negative error code
  343. */
  344. int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state)
  345. {
  346. struct can_info *ci = link->l_info;
  347. IS_CAN_LINK_ASSERT(link);
  348. if (!state)
  349. return -NLE_INVAL;
  350. *state = ci->ci_state;
  351. return 0;
  352. }
  353. /**
  354. * Get CAN RX bus error count
  355. * @arg link Link object
  356. *
  357. * @return RX bus error count on success or a negative error code
  358. */
  359. int rtnl_link_can_berr_rx(struct rtnl_link *link)
  360. {
  361. struct can_info *ci = link->l_info;
  362. IS_CAN_LINK_ASSERT(link);
  363. if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
  364. return ci->ci_berr_counter.rxerr;
  365. else
  366. return -NLE_AGAIN;
  367. }
  368. /**
  369. * Get CAN TX bus error count
  370. * @arg link Link object
  371. *
  372. * @return TX bus error count on success or a negative error code
  373. */
  374. int rtnl_link_can_berr_tx(struct rtnl_link *link)
  375. {
  376. struct can_info *ci = link->l_info;
  377. IS_CAN_LINK_ASSERT(link);
  378. if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
  379. return ci->ci_berr_counter.txerr;
  380. else
  381. return -NLE_AGAIN;
  382. }
  383. /**
  384. * Get CAN bus error count
  385. * @arg link Link object
  386. * @arg berr Bus error count
  387. *
  388. * @return 0 on success or a negative error code
  389. */
  390. int rtnl_link_can_berr(struct rtnl_link *link, struct can_berr_counter *berr)
  391. {
  392. struct can_info *ci = link->l_info;
  393. IS_CAN_LINK_ASSERT(link);
  394. if (!berr)
  395. return -NLE_INVAL;
  396. if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
  397. *berr = ci->ci_berr_counter;
  398. else
  399. return -NLE_AGAIN;
  400. return 0;
  401. }
  402. /**
  403. * Get CAN harware-dependent bit-timing constant
  404. * @arg link Link object
  405. * @arg bt_const Bit-timing constant
  406. *
  407. * @return 0 on success or a negative error code
  408. */
  409. int rtnl_link_can_get_bt_const(struct rtnl_link *link,
  410. struct can_bittiming_const *bt_const)
  411. {
  412. struct can_info *ci = link->l_info;
  413. IS_CAN_LINK_ASSERT(link);
  414. if (!bt_const)
  415. return -NLE_INVAL;
  416. if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
  417. *bt_const = ci->ci_bittiming_const;
  418. else
  419. return -NLE_AGAIN;
  420. return 0;
  421. }
  422. /**
  423. * Get CAN device bit-timing
  424. * @arg link Link object
  425. * @arg bit_timing CAN bit-timing
  426. *
  427. * @return 0 on success or a negative error code
  428. */
  429. int rtnl_link_can_get_bittiming(struct rtnl_link *link,
  430. struct can_bittiming *bit_timing)
  431. {
  432. struct can_info *ci = link->l_info;
  433. IS_CAN_LINK_ASSERT(link);
  434. if (!bit_timing)
  435. return -NLE_INVAL;
  436. if (ci->ci_mask & CAN_HAS_BITTIMING)
  437. *bit_timing = ci->ci_bittiming;
  438. else
  439. return -NLE_AGAIN;
  440. return 0;
  441. }
  442. /**
  443. * Set CAN device bit-timing
  444. * @arg link Link object
  445. * @arg bit_timing CAN bit-timing
  446. *
  447. * @return 0 on success or a negative error code
  448. */
  449. int rtnl_link_can_set_bittiming(struct rtnl_link *link,
  450. struct can_bittiming *bit_timing)
  451. {
  452. struct can_info *ci = link->l_info;
  453. IS_CAN_LINK_ASSERT(link);
  454. if (!bit_timing)
  455. return -NLE_INVAL;
  456. ci->ci_bittiming = *bit_timing;
  457. ci->ci_mask |= CAN_HAS_BITTIMING;
  458. return 0;
  459. }
  460. /**
  461. * Get CAN device bit-timing
  462. * @arg link Link object
  463. * @arg bitrate CAN bitrate
  464. *
  465. * @return 0 on success or a negative error code
  466. */
  467. int rtnl_link_can_get_bitrate(struct rtnl_link *link, uint32_t *bitrate)
  468. {
  469. struct can_info *ci = link->l_info;
  470. IS_CAN_LINK_ASSERT(link);
  471. if (!bitrate)
  472. return -NLE_INVAL;
  473. if (ci->ci_mask & CAN_HAS_BITTIMING)
  474. *bitrate = ci->ci_bittiming.bitrate;
  475. else
  476. return -NLE_AGAIN;
  477. return 0;
  478. }
  479. /**
  480. * Set CAN device bit-rate
  481. * @arg link Link object
  482. * @arg bitrate CAN bitrate
  483. *
  484. * @return 0 on success or a negative error code
  485. */
  486. int rtnl_link_can_set_bitrate(struct rtnl_link *link, uint32_t bitrate)
  487. {
  488. struct can_info *ci = link->l_info;
  489. IS_CAN_LINK_ASSERT(link);
  490. ci->ci_bittiming.bitrate = bitrate;
  491. ci->ci_mask |= CAN_HAS_BITTIMING;
  492. return 0;
  493. }
  494. /**
  495. * Get CAN device sample point
  496. * @arg link Link object
  497. * @arg sp CAN sample point
  498. *
  499. * @return 0 on success or a negative error code
  500. */
  501. int rtnl_link_can_get_sample_point(struct rtnl_link *link, uint32_t *sp)
  502. {
  503. struct can_info *ci = link->l_info;
  504. IS_CAN_LINK_ASSERT(link);
  505. if (!sp)
  506. return -NLE_INVAL;
  507. if (ci->ci_mask & CAN_HAS_BITTIMING)
  508. *sp = ci->ci_bittiming.sample_point;
  509. else
  510. return -NLE_AGAIN;
  511. return 0;
  512. }
  513. /**
  514. * Set CAN device sample point
  515. * @arg link Link object
  516. * @arg sp CAN sample point
  517. *
  518. * @return 0 on success or a negative error code
  519. */
  520. int rtnl_link_can_set_sample_point(struct rtnl_link *link, uint32_t sp)
  521. {
  522. struct can_info *ci = link->l_info;
  523. IS_CAN_LINK_ASSERT(link);
  524. ci->ci_bittiming.sample_point = sp;
  525. ci->ci_mask |= CAN_HAS_BITTIMING;
  526. return 0;
  527. }
  528. /**
  529. * Get CAN device restart intervall
  530. * @arg link Link object
  531. * @arg interval Restart intervall in ms
  532. *
  533. * @return 0 on success or a negative error code
  534. */
  535. int rtnl_link_can_get_restart_ms(struct rtnl_link *link, uint32_t *interval)
  536. {
  537. struct can_info *ci = link->l_info;
  538. IS_CAN_LINK_ASSERT(link);
  539. if (!interval)
  540. return -NLE_INVAL;
  541. if (ci->ci_mask & CAN_HAS_RESTART_MS)
  542. *interval = ci->ci_restart_ms;
  543. else
  544. return -NLE_AGAIN;
  545. return 0;
  546. }
  547. /**
  548. * Set CAN device restart intervall
  549. * @arg link Link object
  550. * @arg interval Restart intervall in ms
  551. *
  552. * @return 0 on success or a negative error code
  553. */
  554. int rtnl_link_can_set_restart_ms(struct rtnl_link *link, uint32_t interval)
  555. {
  556. struct can_info *ci = link->l_info;
  557. IS_CAN_LINK_ASSERT(link);
  558. ci->ci_restart_ms = interval;
  559. ci->ci_mask |= CAN_HAS_RESTART_MS;
  560. return 0;
  561. }
  562. /**
  563. * Get CAN control mode
  564. * @arg link Link object
  565. * @arg ctrlmode CAN control mode
  566. *
  567. * @return 0 on success or a negative error code
  568. */
  569. int rtnl_link_can_get_ctrlmode(struct rtnl_link *link, uint32_t *ctrlmode)
  570. {
  571. struct can_info *ci = link->l_info;
  572. IS_CAN_LINK_ASSERT(link);
  573. if (!ctrlmode)
  574. return -NLE_INVAL;
  575. if (ci->ci_mask & CAN_HAS_CTRLMODE)
  576. *ctrlmode = ci->ci_ctrlmode.flags;
  577. else
  578. return -NLE_AGAIN;
  579. return 0;
  580. }
  581. /**
  582. * Set a CAN Control Mode
  583. * @arg link Link object
  584. * @arg ctrlmode CAN control mode
  585. *
  586. * @return 0 on success or a negative error code
  587. */
  588. int rtnl_link_can_set_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
  589. {
  590. struct can_info *ci = link->l_info;
  591. IS_CAN_LINK_ASSERT(link);
  592. ci->ci_ctrlmode.flags |= ctrlmode;
  593. ci->ci_ctrlmode.mask |= ctrlmode;
  594. ci->ci_mask |= CAN_HAS_CTRLMODE;
  595. return 0;
  596. }
  597. /**
  598. * Unset a CAN Control Mode
  599. * @arg link Link object
  600. * @arg ctrlmode CAN control mode
  601. *
  602. * @return 0 on success or a negative error code
  603. */
  604. int rtnl_link_can_unset_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
  605. {
  606. struct can_info *ci = link->l_info;
  607. IS_CAN_LINK_ASSERT(link);
  608. ci->ci_ctrlmode.flags &= ~ctrlmode;
  609. ci->ci_ctrlmode.mask |= ctrlmode;
  610. ci->ci_mask |= CAN_HAS_CTRLMODE;
  611. return 0;
  612. }
  613. /** @} */
  614. /**
  615. * @name Control Mode Translation
  616. * @{
  617. */
  618. static const struct trans_tbl can_ctrlmode[] = {
  619. __ADD(CAN_CTRLMODE_LOOPBACK, loopback)
  620. __ADD(CAN_CTRLMODE_LISTENONLY, listen-only)
  621. __ADD(CAN_CTRLMODE_3_SAMPLES, triple-sampling)
  622. __ADD(CAN_CTRLMODE_ONE_SHOT, one-shot)
  623. __ADD(CAN_CTRLMODE_BERR_REPORTING, berr-reporting)
  624. };
  625. char *rtnl_link_can_ctrlmode2str(int ctrlmode, char *buf, size_t len)
  626. {
  627. return __flags2str(ctrlmode, buf, len, can_ctrlmode,
  628. ARRAY_SIZE(can_ctrlmode));
  629. }
  630. int rtnl_link_can_str2ctrlmode(const char *name)
  631. {
  632. return __str2flags(name, can_ctrlmode, ARRAY_SIZE(can_ctrlmode));
  633. }
  634. /** @} */
  635. static void __init can_init(void)
  636. {
  637. rtnl_link_register_info(&can_info_ops);
  638. }
  639. static void __exit can_exit(void)
  640. {
  641. rtnl_link_unregister_info(&can_info_ops);
  642. }
  643. /** @} */