dev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  3. * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/can.h>
  24. #include <linux/can/dev.h>
  25. #include <linux/can/skb.h>
  26. #include <linux/can/netlink.h>
  27. #include <linux/can/led.h>
  28. #include <net/rtnetlink.h>
  29. #define MOD_DESC "CAN device driver interface"
  30. MODULE_DESCRIPTION(MOD_DESC);
  31. MODULE_LICENSE("GPL v2");
  32. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  33. /* CAN DLC to real data length conversion helpers */
  34. static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
  35. 8, 12, 16, 20, 24, 32, 48, 64};
  36. /* get data length from can_dlc with sanitized can_dlc */
  37. u8 can_dlc2len(u8 can_dlc)
  38. {
  39. return dlc2len[can_dlc & 0x0F];
  40. }
  41. EXPORT_SYMBOL_GPL(can_dlc2len);
  42. static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
  43. 9, 9, 9, 9, /* 9 - 12 */
  44. 10, 10, 10, 10, /* 13 - 16 */
  45. 11, 11, 11, 11, /* 17 - 20 */
  46. 12, 12, 12, 12, /* 21 - 24 */
  47. 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
  48. 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
  49. 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
  50. 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
  51. 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
  52. /* map the sanitized data length to an appropriate data length code */
  53. u8 can_len2dlc(u8 len)
  54. {
  55. if (unlikely(len > 64))
  56. return 0xF;
  57. return len2dlc[len];
  58. }
  59. EXPORT_SYMBOL_GPL(can_len2dlc);
  60. #ifdef CONFIG_CAN_CALC_BITTIMING
  61. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  62. /*
  63. * Bit-timing calculation derived from:
  64. *
  65. * Code based on LinCAN sources and H8S2638 project
  66. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  67. * Copyright 2005 Stanislav Marek
  68. * email: pisa@cmp.felk.cvut.cz
  69. *
  70. * Calculates proper bit-timing parameters for a specified bit-rate
  71. * and sample-point, which can then be used to set the bit-timing
  72. * registers of the CAN controller. You can find more information
  73. * in the header file linux/can/netlink.h.
  74. */
  75. static int can_update_spt(const struct can_bittiming_const *btc,
  76. int sampl_pt, int tseg, int *tseg1, int *tseg2)
  77. {
  78. *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
  79. if (*tseg2 < btc->tseg2_min)
  80. *tseg2 = btc->tseg2_min;
  81. if (*tseg2 > btc->tseg2_max)
  82. *tseg2 = btc->tseg2_max;
  83. *tseg1 = tseg - *tseg2;
  84. if (*tseg1 > btc->tseg1_max) {
  85. *tseg1 = btc->tseg1_max;
  86. *tseg2 = tseg - *tseg1;
  87. }
  88. return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
  89. }
  90. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  91. const struct can_bittiming_const *btc)
  92. {
  93. struct can_priv *priv = netdev_priv(dev);
  94. long best_error = 1000000000, error = 0;
  95. int best_tseg = 0, best_brp = 0, brp = 0;
  96. int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
  97. int spt_error = 1000, spt = 0, sampl_pt;
  98. long rate;
  99. u64 v64;
  100. /* Use CiA recommended sample points */
  101. if (bt->sample_point) {
  102. sampl_pt = bt->sample_point;
  103. } else {
  104. if (bt->bitrate > 800000)
  105. sampl_pt = 750;
  106. else if (bt->bitrate > 500000)
  107. sampl_pt = 800;
  108. else
  109. sampl_pt = 875;
  110. }
  111. /* tseg even = round down, odd = round up */
  112. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  113. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  114. tsegall = 1 + tseg / 2;
  115. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  116. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  117. /* chose brp step which is possible in system */
  118. brp = (brp / btc->brp_inc) * btc->brp_inc;
  119. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  120. continue;
  121. rate = priv->clock.freq / (brp * tsegall);
  122. error = bt->bitrate - rate;
  123. /* tseg brp biterror */
  124. if (error < 0)
  125. error = -error;
  126. if (error > best_error)
  127. continue;
  128. best_error = error;
  129. if (error == 0) {
  130. spt = can_update_spt(btc, sampl_pt, tseg / 2,
  131. &tseg1, &tseg2);
  132. error = sampl_pt - spt;
  133. if (error < 0)
  134. error = -error;
  135. if (error > spt_error)
  136. continue;
  137. spt_error = error;
  138. }
  139. best_tseg = tseg / 2;
  140. best_brp = brp;
  141. if (error == 0)
  142. break;
  143. }
  144. if (best_error) {
  145. /* Error in one-tenth of a percent */
  146. error = (best_error * 1000) / bt->bitrate;
  147. if (error > CAN_CALC_MAX_ERROR) {
  148. netdev_err(dev,
  149. "bitrate error %ld.%ld%% too high\n",
  150. error / 10, error % 10);
  151. return -EDOM;
  152. } else {
  153. netdev_warn(dev, "bitrate error %ld.%ld%%\n",
  154. error / 10, error % 10);
  155. }
  156. }
  157. /* real sample point */
  158. bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
  159. &tseg1, &tseg2);
  160. v64 = (u64)best_brp * 1000000000UL;
  161. do_div(v64, priv->clock.freq);
  162. bt->tq = (u32)v64;
  163. bt->prop_seg = tseg1 / 2;
  164. bt->phase_seg1 = tseg1 - bt->prop_seg;
  165. bt->phase_seg2 = tseg2;
  166. /* check for sjw user settings */
  167. if (!bt->sjw || !btc->sjw_max)
  168. bt->sjw = 1;
  169. else {
  170. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  171. if (bt->sjw > btc->sjw_max)
  172. bt->sjw = btc->sjw_max;
  173. /* bt->sjw must not be higher than tseg2 */
  174. if (tseg2 < bt->sjw)
  175. bt->sjw = tseg2;
  176. }
  177. bt->brp = best_brp;
  178. /* real bit-rate */
  179. bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
  180. return 0;
  181. }
  182. #else /* !CONFIG_CAN_CALC_BITTIMING */
  183. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  184. const struct can_bittiming_const *btc)
  185. {
  186. netdev_err(dev, "bit-timing calculation not available\n");
  187. return -EINVAL;
  188. }
  189. #endif /* CONFIG_CAN_CALC_BITTIMING */
  190. /*
  191. * Checks the validity of the specified bit-timing parameters prop_seg,
  192. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  193. * prescaler value brp. You can find more information in the header
  194. * file linux/can/netlink.h.
  195. */
  196. static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
  197. const struct can_bittiming_const *btc)
  198. {
  199. struct can_priv *priv = netdev_priv(dev);
  200. int tseg1, alltseg;
  201. u64 brp64;
  202. tseg1 = bt->prop_seg + bt->phase_seg1;
  203. if (!bt->sjw)
  204. bt->sjw = 1;
  205. if (bt->sjw > btc->sjw_max ||
  206. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  207. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  208. return -ERANGE;
  209. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  210. if (btc->brp_inc > 1)
  211. do_div(brp64, btc->brp_inc);
  212. brp64 += 500000000UL - 1;
  213. do_div(brp64, 1000000000UL); /* the practicable BRP */
  214. if (btc->brp_inc > 1)
  215. brp64 *= btc->brp_inc;
  216. bt->brp = (u32)brp64;
  217. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  218. return -EINVAL;
  219. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  220. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  221. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  222. return 0;
  223. }
  224. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  225. const struct can_bittiming_const *btc)
  226. {
  227. int err;
  228. /* Check if the CAN device has bit-timing parameters */
  229. if (!btc)
  230. return -EOPNOTSUPP;
  231. /*
  232. * Depending on the given can_bittiming parameter structure the CAN
  233. * timing parameters are calculated based on the provided bitrate OR
  234. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  235. * provided directly which are then checked and fixed up.
  236. */
  237. if (!bt->tq && bt->bitrate)
  238. err = can_calc_bittiming(dev, bt, btc);
  239. else if (bt->tq && !bt->bitrate)
  240. err = can_fixup_bittiming(dev, bt, btc);
  241. else
  242. err = -EINVAL;
  243. return err;
  244. }
  245. static void can_update_state_error_stats(struct net_device *dev,
  246. enum can_state new_state)
  247. {
  248. struct can_priv *priv = netdev_priv(dev);
  249. if (new_state <= priv->state)
  250. return;
  251. switch (new_state) {
  252. case CAN_STATE_ERROR_WARNING:
  253. priv->can_stats.error_warning++;
  254. break;
  255. case CAN_STATE_ERROR_PASSIVE:
  256. priv->can_stats.error_passive++;
  257. break;
  258. case CAN_STATE_BUS_OFF:
  259. priv->can_stats.bus_off++;
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  266. {
  267. switch (state) {
  268. case CAN_STATE_ERROR_ACTIVE:
  269. return CAN_ERR_CRTL_ACTIVE;
  270. case CAN_STATE_ERROR_WARNING:
  271. return CAN_ERR_CRTL_TX_WARNING;
  272. case CAN_STATE_ERROR_PASSIVE:
  273. return CAN_ERR_CRTL_TX_PASSIVE;
  274. default:
  275. return 0;
  276. }
  277. }
  278. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  279. {
  280. switch (state) {
  281. case CAN_STATE_ERROR_ACTIVE:
  282. return CAN_ERR_CRTL_ACTIVE;
  283. case CAN_STATE_ERROR_WARNING:
  284. return CAN_ERR_CRTL_RX_WARNING;
  285. case CAN_STATE_ERROR_PASSIVE:
  286. return CAN_ERR_CRTL_RX_PASSIVE;
  287. default:
  288. return 0;
  289. }
  290. }
  291. void can_change_state(struct net_device *dev, struct can_frame *cf,
  292. enum can_state tx_state, enum can_state rx_state)
  293. {
  294. struct can_priv *priv = netdev_priv(dev);
  295. enum can_state new_state = max(tx_state, rx_state);
  296. if (unlikely(new_state == priv->state)) {
  297. netdev_warn(dev, "%s: oops, state did not change", __func__);
  298. return;
  299. }
  300. netdev_dbg(dev, "New error state: %d\n", new_state);
  301. can_update_state_error_stats(dev, new_state);
  302. priv->state = new_state;
  303. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  304. cf->can_id |= CAN_ERR_BUSOFF;
  305. return;
  306. }
  307. cf->can_id |= CAN_ERR_CRTL;
  308. cf->data[1] |= tx_state >= rx_state ?
  309. can_tx_state_to_frame(dev, tx_state) : 0;
  310. cf->data[1] |= tx_state <= rx_state ?
  311. can_rx_state_to_frame(dev, rx_state) : 0;
  312. }
  313. EXPORT_SYMBOL_GPL(can_change_state);
  314. /*
  315. * Local echo of CAN messages
  316. *
  317. * CAN network devices *should* support a local echo functionality
  318. * (see Documentation/networking/can.txt). To test the handling of CAN
  319. * interfaces that do not support the local echo both driver types are
  320. * implemented. In the case that the driver does not support the echo
  321. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  322. * to perform the echo as a fallback solution.
  323. */
  324. static void can_flush_echo_skb(struct net_device *dev)
  325. {
  326. struct can_priv *priv = netdev_priv(dev);
  327. struct net_device_stats *stats = &dev->stats;
  328. int i;
  329. for (i = 0; i < priv->echo_skb_max; i++) {
  330. if (priv->echo_skb[i]) {
  331. kfree_skb(priv->echo_skb[i]);
  332. priv->echo_skb[i] = NULL;
  333. stats->tx_dropped++;
  334. stats->tx_aborted_errors++;
  335. }
  336. }
  337. }
  338. /*
  339. * Put the skb on the stack to be looped backed locally lateron
  340. *
  341. * The function is typically called in the start_xmit function
  342. * of the device driver. The driver must protect access to
  343. * priv->echo_skb, if necessary.
  344. */
  345. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  346. unsigned int idx)
  347. {
  348. struct can_priv *priv = netdev_priv(dev);
  349. BUG_ON(idx >= priv->echo_skb_max);
  350. /* check flag whether this packet has to be looped back */
  351. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  352. (skb->protocol != htons(ETH_P_CAN) &&
  353. skb->protocol != htons(ETH_P_CANFD))) {
  354. kfree_skb(skb);
  355. return;
  356. }
  357. if (!priv->echo_skb[idx]) {
  358. skb = can_create_echo_skb(skb);
  359. if (!skb)
  360. return;
  361. /* make settings for echo to reduce code in irq context */
  362. skb->pkt_type = PACKET_BROADCAST;
  363. skb->ip_summed = CHECKSUM_UNNECESSARY;
  364. skb->dev = dev;
  365. /* save this skb for tx interrupt echo handling */
  366. priv->echo_skb[idx] = skb;
  367. } else {
  368. /* locking problem with netif_stop_queue() ?? */
  369. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  370. kfree_skb(skb);
  371. }
  372. }
  373. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  374. /*
  375. * Get the skb from the stack and loop it back locally
  376. *
  377. * The function is typically called when the TX done interrupt
  378. * is handled in the device driver. The driver must protect
  379. * access to priv->echo_skb, if necessary.
  380. */
  381. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  382. {
  383. struct can_priv *priv = netdev_priv(dev);
  384. BUG_ON(idx >= priv->echo_skb_max);
  385. if (priv->echo_skb[idx]) {
  386. struct sk_buff *skb = priv->echo_skb[idx];
  387. struct can_frame *cf = (struct can_frame *)skb->data;
  388. u8 dlc = cf->can_dlc;
  389. netif_rx(priv->echo_skb[idx]);
  390. priv->echo_skb[idx] = NULL;
  391. return dlc;
  392. }
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  396. /*
  397. * Remove the skb from the stack and free it.
  398. *
  399. * The function is typically called when TX failed.
  400. */
  401. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  402. {
  403. struct can_priv *priv = netdev_priv(dev);
  404. BUG_ON(idx >= priv->echo_skb_max);
  405. if (priv->echo_skb[idx]) {
  406. dev_kfree_skb_any(priv->echo_skb[idx]);
  407. priv->echo_skb[idx] = NULL;
  408. }
  409. }
  410. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  411. /*
  412. * CAN device restart for bus-off recovery
  413. */
  414. static void can_restart(unsigned long data)
  415. {
  416. struct net_device *dev = (struct net_device *)data;
  417. struct can_priv *priv = netdev_priv(dev);
  418. struct net_device_stats *stats = &dev->stats;
  419. struct sk_buff *skb;
  420. struct can_frame *cf;
  421. int err;
  422. BUG_ON(netif_carrier_ok(dev));
  423. /*
  424. * No synchronization needed because the device is bus-off and
  425. * no messages can come in or go out.
  426. */
  427. can_flush_echo_skb(dev);
  428. /* send restart message upstream */
  429. skb = alloc_can_err_skb(dev, &cf);
  430. if (skb == NULL) {
  431. err = -ENOMEM;
  432. goto restart;
  433. }
  434. cf->can_id |= CAN_ERR_RESTARTED;
  435. netif_rx(skb);
  436. stats->rx_packets++;
  437. stats->rx_bytes += cf->can_dlc;
  438. restart:
  439. netdev_dbg(dev, "restarted\n");
  440. priv->can_stats.restarts++;
  441. /* Now restart the device */
  442. err = priv->do_set_mode(dev, CAN_MODE_START);
  443. netif_carrier_on(dev);
  444. if (err)
  445. netdev_err(dev, "Error %d during restart", err);
  446. }
  447. int can_restart_now(struct net_device *dev)
  448. {
  449. struct can_priv *priv = netdev_priv(dev);
  450. /*
  451. * A manual restart is only permitted if automatic restart is
  452. * disabled and the device is in the bus-off state
  453. */
  454. if (priv->restart_ms)
  455. return -EINVAL;
  456. if (priv->state != CAN_STATE_BUS_OFF)
  457. return -EBUSY;
  458. /* Runs as soon as possible in the timer context */
  459. mod_timer(&priv->restart_timer, jiffies);
  460. return 0;
  461. }
  462. /*
  463. * CAN bus-off
  464. *
  465. * This functions should be called when the device goes bus-off to
  466. * tell the netif layer that no more packets can be sent or received.
  467. * If enabled, a timer is started to trigger bus-off recovery.
  468. */
  469. void can_bus_off(struct net_device *dev)
  470. {
  471. struct can_priv *priv = netdev_priv(dev);
  472. netdev_dbg(dev, "bus-off\n");
  473. netif_carrier_off(dev);
  474. if (priv->restart_ms)
  475. mod_timer(&priv->restart_timer,
  476. jiffies + (priv->restart_ms * HZ) / 1000);
  477. }
  478. EXPORT_SYMBOL_GPL(can_bus_off);
  479. static void can_setup(struct net_device *dev)
  480. {
  481. dev->type = ARPHRD_CAN;
  482. dev->mtu = CAN_MTU;
  483. dev->hard_header_len = 0;
  484. dev->addr_len = 0;
  485. dev->tx_queue_len = 10;
  486. /* New-style flags. */
  487. dev->flags = IFF_NOARP;
  488. dev->features = NETIF_F_HW_CSUM;
  489. }
  490. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  491. {
  492. struct sk_buff *skb;
  493. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  494. sizeof(struct can_frame));
  495. if (unlikely(!skb))
  496. return NULL;
  497. skb->protocol = htons(ETH_P_CAN);
  498. skb->pkt_type = PACKET_BROADCAST;
  499. skb->ip_summed = CHECKSUM_UNNECESSARY;
  500. skb_reset_mac_header(skb);
  501. skb_reset_network_header(skb);
  502. skb_reset_transport_header(skb);
  503. can_skb_reserve(skb);
  504. can_skb_prv(skb)->ifindex = dev->ifindex;
  505. can_skb_prv(skb)->skbcnt = 0;
  506. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  507. memset(*cf, 0, sizeof(struct can_frame));
  508. return skb;
  509. }
  510. EXPORT_SYMBOL_GPL(alloc_can_skb);
  511. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  512. struct canfd_frame **cfd)
  513. {
  514. struct sk_buff *skb;
  515. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  516. sizeof(struct canfd_frame));
  517. if (unlikely(!skb))
  518. return NULL;
  519. skb->protocol = htons(ETH_P_CANFD);
  520. skb->pkt_type = PACKET_BROADCAST;
  521. skb->ip_summed = CHECKSUM_UNNECESSARY;
  522. skb_reset_mac_header(skb);
  523. skb_reset_network_header(skb);
  524. skb_reset_transport_header(skb);
  525. can_skb_reserve(skb);
  526. can_skb_prv(skb)->ifindex = dev->ifindex;
  527. can_skb_prv(skb)->skbcnt = 0;
  528. *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
  529. memset(*cfd, 0, sizeof(struct canfd_frame));
  530. return skb;
  531. }
  532. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  533. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  534. {
  535. struct sk_buff *skb;
  536. skb = alloc_can_skb(dev, cf);
  537. if (unlikely(!skb))
  538. return NULL;
  539. (*cf)->can_id = CAN_ERR_FLAG;
  540. (*cf)->can_dlc = CAN_ERR_DLC;
  541. return skb;
  542. }
  543. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  544. /*
  545. * Allocate and setup space for the CAN network device
  546. */
  547. struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
  548. {
  549. struct net_device *dev;
  550. struct can_priv *priv;
  551. int size;
  552. if (echo_skb_max)
  553. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  554. echo_skb_max * sizeof(struct sk_buff *);
  555. else
  556. size = sizeof_priv;
  557. dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
  558. if (!dev)
  559. return NULL;
  560. priv = netdev_priv(dev);
  561. if (echo_skb_max) {
  562. priv->echo_skb_max = echo_skb_max;
  563. priv->echo_skb = (void *)priv +
  564. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  565. }
  566. priv->state = CAN_STATE_STOPPED;
  567. init_timer(&priv->restart_timer);
  568. return dev;
  569. }
  570. EXPORT_SYMBOL_GPL(alloc_candev);
  571. /*
  572. * Free space of the CAN network device
  573. */
  574. void free_candev(struct net_device *dev)
  575. {
  576. free_netdev(dev);
  577. }
  578. EXPORT_SYMBOL_GPL(free_candev);
  579. /*
  580. * changing MTU and control mode for CAN/CANFD devices
  581. */
  582. int can_change_mtu(struct net_device *dev, int new_mtu)
  583. {
  584. struct can_priv *priv = netdev_priv(dev);
  585. /* Do not allow changing the MTU while running */
  586. if (dev->flags & IFF_UP)
  587. return -EBUSY;
  588. /* allow change of MTU according to the CANFD ability of the device */
  589. switch (new_mtu) {
  590. case CAN_MTU:
  591. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  592. break;
  593. case CANFD_MTU:
  594. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD))
  595. return -EINVAL;
  596. priv->ctrlmode |= CAN_CTRLMODE_FD;
  597. break;
  598. default:
  599. return -EINVAL;
  600. }
  601. dev->mtu = new_mtu;
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(can_change_mtu);
  605. /*
  606. * Common open function when the device gets opened.
  607. *
  608. * This function should be called in the open function of the device
  609. * driver.
  610. */
  611. int open_candev(struct net_device *dev)
  612. {
  613. struct can_priv *priv = netdev_priv(dev);
  614. if (!priv->bittiming.bitrate) {
  615. netdev_err(dev, "bit-timing not yet defined\n");
  616. return -EINVAL;
  617. }
  618. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  619. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  620. (!priv->data_bittiming.bitrate ||
  621. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  622. netdev_err(dev, "incorrect/missing data bit-timing\n");
  623. return -EINVAL;
  624. }
  625. /* Switch carrier on if device was stopped while in bus-off state */
  626. if (!netif_carrier_ok(dev))
  627. netif_carrier_on(dev);
  628. setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
  629. return 0;
  630. }
  631. EXPORT_SYMBOL_GPL(open_candev);
  632. /*
  633. * Common close function for cleanup before the device gets closed.
  634. *
  635. * This function should be called in the close function of the device
  636. * driver.
  637. */
  638. void close_candev(struct net_device *dev)
  639. {
  640. struct can_priv *priv = netdev_priv(dev);
  641. del_timer_sync(&priv->restart_timer);
  642. can_flush_echo_skb(dev);
  643. }
  644. EXPORT_SYMBOL_GPL(close_candev);
  645. /*
  646. * CAN netlink interface
  647. */
  648. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  649. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  650. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  651. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  652. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  653. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  654. [IFLA_CAN_BITTIMING_CONST]
  655. = { .len = sizeof(struct can_bittiming_const) },
  656. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  657. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  658. [IFLA_CAN_DATA_BITTIMING]
  659. = { .len = sizeof(struct can_bittiming) },
  660. [IFLA_CAN_DATA_BITTIMING_CONST]
  661. = { .len = sizeof(struct can_bittiming_const) },
  662. };
  663. static int can_changelink(struct net_device *dev,
  664. struct nlattr *tb[], struct nlattr *data[])
  665. {
  666. struct can_priv *priv = netdev_priv(dev);
  667. int err;
  668. /* We need synchronization with dev->stop() */
  669. ASSERT_RTNL();
  670. if (data[IFLA_CAN_BITTIMING]) {
  671. struct can_bittiming bt;
  672. /* Do not allow changing bittiming while running */
  673. if (dev->flags & IFF_UP)
  674. return -EBUSY;
  675. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  676. err = can_get_bittiming(dev, &bt, priv->bittiming_const);
  677. if (err)
  678. return err;
  679. memcpy(&priv->bittiming, &bt, sizeof(bt));
  680. if (priv->do_set_bittiming) {
  681. /* Finally, set the bit-timing registers */
  682. err = priv->do_set_bittiming(dev);
  683. if (err)
  684. return err;
  685. }
  686. }
  687. if (data[IFLA_CAN_CTRLMODE]) {
  688. struct can_ctrlmode *cm;
  689. /* Do not allow changing controller mode while running */
  690. if (dev->flags & IFF_UP)
  691. return -EBUSY;
  692. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  693. /* check whether changed bits are allowed to be modified */
  694. if (cm->mask & ~priv->ctrlmode_supported)
  695. return -EOPNOTSUPP;
  696. /* clear bits to be modified and copy the flag values */
  697. priv->ctrlmode &= ~cm->mask;
  698. priv->ctrlmode |= (cm->flags & cm->mask);
  699. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  700. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  701. dev->mtu = CANFD_MTU;
  702. else
  703. dev->mtu = CAN_MTU;
  704. }
  705. if (data[IFLA_CAN_RESTART_MS]) {
  706. /* Do not allow changing restart delay while running */
  707. if (dev->flags & IFF_UP)
  708. return -EBUSY;
  709. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  710. }
  711. if (data[IFLA_CAN_RESTART]) {
  712. /* Do not allow a restart while not running */
  713. if (!(dev->flags & IFF_UP))
  714. return -EINVAL;
  715. err = can_restart_now(dev);
  716. if (err)
  717. return err;
  718. }
  719. if (data[IFLA_CAN_DATA_BITTIMING]) {
  720. struct can_bittiming dbt;
  721. /* Do not allow changing bittiming while running */
  722. if (dev->flags & IFF_UP)
  723. return -EBUSY;
  724. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  725. sizeof(dbt));
  726. err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const);
  727. if (err)
  728. return err;
  729. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  730. if (priv->do_set_data_bittiming) {
  731. /* Finally, set the bit-timing registers */
  732. err = priv->do_set_data_bittiming(dev);
  733. if (err)
  734. return err;
  735. }
  736. }
  737. return 0;
  738. }
  739. static size_t can_get_size(const struct net_device *dev)
  740. {
  741. struct can_priv *priv = netdev_priv(dev);
  742. size_t size = 0;
  743. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  744. size += nla_total_size(sizeof(struct can_bittiming));
  745. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  746. size += nla_total_size(sizeof(struct can_bittiming_const));
  747. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  748. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  749. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  750. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  751. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  752. size += nla_total_size(sizeof(struct can_berr_counter));
  753. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  754. size += nla_total_size(sizeof(struct can_bittiming));
  755. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  756. size += nla_total_size(sizeof(struct can_bittiming_const));
  757. return size;
  758. }
  759. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  760. {
  761. struct can_priv *priv = netdev_priv(dev);
  762. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  763. struct can_berr_counter bec;
  764. enum can_state state = priv->state;
  765. if (priv->do_get_state)
  766. priv->do_get_state(dev, &state);
  767. if ((priv->bittiming.bitrate &&
  768. nla_put(skb, IFLA_CAN_BITTIMING,
  769. sizeof(priv->bittiming), &priv->bittiming)) ||
  770. (priv->bittiming_const &&
  771. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  772. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  773. nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
  774. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  775. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  776. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  777. (priv->do_get_berr_counter &&
  778. !priv->do_get_berr_counter(dev, &bec) &&
  779. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  780. (priv->data_bittiming.bitrate &&
  781. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  782. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  783. (priv->data_bittiming_const &&
  784. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  785. sizeof(*priv->data_bittiming_const),
  786. priv->data_bittiming_const)))
  787. return -EMSGSIZE;
  788. return 0;
  789. }
  790. static size_t can_get_xstats_size(const struct net_device *dev)
  791. {
  792. return sizeof(struct can_device_stats);
  793. }
  794. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  795. {
  796. struct can_priv *priv = netdev_priv(dev);
  797. if (nla_put(skb, IFLA_INFO_XSTATS,
  798. sizeof(priv->can_stats), &priv->can_stats))
  799. goto nla_put_failure;
  800. return 0;
  801. nla_put_failure:
  802. return -EMSGSIZE;
  803. }
  804. static int can_newlink(struct net *src_net, struct net_device *dev,
  805. struct nlattr *tb[], struct nlattr *data[])
  806. {
  807. return -EOPNOTSUPP;
  808. }
  809. static struct rtnl_link_ops can_link_ops __read_mostly = {
  810. .kind = "can",
  811. .maxtype = IFLA_CAN_MAX,
  812. .policy = can_policy,
  813. .setup = can_setup,
  814. .newlink = can_newlink,
  815. .changelink = can_changelink,
  816. .get_size = can_get_size,
  817. .fill_info = can_fill_info,
  818. .get_xstats_size = can_get_xstats_size,
  819. .fill_xstats = can_fill_xstats,
  820. };
  821. /*
  822. * Register the CAN network device
  823. */
  824. int register_candev(struct net_device *dev)
  825. {
  826. dev->rtnl_link_ops = &can_link_ops;
  827. return register_netdev(dev);
  828. }
  829. EXPORT_SYMBOL_GPL(register_candev);
  830. /*
  831. * Unregister the CAN network device
  832. */
  833. void unregister_candev(struct net_device *dev)
  834. {
  835. unregister_netdev(dev);
  836. }
  837. EXPORT_SYMBOL_GPL(unregister_candev);
  838. /*
  839. * Test if a network device is a candev based device
  840. * and return the can_priv* if so.
  841. */
  842. struct can_priv *safe_candev_priv(struct net_device *dev)
  843. {
  844. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  845. return NULL;
  846. return netdev_priv(dev);
  847. }
  848. EXPORT_SYMBOL_GPL(safe_candev_priv);
  849. static __init int can_dev_init(void)
  850. {
  851. int err;
  852. can_led_notifier_init();
  853. err = rtnl_link_register(&can_link_ops);
  854. if (!err)
  855. printk(KERN_INFO MOD_DESC "\n");
  856. return err;
  857. }
  858. module_init(can_dev_init);
  859. static __exit void can_dev_exit(void)
  860. {
  861. rtnl_link_unregister(&can_link_ops);
  862. can_led_notifier_exit();
  863. }
  864. module_exit(can_dev_exit);
  865. MODULE_ALIAS_RTNL_LINK("can");