dev.c 33 KB

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