dev.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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 (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  338. cf->can_id |= CAN_ERR_BUSOFF;
  339. return;
  340. }
  341. cf->can_id |= CAN_ERR_CRTL;
  342. cf->data[1] |= tx_state >= rx_state ?
  343. can_tx_state_to_frame(dev, tx_state) : 0;
  344. cf->data[1] |= tx_state <= rx_state ?
  345. can_rx_state_to_frame(dev, rx_state) : 0;
  346. }
  347. EXPORT_SYMBOL_GPL(can_change_state);
  348. /*
  349. * Local echo of CAN messages
  350. *
  351. * CAN network devices *should* support a local echo functionality
  352. * (see Documentation/networking/can.txt). To test the handling of CAN
  353. * interfaces that do not support the local echo both driver types are
  354. * implemented. In the case that the driver does not support the echo
  355. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  356. * to perform the echo as a fallback solution.
  357. */
  358. static void can_flush_echo_skb(struct net_device *dev)
  359. {
  360. struct can_priv *priv = netdev_priv(dev);
  361. struct net_device_stats *stats = &dev->stats;
  362. int i;
  363. for (i = 0; i < priv->echo_skb_max; i++) {
  364. if (priv->echo_skb[i]) {
  365. kfree_skb(priv->echo_skb[i]);
  366. priv->echo_skb[i] = NULL;
  367. stats->tx_dropped++;
  368. stats->tx_aborted_errors++;
  369. }
  370. }
  371. }
  372. /*
  373. * Put the skb on the stack to be looped backed locally lateron
  374. *
  375. * The function is typically called in the start_xmit function
  376. * of the device driver. The driver must protect access to
  377. * priv->echo_skb, if necessary.
  378. */
  379. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  380. unsigned int idx)
  381. {
  382. struct can_priv *priv = netdev_priv(dev);
  383. BUG_ON(idx >= priv->echo_skb_max);
  384. /* check flag whether this packet has to be looped back */
  385. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  386. (skb->protocol != htons(ETH_P_CAN) &&
  387. skb->protocol != htons(ETH_P_CANFD))) {
  388. kfree_skb(skb);
  389. return;
  390. }
  391. if (!priv->echo_skb[idx]) {
  392. skb = can_create_echo_skb(skb);
  393. if (!skb)
  394. return;
  395. /* make settings for echo to reduce code in irq context */
  396. skb->pkt_type = PACKET_BROADCAST;
  397. skb->ip_summed = CHECKSUM_UNNECESSARY;
  398. skb->dev = dev;
  399. /* save this skb for tx interrupt echo handling */
  400. priv->echo_skb[idx] = skb;
  401. } else {
  402. /* locking problem with netif_stop_queue() ?? */
  403. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  404. kfree_skb(skb);
  405. }
  406. }
  407. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  408. /*
  409. * Get the skb from the stack and loop it back locally
  410. *
  411. * The function is typically called when the TX done interrupt
  412. * is handled in the device driver. The driver must protect
  413. * access to priv->echo_skb, if necessary.
  414. */
  415. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  416. {
  417. struct can_priv *priv = netdev_priv(dev);
  418. BUG_ON(idx >= priv->echo_skb_max);
  419. if (priv->echo_skb[idx]) {
  420. struct sk_buff *skb = priv->echo_skb[idx];
  421. struct can_frame *cf = (struct can_frame *)skb->data;
  422. u8 dlc = cf->can_dlc;
  423. netif_rx(priv->echo_skb[idx]);
  424. priv->echo_skb[idx] = NULL;
  425. return dlc;
  426. }
  427. return 0;
  428. }
  429. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  430. /*
  431. * Remove the skb from the stack and free it.
  432. *
  433. * The function is typically called when TX failed.
  434. */
  435. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  436. {
  437. struct can_priv *priv = netdev_priv(dev);
  438. BUG_ON(idx >= priv->echo_skb_max);
  439. if (priv->echo_skb[idx]) {
  440. dev_kfree_skb_any(priv->echo_skb[idx]);
  441. priv->echo_skb[idx] = NULL;
  442. }
  443. }
  444. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  445. /*
  446. * CAN device restart for bus-off recovery
  447. */
  448. static void can_restart(struct net_device *dev)
  449. {
  450. struct can_priv *priv = netdev_priv(dev);
  451. struct net_device_stats *stats = &dev->stats;
  452. struct sk_buff *skb;
  453. struct can_frame *cf;
  454. int err;
  455. BUG_ON(netif_carrier_ok(dev));
  456. /*
  457. * No synchronization needed because the device is bus-off and
  458. * no messages can come in or go out.
  459. */
  460. can_flush_echo_skb(dev);
  461. /* send restart message upstream */
  462. skb = alloc_can_err_skb(dev, &cf);
  463. if (skb == NULL) {
  464. err = -ENOMEM;
  465. goto restart;
  466. }
  467. cf->can_id |= CAN_ERR_RESTARTED;
  468. netif_rx(skb);
  469. stats->rx_packets++;
  470. stats->rx_bytes += cf->can_dlc;
  471. restart:
  472. netdev_dbg(dev, "restarted\n");
  473. priv->can_stats.restarts++;
  474. /* Now restart the device */
  475. err = priv->do_set_mode(dev, CAN_MODE_START);
  476. netif_carrier_on(dev);
  477. if (err)
  478. netdev_err(dev, "Error %d during restart", err);
  479. }
  480. static void can_restart_work(struct work_struct *work)
  481. {
  482. struct delayed_work *dwork = to_delayed_work(work);
  483. struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
  484. can_restart(priv->dev);
  485. }
  486. int can_restart_now(struct net_device *dev)
  487. {
  488. struct can_priv *priv = netdev_priv(dev);
  489. /*
  490. * A manual restart is only permitted if automatic restart is
  491. * disabled and the device is in the bus-off state
  492. */
  493. if (priv->restart_ms)
  494. return -EINVAL;
  495. if (priv->state != CAN_STATE_BUS_OFF)
  496. return -EBUSY;
  497. cancel_delayed_work_sync(&priv->restart_work);
  498. can_restart(dev);
  499. return 0;
  500. }
  501. /*
  502. * CAN bus-off
  503. *
  504. * This functions should be called when the device goes bus-off to
  505. * tell the netif layer that no more packets can be sent or received.
  506. * If enabled, a timer is started to trigger bus-off recovery.
  507. */
  508. void can_bus_off(struct net_device *dev)
  509. {
  510. struct can_priv *priv = netdev_priv(dev);
  511. netdev_dbg(dev, "bus-off\n");
  512. netif_carrier_off(dev);
  513. if (priv->restart_ms)
  514. schedule_delayed_work(&priv->restart_work,
  515. msecs_to_jiffies(priv->restart_ms));
  516. }
  517. EXPORT_SYMBOL_GPL(can_bus_off);
  518. static void can_setup(struct net_device *dev)
  519. {
  520. dev->type = ARPHRD_CAN;
  521. dev->mtu = CAN_MTU;
  522. dev->hard_header_len = 0;
  523. dev->addr_len = 0;
  524. dev->tx_queue_len = 10;
  525. /* New-style flags. */
  526. dev->flags = IFF_NOARP;
  527. dev->features = NETIF_F_HW_CSUM;
  528. }
  529. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  530. {
  531. struct sk_buff *skb;
  532. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  533. sizeof(struct can_frame));
  534. if (unlikely(!skb))
  535. return NULL;
  536. skb->protocol = htons(ETH_P_CAN);
  537. skb->pkt_type = PACKET_BROADCAST;
  538. skb->ip_summed = CHECKSUM_UNNECESSARY;
  539. skb_reset_mac_header(skb);
  540. skb_reset_network_header(skb);
  541. skb_reset_transport_header(skb);
  542. can_skb_reserve(skb);
  543. can_skb_prv(skb)->ifindex = dev->ifindex;
  544. can_skb_prv(skb)->skbcnt = 0;
  545. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  546. memset(*cf, 0, sizeof(struct can_frame));
  547. return skb;
  548. }
  549. EXPORT_SYMBOL_GPL(alloc_can_skb);
  550. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  551. struct canfd_frame **cfd)
  552. {
  553. struct sk_buff *skb;
  554. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  555. sizeof(struct canfd_frame));
  556. if (unlikely(!skb))
  557. return NULL;
  558. skb->protocol = htons(ETH_P_CANFD);
  559. skb->pkt_type = PACKET_BROADCAST;
  560. skb->ip_summed = CHECKSUM_UNNECESSARY;
  561. skb_reset_mac_header(skb);
  562. skb_reset_network_header(skb);
  563. skb_reset_transport_header(skb);
  564. can_skb_reserve(skb);
  565. can_skb_prv(skb)->ifindex = dev->ifindex;
  566. can_skb_prv(skb)->skbcnt = 0;
  567. *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
  568. memset(*cfd, 0, sizeof(struct canfd_frame));
  569. return skb;
  570. }
  571. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  572. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  573. {
  574. struct sk_buff *skb;
  575. skb = alloc_can_skb(dev, cf);
  576. if (unlikely(!skb))
  577. return NULL;
  578. (*cf)->can_id = CAN_ERR_FLAG;
  579. (*cf)->can_dlc = CAN_ERR_DLC;
  580. return skb;
  581. }
  582. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  583. /*
  584. * Allocate and setup space for the CAN network device
  585. */
  586. struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
  587. {
  588. struct net_device *dev;
  589. struct can_priv *priv;
  590. int size;
  591. if (echo_skb_max)
  592. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  593. echo_skb_max * sizeof(struct sk_buff *);
  594. else
  595. size = sizeof_priv;
  596. dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
  597. if (!dev)
  598. return NULL;
  599. priv = netdev_priv(dev);
  600. priv->dev = dev;
  601. if (echo_skb_max) {
  602. priv->echo_skb_max = echo_skb_max;
  603. priv->echo_skb = (void *)priv +
  604. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  605. }
  606. priv->state = CAN_STATE_STOPPED;
  607. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  608. return dev;
  609. }
  610. EXPORT_SYMBOL_GPL(alloc_candev);
  611. /*
  612. * Free space of the CAN network device
  613. */
  614. void free_candev(struct net_device *dev)
  615. {
  616. free_netdev(dev);
  617. }
  618. EXPORT_SYMBOL_GPL(free_candev);
  619. /*
  620. * changing MTU and control mode for CAN/CANFD devices
  621. */
  622. int can_change_mtu(struct net_device *dev, int new_mtu)
  623. {
  624. struct can_priv *priv = netdev_priv(dev);
  625. /* Do not allow changing the MTU while running */
  626. if (dev->flags & IFF_UP)
  627. return -EBUSY;
  628. /* allow change of MTU according to the CANFD ability of the device */
  629. switch (new_mtu) {
  630. case CAN_MTU:
  631. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  632. if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
  633. return -EINVAL;
  634. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  635. break;
  636. case CANFD_MTU:
  637. /* check for potential CANFD ability */
  638. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  639. !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
  640. return -EINVAL;
  641. priv->ctrlmode |= CAN_CTRLMODE_FD;
  642. break;
  643. default:
  644. return -EINVAL;
  645. }
  646. dev->mtu = new_mtu;
  647. return 0;
  648. }
  649. EXPORT_SYMBOL_GPL(can_change_mtu);
  650. /*
  651. * Common open function when the device gets opened.
  652. *
  653. * This function should be called in the open function of the device
  654. * driver.
  655. */
  656. int open_candev(struct net_device *dev)
  657. {
  658. struct can_priv *priv = netdev_priv(dev);
  659. if (!priv->bittiming.bitrate) {
  660. netdev_err(dev, "bit-timing not yet defined\n");
  661. return -EINVAL;
  662. }
  663. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  664. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  665. (!priv->data_bittiming.bitrate ||
  666. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  667. netdev_err(dev, "incorrect/missing data bit-timing\n");
  668. return -EINVAL;
  669. }
  670. /* Switch carrier on if device was stopped while in bus-off state */
  671. if (!netif_carrier_ok(dev))
  672. netif_carrier_on(dev);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL_GPL(open_candev);
  676. /*
  677. * Common close function for cleanup before the device gets closed.
  678. *
  679. * This function should be called in the close function of the device
  680. * driver.
  681. */
  682. void close_candev(struct net_device *dev)
  683. {
  684. struct can_priv *priv = netdev_priv(dev);
  685. cancel_delayed_work_sync(&priv->restart_work);
  686. can_flush_echo_skb(dev);
  687. }
  688. EXPORT_SYMBOL_GPL(close_candev);
  689. /*
  690. * CAN netlink interface
  691. */
  692. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  693. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  694. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  695. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  696. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  697. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  698. [IFLA_CAN_BITTIMING_CONST]
  699. = { .len = sizeof(struct can_bittiming_const) },
  700. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  701. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  702. [IFLA_CAN_DATA_BITTIMING]
  703. = { .len = sizeof(struct can_bittiming) },
  704. [IFLA_CAN_DATA_BITTIMING_CONST]
  705. = { .len = sizeof(struct can_bittiming_const) },
  706. };
  707. static int can_validate(struct nlattr *tb[], struct nlattr *data[])
  708. {
  709. bool is_can_fd = false;
  710. /* Make sure that valid CAN FD configurations always consist of
  711. * - nominal/arbitration bittiming
  712. * - data bittiming
  713. * - control mode with CAN_CTRLMODE_FD set
  714. */
  715. if (!data)
  716. return 0;
  717. if (data[IFLA_CAN_CTRLMODE]) {
  718. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  719. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  720. }
  721. if (is_can_fd) {
  722. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  723. return -EOPNOTSUPP;
  724. }
  725. if (data[IFLA_CAN_DATA_BITTIMING]) {
  726. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  727. return -EOPNOTSUPP;
  728. }
  729. return 0;
  730. }
  731. static int can_changelink(struct net_device *dev,
  732. struct nlattr *tb[], struct nlattr *data[])
  733. {
  734. struct can_priv *priv = netdev_priv(dev);
  735. int err;
  736. /* We need synchronization with dev->stop() */
  737. ASSERT_RTNL();
  738. if (data[IFLA_CAN_BITTIMING]) {
  739. struct can_bittiming bt;
  740. /* Do not allow changing bittiming while running */
  741. if (dev->flags & IFF_UP)
  742. return -EBUSY;
  743. /* Calculate bittiming parameters based on
  744. * bittiming_const if set, otherwise pass bitrate
  745. * directly via do_set_bitrate(). Bail out if neither
  746. * is given.
  747. */
  748. if (!priv->bittiming_const && !priv->do_set_bittiming)
  749. return -EOPNOTSUPP;
  750. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  751. err = can_get_bittiming(dev, &bt,
  752. priv->bittiming_const,
  753. priv->bitrate_const,
  754. priv->bitrate_const_cnt);
  755. if (err)
  756. return err;
  757. memcpy(&priv->bittiming, &bt, sizeof(bt));
  758. if (priv->do_set_bittiming) {
  759. /* Finally, set the bit-timing registers */
  760. err = priv->do_set_bittiming(dev);
  761. if (err)
  762. return err;
  763. }
  764. }
  765. if (data[IFLA_CAN_CTRLMODE]) {
  766. struct can_ctrlmode *cm;
  767. u32 ctrlstatic;
  768. u32 maskedflags;
  769. /* Do not allow changing controller mode while running */
  770. if (dev->flags & IFF_UP)
  771. return -EBUSY;
  772. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  773. ctrlstatic = priv->ctrlmode_static;
  774. maskedflags = cm->flags & cm->mask;
  775. /* check whether provided bits are allowed to be passed */
  776. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  777. return -EOPNOTSUPP;
  778. /* do not check for static fd-non-iso if 'fd' is disabled */
  779. if (!(maskedflags & CAN_CTRLMODE_FD))
  780. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  781. /* make sure static options are provided by configuration */
  782. if ((maskedflags & ctrlstatic) != ctrlstatic)
  783. return -EOPNOTSUPP;
  784. /* clear bits to be modified and copy the flag values */
  785. priv->ctrlmode &= ~cm->mask;
  786. priv->ctrlmode |= maskedflags;
  787. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  788. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  789. dev->mtu = CANFD_MTU;
  790. else
  791. dev->mtu = CAN_MTU;
  792. }
  793. if (data[IFLA_CAN_RESTART_MS]) {
  794. /* Do not allow changing restart delay while running */
  795. if (dev->flags & IFF_UP)
  796. return -EBUSY;
  797. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  798. }
  799. if (data[IFLA_CAN_RESTART]) {
  800. /* Do not allow a restart while not running */
  801. if (!(dev->flags & IFF_UP))
  802. return -EINVAL;
  803. err = can_restart_now(dev);
  804. if (err)
  805. return err;
  806. }
  807. if (data[IFLA_CAN_DATA_BITTIMING]) {
  808. struct can_bittiming dbt;
  809. /* Do not allow changing bittiming while running */
  810. if (dev->flags & IFF_UP)
  811. return -EBUSY;
  812. /* Calculate bittiming parameters based on
  813. * data_bittiming_const if set, otherwise pass bitrate
  814. * directly via do_set_bitrate(). Bail out if neither
  815. * is given.
  816. */
  817. if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
  818. return -EOPNOTSUPP;
  819. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  820. sizeof(dbt));
  821. err = can_get_bittiming(dev, &dbt,
  822. priv->data_bittiming_const,
  823. priv->data_bitrate_const,
  824. priv->data_bitrate_const_cnt);
  825. if (err)
  826. return err;
  827. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  828. if (priv->do_set_data_bittiming) {
  829. /* Finally, set the bit-timing registers */
  830. err = priv->do_set_data_bittiming(dev);
  831. if (err)
  832. return err;
  833. }
  834. }
  835. if (data[IFLA_CAN_TERMINATION]) {
  836. const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
  837. const unsigned int num_term = priv->termination_const_cnt;
  838. unsigned int i;
  839. if (!priv->do_set_termination)
  840. return -EOPNOTSUPP;
  841. /* check whether given value is supported by the interface */
  842. for (i = 0; i < num_term; i++) {
  843. if (termval == priv->termination_const[i])
  844. break;
  845. }
  846. if (i >= num_term)
  847. return -EINVAL;
  848. /* Finally, set the termination value */
  849. err = priv->do_set_termination(dev, termval);
  850. if (err)
  851. return err;
  852. priv->termination = termval;
  853. }
  854. return 0;
  855. }
  856. static size_t can_get_size(const struct net_device *dev)
  857. {
  858. struct can_priv *priv = netdev_priv(dev);
  859. size_t size = 0;
  860. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  861. size += nla_total_size(sizeof(struct can_bittiming));
  862. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  863. size += nla_total_size(sizeof(struct can_bittiming_const));
  864. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  865. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  866. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  867. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  868. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  869. size += nla_total_size(sizeof(struct can_berr_counter));
  870. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  871. size += nla_total_size(sizeof(struct can_bittiming));
  872. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  873. size += nla_total_size(sizeof(struct can_bittiming_const));
  874. if (priv->termination_const) {
  875. size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
  876. size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
  877. priv->termination_const_cnt);
  878. }
  879. if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
  880. size += nla_total_size(sizeof(*priv->bitrate_const) *
  881. priv->bitrate_const_cnt);
  882. if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
  883. size += nla_total_size(sizeof(*priv->data_bitrate_const) *
  884. priv->data_bitrate_const_cnt);
  885. return size;
  886. }
  887. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  888. {
  889. struct can_priv *priv = netdev_priv(dev);
  890. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  891. struct can_berr_counter bec;
  892. enum can_state state = priv->state;
  893. if (priv->do_get_state)
  894. priv->do_get_state(dev, &state);
  895. if ((priv->bittiming.bitrate &&
  896. nla_put(skb, IFLA_CAN_BITTIMING,
  897. sizeof(priv->bittiming), &priv->bittiming)) ||
  898. (priv->bittiming_const &&
  899. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  900. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  901. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  902. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  903. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  904. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  905. (priv->do_get_berr_counter &&
  906. !priv->do_get_berr_counter(dev, &bec) &&
  907. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  908. (priv->data_bittiming.bitrate &&
  909. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  910. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  911. (priv->data_bittiming_const &&
  912. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  913. sizeof(*priv->data_bittiming_const),
  914. priv->data_bittiming_const)) ||
  915. (priv->termination_const &&
  916. (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
  917. nla_put(skb, IFLA_CAN_TERMINATION_CONST,
  918. sizeof(*priv->termination_const) *
  919. priv->termination_const_cnt,
  920. priv->termination_const))) ||
  921. (priv->bitrate_const &&
  922. nla_put(skb, IFLA_CAN_BITRATE_CONST,
  923. sizeof(*priv->bitrate_const) *
  924. priv->bitrate_const_cnt,
  925. priv->bitrate_const)) ||
  926. (priv->data_bitrate_const &&
  927. nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
  928. sizeof(*priv->data_bitrate_const) *
  929. priv->data_bitrate_const_cnt,
  930. priv->data_bitrate_const))
  931. )
  932. return -EMSGSIZE;
  933. return 0;
  934. }
  935. static size_t can_get_xstats_size(const struct net_device *dev)
  936. {
  937. return sizeof(struct can_device_stats);
  938. }
  939. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  940. {
  941. struct can_priv *priv = netdev_priv(dev);
  942. if (nla_put(skb, IFLA_INFO_XSTATS,
  943. sizeof(priv->can_stats), &priv->can_stats))
  944. goto nla_put_failure;
  945. return 0;
  946. nla_put_failure:
  947. return -EMSGSIZE;
  948. }
  949. static int can_newlink(struct net *src_net, struct net_device *dev,
  950. struct nlattr *tb[], struct nlattr *data[])
  951. {
  952. return -EOPNOTSUPP;
  953. }
  954. static void can_dellink(struct net_device *dev, struct list_head *head)
  955. {
  956. return;
  957. }
  958. static struct rtnl_link_ops can_link_ops __read_mostly = {
  959. .kind = "can",
  960. .maxtype = IFLA_CAN_MAX,
  961. .policy = can_policy,
  962. .setup = can_setup,
  963. .validate = can_validate,
  964. .newlink = can_newlink,
  965. .changelink = can_changelink,
  966. .dellink = can_dellink,
  967. .get_size = can_get_size,
  968. .fill_info = can_fill_info,
  969. .get_xstats_size = can_get_xstats_size,
  970. .fill_xstats = can_fill_xstats,
  971. };
  972. /*
  973. * Register the CAN network device
  974. */
  975. int register_candev(struct net_device *dev)
  976. {
  977. struct can_priv *priv = netdev_priv(dev);
  978. /* Ensure termination_const, termination_const_cnt and
  979. * do_set_termination consistency. All must be either set or
  980. * unset.
  981. */
  982. if ((!priv->termination_const != !priv->termination_const_cnt) ||
  983. (!priv->termination_const != !priv->do_set_termination))
  984. return -EINVAL;
  985. if (!priv->bitrate_const != !priv->bitrate_const_cnt)
  986. return -EINVAL;
  987. if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
  988. return -EINVAL;
  989. dev->rtnl_link_ops = &can_link_ops;
  990. return register_netdev(dev);
  991. }
  992. EXPORT_SYMBOL_GPL(register_candev);
  993. /*
  994. * Unregister the CAN network device
  995. */
  996. void unregister_candev(struct net_device *dev)
  997. {
  998. unregister_netdev(dev);
  999. }
  1000. EXPORT_SYMBOL_GPL(unregister_candev);
  1001. /*
  1002. * Test if a network device is a candev based device
  1003. * and return the can_priv* if so.
  1004. */
  1005. struct can_priv *safe_candev_priv(struct net_device *dev)
  1006. {
  1007. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  1008. return NULL;
  1009. return netdev_priv(dev);
  1010. }
  1011. EXPORT_SYMBOL_GPL(safe_candev_priv);
  1012. static __init int can_dev_init(void)
  1013. {
  1014. int err;
  1015. can_led_notifier_init();
  1016. err = rtnl_link_register(&can_link_ops);
  1017. if (!err)
  1018. printk(KERN_INFO MOD_DESC "\n");
  1019. return err;
  1020. }
  1021. module_init(can_dev_init);
  1022. static __exit void can_dev_exit(void)
  1023. {
  1024. rtnl_link_unregister(&can_link_ops);
  1025. can_led_notifier_exit();
  1026. }
  1027. module_exit(can_dev_exit);
  1028. MODULE_ALIAS_RTNL_LINK("can");