dev.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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/workqueue.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 <linux/of.h>
  30. #include <net/rtnetlink.h>
  31. #define MOD_DESC "CAN device driver interface"
  32. MODULE_DESCRIPTION(MOD_DESC);
  33. MODULE_LICENSE("GPL v2");
  34. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  35. /* CAN DLC to real data length conversion helpers */
  36. static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
  37. 8, 12, 16, 20, 24, 32, 48, 64};
  38. /* get data length from can_dlc with sanitized can_dlc */
  39. u8 can_dlc2len(u8 can_dlc)
  40. {
  41. return dlc2len[can_dlc & 0x0F];
  42. }
  43. EXPORT_SYMBOL_GPL(can_dlc2len);
  44. static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
  45. 9, 9, 9, 9, /* 9 - 12 */
  46. 10, 10, 10, 10, /* 13 - 16 */
  47. 11, 11, 11, 11, /* 17 - 20 */
  48. 12, 12, 12, 12, /* 21 - 24 */
  49. 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
  50. 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
  51. 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
  52. 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
  53. 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
  54. /* map the sanitized data length to an appropriate data length code */
  55. u8 can_len2dlc(u8 len)
  56. {
  57. if (unlikely(len > 64))
  58. return 0xF;
  59. return len2dlc[len];
  60. }
  61. EXPORT_SYMBOL_GPL(can_len2dlc);
  62. #ifdef CONFIG_CAN_CALC_BITTIMING
  63. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  64. #define CAN_CALC_SYNC_SEG 1
  65. /*
  66. * Bit-timing calculation derived from:
  67. *
  68. * Code based on LinCAN sources and H8S2638 project
  69. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  70. * Copyright 2005 Stanislav Marek
  71. * email: pisa@cmp.felk.cvut.cz
  72. *
  73. * Calculates proper bit-timing parameters for a specified bit-rate
  74. * and sample-point, which can then be used to set the bit-timing
  75. * registers of the CAN controller. You can find more information
  76. * in the header file linux/can/netlink.h.
  77. */
  78. static int can_update_sample_point(const struct can_bittiming_const *btc,
  79. unsigned int sample_point_nominal, unsigned int tseg,
  80. unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
  81. unsigned int *sample_point_error_ptr)
  82. {
  83. unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
  84. unsigned int sample_point, best_sample_point = 0;
  85. unsigned int tseg1, tseg2;
  86. int i;
  87. for (i = 0; i <= 1; i++) {
  88. tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
  89. tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
  90. tseg1 = tseg - tseg2;
  91. if (tseg1 > btc->tseg1_max) {
  92. tseg1 = btc->tseg1_max;
  93. tseg2 = tseg - tseg1;
  94. }
  95. sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
  96. sample_point_error = abs(sample_point_nominal - sample_point);
  97. if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
  98. best_sample_point = sample_point;
  99. best_sample_point_error = sample_point_error;
  100. *tseg1_ptr = tseg1;
  101. *tseg2_ptr = tseg2;
  102. }
  103. }
  104. if (sample_point_error_ptr)
  105. *sample_point_error_ptr = best_sample_point_error;
  106. return best_sample_point;
  107. }
  108. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  109. const struct can_bittiming_const *btc)
  110. {
  111. struct can_priv *priv = netdev_priv(dev);
  112. unsigned int bitrate; /* current bitrate */
  113. unsigned int bitrate_error; /* difference between current and nominal value */
  114. unsigned int best_bitrate_error = UINT_MAX;
  115. unsigned int sample_point_error; /* difference between current and nominal value */
  116. unsigned int best_sample_point_error = UINT_MAX;
  117. unsigned int sample_point_nominal; /* nominal sample point */
  118. unsigned int best_tseg = 0; /* current best value for tseg */
  119. unsigned int best_brp = 0; /* current best value for brp */
  120. unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
  121. u64 v64;
  122. /* Use CiA recommended sample points */
  123. if (bt->sample_point) {
  124. sample_point_nominal = bt->sample_point;
  125. } else {
  126. if (bt->bitrate > 800000)
  127. sample_point_nominal = 750;
  128. else if (bt->bitrate > 500000)
  129. sample_point_nominal = 800;
  130. else
  131. sample_point_nominal = 875;
  132. }
  133. /* tseg even = round down, odd = round up */
  134. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  135. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  136. tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
  137. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  138. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  139. /* choose brp step which is possible in system */
  140. brp = (brp / btc->brp_inc) * btc->brp_inc;
  141. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  142. continue;
  143. bitrate = priv->clock.freq / (brp * tsegall);
  144. bitrate_error = abs(bt->bitrate - bitrate);
  145. /* tseg brp biterror */
  146. if (bitrate_error > best_bitrate_error)
  147. continue;
  148. /* reset sample point error if we have a better bitrate */
  149. if (bitrate_error < best_bitrate_error)
  150. best_sample_point_error = UINT_MAX;
  151. can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
  152. if (sample_point_error > best_sample_point_error)
  153. continue;
  154. best_sample_point_error = sample_point_error;
  155. best_bitrate_error = bitrate_error;
  156. best_tseg = tseg / 2;
  157. best_brp = brp;
  158. if (bitrate_error == 0 && sample_point_error == 0)
  159. break;
  160. }
  161. if (best_bitrate_error) {
  162. /* Error in one-tenth of a percent */
  163. v64 = (u64)best_bitrate_error * 1000;
  164. do_div(v64, bt->bitrate);
  165. bitrate_error = (u32)v64;
  166. if (bitrate_error > CAN_CALC_MAX_ERROR) {
  167. netdev_err(dev,
  168. "bitrate error %d.%d%% too high\n",
  169. bitrate_error / 10, bitrate_error % 10);
  170. return -EDOM;
  171. }
  172. netdev_warn(dev, "bitrate error %d.%d%%\n",
  173. bitrate_error / 10, bitrate_error % 10);
  174. }
  175. /* real sample point */
  176. bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
  177. &tseg1, &tseg2, NULL);
  178. v64 = (u64)best_brp * 1000 * 1000 * 1000;
  179. do_div(v64, priv->clock.freq);
  180. bt->tq = (u32)v64;
  181. bt->prop_seg = tseg1 / 2;
  182. bt->phase_seg1 = tseg1 - bt->prop_seg;
  183. bt->phase_seg2 = tseg2;
  184. /* check for sjw user settings */
  185. if (!bt->sjw || !btc->sjw_max) {
  186. bt->sjw = 1;
  187. } else {
  188. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  189. if (bt->sjw > btc->sjw_max)
  190. bt->sjw = btc->sjw_max;
  191. /* bt->sjw must not be higher than tseg2 */
  192. if (tseg2 < bt->sjw)
  193. bt->sjw = tseg2;
  194. }
  195. bt->brp = best_brp;
  196. /* real bitrate */
  197. bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
  198. return 0;
  199. }
  200. #else /* !CONFIG_CAN_CALC_BITTIMING */
  201. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  202. const struct can_bittiming_const *btc)
  203. {
  204. netdev_err(dev, "bit-timing calculation not available\n");
  205. return -EINVAL;
  206. }
  207. #endif /* CONFIG_CAN_CALC_BITTIMING */
  208. /*
  209. * Checks the validity of the specified bit-timing parameters prop_seg,
  210. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  211. * prescaler value brp. You can find more information in the header
  212. * file linux/can/netlink.h.
  213. */
  214. static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
  215. const struct can_bittiming_const *btc)
  216. {
  217. struct can_priv *priv = netdev_priv(dev);
  218. int tseg1, alltseg;
  219. u64 brp64;
  220. tseg1 = bt->prop_seg + bt->phase_seg1;
  221. if (!bt->sjw)
  222. bt->sjw = 1;
  223. if (bt->sjw > btc->sjw_max ||
  224. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  225. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  226. return -ERANGE;
  227. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  228. if (btc->brp_inc > 1)
  229. do_div(brp64, btc->brp_inc);
  230. brp64 += 500000000UL - 1;
  231. do_div(brp64, 1000000000UL); /* the practicable BRP */
  232. if (btc->brp_inc > 1)
  233. brp64 *= btc->brp_inc;
  234. bt->brp = (u32)brp64;
  235. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  236. return -EINVAL;
  237. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  238. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  239. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  240. return 0;
  241. }
  242. /* Checks the validity of predefined bitrate settings */
  243. static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
  244. const u32 *bitrate_const,
  245. const unsigned int bitrate_const_cnt)
  246. {
  247. struct can_priv *priv = netdev_priv(dev);
  248. unsigned int i;
  249. for (i = 0; i < bitrate_const_cnt; i++) {
  250. if (bt->bitrate == bitrate_const[i])
  251. break;
  252. }
  253. if (i >= priv->bitrate_const_cnt)
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  258. const struct can_bittiming_const *btc,
  259. const u32 *bitrate_const,
  260. const unsigned int bitrate_const_cnt)
  261. {
  262. int err;
  263. /*
  264. * Depending on the given can_bittiming parameter structure the CAN
  265. * timing parameters are calculated based on the provided bitrate OR
  266. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  267. * provided directly which are then checked and fixed up.
  268. */
  269. if (!bt->tq && bt->bitrate && btc)
  270. err = can_calc_bittiming(dev, bt, btc);
  271. else if (bt->tq && !bt->bitrate && btc)
  272. err = can_fixup_bittiming(dev, bt, btc);
  273. else if (!bt->tq && bt->bitrate && bitrate_const)
  274. err = can_validate_bitrate(dev, bt, bitrate_const,
  275. bitrate_const_cnt);
  276. else
  277. err = -EINVAL;
  278. return err;
  279. }
  280. static void can_update_state_error_stats(struct net_device *dev,
  281. enum can_state new_state)
  282. {
  283. struct can_priv *priv = netdev_priv(dev);
  284. if (new_state <= priv->state)
  285. return;
  286. switch (new_state) {
  287. case CAN_STATE_ERROR_WARNING:
  288. priv->can_stats.error_warning++;
  289. break;
  290. case CAN_STATE_ERROR_PASSIVE:
  291. priv->can_stats.error_passive++;
  292. break;
  293. case CAN_STATE_BUS_OFF:
  294. priv->can_stats.bus_off++;
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  301. {
  302. switch (state) {
  303. case CAN_STATE_ERROR_ACTIVE:
  304. return CAN_ERR_CRTL_ACTIVE;
  305. case CAN_STATE_ERROR_WARNING:
  306. return CAN_ERR_CRTL_TX_WARNING;
  307. case CAN_STATE_ERROR_PASSIVE:
  308. return CAN_ERR_CRTL_TX_PASSIVE;
  309. default:
  310. return 0;
  311. }
  312. }
  313. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  314. {
  315. switch (state) {
  316. case CAN_STATE_ERROR_ACTIVE:
  317. return CAN_ERR_CRTL_ACTIVE;
  318. case CAN_STATE_ERROR_WARNING:
  319. return CAN_ERR_CRTL_RX_WARNING;
  320. case CAN_STATE_ERROR_PASSIVE:
  321. return CAN_ERR_CRTL_RX_PASSIVE;
  322. default:
  323. return 0;
  324. }
  325. }
  326. void can_change_state(struct net_device *dev, struct can_frame *cf,
  327. enum can_state tx_state, enum can_state rx_state)
  328. {
  329. struct can_priv *priv = netdev_priv(dev);
  330. enum can_state new_state = max(tx_state, rx_state);
  331. if (unlikely(new_state == priv->state)) {
  332. netdev_warn(dev, "%s: oops, state did not change", __func__);
  333. return;
  334. }
  335. netdev_dbg(dev, "New error state: %d\n", new_state);
  336. can_update_state_error_stats(dev, new_state);
  337. priv->state = new_state;
  338. if (!cf)
  339. return;
  340. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  341. cf->can_id |= CAN_ERR_BUSOFF;
  342. return;
  343. }
  344. cf->can_id |= CAN_ERR_CRTL;
  345. cf->data[1] |= tx_state >= rx_state ?
  346. can_tx_state_to_frame(dev, tx_state) : 0;
  347. cf->data[1] |= tx_state <= rx_state ?
  348. can_rx_state_to_frame(dev, rx_state) : 0;
  349. }
  350. EXPORT_SYMBOL_GPL(can_change_state);
  351. /*
  352. * Local echo of CAN messages
  353. *
  354. * CAN network devices *should* support a local echo functionality
  355. * (see Documentation/networking/can.rst). To test the handling of CAN
  356. * interfaces that do not support the local echo both driver types are
  357. * implemented. In the case that the driver does not support the echo
  358. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  359. * to perform the echo as a fallback solution.
  360. */
  361. static void can_flush_echo_skb(struct net_device *dev)
  362. {
  363. struct can_priv *priv = netdev_priv(dev);
  364. struct net_device_stats *stats = &dev->stats;
  365. int i;
  366. for (i = 0; i < priv->echo_skb_max; i++) {
  367. if (priv->echo_skb[i]) {
  368. kfree_skb(priv->echo_skb[i]);
  369. priv->echo_skb[i] = NULL;
  370. stats->tx_dropped++;
  371. stats->tx_aborted_errors++;
  372. }
  373. }
  374. }
  375. /*
  376. * Put the skb on the stack to be looped backed locally lateron
  377. *
  378. * The function is typically called in the start_xmit function
  379. * of the device driver. The driver must protect access to
  380. * priv->echo_skb, if necessary.
  381. */
  382. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  383. unsigned int idx)
  384. {
  385. struct can_priv *priv = netdev_priv(dev);
  386. BUG_ON(idx >= priv->echo_skb_max);
  387. /* check flag whether this packet has to be looped back */
  388. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  389. (skb->protocol != htons(ETH_P_CAN) &&
  390. skb->protocol != htons(ETH_P_CANFD))) {
  391. kfree_skb(skb);
  392. return;
  393. }
  394. if (!priv->echo_skb[idx]) {
  395. skb = can_create_echo_skb(skb);
  396. if (!skb)
  397. return;
  398. /* make settings for echo to reduce code in irq context */
  399. skb->pkt_type = PACKET_BROADCAST;
  400. skb->ip_summed = CHECKSUM_UNNECESSARY;
  401. skb->dev = dev;
  402. /* save this skb for tx interrupt echo handling */
  403. priv->echo_skb[idx] = skb;
  404. } else {
  405. /* locking problem with netif_stop_queue() ?? */
  406. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  407. kfree_skb(skb);
  408. }
  409. }
  410. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  411. /*
  412. * Get the skb from the stack and loop it back locally
  413. *
  414. * The function is typically called when the TX done interrupt
  415. * is handled in the device driver. The driver must protect
  416. * access to priv->echo_skb, if necessary.
  417. */
  418. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  419. {
  420. struct can_priv *priv = netdev_priv(dev);
  421. BUG_ON(idx >= priv->echo_skb_max);
  422. if (priv->echo_skb[idx]) {
  423. struct sk_buff *skb = priv->echo_skb[idx];
  424. struct can_frame *cf = (struct can_frame *)skb->data;
  425. u8 dlc = cf->can_dlc;
  426. netif_rx(priv->echo_skb[idx]);
  427. priv->echo_skb[idx] = NULL;
  428. return dlc;
  429. }
  430. return 0;
  431. }
  432. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  433. /*
  434. * Remove the skb from the stack and free it.
  435. *
  436. * The function is typically called when TX failed.
  437. */
  438. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  439. {
  440. struct can_priv *priv = netdev_priv(dev);
  441. BUG_ON(idx >= priv->echo_skb_max);
  442. if (priv->echo_skb[idx]) {
  443. dev_kfree_skb_any(priv->echo_skb[idx]);
  444. priv->echo_skb[idx] = NULL;
  445. }
  446. }
  447. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  448. /*
  449. * CAN device restart for bus-off recovery
  450. */
  451. static void can_restart(struct net_device *dev)
  452. {
  453. struct can_priv *priv = netdev_priv(dev);
  454. struct net_device_stats *stats = &dev->stats;
  455. struct sk_buff *skb;
  456. struct can_frame *cf;
  457. int err;
  458. BUG_ON(netif_carrier_ok(dev));
  459. /*
  460. * No synchronization needed because the device is bus-off and
  461. * no messages can come in or go out.
  462. */
  463. can_flush_echo_skb(dev);
  464. /* send restart message upstream */
  465. skb = alloc_can_err_skb(dev, &cf);
  466. if (skb == NULL) {
  467. err = -ENOMEM;
  468. goto restart;
  469. }
  470. cf->can_id |= CAN_ERR_RESTARTED;
  471. netif_rx(skb);
  472. stats->rx_packets++;
  473. stats->rx_bytes += cf->can_dlc;
  474. restart:
  475. netdev_dbg(dev, "restarted\n");
  476. priv->can_stats.restarts++;
  477. /* Now restart the device */
  478. err = priv->do_set_mode(dev, CAN_MODE_START);
  479. netif_carrier_on(dev);
  480. if (err)
  481. netdev_err(dev, "Error %d during restart", err);
  482. }
  483. static void can_restart_work(struct work_struct *work)
  484. {
  485. struct delayed_work *dwork = to_delayed_work(work);
  486. struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
  487. can_restart(priv->dev);
  488. }
  489. int can_restart_now(struct net_device *dev)
  490. {
  491. struct can_priv *priv = netdev_priv(dev);
  492. /*
  493. * A manual restart is only permitted if automatic restart is
  494. * disabled and the device is in the bus-off state
  495. */
  496. if (priv->restart_ms)
  497. return -EINVAL;
  498. if (priv->state != CAN_STATE_BUS_OFF)
  499. return -EBUSY;
  500. cancel_delayed_work_sync(&priv->restart_work);
  501. can_restart(dev);
  502. return 0;
  503. }
  504. /*
  505. * CAN bus-off
  506. *
  507. * This functions should be called when the device goes bus-off to
  508. * tell the netif layer that no more packets can be sent or received.
  509. * If enabled, a timer is started to trigger bus-off recovery.
  510. */
  511. void can_bus_off(struct net_device *dev)
  512. {
  513. struct can_priv *priv = netdev_priv(dev);
  514. netdev_info(dev, "bus-off\n");
  515. netif_carrier_off(dev);
  516. if (priv->restart_ms)
  517. schedule_delayed_work(&priv->restart_work,
  518. msecs_to_jiffies(priv->restart_ms));
  519. }
  520. EXPORT_SYMBOL_GPL(can_bus_off);
  521. static void can_setup(struct net_device *dev)
  522. {
  523. dev->type = ARPHRD_CAN;
  524. dev->mtu = CAN_MTU;
  525. dev->hard_header_len = 0;
  526. dev->addr_len = 0;
  527. dev->tx_queue_len = 10;
  528. /* New-style flags. */
  529. dev->flags = IFF_NOARP;
  530. dev->features = NETIF_F_HW_CSUM;
  531. }
  532. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  533. {
  534. struct sk_buff *skb;
  535. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  536. sizeof(struct can_frame));
  537. if (unlikely(!skb))
  538. return NULL;
  539. skb->protocol = htons(ETH_P_CAN);
  540. skb->pkt_type = PACKET_BROADCAST;
  541. skb->ip_summed = CHECKSUM_UNNECESSARY;
  542. skb_reset_mac_header(skb);
  543. skb_reset_network_header(skb);
  544. skb_reset_transport_header(skb);
  545. can_skb_reserve(skb);
  546. can_skb_prv(skb)->ifindex = dev->ifindex;
  547. can_skb_prv(skb)->skbcnt = 0;
  548. *cf = skb_put_zero(skb, sizeof(struct can_frame));
  549. return skb;
  550. }
  551. EXPORT_SYMBOL_GPL(alloc_can_skb);
  552. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  553. struct canfd_frame **cfd)
  554. {
  555. struct sk_buff *skb;
  556. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  557. sizeof(struct canfd_frame));
  558. if (unlikely(!skb))
  559. return NULL;
  560. skb->protocol = htons(ETH_P_CANFD);
  561. skb->pkt_type = PACKET_BROADCAST;
  562. skb->ip_summed = CHECKSUM_UNNECESSARY;
  563. skb_reset_mac_header(skb);
  564. skb_reset_network_header(skb);
  565. skb_reset_transport_header(skb);
  566. can_skb_reserve(skb);
  567. can_skb_prv(skb)->ifindex = dev->ifindex;
  568. can_skb_prv(skb)->skbcnt = 0;
  569. *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
  570. return skb;
  571. }
  572. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  573. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  574. {
  575. struct sk_buff *skb;
  576. skb = alloc_can_skb(dev, cf);
  577. if (unlikely(!skb))
  578. return NULL;
  579. (*cf)->can_id = CAN_ERR_FLAG;
  580. (*cf)->can_dlc = CAN_ERR_DLC;
  581. return skb;
  582. }
  583. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  584. /*
  585. * Allocate and setup space for the CAN network device
  586. */
  587. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  588. unsigned int txqs, unsigned int rxqs)
  589. {
  590. struct net_device *dev;
  591. struct can_priv *priv;
  592. int size;
  593. if (echo_skb_max)
  594. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  595. echo_skb_max * sizeof(struct sk_buff *);
  596. else
  597. size = sizeof_priv;
  598. dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
  599. txqs, rxqs);
  600. if (!dev)
  601. return NULL;
  602. priv = netdev_priv(dev);
  603. priv->dev = dev;
  604. if (echo_skb_max) {
  605. priv->echo_skb_max = echo_skb_max;
  606. priv->echo_skb = (void *)priv +
  607. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  608. }
  609. priv->state = CAN_STATE_STOPPED;
  610. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  611. return dev;
  612. }
  613. EXPORT_SYMBOL_GPL(alloc_candev_mqs);
  614. /*
  615. * Free space of the CAN network device
  616. */
  617. void free_candev(struct net_device *dev)
  618. {
  619. free_netdev(dev);
  620. }
  621. EXPORT_SYMBOL_GPL(free_candev);
  622. /*
  623. * changing MTU and control mode for CAN/CANFD devices
  624. */
  625. int can_change_mtu(struct net_device *dev, int new_mtu)
  626. {
  627. struct can_priv *priv = netdev_priv(dev);
  628. /* Do not allow changing the MTU while running */
  629. if (dev->flags & IFF_UP)
  630. return -EBUSY;
  631. /* allow change of MTU according to the CANFD ability of the device */
  632. switch (new_mtu) {
  633. case CAN_MTU:
  634. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  635. if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
  636. return -EINVAL;
  637. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  638. break;
  639. case CANFD_MTU:
  640. /* check for potential CANFD ability */
  641. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  642. !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
  643. return -EINVAL;
  644. priv->ctrlmode |= CAN_CTRLMODE_FD;
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. dev->mtu = new_mtu;
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(can_change_mtu);
  653. /*
  654. * Common open function when the device gets opened.
  655. *
  656. * This function should be called in the open function of the device
  657. * driver.
  658. */
  659. int open_candev(struct net_device *dev)
  660. {
  661. struct can_priv *priv = netdev_priv(dev);
  662. if (!priv->bittiming.bitrate) {
  663. netdev_err(dev, "bit-timing not yet defined\n");
  664. return -EINVAL;
  665. }
  666. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  667. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  668. (!priv->data_bittiming.bitrate ||
  669. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  670. netdev_err(dev, "incorrect/missing data bit-timing\n");
  671. return -EINVAL;
  672. }
  673. /* Switch carrier on if device was stopped while in bus-off state */
  674. if (!netif_carrier_ok(dev))
  675. netif_carrier_on(dev);
  676. return 0;
  677. }
  678. EXPORT_SYMBOL_GPL(open_candev);
  679. #ifdef CONFIG_OF
  680. /* Common function that can be used to understand the limitation of
  681. * a transceiver when it provides no means to determine these limitations
  682. * at runtime.
  683. */
  684. void of_can_transceiver(struct net_device *dev)
  685. {
  686. struct device_node *dn;
  687. struct can_priv *priv = netdev_priv(dev);
  688. struct device_node *np = dev->dev.parent->of_node;
  689. int ret;
  690. dn = of_get_child_by_name(np, "can-transceiver");
  691. if (!dn)
  692. return;
  693. ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
  694. if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
  695. netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
  696. }
  697. EXPORT_SYMBOL_GPL(of_can_transceiver);
  698. #endif
  699. /*
  700. * Common close function for cleanup before the device gets closed.
  701. *
  702. * This function should be called in the close function of the device
  703. * driver.
  704. */
  705. void close_candev(struct net_device *dev)
  706. {
  707. struct can_priv *priv = netdev_priv(dev);
  708. cancel_delayed_work_sync(&priv->restart_work);
  709. can_flush_echo_skb(dev);
  710. }
  711. EXPORT_SYMBOL_GPL(close_candev);
  712. /*
  713. * CAN netlink interface
  714. */
  715. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  716. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  717. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  718. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  719. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  720. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  721. [IFLA_CAN_BITTIMING_CONST]
  722. = { .len = sizeof(struct can_bittiming_const) },
  723. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  724. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  725. [IFLA_CAN_DATA_BITTIMING]
  726. = { .len = sizeof(struct can_bittiming) },
  727. [IFLA_CAN_DATA_BITTIMING_CONST]
  728. = { .len = sizeof(struct can_bittiming_const) },
  729. };
  730. static int can_validate(struct nlattr *tb[], struct nlattr *data[],
  731. struct netlink_ext_ack *extack)
  732. {
  733. bool is_can_fd = false;
  734. /* Make sure that valid CAN FD configurations always consist of
  735. * - nominal/arbitration bittiming
  736. * - data bittiming
  737. * - control mode with CAN_CTRLMODE_FD set
  738. */
  739. if (!data)
  740. return 0;
  741. if (data[IFLA_CAN_CTRLMODE]) {
  742. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  743. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  744. }
  745. if (is_can_fd) {
  746. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  747. return -EOPNOTSUPP;
  748. }
  749. if (data[IFLA_CAN_DATA_BITTIMING]) {
  750. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  751. return -EOPNOTSUPP;
  752. }
  753. return 0;
  754. }
  755. static int can_changelink(struct net_device *dev, struct nlattr *tb[],
  756. struct nlattr *data[],
  757. struct netlink_ext_ack *extack)
  758. {
  759. struct can_priv *priv = netdev_priv(dev);
  760. int err;
  761. /* We need synchronization with dev->stop() */
  762. ASSERT_RTNL();
  763. if (data[IFLA_CAN_BITTIMING]) {
  764. struct can_bittiming bt;
  765. /* Do not allow changing bittiming while running */
  766. if (dev->flags & IFF_UP)
  767. return -EBUSY;
  768. /* Calculate bittiming parameters based on
  769. * bittiming_const if set, otherwise pass bitrate
  770. * directly via do_set_bitrate(). Bail out if neither
  771. * is given.
  772. */
  773. if (!priv->bittiming_const && !priv->do_set_bittiming)
  774. return -EOPNOTSUPP;
  775. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  776. err = can_get_bittiming(dev, &bt,
  777. priv->bittiming_const,
  778. priv->bitrate_const,
  779. priv->bitrate_const_cnt);
  780. if (err)
  781. return err;
  782. if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
  783. netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
  784. priv->bitrate_max);
  785. return -EINVAL;
  786. }
  787. memcpy(&priv->bittiming, &bt, sizeof(bt));
  788. if (priv->do_set_bittiming) {
  789. /* Finally, set the bit-timing registers */
  790. err = priv->do_set_bittiming(dev);
  791. if (err)
  792. return err;
  793. }
  794. }
  795. if (data[IFLA_CAN_CTRLMODE]) {
  796. struct can_ctrlmode *cm;
  797. u32 ctrlstatic;
  798. u32 maskedflags;
  799. /* Do not allow changing controller mode while running */
  800. if (dev->flags & IFF_UP)
  801. return -EBUSY;
  802. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  803. ctrlstatic = priv->ctrlmode_static;
  804. maskedflags = cm->flags & cm->mask;
  805. /* check whether provided bits are allowed to be passed */
  806. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  807. return -EOPNOTSUPP;
  808. /* do not check for static fd-non-iso if 'fd' is disabled */
  809. if (!(maskedflags & CAN_CTRLMODE_FD))
  810. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  811. /* make sure static options are provided by configuration */
  812. if ((maskedflags & ctrlstatic) != ctrlstatic)
  813. return -EOPNOTSUPP;
  814. /* clear bits to be modified and copy the flag values */
  815. priv->ctrlmode &= ~cm->mask;
  816. priv->ctrlmode |= maskedflags;
  817. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  818. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  819. dev->mtu = CANFD_MTU;
  820. else
  821. dev->mtu = CAN_MTU;
  822. }
  823. if (data[IFLA_CAN_RESTART_MS]) {
  824. /* Do not allow changing restart delay while running */
  825. if (dev->flags & IFF_UP)
  826. return -EBUSY;
  827. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  828. }
  829. if (data[IFLA_CAN_RESTART]) {
  830. /* Do not allow a restart while not running */
  831. if (!(dev->flags & IFF_UP))
  832. return -EINVAL;
  833. err = can_restart_now(dev);
  834. if (err)
  835. return err;
  836. }
  837. if (data[IFLA_CAN_DATA_BITTIMING]) {
  838. struct can_bittiming dbt;
  839. /* Do not allow changing bittiming while running */
  840. if (dev->flags & IFF_UP)
  841. return -EBUSY;
  842. /* Calculate bittiming parameters based on
  843. * data_bittiming_const if set, otherwise pass bitrate
  844. * directly via do_set_bitrate(). Bail out if neither
  845. * is given.
  846. */
  847. if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
  848. return -EOPNOTSUPP;
  849. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  850. sizeof(dbt));
  851. err = can_get_bittiming(dev, &dbt,
  852. priv->data_bittiming_const,
  853. priv->data_bitrate_const,
  854. priv->data_bitrate_const_cnt);
  855. if (err)
  856. return err;
  857. if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
  858. netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
  859. priv->bitrate_max);
  860. return -EINVAL;
  861. }
  862. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  863. if (priv->do_set_data_bittiming) {
  864. /* Finally, set the bit-timing registers */
  865. err = priv->do_set_data_bittiming(dev);
  866. if (err)
  867. return err;
  868. }
  869. }
  870. if (data[IFLA_CAN_TERMINATION]) {
  871. const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
  872. const unsigned int num_term = priv->termination_const_cnt;
  873. unsigned int i;
  874. if (!priv->do_set_termination)
  875. return -EOPNOTSUPP;
  876. /* check whether given value is supported by the interface */
  877. for (i = 0; i < num_term; i++) {
  878. if (termval == priv->termination_const[i])
  879. break;
  880. }
  881. if (i >= num_term)
  882. return -EINVAL;
  883. /* Finally, set the termination value */
  884. err = priv->do_set_termination(dev, termval);
  885. if (err)
  886. return err;
  887. priv->termination = termval;
  888. }
  889. return 0;
  890. }
  891. static size_t can_get_size(const struct net_device *dev)
  892. {
  893. struct can_priv *priv = netdev_priv(dev);
  894. size_t size = 0;
  895. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  896. size += nla_total_size(sizeof(struct can_bittiming));
  897. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  898. size += nla_total_size(sizeof(struct can_bittiming_const));
  899. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  900. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  901. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  902. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  903. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  904. size += nla_total_size(sizeof(struct can_berr_counter));
  905. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  906. size += nla_total_size(sizeof(struct can_bittiming));
  907. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  908. size += nla_total_size(sizeof(struct can_bittiming_const));
  909. if (priv->termination_const) {
  910. size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
  911. size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
  912. priv->termination_const_cnt);
  913. }
  914. if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
  915. size += nla_total_size(sizeof(*priv->bitrate_const) *
  916. priv->bitrate_const_cnt);
  917. if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
  918. size += nla_total_size(sizeof(*priv->data_bitrate_const) *
  919. priv->data_bitrate_const_cnt);
  920. size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
  921. return size;
  922. }
  923. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  924. {
  925. struct can_priv *priv = netdev_priv(dev);
  926. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  927. struct can_berr_counter bec;
  928. enum can_state state = priv->state;
  929. if (priv->do_get_state)
  930. priv->do_get_state(dev, &state);
  931. if ((priv->bittiming.bitrate &&
  932. nla_put(skb, IFLA_CAN_BITTIMING,
  933. sizeof(priv->bittiming), &priv->bittiming)) ||
  934. (priv->bittiming_const &&
  935. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  936. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  937. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  938. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  939. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  940. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  941. (priv->do_get_berr_counter &&
  942. !priv->do_get_berr_counter(dev, &bec) &&
  943. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  944. (priv->data_bittiming.bitrate &&
  945. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  946. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  947. (priv->data_bittiming_const &&
  948. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  949. sizeof(*priv->data_bittiming_const),
  950. priv->data_bittiming_const)) ||
  951. (priv->termination_const &&
  952. (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
  953. nla_put(skb, IFLA_CAN_TERMINATION_CONST,
  954. sizeof(*priv->termination_const) *
  955. priv->termination_const_cnt,
  956. priv->termination_const))) ||
  957. (priv->bitrate_const &&
  958. nla_put(skb, IFLA_CAN_BITRATE_CONST,
  959. sizeof(*priv->bitrate_const) *
  960. priv->bitrate_const_cnt,
  961. priv->bitrate_const)) ||
  962. (priv->data_bitrate_const &&
  963. nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
  964. sizeof(*priv->data_bitrate_const) *
  965. priv->data_bitrate_const_cnt,
  966. priv->data_bitrate_const)) ||
  967. (nla_put(skb, IFLA_CAN_BITRATE_MAX,
  968. sizeof(priv->bitrate_max),
  969. &priv->bitrate_max))
  970. )
  971. return -EMSGSIZE;
  972. return 0;
  973. }
  974. static size_t can_get_xstats_size(const struct net_device *dev)
  975. {
  976. return sizeof(struct can_device_stats);
  977. }
  978. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  979. {
  980. struct can_priv *priv = netdev_priv(dev);
  981. if (nla_put(skb, IFLA_INFO_XSTATS,
  982. sizeof(priv->can_stats), &priv->can_stats))
  983. goto nla_put_failure;
  984. return 0;
  985. nla_put_failure:
  986. return -EMSGSIZE;
  987. }
  988. static int can_newlink(struct net *src_net, struct net_device *dev,
  989. struct nlattr *tb[], struct nlattr *data[],
  990. struct netlink_ext_ack *extack)
  991. {
  992. return -EOPNOTSUPP;
  993. }
  994. static void can_dellink(struct net_device *dev, struct list_head *head)
  995. {
  996. return;
  997. }
  998. static struct rtnl_link_ops can_link_ops __read_mostly = {
  999. .kind = "can",
  1000. .maxtype = IFLA_CAN_MAX,
  1001. .policy = can_policy,
  1002. .setup = can_setup,
  1003. .validate = can_validate,
  1004. .newlink = can_newlink,
  1005. .changelink = can_changelink,
  1006. .dellink = can_dellink,
  1007. .get_size = can_get_size,
  1008. .fill_info = can_fill_info,
  1009. .get_xstats_size = can_get_xstats_size,
  1010. .fill_xstats = can_fill_xstats,
  1011. };
  1012. /*
  1013. * Register the CAN network device
  1014. */
  1015. int register_candev(struct net_device *dev)
  1016. {
  1017. struct can_priv *priv = netdev_priv(dev);
  1018. /* Ensure termination_const, termination_const_cnt and
  1019. * do_set_termination consistency. All must be either set or
  1020. * unset.
  1021. */
  1022. if ((!priv->termination_const != !priv->termination_const_cnt) ||
  1023. (!priv->termination_const != !priv->do_set_termination))
  1024. return -EINVAL;
  1025. if (!priv->bitrate_const != !priv->bitrate_const_cnt)
  1026. return -EINVAL;
  1027. if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
  1028. return -EINVAL;
  1029. dev->rtnl_link_ops = &can_link_ops;
  1030. return register_netdev(dev);
  1031. }
  1032. EXPORT_SYMBOL_GPL(register_candev);
  1033. /*
  1034. * Unregister the CAN network device
  1035. */
  1036. void unregister_candev(struct net_device *dev)
  1037. {
  1038. unregister_netdev(dev);
  1039. }
  1040. EXPORT_SYMBOL_GPL(unregister_candev);
  1041. /*
  1042. * Test if a network device is a candev based device
  1043. * and return the can_priv* if so.
  1044. */
  1045. struct can_priv *safe_candev_priv(struct net_device *dev)
  1046. {
  1047. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  1048. return NULL;
  1049. return netdev_priv(dev);
  1050. }
  1051. EXPORT_SYMBOL_GPL(safe_candev_priv);
  1052. static __init int can_dev_init(void)
  1053. {
  1054. int err;
  1055. can_led_notifier_init();
  1056. err = rtnl_link_register(&can_link_ops);
  1057. if (!err)
  1058. printk(KERN_INFO MOD_DESC "\n");
  1059. return err;
  1060. }
  1061. module_init(can_dev_init);
  1062. static __exit void can_dev_exit(void)
  1063. {
  1064. rtnl_link_unregister(&can_link_ops);
  1065. can_led_notifier_exit();
  1066. }
  1067. module_exit(can_dev_exit);
  1068. MODULE_ALIAS_RTNL_LINK("can");