6pack.c

来自「powerpc内核mpc8241linux系统下net驱动程序」· C语言 代码 · 共 1,130 行 · 第 1/2 页

C
1,130
字号
	/* Perform the low-level 6pack initialization. */	if ((err = sp_open(sp->dev)))  		return err;		MOD_INC_USE_COUNT;	/* Done.  We have linked the TTY line to a channel. */	tnc_init(sp);	return sp->dev->base_addr;}/* * 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 voidsixpack_close(struct tty_struct *tty){	struct sixpack *sp = (struct sixpack *) tty->disc_data;	/* First make sure we're connected. */	if (!sp || sp->magic != SIXPACK_MAGIC)		return;	rtnl_lock();	if (sp->dev->flags & IFF_UP)		(void) dev_close(sp->dev);	del_timer(&(sp->tx_t));	del_timer(&(sp->resync_t));		tty->disc_data = 0;	sp->tty = NULL;	/* VSV = very important to remove timers */	sp_free(sp);	unregister_netdev(sp->dev);	rtnl_unlock();	MOD_DEC_USE_COUNT;}static struct net_device_stats *sp_get_stats(struct device *dev){	static struct net_device_stats stats;	struct sixpack *sp = (struct sixpack*)(dev->priv);	memset(&stats, 0, sizeof(struct net_device_stats));	stats.rx_packets     = sp->rx_packets;	stats.tx_packets     = sp->tx_packets;	stats.rx_bytes       = sp->rx_bytes;	stats.tx_bytes       = sp->tx_bytes;	stats.rx_dropped     = sp->rx_dropped;	stats.tx_dropped     = sp->tx_dropped;	stats.tx_errors      = sp->tx_errors;	stats.rx_errors      = sp->rx_errors;	stats.rx_over_errors = sp->rx_over_errors;	return (&stats);}intsp_set_mac_address(struct device *dev, void *addr){	int err;	err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);	if (err)  {		return err;	}	copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN);	/* addr is an AX.25 shifted ASCII mac address */	return 0;}static intsp_set_dev_mac_address(struct device *dev, void *addr){	struct sockaddr *sa=addr;	memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);	return 0;}/* Perform I/O control on an active 6pack channel. */static intsixpack_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg){	struct sixpack *sp = (struct sixpack *) tty->disc_data;	int err;	unsigned int tmp;	/* First make sure we're connected. */	if (!sp || sp->magic != SIXPACK_MAGIC) {		return -EINVAL;	}	switch(cmd) {	 case SIOCGIFNAME:		err = verify_area(VERIFY_WRITE, arg, strlen(sp->dev->name) + 1);		if (err)  {			return err;		}		copy_to_user(arg, sp->dev->name, strlen(sp->dev->name) + 1);		return 0;	case SIOCGIFENCAP:		err = verify_area(VERIFY_WRITE, arg, sizeof(int));		if (err)  {			return err;		}		put_user(0, (int *)arg);		return 0;	case SIOCSIFENCAP:		err = verify_area(VERIFY_READ, arg, sizeof(int));		if (err)  {			return err;		}		get_user(tmp,(int *)arg); 		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;		return 0;	 case SIOCSIFHWADDR:		return sp_set_mac_address(sp->dev, arg);	/* Allow stty to read, but not set, the serial port */	case TCGETS:	case TCGETA:		return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);	default:		return -ENOIOCTLCMD;	}}static int sp_open_dev(struct device *dev){	struct sixpack *sp = (struct sixpack*)(dev->priv);	if(sp->tty==NULL)		return -ENODEV;	return 0;}/* Initialize 6pack control device -- register 6pack line discipline */#ifdef MODULEstatic int sixpack_init_ctrl_dev(void)#else	/* !MODULE */__initfunc(int sixpack_init_ctrl_dev(struct device *dummy))#endif	/* !MODULE */{	int status;	if (sixpack_maxdev < 4) sixpack_maxdev = 4; /* Sanity */	printk(KERN_INFO "6pack: %s (dynamic channels, max=%d)\n",	       SIXPACK_VERSION, sixpack_maxdev);	sixpack_ctrls = (sixpack_ctrl_t **) kmalloc(sizeof(void*)*sixpack_maxdev, GFP_KERNEL);	if (sixpack_ctrls == NULL) 	{		printk(KERN_WARNING "6pack: Can't allocate sixpack_ctrls[] array!  Uaargh! (-> No 6pack available)\n");		return -ENOMEM;	}	/* Clear the pointer array, we allocate devices when we need them */	memset(sixpack_ctrls, 0, sizeof(void*)*sixpack_maxdev); /* Pointers */	/* Fill in our line protocol discipline, and register it */	memset(&sp_ldisc, 0, sizeof(sp_ldisc));	sp_ldisc.magic  = TTY_LDISC_MAGIC;	sp_ldisc.name   = "6pack";	sp_ldisc.flags  = 0;	sp_ldisc.open   = sixpack_open;	sp_ldisc.close  = sixpack_close;	sp_ldisc.read   = NULL;	sp_ldisc.write  = NULL;	sp_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *,				   unsigned int, unsigned long)) sixpack_ioctl;	sp_ldisc.poll = NULL;	sp_ldisc.receive_buf = sixpack_receive_buf;	sp_ldisc.receive_room = sixpack_receive_room;	sp_ldisc.write_wakeup = sixpack_write_wakeup;	if ((status = tty_register_ldisc(N_6PACK, &sp_ldisc)) != 0)  {		printk(KERN_WARNING "6pack: can't register line discipline (err = %d)\n", status);	}#ifdef MODULE	return status;#else	/* Return "not found", so that dev_init() will unlink	 * the placeholder device entry for us.	 */	return ENODEV;#endif}/* Initialize the 6pack driver.  Called by DDI. */intsixpack_init(struct device *dev){	struct sixpack *sp = (struct sixpack*)(dev->priv);	static char ax25_bcast[AX25_ADDR_LEN] =		{'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};	static char ax25_test[AX25_ADDR_LEN] =		{'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};	if (sp == NULL)		/* Allocation failed ?? */	  return -ENODEV;	/* Set up the "6pack Control Block". (And clear statistics) */		memset(sp, 0, sizeof (struct sixpack));	sp->magic  = SIXPACK_MAGIC;	sp->dev	   = dev;		/* Finish setting up the DEVICE info. */	dev->mtu		= SIXP_MTU;	dev->hard_start_xmit	= sp_xmit;	dev->open		= sp_open_dev;	dev->stop		= sp_close;	dev->hard_header	= sp_header;	dev->get_stats	        = sp_get_stats;	dev->set_mac_address    = sp_set_dev_mac_address;	dev->hard_header_len	= AX25_MAX_HEADER_LEN;	dev->addr_len		= AX25_ADDR_LEN;	dev->type		= ARPHRD_AX25;	dev->tx_queue_len	= 10;	dev->rebuild_header	= sp_rebuild_header;	memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);	/* Only activated in AX.25 mode */	memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);	/*    ""      ""       ""    "" */	dev_init_buffers(dev);	/* New-style flags. */	dev->flags		= 0;	return 0;}#ifdef MODULEintinit_module(void){	return sixpack_init_ctrl_dev();}voidcleanup_module(void){	int i;	if (sixpack_ctrls != NULL) 	{		for (i = 0; i < sixpack_maxdev; i++)  		{			if (sixpack_ctrls[i])			{				/*				 * VSV = if dev->start==0, then device				 * unregistered while close proc.				 */ 				if (sixpack_ctrls[i]->dev.start)					unregister_netdev(&(sixpack_ctrls[i]->dev));				kfree(sixpack_ctrls[i]);				sixpack_ctrls[i] = NULL;			}		}		kfree(sixpack_ctrls);		sixpack_ctrls = NULL;	}	if ((i = tty_register_ldisc(N_6PACK, NULL)))  	{		printk(KERN_WARNING "6pack: can't unregister line discipline (err = %d)\n", i);	}}#endif /* MODULE *//* ----> 6pack timer interrupt handler and friends. <---- */static void sp_start_tx_timer(struct sixpack *sp){	int when = sp->slottime;		del_timer(&(sp->tx_t));	sp->tx_t.data = (unsigned long) sp;	sp->tx_t.function = sp_xmit_on_air;	sp->tx_t.expires = jiffies + ((when+1)*HZ)/100;	add_timer(&(sp->tx_t));}/* encode an AX.25 packet into 6pack */int encode_sixpack(byte *tx_buf, byte *tx_buf_raw, int length, byte tx_delay){	int count = 0;	byte 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] = (byte)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);		} /* else */	} /* for */	if ((length % 3) != 2)		raw_count++;	tx_buf_raw[raw_count++] = SIXP_SEOF;	return(raw_count);}/* decode a 6pack packet */voidsixpack_decode(struct sixpack *sp, unsigned char pre_rbuff[], int count){	byte 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);		} /* else */	} /* for */}static inttnc_init(struct sixpack *sp){	static byte inbyte;		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;}/* identify and execute a 6pack priority command byte */void decode_prio_command(byte cmd, struct sixpack *sp){	byte 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;	} /* if */	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;		} /* if */	} /* else */	/* 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;}/* try to resync the TNC. Called by the resync timer defined in  decode_prio_command */static voidresync_tnc(unsigned long channel){	static char resync_cmd = 0xe8;	struct sixpack *sp = (struct sixpack *) channel;	printk(KERN_INFO "6pack: resyncing TNC\n");	/* 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));}/* identify and execute a standard 6pack command byte */void decode_std_command(byte cmd, struct sixpack *sp){	byte 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);				} /* if */			} 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);				} /* else */				sp->rx_count_cooked = 0;			} /* else */			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");	} /* switch */} /* function *//* decode 4 sixpack-encoded bytes into 3 data bytes */void decode_data(byte inbyte, struct sixpack *sp){	unsigned char *buf;	if (sp->rx_count != 3)		sp->raw_buf[sp->rx_count++] = inbyte;	else {		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;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?