📄 6pack.c
字号:
* Allocate the 6pack frame buffers: * * rbuff Receive buffer. * xbuff Transmit buffer. */ rbuff = xchg(&sp->rbuff, rbuff); xbuff = xchg(&sp->xbuff, xbuff); sp->mtu = AX25_MTU + 73; sp->buffsize = len; sp->rcount = 0; sp->rx_count = 0; sp->rx_count_cooked = 0; sp->xleft = 0; sp->flags = 0; /* Clear ESCAPE & ERROR flags */ sp->duplex = 0; sp->tx_delay = SIXP_TXDELAY; sp->persistence = SIXP_PERSIST; sp->slottime = SIXP_SLOTTIME; sp->led_state = 0x60; sp->status = 1; sp->status1 = 1; sp->status2 = 0; sp->tnc_ok = 0; sp->tx_enable = 0; netif_start_queue(dev); init_timer(&sp->tx_t); init_timer(&sp->resync_t); spin_unlock_bh(&sp->lock); err = 0;err_exit: if (xbuff) kfree(xbuff); if (rbuff) kfree(rbuff); return err;}static int sixpack_receive_room(struct tty_struct *tty){ return 65536; /* We can handle an infinite amount of data. :-) */}/* * Handle the 'receiver data ready' interrupt. * This function is called by the 'tty_io' module in the kernel when * a block of 6pack data has been received, which can now be decapsulated * and sent on to some IP layer for further processing. */static void sixpack_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count){ struct sixpack *sp; unsigned char buf[512]; int count1; if (!count) return; sp = sp_get(tty); if (!sp) return; memcpy(buf, cp, count < sizeof(buf) ? count : sizeof(buf)); /* Read the characters out of the buffer */ count1 = count; while (count) { count--; if (fp && *fp++) { if (!test_and_set_bit(SIXPF_ERROR, &sp->flags)) sp->stats.rx_errors++; continue; } } sixpack_decode(sp, buf, count1); sp_put(sp); if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && tty->driver->unthrottle) tty->driver->unthrottle(tty);}/* * Try to resync the TNC. Called by the resync timer defined in * decode_prio_command */static void resync_tnc(unsigned long channel){ struct sixpack *sp = (struct sixpack *) channel; struct net_device *dev = sp->dev; static char resync_cmd = 0xe8; printk(KERN_INFO "%s: resyncing TNC\n", dev->name); /* clear any data that might have been received */ sp->rx_count = 0; sp->rx_count_cooked = 0; /* reset state machine */ sp->status = 1; sp->status1 = 1; sp->status2 = 0; sp->tnc_ok = 0; /* resync the TNC */ sp->led_state = 0x60; sp->tty->driver->write(sp->tty, 0, &sp->led_state, 1); sp->tty->driver->write(sp->tty, 0, &resync_cmd, 1); /* Start resync timer again -- the TNC might be still absent */ del_timer(&sp->resync_t); sp->resync_t.data = (unsigned long) sp; sp->resync_t.function = resync_tnc; sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT; add_timer(&sp->resync_t);}static inline int tnc_init(struct sixpack *sp){ unsigned char inbyte = 0xe8; sp->tty->driver->write(sp->tty, 0, &inbyte, 1); del_timer(&sp->resync_t); sp->resync_t.data = (unsigned long) sp; sp->resync_t.function = resync_tnc; sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT; add_timer(&sp->resync_t); return 0;}/* * Open the high-level part of the 6pack channel. * This function is called by the TTY module when the * 6pack line discipline is called for. Because we are * sure the tty line exists, we only have to link it to * a free 6pcack channel... */static int sixpack_open(struct tty_struct *tty){ struct sixpack *sp; int err = 0; if (!capable(CAP_NET_ADMIN)) return -EPERM; sp = sp_alloc(); if (!sp) { err = -ENOMEM; goto out; } sp->tty = tty; atomic_set(&sp->refcnt, 1); init_MUTEX_LOCKED(&sp->dead_sem); /* Perform the low-level 6pack initialization. */ if ((err = sp_open(sp->dev))) goto out; /* Done. We have linked the TTY line to a channel. */ tty->disc_data = sp; tnc_init(sp);out: return err;}/* * Close down a 6pack channel. * This means flushing out any pending queues, and then restoring the * TTY line discipline to what it was before it got hooked to 6pack * (which usually is TTY again). */static void sixpack_close(struct tty_struct *tty){ struct sixpack *sp = (struct sixpack *) tty->disc_data; write_lock(&disc_data_lock); sp = tty->disc_data; tty->disc_data = NULL; write_unlock(&disc_data_lock); if (sp == 0) return; /* * We have now ensured that nobody can start using ap from now on, but * we have to wait for all existing users to finish. */ if (!atomic_dec_and_test(&sp->refcnt)) down(&sp->dead_sem); del_timer(&sp->tx_t); del_timer(&sp->resync_t); sp_free(sp); unregister_netdev(sp->dev);}static int sp_set_mac_address(struct net_device *dev, void __user *addr){ return copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN) ? -EFAULT : 0;}/* Perform I/O control on an active 6pack channel. */static int sixpack_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg){ struct sixpack *sp = sp_get(tty); unsigned int tmp, err; if (!sp) return -ENXIO; switch(cmd) { case SIOCGIFNAME: err = copy_to_user((void __user *) arg, sp->dev->name, strlen(sp->dev->name) + 1) ? -EFAULT : 0; break; case SIOCGIFENCAP: err = put_user(0, (int __user *)arg); break; case SIOCSIFENCAP: if (get_user(tmp, (int __user *) arg)) { err = -EFAULT; break; } sp->mode = tmp; sp->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */ sp->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3; sp->dev->type = ARPHRD_AX25; err = 0; break; case SIOCSIFHWADDR: err = sp_set_mac_address(sp->dev, (void __user *) arg); break; /* Allow stty to read, but not set, the serial port */ case TCGETS: case TCGETA: err = n_tty_ioctl(tty, (struct file *) file, cmd, arg); break; default: return -ENOIOCTLCMD; } sp_put(sp); return err;}/* Fill in our line protocol discipline */static struct tty_ldisc sp_ldisc = { .owner = THIS_MODULE, .magic = TTY_LDISC_MAGIC, .name = "6pack", .open = sixpack_open, .close = sixpack_close, .ioctl = sixpack_ioctl, .receive_buf = sixpack_receive_buf, .receive_room = sixpack_receive_room, .write_wakeup = sixpack_write_wakeup,};/* Initialize 6pack control device -- register 6pack line discipline */static char msg_banner[] __initdata = KERN_INFO "AX.25: 6pack driver, " SIXPACK_VERSION "\n";static char msg_regfail[] __initdata = KERN_ERR "6pack: can't register line discipline (err = %d)\n";static int __init sixpack_init_driver(void){ int status; printk(msg_banner); /* Register the provided line protocol discipline */ if ((status = tty_register_ldisc(N_6PACK, &sp_ldisc)) != 0) printk(msg_regfail, status); return status;}static const char msg_unregfail[] __exitdata = KERN_ERR "6pack: can't unregister line discipline (err = %d)\n";static void __exit sixpack_exit_driver(void){ int ret; if ((ret = tty_register_ldisc(N_6PACK, NULL))) printk(msg_unregfail, ret);}/* Initialize the 6pack driver. Called by DDI. */static int sixpack_init(struct net_device *dev){ struct sixpack *sp = netdev_priv(dev); if (sp == NULL) /* Allocation failed ?? */ return -ENODEV; /* Set up the "6pack Control Block". (And clear statistics) */ memset(sp, 0, sizeof (struct sixpack)); sp->dev = dev; return 0;}/* encode an AX.25 packet into 6pack */static int encode_sixpack(unsigned char *tx_buf, unsigned char *tx_buf_raw, int length, unsigned char tx_delay){ int count = 0; unsigned char checksum = 0, buf[400]; int raw_count = 0; tx_buf_raw[raw_count++] = SIXP_PRIO_CMD_MASK | SIXP_TX_MASK; tx_buf_raw[raw_count++] = SIXP_SEOF; buf[0] = tx_delay; for (count = 1; count < length; count++) buf[count] = tx_buf[count]; for (count = 0; count < length; count++) checksum += buf[count]; buf[length] = (unsigned char) 0xff - checksum; for (count = 0; count <= length; count++) { if ((count % 3) == 0) { tx_buf_raw[raw_count++] = (buf[count] & 0x3f); tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x30); } else if ((count % 3) == 1) { tx_buf_raw[raw_count++] |= (buf[count] & 0x0f); tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x3c); } else { tx_buf_raw[raw_count++] |= (buf[count] & 0x03); tx_buf_raw[raw_count++] = (buf[count] >> 2); } } if ((length % 3) != 2) raw_count++; tx_buf_raw[raw_count++] = SIXP_SEOF; return raw_count;}/* decode 4 sixpack-encoded bytes into 3 data bytes */static void decode_data(unsigned char inbyte, struct sixpack *sp){ unsigned char *buf; if (sp->rx_count != 3) { sp->raw_buf[sp->rx_count++] = inbyte; return; } buf = sp->raw_buf; sp->cooked_buf[sp->rx_count_cooked++] = buf[0] | ((buf[1] << 2) & 0xc0); sp->cooked_buf[sp->rx_count_cooked++] = (buf[1] & 0x0f) | ((buf[2] << 2) & 0xf0); sp->cooked_buf[sp->rx_count_cooked++] = (buf[2] & 0x03) | (inbyte << 2); sp->rx_count = 0;}/* identify and execute a 6pack priority command byte */static void decode_prio_command(unsigned char cmd, struct sixpack *sp){ unsigned char channel; int actual; channel = cmd & SIXP_CHN_MASK; if ((cmd & SIXP_PRIO_DATA_MASK) != 0) { /* idle ? */ /* RX and DCD flags can only be set in the same prio command, if the DCD flag has been set without the RX flag in the previous prio command. If DCD has not been set before, something in the transmission has gone wrong. In this case, RX and DCD are cleared in order to prevent the decode_data routine from reading further data that might be corrupt. */ if (((sp->status & SIXP_DCD_MASK) == 0) && ((cmd & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)) { if (sp->status != 1) printk(KERN_DEBUG "6pack: protocol violation\n"); else sp->status = 0; cmd &= !SIXP_RX_DCD_MASK; } sp->status = cmd & SIXP_PRIO_DATA_MASK; } else { /* output watchdog char if idle */ if ((sp->status2 != 0) && (sp->duplex == 1)) { sp->led_state = 0x70; sp->tty->driver->write(sp->tty, 0, &sp->led_state, 1); sp->tx_enable = 1; actual = sp->tty->driver->write(sp->tty, 0, sp->xbuff, sp->status2); sp->xleft -= actual; sp->xhead += actual; sp->led_state = 0x60; sp->status2 = 0; } } /* needed to trigger the TNC watchdog */ sp->tty->driver->write(sp->tty, 0, &sp->led_state, 1); /* if the state byte has been received, the TNC is present, so the resync timer can be reset. */ if (sp->tnc_ok == 1) { del_timer(&sp->resync_t); sp->resync_t.data = (unsigned long) sp; sp->resync_t.function = resync_tnc; sp->resync_t.expires = jiffies + SIXP_INIT_RESYNC_TIMEOUT; add_timer(&sp->resync_t); } sp->status1 = cmd & SIXP_PRIO_DATA_MASK;}/* identify and execute a standard 6pack command byte */static void decode_std_command(unsigned char cmd, struct sixpack *sp){ unsigned char checksum = 0, rest = 0, channel; short i; channel = cmd & SIXP_CHN_MASK; switch (cmd & SIXP_CMD_MASK) { /* normal command */ case SIXP_SEOF: if ((sp->rx_count == 0) && (sp->rx_count_cooked == 0)) { if ((sp->status & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK) { sp->led_state = 0x68; sp->tty->driver->write(sp->tty, 0, &sp->led_state, 1); } } else { sp->led_state = 0x60; /* fill trailing bytes with zeroes */ sp->tty->driver->write(sp->tty, 0, &sp->led_state, 1); rest = sp->rx_count; if (rest != 0) for (i = rest; i <= 3; i++) decode_data(0, sp); if (rest == 2) sp->rx_count_cooked -= 2; else if (rest == 3) sp->rx_count_cooked -= 1; for (i = 0; i < sp->rx_count_cooked; i++) checksum += sp->cooked_buf[i]; if (checksum != SIXP_CHKSUM) { printk(KERN_DEBUG "6pack: bad checksum %2.2x\n", checksum); } else { sp->rcount = sp->rx_count_cooked-2; sp_bump(sp, 0); } sp->rx_count_cooked = 0; } break; case SIXP_TX_URUN: printk(KERN_DEBUG "6pack: TX underrun\n"); break; case SIXP_RX_ORUN: printk(KERN_DEBUG "6pack: RX overrun\n"); break; case SIXP_RX_BUF_OVL: printk(KERN_DEBUG "6pack: RX buffer overflow\n"); }}/* decode a 6pack packet */static voidsixpack_decode(struct sixpack *sp, unsigned char pre_rbuff[], int count){ unsigned char inbyte; int count1; for (count1 = 0; count1 < count; count1++) { inbyte = pre_rbuff[count1]; if (inbyte == SIXP_FOUND_TNC) { printk(KERN_INFO "6pack: TNC found.\n"); sp->tnc_ok = 1; del_timer(&sp->resync_t); } if ((inbyte & SIXP_PRIO_CMD_MASK) != 0) decode_prio_command(inbyte, sp); else if ((inbyte & SIXP_STD_CMD_MASK) != 0) decode_std_command(inbyte, sp); else if ((sp->status & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK) decode_data(inbyte, sp); }}MODULE_AUTHOR("Ralf Baechle DO1GRB <ralf@linux-mips.org>");MODULE_DESCRIPTION("6pack driver for AX.25");MODULE_LICENSE("GPL");MODULE_ALIAS_LDISC(N_6PACK);module_init(sixpack_init_driver);module_exit(sixpack_exit_driver);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -