slcan.c 18 KB

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