slcan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * slcan.c - serial line CAN interface driver (using tty line discipline)
  3. *
  4. * This file is derived from linux/drivers/net/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 <asm/system.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/bitops.h>
  44. #include <linux/string.h>
  45. #include <linux/tty.h>
  46. #include <linux/errno.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/rtnetlink.h>
  50. #include <linux/if_arp.h>
  51. #include <linux/if_ether.h>
  52. #include <linux/sched.h>
  53. #include <linux/delay.h>
  54. #include <linux/init.h>
  55. #include <linux/kernel.h>
  56. #include <linux/can.h>
  57. static __initdata 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. if (!skb)
  162. return;
  163. skb->dev = sl->dev;
  164. skb->protocol = htons(ETH_P_CAN);
  165. skb->pkt_type = PACKET_BROADCAST;
  166. skb->ip_summed = CHECKSUM_UNNECESSARY;
  167. memcpy(skb_put(skb, sizeof(struct can_frame)),
  168. &cf, sizeof(struct can_frame));
  169. netif_rx_ni(skb);
  170. sl->dev->stats.rx_packets++;
  171. sl->dev->stats.rx_bytes += cf.can_dlc;
  172. }
  173. /* parse tty input stream */
  174. static void slcan_unesc(struct slcan *sl, unsigned char s)
  175. {
  176. if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
  177. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  178. (sl->rcount > 4)) {
  179. slc_bump(sl);
  180. }
  181. sl->rcount = 0;
  182. } else {
  183. if (!test_bit(SLF_ERROR, &sl->flags)) {
  184. if (sl->rcount < SLC_MTU) {
  185. sl->rbuff[sl->rcount++] = s;
  186. return;
  187. } else {
  188. sl->dev->stats.rx_over_errors++;
  189. set_bit(SLF_ERROR, &sl->flags);
  190. }
  191. }
  192. }
  193. }
  194. /************************************************************************
  195. * STANDARD SLCAN ENCAPSULATION *
  196. ************************************************************************/
  197. /* Encapsulate one can_frame and stuff into a TTY queue. */
  198. static void slc_encaps(struct slcan *sl, struct can_frame *cf)
  199. {
  200. int actual, idx, i;
  201. char cmd;
  202. if (cf->can_id & CAN_RTR_FLAG)
  203. cmd = 'R'; /* becomes 'r' in standard frame format */
  204. else
  205. cmd = 'T'; /* becomes 't' in standard frame format */
  206. if (cf->can_id & CAN_EFF_FLAG)
  207. sprintf(sl->xbuff, "%c%08X%d", cmd,
  208. cf->can_id & CAN_EFF_MASK, cf->can_dlc);
  209. else
  210. sprintf(sl->xbuff, "%c%03X%d", cmd | 0x20,
  211. cf->can_id & CAN_SFF_MASK, cf->can_dlc);
  212. idx = strlen(sl->xbuff);
  213. for (i = 0; i < cf->can_dlc; i++)
  214. sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
  215. strcat(sl->xbuff, "\r"); /* add terminating character */
  216. /* Order of next two lines is *very* important.
  217. * When we are sending a little amount of data,
  218. * the transfer may be completed inside the ops->write()
  219. * routine, because it's running with interrupts enabled.
  220. * In this case we *never* got WRITE_WAKEUP event,
  221. * if we did not request it before write operation.
  222. * 14 Oct 1994 Dmitry Gorodchanin.
  223. */
  224. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  225. actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
  226. sl->xleft = strlen(sl->xbuff) - actual;
  227. sl->xhead = sl->xbuff + actual;
  228. sl->dev->stats.tx_bytes += cf->can_dlc;
  229. }
  230. /*
  231. * Called by the driver when there's room for more data. If we have
  232. * more packets to send, we send them here.
  233. */
  234. static void slcan_write_wakeup(struct tty_struct *tty)
  235. {
  236. int actual;
  237. struct slcan *sl = (struct slcan *) tty->disc_data;
  238. /* First make sure we're connected. */
  239. if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
  240. return;
  241. if (sl->xleft <= 0) {
  242. /* Now serial buffer is almost free & we can start
  243. * transmission of another packet */
  244. sl->dev->stats.tx_packets++;
  245. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  246. netif_wake_queue(sl->dev);
  247. return;
  248. }
  249. actual = tty->ops->write(tty, sl->xhead, sl->xleft);
  250. sl->xleft -= actual;
  251. sl->xhead += actual;
  252. }
  253. /* Send a can_frame to a TTY queue. */
  254. static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
  255. {
  256. struct slcan *sl = netdev_priv(dev);
  257. if (skb->len != sizeof(struct can_frame))
  258. goto out;
  259. spin_lock(&sl->lock);
  260. if (!netif_running(dev)) {
  261. spin_unlock(&sl->lock);
  262. printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
  263. goto out;
  264. }
  265. if (sl->tty == NULL) {
  266. spin_unlock(&sl->lock);
  267. goto out;
  268. }
  269. netif_stop_queue(sl->dev);
  270. slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
  271. spin_unlock(&sl->lock);
  272. out:
  273. kfree_skb(skb);
  274. return NETDEV_TX_OK;
  275. }
  276. /******************************************
  277. * Routines looking at netdevice side.
  278. ******************************************/
  279. /* Netdevice UP -> DOWN routine */
  280. static int slc_close(struct net_device *dev)
  281. {
  282. struct slcan *sl = netdev_priv(dev);
  283. spin_lock_bh(&sl->lock);
  284. if (sl->tty) {
  285. /* TTY discipline is running. */
  286. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  287. }
  288. netif_stop_queue(dev);
  289. sl->rcount = 0;
  290. sl->xleft = 0;
  291. spin_unlock_bh(&sl->lock);
  292. return 0;
  293. }
  294. /* Netdevice DOWN -> UP routine */
  295. static int slc_open(struct net_device *dev)
  296. {
  297. struct slcan *sl = netdev_priv(dev);
  298. if (sl->tty == NULL)
  299. return -ENODEV;
  300. sl->flags &= (1 << SLF_INUSE);
  301. netif_start_queue(dev);
  302. return 0;
  303. }
  304. /* Hook the destructor so we can free slcan devs at the right point in time */
  305. static void slc_free_netdev(struct net_device *dev)
  306. {
  307. int i = dev->base_addr;
  308. free_netdev(dev);
  309. slcan_devs[i] = NULL;
  310. }
  311. static const struct net_device_ops slc_netdev_ops = {
  312. .ndo_open = slc_open,
  313. .ndo_stop = slc_close,
  314. .ndo_start_xmit = slc_xmit,
  315. };
  316. static void slc_setup(struct net_device *dev)
  317. {
  318. dev->netdev_ops = &slc_netdev_ops;
  319. dev->destructor = slc_free_netdev;
  320. dev->hard_header_len = 0;
  321. dev->addr_len = 0;
  322. dev->tx_queue_len = 10;
  323. dev->mtu = sizeof(struct can_frame);
  324. dev->type = ARPHRD_CAN;
  325. /* New-style flags. */
  326. dev->flags = IFF_NOARP;
  327. dev->features = NETIF_F_NO_CSUM;
  328. }
  329. /******************************************
  330. Routines looking at TTY side.
  331. ******************************************/
  332. /*
  333. * Handle the 'receiver data ready' interrupt.
  334. * This function is called by the 'tty_io' module in the kernel when
  335. * a block of SLCAN data has been received, which can now be decapsulated
  336. * and sent on to some IP layer for further processing. This will not
  337. * be re-entered while running but other ldisc functions may be called
  338. * in parallel
  339. */
  340. static void slcan_receive_buf(struct tty_struct *tty,
  341. const unsigned char *cp, char *fp, int count)
  342. {
  343. struct slcan *sl = (struct slcan *) tty->disc_data;
  344. if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
  345. return;
  346. /* Read the characters out of the buffer */
  347. while (count--) {
  348. if (fp && *fp++) {
  349. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  350. sl->dev->stats.rx_errors++;
  351. cp++;
  352. continue;
  353. }
  354. slcan_unesc(sl, *cp++);
  355. }
  356. }
  357. /************************************
  358. * slcan_open helper routines.
  359. ************************************/
  360. /* Collect hanged up channels */
  361. static void slc_sync(void)
  362. {
  363. int i;
  364. struct net_device *dev;
  365. struct slcan *sl;
  366. for (i = 0; i < maxdev; i++) {
  367. dev = slcan_devs[i];
  368. if (dev == NULL)
  369. break;
  370. sl = netdev_priv(dev);
  371. if (sl->tty)
  372. continue;
  373. if (dev->flags & IFF_UP)
  374. dev_close(dev);
  375. }
  376. }
  377. /* Find a free SLCAN channel, and link in this `tty' line. */
  378. static struct slcan *slc_alloc(dev_t line)
  379. {
  380. int i;
  381. char name[IFNAMSIZ];
  382. struct net_device *dev = NULL;
  383. struct slcan *sl;
  384. for (i = 0; i < maxdev; i++) {
  385. dev = slcan_devs[i];
  386. if (dev == NULL)
  387. break;
  388. }
  389. /* Sorry, too many, all slots in use */
  390. if (i >= maxdev)
  391. return NULL;
  392. sprintf(name, "slcan%d", i);
  393. dev = alloc_netdev(sizeof(*sl), name, slc_setup);
  394. if (!dev)
  395. return NULL;
  396. dev->base_addr = i;
  397. sl = netdev_priv(dev);
  398. /* Initialize channel control data */
  399. sl->magic = SLCAN_MAGIC;
  400. sl->dev = dev;
  401. spin_lock_init(&sl->lock);
  402. slcan_devs[i] = dev;
  403. return sl;
  404. }
  405. /*
  406. * Open the high-level part of the SLCAN channel.
  407. * This function is called by the TTY module when the
  408. * SLCAN line discipline is called for. Because we are
  409. * sure the tty line exists, we only have to link it to
  410. * a free SLCAN channel...
  411. *
  412. * Called in process context serialized from other ldisc calls.
  413. */
  414. static int slcan_open(struct tty_struct *tty)
  415. {
  416. struct slcan *sl;
  417. int err;
  418. if (!capable(CAP_NET_ADMIN))
  419. return -EPERM;
  420. if (tty->ops->write == NULL)
  421. return -EOPNOTSUPP;
  422. /* RTnetlink lock is misused here to serialize concurrent
  423. opens of slcan channels. There are better ways, but it is
  424. the simplest one.
  425. */
  426. rtnl_lock();
  427. /* Collect hanged up channels. */
  428. slc_sync();
  429. sl = tty->disc_data;
  430. err = -EEXIST;
  431. /* First make sure we're not already connected. */
  432. if (sl && sl->magic == SLCAN_MAGIC)
  433. goto err_exit;
  434. /* OK. Find a free SLCAN channel to use. */
  435. err = -ENFILE;
  436. sl = slc_alloc(tty_devnum(tty));
  437. if (sl == NULL)
  438. goto err_exit;
  439. sl->tty = tty;
  440. tty->disc_data = sl;
  441. if (!test_bit(SLF_INUSE, &sl->flags)) {
  442. /* Perform the low-level SLCAN initialization. */
  443. sl->rcount = 0;
  444. sl->xleft = 0;
  445. set_bit(SLF_INUSE, &sl->flags);
  446. err = register_netdevice(sl->dev);
  447. if (err)
  448. goto err_free_chan;
  449. }
  450. /* Done. We have linked the TTY line to a channel. */
  451. rtnl_unlock();
  452. tty->receive_room = 65536; /* We don't flow control */
  453. /* TTY layer expects 0 on success */
  454. return 0;
  455. err_free_chan:
  456. sl->tty = NULL;
  457. tty->disc_data = NULL;
  458. clear_bit(SLF_INUSE, &sl->flags);
  459. err_exit:
  460. rtnl_unlock();
  461. /* Count references from TTY module */
  462. return err;
  463. }
  464. /*
  465. * Close down a SLCAN channel.
  466. * This means flushing out any pending queues, and then returning. This
  467. * call is serialized against other ldisc functions.
  468. *
  469. * We also use this method for a hangup event.
  470. */
  471. static void slcan_close(struct tty_struct *tty)
  472. {
  473. struct slcan *sl = (struct slcan *) tty->disc_data;
  474. /* First make sure we're connected. */
  475. if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
  476. return;
  477. tty->disc_data = NULL;
  478. sl->tty = NULL;
  479. /* Flush network side */
  480. unregister_netdev(sl->dev);
  481. /* This will complete via sl_free_netdev */
  482. }
  483. static int slcan_hangup(struct tty_struct *tty)
  484. {
  485. slcan_close(tty);
  486. return 0;
  487. }
  488. /* Perform I/O control on an active SLCAN channel. */
  489. static int slcan_ioctl(struct tty_struct *tty, struct file *file,
  490. unsigned int cmd, unsigned long arg)
  491. {
  492. struct slcan *sl = (struct slcan *) tty->disc_data;
  493. unsigned int tmp;
  494. /* First make sure we're connected. */
  495. if (!sl || sl->magic != SLCAN_MAGIC)
  496. return -EINVAL;
  497. switch (cmd) {
  498. case SIOCGIFNAME:
  499. tmp = strlen(sl->dev->name) + 1;
  500. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  501. return -EFAULT;
  502. return 0;
  503. case SIOCSIFHWADDR:
  504. return -EINVAL;
  505. default:
  506. return tty_mode_ioctl(tty, file, cmd, arg);
  507. }
  508. }
  509. static struct tty_ldisc_ops slc_ldisc = {
  510. .owner = THIS_MODULE,
  511. .magic = TTY_LDISC_MAGIC,
  512. .name = "slcan",
  513. .open = slcan_open,
  514. .close = slcan_close,
  515. .hangup = slcan_hangup,
  516. .ioctl = slcan_ioctl,
  517. .receive_buf = slcan_receive_buf,
  518. .write_wakeup = slcan_write_wakeup,
  519. };
  520. static int __init slcan_init(void)
  521. {
  522. int status;
  523. if (maxdev < 4)
  524. maxdev = 4; /* Sanity */
  525. printk(banner);
  526. printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
  527. slcan_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
  528. if (!slcan_devs) {
  529. printk(KERN_ERR "slcan: can't allocate slcan device array!\n");
  530. return -ENOMEM;
  531. }
  532. /* Fill in our line protocol discipline, and register it */
  533. status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
  534. if (status) {
  535. printk(KERN_ERR "slcan: can't register line discipline\n");
  536. kfree(slcan_devs);
  537. }
  538. return status;
  539. }
  540. static void __exit slcan_exit(void)
  541. {
  542. int i;
  543. struct net_device *dev;
  544. struct slcan *sl;
  545. unsigned long timeout = jiffies + HZ;
  546. int busy = 0;
  547. if (slcan_devs == NULL)
  548. return;
  549. /* First of all: check for active disciplines and hangup them.
  550. */
  551. do {
  552. if (busy)
  553. msleep_interruptible(100);
  554. busy = 0;
  555. for (i = 0; i < maxdev; i++) {
  556. dev = slcan_devs[i];
  557. if (!dev)
  558. continue;
  559. sl = netdev_priv(dev);
  560. spin_lock_bh(&sl->lock);
  561. if (sl->tty) {
  562. busy++;
  563. tty_hangup(sl->tty);
  564. }
  565. spin_unlock_bh(&sl->lock);
  566. }
  567. } while (busy && time_before(jiffies, timeout));
  568. /* FIXME: hangup is async so we should wait when doing this second
  569. phase */
  570. for (i = 0; i < maxdev; i++) {
  571. dev = slcan_devs[i];
  572. if (!dev)
  573. continue;
  574. slcan_devs[i] = NULL;
  575. sl = netdev_priv(dev);
  576. if (sl->tty) {
  577. printk(KERN_ERR "%s: tty discipline still running\n",
  578. dev->name);
  579. /* Intentionally leak the control block. */
  580. dev->destructor = NULL;
  581. }
  582. unregister_netdev(dev);
  583. }
  584. kfree(slcan_devs);
  585. slcan_devs = NULL;
  586. i = tty_unregister_ldisc(N_SLCAN);
  587. if (i)
  588. printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
  589. }
  590. module_init(slcan_init);
  591. module_exit(slcan_exit);