2 * sllin.c - serial line LIN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip.c
6 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
7 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
8 * sllin.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
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.
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.
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
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
38 * Send feedback to <socketcan-users@lists.berlios.de>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
45 #include <asm/system.h>
46 #include <linux/uaccess.h>
47 #include <linux/bitops.h>
48 #include <linux/string.h>
49 #include <linux/tty.h>
50 #include <linux/errno.h>
51 #include <linux/netdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_ether.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
58 #include <linux/init.h>
59 #include <linux/can.h>
60 #include <linux/kthread.h>
62 /* Should be in include/linux/tty.h */
65 static __initdata const char banner[] =
66 KERN_INFO "sllin: serial line LIN interface driver\n";
68 MODULE_ALIAS_LDISC(N_SLLIN);
69 MODULE_DESCRIPTION("serial line LIN interface");
70 MODULE_LICENSE("GPL");
71 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
73 #define SLLIN_MAGIC 0x53CA
74 // #define BREAK_BY_BAUD
76 static int maxdev = 10; /* MAX number of SLLIN channels;
77 This can be overridden with
78 insmod sllin.ko maxdev=nnn */
79 module_param(maxdev, int, 0);
80 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
82 /* maximum buffer len to store whole LIN message*/
83 #define SLLIN_DATA_MAX 8
84 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
85 SLLIN_DATA_MAX + 1 /*checksum*/)
86 #define SLLIN_BUFF_BREAK 0
87 #define SLLIN_BUFF_SYNC 1
88 #define SLLIN_BUFF_ID 2
89 #define SLLIN_BUFF_DATA 3
95 SLSTATE_RESPONSE_WAIT,
96 SLSTATE_RESPONSE_SENT,
102 /* Various fields. */
103 struct tty_struct *tty; /* ptr to TTY structure */
104 struct net_device *dev; /* easy for intr handling */
107 /* LIN message buffer and actual processed data counts */
108 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
109 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
110 int rx_expect; /* expected number of Rx chars */
111 int rx_lim; /* maximum Rx chars for ID */
112 int rx_cnt; /* message buffer Rx fill level */
113 int tx_lim; /* actual limit of bytes to Tx */
114 int tx_cnt; /* number of already Tx bytes */
115 char lin_master; /* node is a master node */
116 int lin_baud; /* LIN baudrate */
117 int lin_state; /* state */
118 int id_to_send; /* there is ID to be sent */
120 unsigned long flags; /* Flag values/ mode etc */
121 #define SLF_INUSE 0 /* Channel in use */
122 #define SLF_ERROR 1 /* Parity, etc. error */
123 #define SLF_RXEVENT 2 /* Rx wake event */
124 #define SLF_TXEVENT 3 /* Tx wake event */
125 #define SLF_MSGEVENT 4 /* CAN message to sent */
128 struct task_struct *kwthread;
129 wait_queue_head_t kwt_wq;
131 struct sk_buff *rec_skb; /* Socket buffer with received CAN frame */
134 static struct net_device **sllin_devs;
136 const unsigned char sllin_id_parity_table[] = {
137 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
138 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
139 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
140 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
141 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
142 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
143 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
144 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
147 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
149 struct ktermios old_termios;
152 mutex_lock(&tty->termios_mutex);
154 old_termios = *(tty->termios);
155 cflag = tty->termios->c_cflag;
156 cflag &= ~(CBAUD | CIBAUD);
158 tty->termios->c_cflag = cflag;
160 tty_encode_baud_rate(tty, speed, speed);
162 if (tty->ops->set_termios)
163 tty->ops->set_termios(tty, &old_termios);
165 mutex_unlock(&tty->termios_mutex);
171 /* Send one completely decapsulated can_frame to the network layer */
172 static void sll_bump(struct sllin *sl)
174 // struct sk_buff *skb;
175 // struct can_frame cf;
176 // int i, dlc_pos, tmp;
177 // unsigned long ultmp;
178 // char cmd = sl->rbuff[0];
180 // if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
183 // if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
184 // dlc_pos = 4; /* dlc position tiiid */
186 // dlc_pos = 9; /* dlc position Tiiiiiiiid */
188 // if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
191 // cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
193 // sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
195 // if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
198 // cf.can_id = ultmp;
200 // if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
201 // cf.can_id |= CAN_EFF_FLAG;
203 // if ((cmd | 0x20) == 'r') /* RTR frame */
204 // cf.can_id |= CAN_RTR_FLAG;
206 // *(u64 *) (&cf.data) = 0; /* clear payload */
208 // for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
210 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
213 // cf.data[i] = (tmp << 4);
214 // tmp = asc2nibble(sl->rbuff[dlc_pos++]);
217 // cf.data[i] |= tmp;
221 // skb = dev_alloc_skb(sizeof(struct can_frame));
225 // skb->dev = sl->dev;
226 // skb->protocol = htons(ETH_P_CAN);
227 // skb->pkt_type = PACKET_BROADCAST;
228 // skb->ip_summed = CHECKSUM_UNNECESSARY;
229 // memcpy(skb_put(skb, sizeof(struct can_frame)),
230 // &cf, sizeof(struct can_frame));
233 // sl->dev->stats.rx_packets++;
234 // sl->dev->stats.rx_bytes += cf.can_dlc;
238 /************************************************************************
239 * STANDARD SLLIN ENCAPSULATION *
240 ************************************************************************/
242 /* Convert particular CAN frame into LIN frame and send it to TTY queue. */
243 static void sll_encaps(struct sllin *sl, struct can_frame *cf)
245 // int actual, idx, i;
246 // char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
247 // struct tty_struct *tty = sl->tty;
249 // pr_debug("sllin: %s() invoked\n", __FUNCTION__);
251 // /* We do care only about SFF frames */
252 // if (cf->can_id & CAN_EFF_FLAG)
255 // /* Send only header */
256 // if (cf->can_id & CAN_RTR_FLAG) {
257 // pr_debug("sllin: %s() RTR CAN frame\n", __FUNCTION__);
258 // lframe[2] = (u8)cf->can_id; /* Get one byte LIN ID */
260 // sltty_change_speed(tty, sl->lin_baud * 2 / 3);
261 // tty->ops->write(tty, &lframe[0], 1);
262 // sltty_change_speed(tty, sl->lin_baud);
263 // tty->ops->write(tty, &lframe[1], 1);
264 // tty->ops->write(tty, &lframe[2], 1);
266 // pr_debug("sllin: %s() non-RTR CAN frame\n", __FUNCTION__);
267 // /* idx = strlen(sl->xbuff);
269 // for (i = 0; i < cf->can_dlc; i++)
270 // sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
272 // * Order of next two lines is *very* important.
273 // * When we are sending a little amount of data,
274 // * the transfer may be completed inside the ops->write()
275 // * routine, because it's running with interrupts enabled.
276 // * In this case we *never* got WRITE_WAKEUP event,
277 // * if we did not request it before write operation.
278 // * 14 Oct 1994 Dmitry Gorodchanin.
280 // set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
281 // actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
282 // sl->xleft = strlen(sl->xbuff) - actual;
283 // sl->xhead = sl->xbuff + actual;
284 // sl->dev->stats.tx_bytes += cf->can_dlc;
291 * Called by the driver when there's room for more data. If we have
292 * more packets to send, we send them here.
294 static void sllin_write_wakeup(struct tty_struct *tty)
298 struct sllin *sl = (struct sllin *) tty->disc_data;
300 /* First make sure we're connected. */
301 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
304 if (sl->lin_state != SLSTATE_BREAK_SENT)
305 remains = sl->tx_lim - sl->tx_cnt;
307 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
310 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
311 sl->tx_cnt += actual;
313 if (sl->tx_cnt < sl->tx_lim) {
314 printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
315 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
320 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
321 set_bit(SLF_TXEVENT, &sl->flags);
322 wake_up(&sl->kwt_wq);
324 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
327 /* Send a can_frame to a TTY queue. */
328 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
330 struct sllin *sl = netdev_priv(dev);
332 if (skb->len != sizeof(struct can_frame))
335 spin_lock(&sl->lock);
336 if (!netif_running(dev)) {
337 spin_unlock(&sl->lock);
338 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
341 if (sl->tty == NULL) {
342 spin_unlock(&sl->lock);
346 netif_stop_queue(sl->dev);
349 set_bit(SLF_MSGEVENT, &sl->flags);
350 wake_up(&sl->kwt_wq);
351 spin_unlock(&sl->lock);
360 /******************************************
361 * Routines looking at netdevice side.
362 ******************************************/
364 /* Netdevice UP -> DOWN routine */
365 static int sll_close(struct net_device *dev)
367 struct sllin *sl = netdev_priv(dev);
369 spin_lock_bh(&sl->lock);
371 /* TTY discipline is running. */
372 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
374 netif_stop_queue(dev);
377 spin_unlock_bh(&sl->lock);
382 /* Netdevice DOWN -> UP routine */
383 static int sll_open(struct net_device *dev)
385 struct sllin *sl = netdev_priv(dev);
387 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
392 sl->flags &= (1 << SLF_INUSE);
393 netif_start_queue(dev);
397 /* Hook the destructor so we can free sllin devs at the right point in time */
398 static void sll_free_netdev(struct net_device *dev)
400 int i = dev->base_addr;
402 sllin_devs[i] = NULL;
405 static const struct net_device_ops sll_netdev_ops = {
406 .ndo_open = sll_open,
407 .ndo_stop = sll_close,
408 .ndo_start_xmit = sll_xmit,
411 static void sll_setup(struct net_device *dev)
413 dev->netdev_ops = &sll_netdev_ops;
414 dev->destructor = sll_free_netdev;
416 dev->hard_header_len = 0;
418 dev->tx_queue_len = 10;
420 dev->mtu = sizeof(struct can_frame);
421 dev->type = ARPHRD_CAN;
423 /* New-style flags. */
424 dev->flags = IFF_NOARP;
425 dev->features = NETIF_F_NO_CSUM;
428 /******************************************
429 Routines looking at TTY side.
430 ******************************************/
433 * Handle the 'receiver data ready' interrupt.
434 * This function is called by the 'tty_io' module in the kernel when
435 * a block of SLLIN data has been received, which can now be decapsulated
436 * and sent on to some IP layer for further processing. This will not
437 * be re-entered while running but other ldisc functions may be called
441 static void sllin_receive_buf(struct tty_struct *tty,
442 const unsigned char *cp, char *fp, int count)
444 struct sllin *sl = (struct sllin *) tty->disc_data;
446 printk(KERN_INFO "sllin_receive_buf invoked\n");
448 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
451 /* Read the characters out of the buffer */
454 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
455 sl->dev->stats.rx_errors++;
456 printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored "
457 "due marker 0x%02x, flags 0x%lx\n",
458 *cp, *(fp-1), sl->flags);
463 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
464 #ifndef BREAK_BY_BAUD
465 /* We didn't receive Break character */
466 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
467 sl->rx_buff[sl->rx_cnt++] = 0x00;
470 printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
471 sl->rx_buff[sl->rx_cnt++] = *cp++;
475 if (sl->rx_cnt >= sl->rx_expect) {
476 set_bit(SLF_RXEVENT, &sl->flags);
477 wake_up(&sl->kwt_wq);
478 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
480 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
484 /*****************************************
485 * sllin message helper routines
486 *****************************************/
488 int sllin_setup_msg(struct sllin *sl, int mode, int id,
489 unsigned char *data, int len)
498 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
499 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
500 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
501 sl->tx_lim = SLLIN_BUFF_DATA;
503 if ((data != NULL) && len) {
508 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
509 /* compute data parity there */
510 for (i = SLLIN_BUFF_DATA; i < sl->tx_lim; i++) {
511 csum += sl->tx_buff[i];
516 sl->tx_buff[sl->tx_lim++] = csum;
519 sl->rx_lim += len + 1;
525 int sllin_send_tx_buff(struct sllin *sl)
527 struct tty_struct *tty = sl->tty;
532 if (sl->lin_state != SLSTATE_BREAK_SENT)
533 remains = sl->tx_lim - sl->tx_cnt;
537 remains = sl->tx_lim - sl->tx_cnt;
540 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
548 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
549 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
551 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
559 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
560 sl->tx_cnt, remains);
566 int sllin_send_break(struct sllin *sl)
568 struct tty_struct *tty = sl->tty;
569 unsigned long break_baud;
572 break_baud = ((sl->lin_baud * 2) / 3);
573 sltty_change_speed(tty, break_baud);
575 tty->ops->flush_buffer(tty);
576 sl->rx_cnt = SLLIN_BUFF_BREAK;
578 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
579 sl->lin_state = SLSTATE_BREAK_SENT;
581 res = sllin_send_tx_buff(sl);
583 sl->lin_state = SLSTATE_IDLE;
589 #else /* BREAK_BY_BAUD */
591 int sllin_send_break(struct sllin *sl)
593 struct tty_struct *tty = sl->tty;
594 unsigned long break_baud;
598 sl->rx_cnt = SLLIN_BUFF_BREAK;
599 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
600 sl->lin_state = SLSTATE_BREAK_SENT;
602 /* Do the break ourselves; Inspired by
603 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
604 retval = tty->ops->break_ctl(tty, -1);
609 usleep_range(650, 750);
611 retval = tty->ops->break_ctl(tty, 0);
612 usleep_range(50, 100);
614 tty->ops->flush_buffer(tty);
616 sl->tx_cnt = SLLIN_BUFF_SYNC;
618 printk(KERN_INFO "sllin: Break sent.\n");
619 set_bit(SLF_RXEVENT, &sl->flags);
620 wake_up(&sl->kwt_wq);
624 #endif /* BREAK_BY_BAUD */
626 /*****************************************
627 * sllin_kwthread - kernel worker thread
628 *****************************************/
630 int sllin_kwthread(void *ptr)
632 struct sllin *sl = (struct sllin *)ptr;
633 struct tty_struct *tty = sl->tty;
634 struct sched_param schparam = { .sched_priority = 40 };
636 struct can_frame *cf;
638 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
639 sched_setscheduler(current, SCHED_FIFO, &schparam);
641 clear_bit(SLF_ERROR, &sl->flags);
642 sltty_change_speed(tty, sl->lin_baud);
644 while (!kthread_should_stop()) {
645 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
647 if(sllin_send_break(sl) < 0) {
648 /* error processing */
652 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
653 test_bit(SLF_RXEVENT, &sl->flags) ||
654 test_bit(SLF_TXEVENT, &sl->flags) ||
655 ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)));
657 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
658 printk(KERN_INFO "sllin_kthread RXEVENT \n");
661 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
662 printk(KERN_INFO "sllin_kthread TXEVENT \n");
665 if ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)) {
666 cf = (struct can_frame *)sl->rec_skb->data;
668 /* We do care only about SFF frames */
669 if (cf->can_id & CAN_EFF_FLAG)
672 if (cf->can_id & CAN_RTR_FLAG) {
673 printk(KERN_INFO "%s: RTR CAN frame, ID = %x\n",
674 __FUNCTION__, cf->can_id & CAN_SFF_MASK);
675 if (sllin_setup_msg(sl, 0,
676 cf->can_id & CAN_SFF_MASK, NULL, 0) != -1) {
677 sl->id_to_send = true;
680 printk(KERN_INFO "%s: NON-RTR CAN frame, ID = %x\n",
681 __FUNCTION__, (int)cf->can_id & CAN_SFF_MASK);
683 if (sllin_setup_msg(sl, 0, cf->can_id & CAN_SFF_MASK,
684 cf->data, cf->can_dlc) != -1) {
685 sl->id_to_send = true;
690 clear_bit(SLF_MSGEVENT, &sl->flags);
691 kfree_skb(sl->rec_skb);
692 netif_wake_queue(sl->dev);
695 switch (sl->lin_state) {
696 case SLSTATE_BREAK_SENT:
698 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
701 res = sltty_change_speed(tty, sl->lin_baud);
704 sl->lin_state = SLSTATE_ID_SENT;
705 sllin_send_tx_buff(sl);
708 case SLSTATE_ID_SENT:
709 sl->id_to_send = false;
710 sl->lin_state = SLSTATE_IDLE;
716 /* sll_bump(sl); send packet to the network layer */
718 /* sl->dev->stats.tx_packets++; send frames statistic */
719 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
723 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
729 /************************************
730 * sllin_open helper routines.
731 ************************************/
733 /* Collect hanged up channels */
734 static void sll_sync(void)
737 struct net_device *dev;
740 for (i = 0; i < maxdev; i++) {
745 sl = netdev_priv(dev);
748 if (dev->flags & IFF_UP)
753 /* Find a free SLLIN channel, and link in this `tty' line. */
754 static struct sllin *sll_alloc(dev_t line)
757 struct net_device *dev = NULL;
760 if (sllin_devs == NULL)
761 return NULL; /* Master array missing ! */
763 for (i = 0; i < maxdev; i++) {
770 /* Sorry, too many, all slots in use */
775 sl = netdev_priv(dev);
776 if (test_bit(SLF_INUSE, &sl->flags)) {
777 unregister_netdevice(dev);
779 sllin_devs[i] = NULL;
785 sprintf(name, "sllin%d", i);
787 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
793 sl = netdev_priv(dev);
795 /* Initialize channel control data */
796 sl->magic = SLLIN_MAGIC;
798 spin_lock_init(&sl->lock);
805 * Open the high-level part of the SLLIN channel.
806 * This function is called by the TTY module when the
807 * SLLIN line discipline is called for. Because we are
808 * sure the tty line exists, we only have to link it to
809 * a free SLLIN channel...
811 * Called in process context serialized from other ldisc calls.
814 static int sllin_open(struct tty_struct *tty)
818 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
820 if (!capable(CAP_NET_ADMIN))
823 if (tty->ops->write == NULL)
826 /* RTnetlink lock is misused here to serialize concurrent
827 opens of sllin channels. There are better ways, but it is
832 /* Collect hanged up channels. */
838 /* First make sure we're not already connected. */
839 if (sl && sl->magic == SLLIN_MAGIC)
842 /* OK. Find a free SLLIN channel to use. */
844 sl = sll_alloc(tty_devnum(tty));
850 sl->line = tty_devnum(tty);
852 if (!test_bit(SLF_INUSE, &sl->flags)) {
853 /* Perform the low-level SLLIN initialization. */
860 sl->lin_baud = 19200;
863 sl->lin_state = SLSTATE_IDLE;
865 set_bit(SLF_INUSE, &sl->flags);
867 init_waitqueue_head(&sl->kwt_wq);
868 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
869 if (sl->kwthread == NULL)
872 err = register_netdevice(sl->dev);
874 goto err_free_chan_and_thread;
877 /* Done. We have linked the TTY line to a channel. */
879 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
881 /* TTY layer expects 0 on success */
884 err_free_chan_and_thread:
885 kthread_stop(sl->kwthread);
890 tty->disc_data = NULL;
891 clear_bit(SLF_INUSE, &sl->flags);
896 /* Count references from TTY module */
901 * Close down a SLLIN channel.
902 * This means flushing out any pending queues, and then returning. This
903 * call is serialized against other ldisc functions.
905 * We also use this method for a hangup event.
908 static void sllin_close(struct tty_struct *tty)
910 struct sllin *sl = (struct sllin *) tty->disc_data;
912 /* First make sure we're connected. */
913 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
916 kthread_stop(sl->kwthread);
919 tty->disc_data = NULL;
922 /* Flush network side */
923 unregister_netdev(sl->dev);
924 /* This will complete via sl_free_netdev */
927 static int sllin_hangup(struct tty_struct *tty)
933 /* Perform I/O control on an active SLLIN channel. */
934 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
935 unsigned int cmd, unsigned long arg)
937 struct sllin *sl = (struct sllin *) tty->disc_data;
940 /* First make sure we're connected. */
941 if (!sl || sl->magic != SLLIN_MAGIC)
946 tmp = strlen(sl->dev->name) + 1;
947 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
955 return tty_mode_ioctl(tty, file, cmd, arg);
959 static struct tty_ldisc_ops sll_ldisc = {
960 .owner = THIS_MODULE,
961 .magic = TTY_LDISC_MAGIC,
964 .close = sllin_close,
965 .hangup = sllin_hangup,
966 .ioctl = sllin_ioctl,
967 .receive_buf = sllin_receive_buf,
968 .write_wakeup = sllin_write_wakeup,
971 static int __init sllin_init(void)
976 maxdev = 4; /* Sanity */
979 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
981 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
983 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
987 /* Fill in our line protocol discipline, and register it */
988 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
990 printk(KERN_ERR "sllin: can't register line discipline\n");
996 static void __exit sllin_exit(void)
999 struct net_device *dev;
1001 unsigned long timeout = jiffies + HZ;
1004 if (sllin_devs == NULL)
1007 /* First of all: check for active disciplines and hangup them.
1011 msleep_interruptible(100);
1014 for (i = 0; i < maxdev; i++) {
1015 dev = sllin_devs[i];
1018 sl = netdev_priv(dev);
1019 spin_lock_bh(&sl->lock);
1022 tty_hangup(sl->tty);
1024 spin_unlock_bh(&sl->lock);
1026 } while (busy && time_before(jiffies, timeout));
1028 /* FIXME: hangup is async so we should wait when doing this second
1031 for (i = 0; i < maxdev; i++) {
1032 dev = sllin_devs[i];
1035 sllin_devs[i] = NULL;
1037 sl = netdev_priv(dev);
1039 printk(KERN_ERR "%s: tty discipline still running\n",
1041 /* Intentionally leak the control block. */
1042 dev->destructor = NULL;
1045 unregister_netdev(dev);
1051 i = tty_unregister_ldisc(N_SLLIN);
1053 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1056 module_init(sllin_init);
1057 module_exit(sllin_exit);