slcan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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
  5. *
  6. * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
  7. * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
  8. * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see http://www.gnu.org/licenses/gpl.html
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  34. * DAMAGE.
  35. *
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/bitops.h>
  41. #include <linux/string.h>
  42. #include <linux/tty.h>
  43. #include <linux/errno.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/rtnetlink.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/if_ether.h>
  49. #include <linux/sched.h>
  50. #include <linux/delay.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/skb.h>
  56. #include <linux/can/can-ml.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. #define SLCAN_MAGIC 0x53CA
  62. static int maxdev = 10; /* MAX number of SLCAN channels;
  63. This can be overridden with
  64. insmod slcan.ko maxdev=nnn */
  65. module_param(maxdev, int, 0);
  66. MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
  67. /* maximum rx buffer len: extended CAN frame with timestamp */
  68. #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
  69. #define SLC_CMD_LEN 1
  70. #define SLC_SFF_ID_LEN 3
  71. #define SLC_EFF_ID_LEN 8
  72. struct slcan {
  73. int magic;
  74. /* Various fields. */
  75. struct tty_struct *tty; /* ptr to TTY structure */
  76. struct net_device *dev; /* easy for intr handling */
  77. spinlock_t lock;
  78. struct work_struct tx_work; /* Flushes transmit buffer */
  79. /* These are pointers to the malloc()ed frame buffers. */
  80. unsigned char rbuff[SLC_MTU]; /* receiver buffer */
  81. int rcount; /* received chars counter */
  82. unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
  83. unsigned char *xhead; /* pointer to next XMIT byte */
  84. int xleft; /* bytes left in XMIT queue */
  85. unsigned long flags; /* Flag values/ mode etc */
  86. #define SLF_INUSE 0 /* Channel in use */
  87. #define SLF_ERROR 1 /* Parity, etc. error */
  88. };
  89. static struct net_device **slcan_devs;
  90. /************************************************************************
  91. * SLCAN ENCAPSULATION FORMAT *
  92. ************************************************************************/
  93. /*
  94. * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
  95. * frame format) a data length code (can_dlc) which can be from 0 to 8
  96. * and up to <can_dlc> data bytes as payload.
  97. * Additionally a CAN frame may become a remote transmission frame if the
  98. * RTR-bit is set. This causes another ECU to send a CAN frame with the
  99. * given can_id.
  100. *
  101. * The SLCAN ASCII representation of these different frame types is:
  102. * <type> <id> <dlc> <data>*
  103. *
  104. * Extended frames (29 bit) are defined by capital characters in the type.
  105. * RTR frames are defined as 'r' types - normal frames have 't' type:
  106. * t => 11 bit data frame
  107. * r => 11 bit RTR frame
  108. * T => 29 bit data frame
  109. * R => 29 bit RTR frame
  110. *
  111. * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
  112. * The <dlc> is a one byte ASCII number ('0' - '8')
  113. * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
  114. *
  115. * Examples:
  116. *
  117. * t1230 : can_id 0x123, can_dlc 0, no data
  118. * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
  119. * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
  120. * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
  121. *
  122. */
  123. /************************************************************************
  124. * STANDARD SLCAN DECAPSULATION *
  125. ************************************************************************/
  126. /* Send one completely decapsulated can_frame to the network layer */
  127. static void slc_bump(struct slcan *sl)
  128. {
  129. struct sk_buff *skb;
  130. struct can_frame cf;
  131. int i, tmp;
  132. u32 tmpid;
  133. char *cmd = sl->rbuff;
  134. memset(&cf, 0, sizeof(cf));
  135. switch (*cmd) {
  136. case 'r':
  137. cf.can_id = CAN_RTR_FLAG;
  138. /* fallthrough */
  139. case 't':
  140. /* store dlc ASCII value and terminate SFF CAN ID string */
  141. cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
  142. sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
  143. /* point to payload data behind the dlc */
  144. cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
  145. break;
  146. case 'R':
  147. cf.can_id = CAN_RTR_FLAG;
  148. /* fallthrough */
  149. case 'T':
  150. cf.can_id |= CAN_EFF_FLAG;
  151. /* store dlc ASCII value and terminate EFF CAN ID string */
  152. cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
  153. sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
  154. /* point to payload data behind the dlc */
  155. cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
  156. break;
  157. default:
  158. return;
  159. }
  160. if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
  161. return;
  162. cf.can_id |= tmpid;
  163. /* get can_dlc from sanitized ASCII value */
  164. if (cf.can_dlc >= '0' && cf.can_dlc < '9')
  165. cf.can_dlc -= '0';
  166. else
  167. return;
  168. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  169. if (!(cf.can_id & CAN_RTR_FLAG)) {
  170. for (i = 0; i < cf.can_dlc; i++) {
  171. tmp = hex_to_bin(*cmd++);
  172. if (tmp < 0)
  173. return;
  174. cf.data[i] = (tmp << 4);
  175. tmp = hex_to_bin(*cmd++);
  176. if (tmp < 0)
  177. return;
  178. cf.data[i] |= tmp;
  179. }
  180. }
  181. skb = dev_alloc_skb(sizeof(struct can_frame) +
  182. sizeof(struct can_skb_priv));
  183. if (!skb)
  184. return;
  185. skb->dev = sl->dev;
  186. skb->protocol = htons(ETH_P_CAN);
  187. skb->pkt_type = PACKET_BROADCAST;
  188. skb->ip_summed = CHECKSUM_UNNECESSARY;
  189. can_skb_reserve(skb);
  190. can_skb_prv(skb)->ifindex = sl->dev->ifindex;
  191. can_skb_prv(skb)->skbcnt = 0;
  192. skb_put_data(skb, &cf, sizeof(struct can_frame));
  193. sl->dev->stats.rx_packets++;
  194. sl->dev->stats.rx_bytes += cf.can_dlc;
  195. netif_rx_ni(skb);
  196. }
  197. /* parse tty input stream */
  198. static void slcan_unesc(struct slcan *sl, unsigned char s)
  199. {
  200. if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
  201. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  202. (sl->rcount > 4)) {
  203. slc_bump(sl);
  204. }
  205. sl->rcount = 0;
  206. } else {
  207. if (!test_bit(SLF_ERROR, &sl->flags)) {
  208. if (sl->rcount < SLC_MTU) {
  209. sl->rbuff[sl->rcount++] = s;
  210. return;
  211. } else {
  212. sl->dev->stats.rx_over_errors++;
  213. set_bit(SLF_ERROR, &sl->flags);
  214. }
  215. }
  216. }
  217. }
  218. /************************************************************************
  219. * STANDARD SLCAN ENCAPSULATION *
  220. ************************************************************************/
  221. /* Encapsulate one can_frame and stuff into a TTY queue. */
  222. static void slc_encaps(struct slcan *sl, struct can_frame *cf)
  223. {
  224. int actual, i;
  225. unsigned char *pos;
  226. unsigned char *endpos;
  227. canid_t id = cf->can_id;
  228. pos = sl->xbuff;
  229. if (cf->can_id & CAN_RTR_FLAG)
  230. *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
  231. else
  232. *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
  233. /* determine number of chars for the CAN-identifier */
  234. if (cf->can_id & CAN_EFF_FLAG) {
  235. id &= CAN_EFF_MASK;
  236. endpos = pos + SLC_EFF_ID_LEN;
  237. } else {
  238. *pos |= 0x20; /* convert R/T to lower case for SFF */
  239. id &= CAN_SFF_MASK;
  240. endpos = pos + SLC_SFF_ID_LEN;
  241. }
  242. /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
  243. pos++;
  244. while (endpos >= pos) {
  245. *endpos-- = hex_asc_upper[id & 0xf];
  246. id >>= 4;
  247. }
  248. pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
  249. *pos++ = cf->can_dlc + '0';
  250. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  251. if (!(cf->can_id & CAN_RTR_FLAG)) {
  252. for (i = 0; i < cf->can_dlc; i++)
  253. pos = hex_byte_pack_upper(pos, cf->data[i]);
  254. }
  255. *pos++ = '\r';
  256. /* Order of next two lines is *very* important.
  257. * When we are sending a little amount of data,
  258. * the transfer may be completed inside the ops->write()
  259. * routine, because it's running with interrupts enabled.
  260. * In this case we *never* got WRITE_WAKEUP event,
  261. * if we did not request it before write operation.
  262. * 14 Oct 1994 Dmitry Gorodchanin.
  263. */
  264. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  265. actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
  266. sl->xleft = (pos - sl->xbuff) - actual;
  267. sl->xhead = sl->xbuff + actual;
  268. sl->dev->stats.tx_bytes += cf->can_dlc;
  269. }
  270. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  271. static void slcan_transmit(struct work_struct *work)
  272. {
  273. struct slcan *sl = container_of(work, struct slcan, tx_work);
  274. int actual;
  275. spin_lock_bh(&sl->lock);
  276. /* First make sure we're connected. */
  277. if (!sl->tty || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) {
  278. spin_unlock_bh(&sl->lock);
  279. return;
  280. }
  281. if (sl->xleft <= 0) {
  282. /* Now serial buffer is almost free & we can start
  283. * transmission of another packet */
  284. sl->dev->stats.tx_packets++;
  285. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  286. spin_unlock_bh(&sl->lock);
  287. netif_wake_queue(sl->dev);
  288. return;
  289. }
  290. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  291. sl->xleft -= actual;
  292. sl->xhead += actual;
  293. spin_unlock_bh(&sl->lock);
  294. }
  295. /*
  296. * Called by the driver when there's room for more data.
  297. * Schedule the transmit.
  298. */
  299. static void slcan_write_wakeup(struct tty_struct *tty)
  300. {
  301. struct slcan *sl;
  302. rcu_read_lock();
  303. sl = rcu_dereference(tty->disc_data);
  304. if (!sl)
  305. goto out;
  306. schedule_work(&sl->tx_work);
  307. out:
  308. rcu_read_unlock();
  309. }
  310. /* Send a can_frame to a TTY queue. */
  311. static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
  312. {
  313. struct slcan *sl = netdev_priv(dev);
  314. if (skb->len != CAN_MTU)
  315. goto out;
  316. spin_lock(&sl->lock);
  317. if (!netif_running(dev)) {
  318. spin_unlock(&sl->lock);
  319. printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
  320. goto out;
  321. }
  322. if (sl->tty == NULL) {
  323. spin_unlock(&sl->lock);
  324. goto out;
  325. }
  326. netif_stop_queue(sl->dev);
  327. slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
  328. spin_unlock(&sl->lock);
  329. out:
  330. kfree_skb(skb);
  331. return NETDEV_TX_OK;
  332. }
  333. /******************************************
  334. * Routines looking at netdevice side.
  335. ******************************************/
  336. /* Netdevice UP -> DOWN routine */
  337. static int slc_close(struct net_device *dev)
  338. {
  339. struct slcan *sl = netdev_priv(dev);
  340. spin_lock_bh(&sl->lock);
  341. if (sl->tty) {
  342. /* TTY discipline is running. */
  343. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  344. }
  345. netif_stop_queue(dev);
  346. sl->rcount = 0;
  347. sl->xleft = 0;
  348. spin_unlock_bh(&sl->lock);
  349. return 0;
  350. }
  351. /* Netdevice DOWN -> UP routine */
  352. static int slc_open(struct net_device *dev)
  353. {
  354. struct slcan *sl = netdev_priv(dev);
  355. if (sl->tty == NULL)
  356. return -ENODEV;
  357. sl->flags &= (1 << SLF_INUSE);
  358. netif_start_queue(dev);
  359. return 0;
  360. }
  361. /* Hook the destructor so we can free slcan devs at the right point in time */
  362. static void slc_free_netdev(struct net_device *dev)
  363. {
  364. int i = dev->base_addr;
  365. slcan_devs[i] = NULL;
  366. }
  367. static int slcan_change_mtu(struct net_device *dev, int new_mtu)
  368. {
  369. return -EINVAL;
  370. }
  371. static const struct net_device_ops slc_netdev_ops = {
  372. .ndo_open = slc_open,
  373. .ndo_stop = slc_close,
  374. .ndo_start_xmit = slc_xmit,
  375. .ndo_change_mtu = slcan_change_mtu,
  376. };
  377. static void slc_setup(struct net_device *dev)
  378. {
  379. dev->netdev_ops = &slc_netdev_ops;
  380. dev->needs_free_netdev = true;
  381. dev->priv_destructor = slc_free_netdev;
  382. dev->hard_header_len = 0;
  383. dev->addr_len = 0;
  384. dev->tx_queue_len = 10;
  385. dev->mtu = CAN_MTU;
  386. dev->type = ARPHRD_CAN;
  387. /* New-style flags. */
  388. dev->flags = IFF_NOARP;
  389. dev->features = NETIF_F_HW_CSUM;
  390. }
  391. /******************************************
  392. Routines looking at TTY side.
  393. ******************************************/
  394. /*
  395. * Handle the 'receiver data ready' interrupt.
  396. * This function is called by the 'tty_io' module in the kernel when
  397. * a block of SLCAN data has been received, which can now be decapsulated
  398. * and sent on to some IP layer for further processing. This will not
  399. * be re-entered while running but other ldisc functions may be called
  400. * in parallel
  401. */
  402. static void slcan_receive_buf(struct tty_struct *tty,
  403. const unsigned char *cp, char *fp, int count)
  404. {
  405. struct slcan *sl = (struct slcan *) tty->disc_data;
  406. if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
  407. return;
  408. /* Read the characters out of the buffer */
  409. while (count--) {
  410. if (fp && *fp++) {
  411. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  412. sl->dev->stats.rx_errors++;
  413. cp++;
  414. continue;
  415. }
  416. slcan_unesc(sl, *cp++);
  417. }
  418. }
  419. /************************************
  420. * slcan_open helper routines.
  421. ************************************/
  422. /* Collect hanged up channels */
  423. static void slc_sync(void)
  424. {
  425. int i;
  426. struct net_device *dev;
  427. struct slcan *sl;
  428. for (i = 0; i < maxdev; i++) {
  429. dev = slcan_devs[i];
  430. if (dev == NULL)
  431. break;
  432. sl = netdev_priv(dev);
  433. if (sl->tty)
  434. continue;
  435. if (dev->flags & IFF_UP)
  436. dev_close(dev);
  437. }
  438. }
  439. /* Find a free SLCAN channel, and link in this `tty' line. */
  440. static struct slcan *slc_alloc(void)
  441. {
  442. int i;
  443. char name[IFNAMSIZ];
  444. struct net_device *dev = NULL;
  445. struct can_ml_priv *can_ml;
  446. struct slcan *sl;
  447. int size;
  448. for (i = 0; i < maxdev; i++) {
  449. dev = slcan_devs[i];
  450. if (dev == NULL)
  451. break;
  452. }
  453. /* Sorry, too many, all slots in use */
  454. if (i >= maxdev)
  455. return NULL;
  456. sprintf(name, "slcan%d", i);
  457. size = ALIGN(sizeof(*sl), NETDEV_ALIGN) + sizeof(struct can_ml_priv);
  458. dev = alloc_netdev(size, name, NET_NAME_UNKNOWN, slc_setup);
  459. if (!dev)
  460. return NULL;
  461. dev->base_addr = i;
  462. sl = netdev_priv(dev);
  463. can_ml = (void *)sl + ALIGN(sizeof(*sl), NETDEV_ALIGN);
  464. can_set_ml_priv(dev, can_ml);
  465. /* Initialize channel control data */
  466. sl->magic = SLCAN_MAGIC;
  467. sl->dev = dev;
  468. spin_lock_init(&sl->lock);
  469. INIT_WORK(&sl->tx_work, slcan_transmit);
  470. slcan_devs[i] = dev;
  471. return sl;
  472. }
  473. /*
  474. * Open the high-level part of the SLCAN channel.
  475. * This function is called by the TTY module when the
  476. * SLCAN line discipline is called for. Because we are
  477. * sure the tty line exists, we only have to link it to
  478. * a free SLCAN channel...
  479. *
  480. * Called in process context serialized from other ldisc calls.
  481. */
  482. static int slcan_open(struct tty_struct *tty)
  483. {
  484. struct slcan *sl;
  485. int err;
  486. if (!capable(CAP_NET_ADMIN))
  487. return -EPERM;
  488. if (tty->ops->write == NULL)
  489. return -EOPNOTSUPP;
  490. /* RTnetlink lock is misused here to serialize concurrent
  491. opens of slcan channels. There are better ways, but it is
  492. the simplest one.
  493. */
  494. rtnl_lock();
  495. /* Collect hanged up channels. */
  496. slc_sync();
  497. sl = tty->disc_data;
  498. err = -EEXIST;
  499. /* First make sure we're not already connected. */
  500. if (sl && sl->magic == SLCAN_MAGIC)
  501. goto err_exit;
  502. /* OK. Find a free SLCAN channel to use. */
  503. err = -ENFILE;
  504. sl = slc_alloc();
  505. if (sl == NULL)
  506. goto err_exit;
  507. sl->tty = tty;
  508. tty->disc_data = sl;
  509. if (!test_bit(SLF_INUSE, &sl->flags)) {
  510. /* Perform the low-level SLCAN initialization. */
  511. sl->rcount = 0;
  512. sl->xleft = 0;
  513. set_bit(SLF_INUSE, &sl->flags);
  514. err = register_netdevice(sl->dev);
  515. if (err)
  516. goto err_free_chan;
  517. }
  518. /* Done. We have linked the TTY line to a channel. */
  519. rtnl_unlock();
  520. tty->receive_room = 65536; /* We don't flow control */
  521. /* TTY layer expects 0 on success */
  522. return 0;
  523. err_free_chan:
  524. sl->tty = NULL;
  525. tty->disc_data = NULL;
  526. clear_bit(SLF_INUSE, &sl->flags);
  527. slc_free_netdev(sl->dev);
  528. /* do not call free_netdev before rtnl_unlock */
  529. rtnl_unlock();
  530. free_netdev(sl->dev);
  531. return err;
  532. err_exit:
  533. rtnl_unlock();
  534. /* Count references from TTY module */
  535. return err;
  536. }
  537. /*
  538. * Close down a SLCAN channel.
  539. * This means flushing out any pending queues, and then returning. This
  540. * call is serialized against other ldisc functions.
  541. *
  542. * We also use this method for a hangup event.
  543. */
  544. static void slcan_close(struct tty_struct *tty)
  545. {
  546. struct slcan *sl = (struct slcan *) tty->disc_data;
  547. /* First make sure we're connected. */
  548. if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
  549. return;
  550. spin_lock_bh(&sl->lock);
  551. rcu_assign_pointer(tty->disc_data, NULL);
  552. sl->tty = NULL;
  553. spin_unlock_bh(&sl->lock);
  554. synchronize_rcu();
  555. flush_work(&sl->tx_work);
  556. /* Flush network side */
  557. unregister_netdev(sl->dev);
  558. /* This will complete via sl_free_netdev */
  559. }
  560. static int slcan_hangup(struct tty_struct *tty)
  561. {
  562. slcan_close(tty);
  563. return 0;
  564. }
  565. /* Perform I/O control on an active SLCAN channel. */
  566. static int slcan_ioctl(struct tty_struct *tty, struct file *file,
  567. unsigned int cmd, unsigned long arg)
  568. {
  569. struct slcan *sl = (struct slcan *) tty->disc_data;
  570. unsigned int tmp;
  571. /* First make sure we're connected. */
  572. if (!sl || sl->magic != SLCAN_MAGIC)
  573. return -EINVAL;
  574. switch (cmd) {
  575. case SIOCGIFNAME:
  576. tmp = strlen(sl->dev->name) + 1;
  577. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  578. return -EFAULT;
  579. return 0;
  580. case SIOCSIFHWADDR:
  581. return -EINVAL;
  582. default:
  583. return tty_mode_ioctl(tty, file, cmd, arg);
  584. }
  585. }
  586. static struct tty_ldisc_ops slc_ldisc = {
  587. .owner = THIS_MODULE,
  588. .magic = TTY_LDISC_MAGIC,
  589. .name = "slcan",
  590. .open = slcan_open,
  591. .close = slcan_close,
  592. .hangup = slcan_hangup,
  593. .ioctl = slcan_ioctl,
  594. .receive_buf = slcan_receive_buf,
  595. .write_wakeup = slcan_write_wakeup,
  596. };
  597. static int __init slcan_init(void)
  598. {
  599. int status;
  600. if (maxdev < 4)
  601. maxdev = 4; /* Sanity */
  602. pr_info("slcan: serial line CAN interface driver\n");
  603. pr_info("slcan: %d dynamic interface channels.\n", maxdev);
  604. slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
  605. if (!slcan_devs)
  606. return -ENOMEM;
  607. /* Fill in our line protocol discipline, and register it */
  608. status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
  609. if (status) {
  610. printk(KERN_ERR "slcan: can't register line discipline\n");
  611. kfree(slcan_devs);
  612. }
  613. return status;
  614. }
  615. static void __exit slcan_exit(void)
  616. {
  617. int i;
  618. struct net_device *dev;
  619. struct slcan *sl;
  620. unsigned long timeout = jiffies + HZ;
  621. int busy = 0;
  622. if (slcan_devs == NULL)
  623. return;
  624. /* First of all: check for active disciplines and hangup them.
  625. */
  626. do {
  627. if (busy)
  628. msleep_interruptible(100);
  629. busy = 0;
  630. for (i = 0; i < maxdev; i++) {
  631. dev = slcan_devs[i];
  632. if (!dev)
  633. continue;
  634. sl = netdev_priv(dev);
  635. spin_lock_bh(&sl->lock);
  636. if (sl->tty) {
  637. busy++;
  638. tty_hangup(sl->tty);
  639. }
  640. spin_unlock_bh(&sl->lock);
  641. }
  642. } while (busy && time_before(jiffies, timeout));
  643. /* FIXME: hangup is async so we should wait when doing this second
  644. phase */
  645. for (i = 0; i < maxdev; i++) {
  646. dev = slcan_devs[i];
  647. if (!dev)
  648. continue;
  649. slcan_devs[i] = NULL;
  650. sl = netdev_priv(dev);
  651. if (sl->tty) {
  652. printk(KERN_ERR "%s: tty discipline still running\n",
  653. dev->name);
  654. }
  655. unregister_netdev(dev);
  656. }
  657. kfree(slcan_devs);
  658. slcan_devs = NULL;
  659. i = tty_unregister_ldisc(N_SLCAN);
  660. if (i)
  661. printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
  662. }
  663. module_init(slcan_init);
  664. module_exit(slcan_exit);