slcan.c 18 KB

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