slcan.c 18 KB

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