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/init.h>
  51. #include <linux/kernel.h>
  52. #include <linux/workqueue.h>
  53. #include <linux/can.h>
  54. #include <linux/can/dev.h>
  55. #include <linux/can/skb.h>
  56. #include "slcan.h"
  57. MODULE_ALIAS_LDISC(N_SLCAN);
  58. MODULE_DESCRIPTION("serial line CAN interface");
  59. MODULE_LICENSE("GPL");
  60. MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
  61. MODULE_AUTHOR("Dario Binacchi <dario.binacchi@amarulasolutions.com>");
  62. /* maximum rx buffer len: extended CAN frame with timestamp */
  63. #define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1)
  64. #define SLCAN_CMD_LEN 1
  65. #define SLCAN_SFF_ID_LEN 3
  66. #define SLCAN_EFF_ID_LEN 8
  67. #define SLCAN_DATA_LENGTH_LEN 1
  68. #define SLCAN_ERROR_LEN 1
  69. #define SLCAN_STATE_LEN 1
  70. #define SLCAN_STATE_BE_RXCNT_LEN 3
  71. #define SLCAN_STATE_BE_TXCNT_LEN 3
  72. #define SLCAN_STATE_MSG_LEN (SLCAN_CMD_LEN + \
  73. SLCAN_STATE_LEN + \
  74. SLCAN_STATE_BE_RXCNT_LEN + \
  75. SLCAN_STATE_BE_TXCNT_LEN)
  76. #define SLCAN_ERROR_MSG_LEN_MIN (SLCAN_CMD_LEN + \
  77. SLCAN_ERROR_LEN + \
  78. SLCAN_DATA_LENGTH_LEN)
  79. #define SLCAN_FRAME_MSG_LEN_MIN (SLCAN_CMD_LEN + \
  80. SLCAN_SFF_ID_LEN + \
  81. SLCAN_DATA_LENGTH_LEN)
  82. struct slcan {
  83. struct can_priv can;
  84. /* Various fields. */
  85. struct tty_struct *tty; /* ptr to TTY structure */
  86. struct net_device *dev; /* easy for intr handling */
  87. spinlock_t lock;
  88. struct work_struct tx_work; /* Flushes transmit buffer */
  89. /* These are pointers to the malloc()ed frame buffers. */
  90. unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */
  91. int rcount; /* received chars counter */
  92. unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/
  93. unsigned char *xhead; /* pointer to next XMIT byte */
  94. int xleft; /* bytes left in XMIT queue */
  95. unsigned long flags; /* Flag values/ mode etc */
  96. #define SLF_ERROR 0 /* Parity, etc. error */
  97. #define SLF_XCMD 1 /* Command transmission */
  98. unsigned long cmd_flags; /* Command flags */
  99. #define CF_ERR_RST 0 /* Reset errors on open */
  100. wait_queue_head_t xcmd_wait; /* Wait queue for commands */
  101. /* transmission */
  102. };
  103. static const u32 slcan_bitrate_const[] = {
  104. 10000, 20000, 50000, 100000, 125000,
  105. 250000, 500000, 800000, 1000000
  106. };
  107. bool slcan_err_rst_on_open(struct net_device *ndev)
  108. {
  109. struct slcan *sl = netdev_priv(ndev);
  110. return !!test_bit(CF_ERR_RST, &sl->cmd_flags);
  111. }
  112. int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on)
  113. {
  114. struct slcan *sl = netdev_priv(ndev);
  115. if (netif_running(ndev))
  116. return -EBUSY;
  117. if (on)
  118. set_bit(CF_ERR_RST, &sl->cmd_flags);
  119. else
  120. clear_bit(CF_ERR_RST, &sl->cmd_flags);
  121. return 0;
  122. }
  123. /*************************************************************************
  124. * SLCAN ENCAPSULATION FORMAT *
  125. *************************************************************************/
  126. /* A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
  127. * frame format) a data length code (len) which can be from 0 to 8
  128. * and up to <len> data bytes as payload.
  129. * Additionally a CAN frame may become a remote transmission frame if the
  130. * RTR-bit is set. This causes another ECU to send a CAN frame with the
  131. * given can_id.
  132. *
  133. * The SLCAN ASCII representation of these different frame types is:
  134. * <type> <id> <dlc> <data>*
  135. *
  136. * Extended frames (29 bit) are defined by capital characters in the type.
  137. * RTR frames are defined as 'r' types - normal frames have 't' type:
  138. * t => 11 bit data frame
  139. * r => 11 bit RTR frame
  140. * T => 29 bit data frame
  141. * R => 29 bit RTR frame
  142. *
  143. * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
  144. * The <dlc> is a one byte ASCII number ('0' - '8')
  145. * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
  146. *
  147. * Examples:
  148. *
  149. * t1230 : can_id 0x123, len 0, no data
  150. * t4563112233 : can_id 0x456, len 3, data 0x11 0x22 0x33
  151. * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, len 2, data 0xAA 0x55
  152. * r1230 : can_id 0x123, len 0, no data, remote transmission request
  153. *
  154. */
  155. /*************************************************************************
  156. * STANDARD SLCAN DECAPSULATION *
  157. *************************************************************************/
  158. /* Send one completely decapsulated can_frame to the network layer */
  159. static void slcan_bump_frame(struct slcan *sl)
  160. {
  161. struct sk_buff *skb;
  162. struct can_frame *cf;
  163. int i, tmp;
  164. u32 tmpid;
  165. char *cmd = sl->rbuff;
  166. if (sl->rcount < SLCAN_FRAME_MSG_LEN_MIN)
  167. return;
  168. skb = alloc_can_skb(sl->dev, &cf);
  169. if (unlikely(!skb)) {
  170. sl->dev->stats.rx_dropped++;
  171. return;
  172. }
  173. switch (*cmd) {
  174. case 'r':
  175. cf->can_id = CAN_RTR_FLAG;
  176. fallthrough;
  177. case 't':
  178. /* store dlc ASCII value and terminate SFF CAN ID string */
  179. cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN];
  180. sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0;
  181. /* point to payload data behind the dlc */
  182. cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1;
  183. break;
  184. case 'R':
  185. cf->can_id = CAN_RTR_FLAG;
  186. fallthrough;
  187. case 'T':
  188. cf->can_id |= CAN_EFF_FLAG;
  189. /* store dlc ASCII value and terminate EFF CAN ID string */
  190. cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN];
  191. sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0;
  192. /* point to payload data behind the dlc */
  193. cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1;
  194. break;
  195. default:
  196. goto decode_failed;
  197. }
  198. if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid))
  199. goto decode_failed;
  200. cf->can_id |= tmpid;
  201. /* get len from sanitized ASCII value */
  202. if (cf->len >= '0' && cf->len < '9')
  203. cf->len -= '0';
  204. else
  205. goto decode_failed;
  206. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  207. if (!(cf->can_id & CAN_RTR_FLAG)) {
  208. for (i = 0; i < cf->len; i++) {
  209. tmp = hex_to_bin(*cmd++);
  210. if (tmp < 0)
  211. goto decode_failed;
  212. cf->data[i] = (tmp << 4);
  213. tmp = hex_to_bin(*cmd++);
  214. if (tmp < 0)
  215. goto decode_failed;
  216. cf->data[i] |= tmp;
  217. }
  218. }
  219. sl->dev->stats.rx_packets++;
  220. if (!(cf->can_id & CAN_RTR_FLAG))
  221. sl->dev->stats.rx_bytes += cf->len;
  222. netif_rx(skb);
  223. return;
  224. decode_failed:
  225. sl->dev->stats.rx_errors++;
  226. dev_kfree_skb(skb);
  227. }
  228. /* A change state frame must contain state info and receive and transmit
  229. * error counters.
  230. *
  231. * Examples:
  232. *
  233. * sb256256 : state bus-off: rx counter 256, tx counter 256
  234. * sa057033 : state active, rx counter 57, tx counter 33
  235. */
  236. static void slcan_bump_state(struct slcan *sl)
  237. {
  238. struct net_device *dev = sl->dev;
  239. struct sk_buff *skb;
  240. struct can_frame *cf;
  241. char *cmd = sl->rbuff;
  242. u32 rxerr, txerr;
  243. enum can_state state, rx_state, tx_state;
  244. switch (cmd[1]) {
  245. case 'a':
  246. state = CAN_STATE_ERROR_ACTIVE;
  247. break;
  248. case 'w':
  249. state = CAN_STATE_ERROR_WARNING;
  250. break;
  251. case 'p':
  252. state = CAN_STATE_ERROR_PASSIVE;
  253. break;
  254. case 'b':
  255. state = CAN_STATE_BUS_OFF;
  256. break;
  257. default:
  258. return;
  259. }
  260. if (state == sl->can.state || sl->rcount != SLCAN_STATE_MSG_LEN)
  261. return;
  262. cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1;
  263. cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0;
  264. if (kstrtou32(cmd, 10, &txerr))
  265. return;
  266. *cmd = 0;
  267. cmd -= SLCAN_STATE_BE_RXCNT_LEN;
  268. if (kstrtou32(cmd, 10, &rxerr))
  269. return;
  270. skb = alloc_can_err_skb(dev, &cf);
  271. tx_state = txerr >= rxerr ? state : 0;
  272. rx_state = txerr <= rxerr ? state : 0;
  273. can_change_state(dev, cf, tx_state, rx_state);
  274. if (state == CAN_STATE_BUS_OFF) {
  275. can_bus_off(dev);
  276. } else if (skb) {
  277. cf->can_id |= CAN_ERR_CNT;
  278. cf->data[6] = txerr;
  279. cf->data[7] = rxerr;
  280. }
  281. if (skb)
  282. netif_rx(skb);
  283. }
  284. /* An error frame can contain more than one type of error.
  285. *
  286. * Examples:
  287. *
  288. * e1a : len 1, errors: ACK error
  289. * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error
  290. */
  291. static void slcan_bump_err(struct slcan *sl)
  292. {
  293. struct net_device *dev = sl->dev;
  294. struct sk_buff *skb;
  295. struct can_frame *cf;
  296. char *cmd = sl->rbuff;
  297. bool rx_errors = false, tx_errors = false, rx_over_errors = false;
  298. int i, len;
  299. if (sl->rcount < SLCAN_ERROR_MSG_LEN_MIN)
  300. return;
  301. /* get len from sanitized ASCII value */
  302. len = cmd[1];
  303. if (len >= '0' && len < '9')
  304. len -= '0';
  305. else
  306. return;
  307. if ((len + SLCAN_CMD_LEN + 1) > sl->rcount)
  308. return;
  309. skb = alloc_can_err_skb(dev, &cf);
  310. if (skb)
  311. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  312. cmd += SLCAN_CMD_LEN + 1;
  313. for (i = 0; i < len; i++, cmd++) {
  314. switch (*cmd) {
  315. case 'a':
  316. netdev_dbg(dev, "ACK error\n");
  317. tx_errors = true;
  318. if (skb) {
  319. cf->can_id |= CAN_ERR_ACK;
  320. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  321. }
  322. break;
  323. case 'b':
  324. netdev_dbg(dev, "Bit0 error\n");
  325. tx_errors = true;
  326. if (skb)
  327. cf->data[2] |= CAN_ERR_PROT_BIT0;
  328. break;
  329. case 'B':
  330. netdev_dbg(dev, "Bit1 error\n");
  331. tx_errors = true;
  332. if (skb)
  333. cf->data[2] |= CAN_ERR_PROT_BIT1;
  334. break;
  335. case 'c':
  336. netdev_dbg(dev, "CRC error\n");
  337. rx_errors = true;
  338. if (skb) {
  339. cf->data[2] |= CAN_ERR_PROT_BIT;
  340. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  341. }
  342. break;
  343. case 'f':
  344. netdev_dbg(dev, "Form Error\n");
  345. rx_errors = true;
  346. if (skb)
  347. cf->data[2] |= CAN_ERR_PROT_FORM;
  348. break;
  349. case 'o':
  350. netdev_dbg(dev, "Rx overrun error\n");
  351. rx_over_errors = true;
  352. rx_errors = true;
  353. if (skb) {
  354. cf->can_id |= CAN_ERR_CRTL;
  355. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  356. }
  357. break;
  358. case 'O':
  359. netdev_dbg(dev, "Tx overrun error\n");
  360. tx_errors = true;
  361. if (skb) {
  362. cf->can_id |= CAN_ERR_CRTL;
  363. cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW;
  364. }
  365. break;
  366. case 's':
  367. netdev_dbg(dev, "Stuff error\n");
  368. rx_errors = true;
  369. if (skb)
  370. cf->data[2] |= CAN_ERR_PROT_STUFF;
  371. break;
  372. default:
  373. if (skb)
  374. dev_kfree_skb(skb);
  375. return;
  376. }
  377. }
  378. if (rx_errors)
  379. dev->stats.rx_errors++;
  380. if (rx_over_errors)
  381. dev->stats.rx_over_errors++;
  382. if (tx_errors)
  383. dev->stats.tx_errors++;
  384. if (skb)
  385. netif_rx(skb);
  386. }
  387. static void slcan_bump(struct slcan *sl)
  388. {
  389. switch (sl->rbuff[0]) {
  390. case 'r':
  391. fallthrough;
  392. case 't':
  393. fallthrough;
  394. case 'R':
  395. fallthrough;
  396. case 'T':
  397. return slcan_bump_frame(sl);
  398. case 'e':
  399. return slcan_bump_err(sl);
  400. case 's':
  401. return slcan_bump_state(sl);
  402. default:
  403. return;
  404. }
  405. }
  406. /* parse tty input stream */
  407. static void slcan_unesc(struct slcan *sl, unsigned char s)
  408. {
  409. if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
  410. if (!test_and_clear_bit(SLF_ERROR, &sl->flags))
  411. slcan_bump(sl);
  412. sl->rcount = 0;
  413. } else {
  414. if (!test_bit(SLF_ERROR, &sl->flags)) {
  415. if (sl->rcount < SLCAN_MTU) {
  416. sl->rbuff[sl->rcount++] = s;
  417. return;
  418. }
  419. sl->dev->stats.rx_over_errors++;
  420. set_bit(SLF_ERROR, &sl->flags);
  421. }
  422. }
  423. }
  424. /*************************************************************************
  425. * STANDARD SLCAN ENCAPSULATION *
  426. *************************************************************************/
  427. /* Encapsulate one can_frame and stuff into a TTY queue. */
  428. static void slcan_encaps(struct slcan *sl, struct can_frame *cf)
  429. {
  430. int actual, i;
  431. unsigned char *pos;
  432. unsigned char *endpos;
  433. canid_t id = cf->can_id;
  434. pos = sl->xbuff;
  435. if (cf->can_id & CAN_RTR_FLAG)
  436. *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
  437. else
  438. *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
  439. /* determine number of chars for the CAN-identifier */
  440. if (cf->can_id & CAN_EFF_FLAG) {
  441. id &= CAN_EFF_MASK;
  442. endpos = pos + SLCAN_EFF_ID_LEN;
  443. } else {
  444. *pos |= 0x20; /* convert R/T to lower case for SFF */
  445. id &= CAN_SFF_MASK;
  446. endpos = pos + SLCAN_SFF_ID_LEN;
  447. }
  448. /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
  449. pos++;
  450. while (endpos >= pos) {
  451. *endpos-- = hex_asc_upper[id & 0xf];
  452. id >>= 4;
  453. }
  454. pos += (cf->can_id & CAN_EFF_FLAG) ?
  455. SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN;
  456. *pos++ = cf->len + '0';
  457. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  458. if (!(cf->can_id & CAN_RTR_FLAG)) {
  459. for (i = 0; i < cf->len; i++)
  460. pos = hex_byte_pack_upper(pos, cf->data[i]);
  461. sl->dev->stats.tx_bytes += cf->len;
  462. }
  463. *pos++ = '\r';
  464. /* Order of next two lines is *very* important.
  465. * When we are sending a little amount of data,
  466. * the transfer may be completed inside the ops->write()
  467. * routine, because it's running with interrupts enabled.
  468. * In this case we *never* got WRITE_WAKEUP event,
  469. * if we did not request it before write operation.
  470. * 14 Oct 1994 Dmitry Gorodchanin.
  471. */
  472. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  473. actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
  474. sl->xleft = (pos - sl->xbuff) - actual;
  475. sl->xhead = sl->xbuff + actual;
  476. }
  477. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  478. static void slcan_transmit(struct work_struct *work)
  479. {
  480. struct slcan *sl = container_of(work, struct slcan, tx_work);
  481. int actual;
  482. spin_lock_bh(&sl->lock);
  483. /* First make sure we're connected. */
  484. if (unlikely(!netif_running(sl->dev)) &&
  485. likely(!test_bit(SLF_XCMD, &sl->flags))) {
  486. spin_unlock_bh(&sl->lock);
  487. return;
  488. }
  489. if (sl->xleft <= 0) {
  490. if (unlikely(test_bit(SLF_XCMD, &sl->flags))) {
  491. clear_bit(SLF_XCMD, &sl->flags);
  492. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  493. spin_unlock_bh(&sl->lock);
  494. wake_up(&sl->xcmd_wait);
  495. return;
  496. }
  497. /* Now serial buffer is almost free & we can start
  498. * transmission of another packet
  499. */
  500. sl->dev->stats.tx_packets++;
  501. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  502. spin_unlock_bh(&sl->lock);
  503. netif_wake_queue(sl->dev);
  504. return;
  505. }
  506. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  507. sl->xleft -= actual;
  508. sl->xhead += actual;
  509. spin_unlock_bh(&sl->lock);
  510. }
  511. /* Called by the driver when there's room for more data.
  512. * Schedule the transmit.
  513. */
  514. static void slcan_write_wakeup(struct tty_struct *tty)
  515. {
  516. struct slcan *sl = tty->disc_data;
  517. schedule_work(&sl->tx_work);
  518. }
  519. /* Send a can_frame to a TTY queue. */
  520. static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb,
  521. struct net_device *dev)
  522. {
  523. struct slcan *sl = netdev_priv(dev);
  524. if (can_dev_dropped_skb(dev, skb))
  525. return NETDEV_TX_OK;
  526. spin_lock(&sl->lock);
  527. if (!netif_running(dev)) {
  528. spin_unlock(&sl->lock);
  529. netdev_warn(dev, "xmit: iface is down\n");
  530. goto out;
  531. }
  532. if (!sl->tty) {
  533. spin_unlock(&sl->lock);
  534. goto out;
  535. }
  536. netif_stop_queue(sl->dev);
  537. slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */
  538. spin_unlock(&sl->lock);
  539. skb_tx_timestamp(skb);
  540. out:
  541. kfree_skb(skb);
  542. return NETDEV_TX_OK;
  543. }
  544. /******************************************
  545. * Routines looking at netdevice side.
  546. ******************************************/
  547. static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd)
  548. {
  549. int ret, actual, n;
  550. spin_lock(&sl->lock);
  551. if (!sl->tty) {
  552. spin_unlock(&sl->lock);
  553. return -ENODEV;
  554. }
  555. n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd);
  556. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  557. actual = sl->tty->ops->write(sl->tty, sl->xbuff, n);
  558. sl->xleft = n - actual;
  559. sl->xhead = sl->xbuff + actual;
  560. set_bit(SLF_XCMD, &sl->flags);
  561. spin_unlock(&sl->lock);
  562. ret = wait_event_interruptible_timeout(sl->xcmd_wait,
  563. !test_bit(SLF_XCMD, &sl->flags),
  564. HZ);
  565. clear_bit(SLF_XCMD, &sl->flags);
  566. if (ret == -ERESTARTSYS)
  567. return ret;
  568. if (ret == 0)
  569. return -ETIMEDOUT;
  570. return 0;
  571. }
  572. /* Netdevice UP -> DOWN routine */
  573. static int slcan_netdev_close(struct net_device *dev)
  574. {
  575. struct slcan *sl = netdev_priv(dev);
  576. int err;
  577. if (sl->can.bittiming.bitrate &&
  578. sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
  579. err = slcan_transmit_cmd(sl, "C\r");
  580. if (err)
  581. netdev_warn(dev,
  582. "failed to send close command 'C\\r'\n");
  583. }
  584. /* TTY discipline is running. */
  585. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  586. flush_work(&sl->tx_work);
  587. netif_stop_queue(dev);
  588. sl->rcount = 0;
  589. sl->xleft = 0;
  590. close_candev(dev);
  591. sl->can.state = CAN_STATE_STOPPED;
  592. if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN)
  593. sl->can.bittiming.bitrate = CAN_BITRATE_UNSET;
  594. return 0;
  595. }
  596. /* Netdevice DOWN -> UP routine */
  597. static int slcan_netdev_open(struct net_device *dev)
  598. {
  599. struct slcan *sl = netdev_priv(dev);
  600. unsigned char cmd[SLCAN_MTU];
  601. int err, s;
  602. /* The baud rate is not set with the command
  603. * `ip link set <iface> type can bitrate <baud>' and therefore
  604. * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing
  605. * open_candev() to fail. So let's set to a fake value.
  606. */
  607. if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET)
  608. sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN;
  609. err = open_candev(dev);
  610. if (err) {
  611. netdev_err(dev, "failed to open can device\n");
  612. return err;
  613. }
  614. if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) {
  615. for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) {
  616. if (sl->can.bittiming.bitrate == slcan_bitrate_const[s])
  617. break;
  618. }
  619. /* The CAN framework has already validate the bitrate value,
  620. * so we can avoid to check if `s' has been properly set.
  621. */
  622. snprintf(cmd, sizeof(cmd), "C\rS%d\r", s);
  623. err = slcan_transmit_cmd(sl, cmd);
  624. if (err) {
  625. netdev_err(dev,
  626. "failed to send bitrate command 'C\\rS%d\\r'\n",
  627. s);
  628. goto cmd_transmit_failed;
  629. }
  630. if (test_bit(CF_ERR_RST, &sl->cmd_flags)) {
  631. err = slcan_transmit_cmd(sl, "F\r");
  632. if (err) {
  633. netdev_err(dev,
  634. "failed to send error command 'F\\r'\n");
  635. goto cmd_transmit_failed;
  636. }
  637. }
  638. if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  639. err = slcan_transmit_cmd(sl, "L\r");
  640. if (err) {
  641. netdev_err(dev,
  642. "failed to send listen-only command 'L\\r'\n");
  643. goto cmd_transmit_failed;
  644. }
  645. } else {
  646. err = slcan_transmit_cmd(sl, "O\r");
  647. if (err) {
  648. netdev_err(dev,
  649. "failed to send open command 'O\\r'\n");
  650. goto cmd_transmit_failed;
  651. }
  652. }
  653. }
  654. sl->can.state = CAN_STATE_ERROR_ACTIVE;
  655. netif_start_queue(dev);
  656. return 0;
  657. cmd_transmit_failed:
  658. close_candev(dev);
  659. return err;
  660. }
  661. static const struct net_device_ops slcan_netdev_ops = {
  662. .ndo_open = slcan_netdev_open,
  663. .ndo_stop = slcan_netdev_close,
  664. .ndo_start_xmit = slcan_netdev_xmit,
  665. .ndo_change_mtu = can_change_mtu,
  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);