slcan-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * slcan.c - serial line CAN interface driver (using tty line discipline)
  3. *
  4. * This file is derived from linux/drivers/net/slip/slip.c and got
  5. * inspiration from linux/drivers/net/can/can327.c for the rework made
  6. * on the line discipline code.
  7. *
  8. * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
  9. * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
  10. * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
  11. * can327.c Author : Max Staudt <max-linux@enpas.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, see http://www.gnu.org/licenses/gpl.html
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  37. * DAMAGE.
  38. *
  39. */
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/module.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/bitops.h>
  44. #include <linux/string.h>
  45. #include <linux/tty.h>
  46. #include <linux/errno.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/rtnetlink.h>
  50. #include <linux/hex.h>
  51. #include <linux/init.h>
  52. #include <linux/kernel.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/can.h>
  55. #include <linux/can/dev.h>
  56. #include <linux/can/skb.h>
  57. #include "slcan.h"
  58. MODULE_ALIAS_LDISC(N_SLCAN);
  59. MODULE_DESCRIPTION("serial line CAN interface");
  60. MODULE_LICENSE("GPL");
  61. MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
  62. MODULE_AUTHOR("Dario Binacchi <dario.binacchi@amarulasolutions.com>");
  63. /* maximum rx buffer len: extended CAN frame with timestamp */
  64. #define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
  65. #define SLCAN_CMD_LEN 1
  66. #define SLCAN_SFF_ID_LEN 3
  67. #define SLCAN_EFF_ID_LEN 8
  68. #define SLCAN_DATA_LENGTH_LEN 1
  69. #define SLCAN_ERROR_LEN 1
  70. #define SLCAN_STATE_LEN 1
  71. #define SLCAN_STATE_BE_RXCNT_LEN 3
  72. #define SLCAN_STATE_BE_TXCNT_LEN 3
  73. #define SLCAN_STATE_MSG_LEN (SLCAN_CMD_LEN + \
  74. SLCAN_STATE_LEN + \
  75. SLCAN_STATE_BE_RXCNT_LEN + \
  76. SLCAN_STATE_BE_TXCNT_LEN)
  77. #define SLCAN_ERROR_MSG_LEN_MIN (SLCAN_CMD_LEN + \
  78. SLCAN_ERROR_LEN + \
  79. SLCAN_DATA_LENGTH_LEN)
  80. #define SLCAN_FRAME_MSG_LEN_MIN (SLCAN_CMD_LEN + \
  81. SLCAN_SFF_ID_LEN + \
  82. SLCAN_DATA_LENGTH_LEN)
  83. struct slcan {
  84. struct can_priv can;
  85. /* Various fields. */
  86. struct tty_struct *tty; /* ptr to TTY structure */
  87. struct net_device *dev; /* easy for intr handling */
  88. spinlock_t lock;
  89. struct work_struct tx_work; /* Flushes transmit buffer */
  90. /* These are pointers to the malloc()ed frame buffers. */
  91. unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */
  92. int rcount; /* received chars counter */
  93. unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/
  94. unsigned char *xhead; /* pointer to next XMIT byte */
  95. int xleft; /* bytes left in XMIT queue */
  96. unsigned long flags; /* Flag values/ mode etc */
  97. #define SLF_ERROR 0 /* Parity, etc. error */
  98. #define SLF_XCMD 1 /* Command transmission */
  99. unsigned long cmd_flags; /* Command flags */
  100. #define CF_ERR_RST 0 /* Reset errors on open */
  101. wait_queue_head_t xcmd_wait; /* Wait queue for commands */
  102. /* transmission */
  103. };
  104. static const u32 slcan_bitrate_const[] = {
  105. 10000, 20000, 50000, 100000, 125000,
  106. 250000, 500000, 800000, 1000000
  107. };
  108. bool slcan_err_rst_on_open(struct net_device *ndev)
  109. {
  110. struct slcan *sl = netdev_priv(ndev);
  111. return !!test_bit(CF_ERR_RST, &sl->cmd_flags);
  112. }
  113. int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
  114. {
  115. struct slcan *sl = netdev_priv(ndev);
  116. if (netif_running(ndev))
  117. return -EBUSY;
  118. if (on)
  119. set_bit(CF_ERR_RST, &sl->cmd_flags);
  120. else
  121. clear_bit(CF_ERR_RST, &sl->cmd_flags);
  122. return 0;
  123. }
  124. /*************************************************************************
  125. * SLCAN ENCAPSULATION FORMAT *
  126. *************************************************************************/
  127. /* A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
  128. * frame format) a data length code (len) which can be from 0 to 8
  129. * and up to <len> data bytes as payload.
  130. * Additionally a CAN frame may become a remote transmission frame if the
  131. * RTR-bit is set. This causes another ECU to send a CAN frame with the
  132. * given can_id.
  133. *
  134. * The SLCAN ASCII representation of these different frame types is:
  135. * <type> <id> <dlc> <data>*
  136. *
  137. * Extended frames (29 bit) are defined by capital characters in the type.
  138. * RTR frames are defined as 'r' types - normal frames have 't' type:
  139. * t => 11 bit data frame
  140. * r => 11 bit RTR frame
  141. * T => 29 bit data frame
  142. * R => 29 bit RTR frame
  143. *
  144. * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
  145. * The <dlc> is a one byte ASCII number ('0' - '8')
  146. * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
  147. *
  148. * Examples:
  149. *
  150. * t1230 : can_id 0x123, len 0, no data
  151. * t4563112233 : can_id 0x456, len 3, data 0x11 0x22 0x33
  152. * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, len 2, data 0xAA 0x55
  153. * r1230 : can_id 0x123, len 0, no data, remote transmission request
  154. *
  155. */
  156. /*************************************************************************
  157. * STANDARD SLCAN DECAPSULATION *
  158. *************************************************************************/
  159. /* Send one completely decapsulated can_frame to the network layer */
  160. static void slcan_bump_frame(struct slcan *sl)
  161. {
  162. struct sk_buff *skb;
  163. struct can_frame *cf;
  164. int i, tmp;
  165. u32 tmpid;
  166. char *cmd = sl->rbuff;
  167. if (sl->rcount < SLCAN_FRAME_MSG_LEN_MIN)
  168. return;
  169. skb = alloc_can_skb(sl->dev, &cf);
  170. if (unlikely(!skb)) {
  171. sl->dev->stats.rx_dropped++;
  172. return;
  173. }
  174. switch (*cmd) {
  175. case 'r':
  176. cf->can_id = CAN_RTR_FLAG;
  177. fallthrough;
  178. case 't':
  179. /* store dlc ASCII value and terminate SFF CAN ID string */
  180. cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN];
  181. sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0;
  182. /* point to payload data behind the dlc */
  183. cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1;
  184. break;
  185. case 'R':
  186. cf->can_id = CAN_RTR_FLAG;
  187. fallthrough;
  188. case 'T':
  189. cf->can_id |= CAN_EFF_FLAG;
  190. /* store dlc ASCII value and terminate EFF CAN ID string */
  191. cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN];
  192. sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0;
  193. /* point to payload data behind the dlc */
  194. cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1;
  195. break;
  196. default:
  197. goto decode_failed;
  198. }
  199. if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid))
  200. goto decode_failed;
  201. cf->can_id |= tmpid;
  202. /* get len from sanitized ASCII value */
  203. if (cf->len >= '0' && cf->len < '9')
  204. cf->len -= '0';
  205. else
  206. goto decode_failed;
  207. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  208. if (!(cf->can_id & CAN_RTR_FLAG)) {
  209. for (i = 0; i < cf->len; i++) {
  210. tmp = hex_to_bin(*cmd++);
  211. if (tmp < 0)
  212. goto decode_failed;
  213. cf->data[i] = (tmp << 4);
  214. tmp = hex_to_bin(*cmd++);
  215. if (tmp < 0)
  216. goto decode_failed;
  217. cf->data[i] |= tmp;
  218. }
  219. }
  220. sl->dev->stats.rx_packets++;
  221. if (!(cf->can_id & CAN_RTR_FLAG))
  222. sl->dev->stats.rx_bytes += cf->len;
  223. netif_rx(skb);
  224. return;
  225. decode_failed:
  226. sl->dev->stats.rx_errors++;
  227. dev_kfree_skb(skb);
  228. }
  229. /* A change state frame must contain state info and receive and transmit
  230. * error counters.
  231. *
  232. * Examples:
  233. *
  234. * sb256256 : state bus-off: rx counter 256, tx counter 256
  235. * sa057033 : state active, rx counter 57, tx counter 33
  236. */
  237. static void slcan_bump_state(struct slcan *sl)
  238. {
  239. struct net_device *dev = sl->dev;
  240. struct sk_buff *skb;
  241. struct can_frame *cf;
  242. char *cmd = sl->rbuff;
  243. u32 rxerr, txerr;
  244. enum can_state state, rx_state, tx_state;
  245. switch (cmd[1]) {
  246. case 'a':
  247. state = CAN_STATE_ERROR_ACTIVE;
  248. break;
  249. case 'w':
  250. state = CAN_STATE_ERROR_WARNING;
  251. break;
  252. case 'p':
  253. state = CAN_STATE_ERROR_PASSIVE;
  254. break;
  255. case 'b':
  256. state = CAN_STATE_BUS_OFF;
  257. break;
  258. default:
  259. return;
  260. }
  261. if (state == sl->can.state || sl->rcount != SLCAN_STATE_MSG_LEN)
  262. return;
  263. cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1;
  264. cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0;
  265. if (kstrtou32(cmd, 10, &txerr))
  266. return;
  267. *cmd = 0;
  268. cmd -= SLCAN_STATE_BE_RXCNT_LEN;
  269. if (kstrtou32(cmd, 10, &rxerr))
  270. return;
  271. skb = alloc_can_err_skb(dev, &cf);
  272. tx_state = txerr >= rxerr ? state : 0;
  273. rx_state = txerr <= rxerr ? state : 0;
  274. can_change_state(dev, cf, tx_state, rx_state);
  275. if (state == CAN_STATE_BUS_OFF) {
  276. can_bus_off(dev);
  277. } else if (skb) {
  278. cf->can_id |= CAN_ERR_CNT;
  279. cf->data[6] = txerr;
  280. cf->data[7] = rxerr;
  281. }
  282. if (skb)
  283. netif_rx(skb);
  284. }
  285. /* An error frame can contain more than one type of error.
  286. *
  287. * Examples:
  288. *
  289. * e1a : len 1, errors: ACK error
  290. * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
  291. */
  292. static void slcan_bump_err(struct slcan *sl)
  293. {
  294. struct net_device *dev = sl->dev;
  295. struct sk_buff *skb;
  296. struct can_frame *cf;
  297. char *cmd = sl->rbuff;
  298. bool rx_errors = false, tx_errors = false, rx_over_errors = false;
  299. int i, len;
  300. if (sl->rcount < SLCAN_ERROR_MSG_LEN_MIN)
  301. return;
  302. /* get len from sanitized ASCII value */
  303. len = cmd[1];
  304. if (len >= '0' && len < '9')
  305. len -= '0';
  306. else
  307. return;
  308. if ((len + SLCAN_CMD_LEN + 1) > sl->rcount)
  309. return;
  310. skb = alloc_can_err_skb(dev, &cf);
  311. if (skb)
  312. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  313. cmd += SLCAN_CMD_LEN + 1;
  314. for (i = 0; i < len; i++, cmd++) {
  315. switch (*cmd) {
  316. case 'a':
  317. netdev_dbg(dev, "ACK error\n");
  318. tx_errors = true;
  319. if (skb) {
  320. cf->can_id |= CAN_ERR_ACK;
  321. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  322. }
  323. break;
  324. case 'b':
  325. netdev_dbg(dev, "Bit0 error\n");
  326. tx_errors = true;
  327. if (skb)
  328. cf->data[2] |= CAN_ERR_PROT_BIT0;
  329. break;
  330. case 'B':
  331. netdev_dbg(dev, "Bit1 error\n");
  332. tx_errors = true;
  333. if (skb)
  334. cf->data[2] |= CAN_ERR_PROT_BIT1;
  335. break;
  336. case 'c':
  337. netdev_dbg(dev, "CRC error\n");
  338. rx_errors = true;
  339. if (skb) {
  340. cf->data[2] |= CAN_ERR_PROT_BIT;
  341. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  342. }
  343. break;
  344. case 'f':
  345. netdev_dbg(dev, "Form Error\n");
  346. rx_errors = true;
  347. if (skb)
  348. cf->data[2] |= CAN_ERR_PROT_FORM;
  349. break;
  350. case 'o':
  351. netdev_dbg(dev, "Rx overrun error\n");
  352. rx_over_errors = true;
  353. rx_errors = true;
  354. if (skb) {
  355. cf->can_id |= CAN_ERR_CRTL;
  356. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  357. }
  358. break;
  359. case 'O':
  360. netdev_dbg(dev, "Tx overrun error\n");
  361. tx_errors = true;
  362. if (skb) {
  363. cf->can_id |= CAN_ERR_CRTL;
  364. cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW;
  365. }
  366. break;
  367. case 's':
  368. netdev_dbg(dev, "Stuff error\n");
  369. rx_errors = true;
  370. if (skb)
  371. cf->data[2] |= CAN_ERR_PROT_STUFF;
  372. break;
  373. default:
  374. if (skb)
  375. dev_kfree_skb(skb);
  376. return;
  377. }
  378. }
  379. if (rx_errors)
  380. dev->stats.rx_errors++;
  381. if (rx_over_errors)
  382. dev->stats.rx_over_errors++;
  383. if (tx_errors)
  384. dev->stats.tx_errors++;
  385. if (skb)
  386. netif_rx(skb);
  387. }
  388. static void slcan_bump(struct slcan *sl)
  389. {
  390. switch (sl->rbuff[0]) {
  391. case 'r':
  392. fallthrough;
  393. case 't':
  394. fallthrough;
  395. case 'R':
  396. fallthrough;
  397. case 'T':
  398. return slcan_bump_frame(sl);
  399. case 'e':
  400. return slcan_bump_err(sl);
  401. case 's':
  402. return slcan_bump_state(sl);
  403. default:
  404. return;
  405. }
  406. }
  407. /* parse tty input stream */
  408. static void slcan_unesc(struct slcan *sl, unsigned char s)
  409. {
  410. if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
  411. if (!test_and_clear_bit(SLF_ERROR, &sl->flags))
  412. slcan_bump(sl);
  413. sl->rcount = 0;
  414. } else {
  415. if (!test_bit(SLF_ERROR, &sl->flags)) {
  416. if (sl->rcount < SLCAN_MTU) {
  417. sl->rbuff[sl->rcount++] = s;
  418. return;
  419. }
  420. sl->dev->stats.rx_over_errors++;
  421. set_bit(SLF_ERROR, &sl->flags);
  422. }
  423. }
  424. }
  425. /*************************************************************************
  426. * STANDARD SLCAN ENCAPSULATION *
  427. *************************************************************************/
  428. /* Encapsulate one can_frame and stuff into a TTY queue. */
  429. static void slcan_encaps(struct slcan *sl, struct can_frame *cf)
  430. {
  431. int actual, i;
  432. unsigned char *pos;
  433. unsigned char *endpos;
  434. canid_t id = cf->can_id;
  435. pos = sl->xbuff;
  436. if (cf->can_id & CAN_RTR_FLAG)
  437. *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
  438. else
  439. *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
  440. /* determine number of chars for the CAN-identifier */
  441. if (cf->can_id & CAN_EFF_FLAG) {
  442. id &= CAN_EFF_MASK;
  443. endpos = pos + SLCAN_EFF_ID_LEN;
  444. } else {
  445. *pos |= 0x20; /* convert R/T to lower case for SFF */
  446. id &= CAN_SFF_MASK;
  447. endpos = pos + SLCAN_SFF_ID_LEN;
  448. }
  449. /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
  450. pos++;
  451. while (endpos >= pos) {
  452. *endpos-- = hex_asc_upper[id & 0xf];
  453. id >>= 4;
  454. }
  455. pos += (cf->can_id & CAN_EFF_FLAG) ?
  456. SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN;
  457. *pos++ = cf->len + '0';
  458. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  459. if (!(cf->can_id & CAN_RTR_FLAG)) {
  460. for (i = 0; i < cf->len; i++)
  461. pos = hex_byte_pack_upper(pos, cf->data[i]);
  462. sl->dev->stats.tx_bytes += cf->len;
  463. }
  464. *pos++ = '\r';
  465. /* Order of next two lines is *very* important.
  466. * When we are sending a little amount of data,
  467. * the transfer may be completed inside the ops->write()
  468. * routine, because it's running with interrupts enabled.
  469. * In this case we *never* got WRITE_WAKEUP event,
  470. * if we did not request it before write operation.
  471. * 14 Oct 1994 Dmitry Gorodchanin.
  472. */
  473. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  474. actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
  475. sl->xleft = (pos - sl->xbuff) - actual;
  476. sl->xhead = sl->xbuff + actual;
  477. }
  478. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  479. static void slcan_transmit(struct work_struct *work)
  480. {
  481. struct slcan *sl = container_of(work, struct slcan, tx_work);
  482. int actual;
  483. spin_lock_bh(&sl->lock);
  484. /* First make sure we're connected. */
  485. if (unlikely(!netif_running(sl->dev)) &&
  486. likely(!test_bit(SLF_XCMD, &sl->flags))) {
  487. spin_unlock_bh(&sl->lock);
  488. return;
  489. }
  490. if (sl->xleft <= 0) {
  491. if (unlikely(test_bit(SLF_XCMD, &sl->flags))) {
  492. clear_bit(SLF_XCMD, &sl->flags);
  493. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  494. spin_unlock_bh(&sl->lock);
  495. wake_up(&sl->xcmd_wait);
  496. return;
  497. }
  498. /* Now serial buffer is almost free & we can start
  499. * transmission of another packet
  500. */
  501. sl->dev->stats.tx_packets++;
  502. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  503. spin_unlock_bh(&sl->lock);
  504. netif_wake_queue(sl->dev);
  505. return;
  506. }
  507. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  508. sl->xleft -= actual;
  509. sl->xhead += actual;
  510. spin_unlock_bh(&sl->lock);
  511. }
  512. /* Called by the driver when there's room for more data.
  513. * Schedule the transmit.
  514. */
  515. static void slcan_write_wakeup(struct tty_struct *tty)
  516. {
  517. struct slcan *sl = tty->disc_data;
  518. schedule_work(&sl->tx_work);
  519. }
  520. /* Send a can_frame to a TTY queue. */
  521. static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb,
  522. struct net_device *dev)
  523. {
  524. struct slcan *sl = netdev_priv(dev);
  525. if (can_dev_dropped_skb(dev, skb))
  526. return NETDEV_TX_OK;
  527. spin_lock(&sl->lock);
  528. if (!netif_running(dev)) {
  529. spin_unlock(&sl->lock);
  530. netdev_warn(dev, "xmit: iface is down\n");
  531. goto out;
  532. }
  533. if (!sl->tty) {
  534. spin_unlock(&sl->lock);
  535. goto out;
  536. }
  537. netif_stop_queue(sl->dev);
  538. slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
  539. spin_unlock(&sl->lock);
  540. skb_tx_timestamp(skb);
  541. out:
  542. kfree_skb(skb);
  543. return NETDEV_TX_OK;
  544. }
  545. /******************************************
  546. * Routines looking at netdevice side.
  547. ******************************************/
  548. static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
  549. {
  550. int ret, actual, n;
  551. spin_lock(&sl->lock);
  552. if (!sl->tty) {
  553. spin_unlock(&sl->lock);
  554. return -ENODEV;
  555. }
  556. n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
  557. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  558. actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
  559. sl->xleft = n - actual;
  560. sl->xhead = sl->xbuff + actual;
  561. set_bit(SLF_XCMD, &sl->flags);
  562. spin_unlock(&sl->lock);
  563. ret = wait_event_interruptible_timeout(sl->xcmd_wait,
  564. !test_bit(SLF_XCMD, &sl->flags),
  565. HZ);
  566. clear_bit(SLF_XCMD, &sl->flags);
  567. if (ret == -ERESTARTSYS)
  568. return ret;
  569. if (ret == 0)
  570. return -ETIMEDOUT;
  571. return 0;
  572. }
  573. /* Netdevice UP -> DOWN routine */
  574. static int slcan_netdev_close(struct net_device *dev)
  575. {
  576. struct slcan *sl = netdev_priv(dev);
  577. int err;
  578. if (sl->can.bittiming.bitrate &&
  579. sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
  580. err = slcan_transmit_cmd(sl, "C\r");
  581. if (err)
  582. netdev_warn(dev,
  583. "failed to send close command 'C\\r'\n");
  584. }
  585. /* TTY discipline is running. */
  586. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  587. flush_work(&sl->tx_work);
  588. netif_stop_queue(dev);
  589. sl->rcount = 0;
  590. sl->xleft = 0;
  591. close_candev(dev);
  592. sl->can.state = CAN_STATE_STOPPED;
  593. if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
  594. sl->can.bittiming.bitrate = CAN_BITRATE_UNSET;
  595. return 0;
  596. }
  597. /* Netdevice DOWN -> UP routine */
  598. static int slcan_netdev_open(struct net_device *dev)
  599. {
  600. struct slcan *sl = netdev_priv(dev);
  601. unsigned char cmd[SLCAN_MTU];
  602. int err, s;
  603. /* The baud rate is not set with the command
  604. * `ip link set <iface> type can bitrate <baud>' and therefore
  605. * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
  606. * open_candev() to fail. So let's set to a fake value.
  607. */
  608. if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET)
  609. sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN;
  610. err = open_candev(dev);
  611. if (err) {
  612. netdev_err(dev, "failed to open can device\n");
  613. return err;
  614. }
  615. if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
  616. for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
  617. if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
  618. break;
  619. }
  620. /* The CAN framework has already validate the bitrate value,
  621. * so we can avoid to check if `s' has been properly set.
  622. */
  623. snprintf(cmd, sizeof(cmd), "C\rS%d\r", s);
  624. err = slcan_transmit_cmd(sl, cmd);
  625. if (err) {
  626. netdev_err(dev,
  627. "failed to send bitrate command 'C\\rS%d\\r'\n",
  628. s);
  629. goto cmd_transmit_failed;
  630. }
  631. if (test_bit(CF_ERR_RST, &sl->cmd_flags)) {
  632. err = slcan_transmit_cmd(sl, "F\r");
  633. if (err) {
  634. netdev_err(dev,
  635. "failed to send error command 'F\\r'\n");
  636. goto cmd_transmit_failed;
  637. }
  638. }
  639. if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  640. err = slcan_transmit_cmd(sl, "L\r");
  641. if (err) {
  642. netdev_err(dev,
  643. "failed to send listen-only command 'L\\r'\n");
  644. goto cmd_transmit_failed;
  645. }
  646. } else {
  647. err = slcan_transmit_cmd(sl, "O\r");
  648. if (err) {
  649. netdev_err(dev,
  650. "failed to send open command 'O\\r'\n");
  651. goto cmd_transmit_failed;
  652. }
  653. }
  654. }
  655. sl->can.state = CAN_STATE_ERROR_ACTIVE;
  656. netif_start_queue(dev);
  657. return 0;
  658. cmd_transmit_failed:
  659. close_candev(dev);
  660. return err;
  661. }
  662. static const struct net_device_ops slcan_netdev_ops = {
  663. .ndo_open = slcan_netdev_open,
  664. .ndo_stop = slcan_netdev_close,
  665. .ndo_start_xmit = slcan_netdev_xmit,
  666. };
  667. /******************************************
  668. * Routines looking at TTY side.
  669. ******************************************/
  670. /* Handle the 'receiver data ready' interrupt.
  671. * This function is called by the 'tty_io' module in the kernel when
  672. * a block of SLCAN data has been received, which can now be decapsulated
  673. * and sent on to some IP layer for further processing. This will not
  674. * be re-entered while running but other ldisc functions may be called
  675. * in parallel
  676. */
  677. static void slcan_receive_buf(struct tty_struct *tty, const u8 *cp,
  678. const u8 *fp, size_t count)
  679. {
  680. struct slcan *sl = tty->disc_data;
  681. if (!netif_running(sl->dev))
  682. return;
  683. /* Read the characters out of the buffer */
  684. while (count--) {
  685. if (fp && *fp++) {
  686. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  687. sl->dev->stats.rx_errors++;
  688. cp++;
  689. continue;
  690. }
  691. slcan_unesc(sl, *cp++);
  692. }
  693. }
  694. /* Open the high-level part of the SLCAN channel.
  695. * This function is called by the TTY module when the
  696. * SLCAN line discipline is called for.
  697. *
  698. * Called in process context serialized from other ldisc calls.
  699. */
  700. static int slcan_open(struct tty_struct *tty)
  701. {
  702. struct net_device *dev;
  703. struct slcan *sl;
  704. int err;
  705. if (!capable(CAP_NET_ADMIN))
  706. return -EPERM;
  707. if (!tty->ops->write)
  708. return -EOPNOTSUPP;
  709. dev = alloc_candev(sizeof(*sl), 1);
  710. if (!dev)
  711. return -ENFILE;
  712. sl = netdev_priv(dev);
  713. /* Configure TTY interface */
  714. tty->receive_room = 65536; /* We don't flow control */
  715. sl->rcount = 0;
  716. sl->xleft = 0;
  717. spin_lock_init(&sl->lock);
  718. INIT_WORK(&sl->tx_work, slcan_transmit);
  719. init_waitqueue_head(&sl->xcmd_wait);
  720. /* Configure CAN metadata */
  721. sl->can.bitrate_const = slcan_bitrate_const;
  722. sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const);
  723. sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
  724. /* Configure netdev interface */
  725. sl->dev = dev;
  726. dev->netdev_ops = &slcan_netdev_ops;
  727. dev->ethtool_ops = &slcan_ethtool_ops;
  728. /* Mark ldisc channel as alive */
  729. sl->tty = tty;
  730. tty->disc_data = sl;
  731. err = register_candev(dev);
  732. if (err) {
  733. free_candev(dev);
  734. pr_err("can't register candev\n");
  735. return err;
  736. }
  737. netdev_info(dev, "slcan on %s.\n", tty->name);
  738. /* TTY layer expects 0 on success */
  739. return 0;
  740. }
  741. /* Close down a SLCAN channel.
  742. * This means flushing out any pending queues, and then returning. This
  743. * call is serialized against other ldisc functions.
  744. * Once this is called, no other ldisc function of ours is entered.
  745. *
  746. * We also use this method for a hangup event.
  747. */
  748. static void slcan_close(struct tty_struct *tty)
  749. {
  750. struct slcan *sl = tty->disc_data;
  751. unregister_candev(sl->dev);
  752. /*
  753. * The netdev needn't be UP (so .ndo_stop() is not called). Hence make
  754. * sure this is not running before freeing it up.
  755. */
  756. flush_work(&sl->tx_work);
  757. /* Mark channel as dead */
  758. spin_lock_bh(&sl->lock);
  759. tty->disc_data = NULL;
  760. sl->tty = NULL;
  761. spin_unlock_bh(&sl->lock);
  762. netdev_info(sl->dev, "slcan off %s.\n", tty->name);
  763. free_candev(sl->dev);
  764. }
  765. /* Perform I/O control on an active SLCAN channel. */
  766. static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd,
  767. unsigned long arg)
  768. {
  769. struct slcan *sl = tty->disc_data;
  770. unsigned int tmp;
  771. switch (cmd) {
  772. case SIOCGIFNAME:
  773. tmp = strlen(sl->dev->name) + 1;
  774. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  775. return -EFAULT;
  776. return 0;
  777. case SIOCSIFHWADDR:
  778. return -EINVAL;
  779. default:
  780. return tty_mode_ioctl(tty, cmd, arg);
  781. }
  782. }
  783. static struct tty_ldisc_ops slcan_ldisc = {
  784. .owner = THIS_MODULE,
  785. .num = N_SLCAN,
  786. .name = KBUILD_MODNAME,
  787. .open = slcan_open,
  788. .close = slcan_close,
  789. .ioctl = slcan_ioctl,
  790. .receive_buf = slcan_receive_buf,
  791. .write_wakeup = slcan_write_wakeup,
  792. };
  793. static int __init slcan_init(void)
  794. {
  795. int status;
  796. pr_info("serial line CAN interface driver\n");
  797. /* Fill in our line protocol discipline, and register it */
  798. status = tty_register_ldisc(&slcan_ldisc);
  799. if (status)
  800. pr_err("can't register line discipline\n");
  801. return status;
  802. }
  803. static void __exit slcan_exit(void)
  804. {
  805. /* This will only be called when all channels have been closed by
  806. * userspace - tty_ldisc.c takes care of the module's refcount.
  807. */
  808. tty_unregister_ldisc(&slcan_ldisc);
  809. }
  810. module_init(slcan_init);
  811. module_exit(slcan_exit);