slcan.c 18 KB

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