slcan.c 19 KB

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