⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctctty.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
			return -ERESTARTSYS;#else		return -EAGAIN;#endif	}	/*	 * If non-blocking mode is set, then make the check up front	 * and then exit.	 */	if ((filp->f_flags & O_NONBLOCK) ||	    (tty->flags & (1 << TTY_IO_ERROR))) {		info->flags |= CTC_ASYNC_NORMAL_ACTIVE;		return 0;	}	if (tty->termios->c_cflag & CLOCAL)		do_clocal = 1;	/*	 * Block waiting for the carrier detect and the line to become	 * free (i.e., not in use by the callout).  While we are in	 * this loop, info->count is dropped by one, so that	 * ctc_tty_close() knows when to free things.  We restore it upon	 * exit, either normal or abnormal.	 */	retval = 0;	add_wait_queue(&info->open_wait, &wait);#ifdef CTC_DEBUG_MODEM_OPEN	printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",	       CTC_TTY_NAME, info->line, info->count);#endif	spin_lock_irqsave(&ctc_tty_lock, flags);	if (!(tty_hung_up_p(filp)))		info->count--;	spin_unlock_irqrestore(&ctc_tty_lock, flags);	info->blocked_open++;	while (1) {		set_current_state(TASK_INTERRUPTIBLE);		if (tty_hung_up_p(filp) ||		    !(info->flags & CTC_ASYNC_INITIALIZED)) {#ifdef MODEM_DO_RESTART			if (info->flags & CTC_ASYNC_HUP_NOTIFY)				retval = -EAGAIN;			else				retval = -ERESTARTSYS;#else			retval = -EAGAIN;#endif			break;		}		if (!(info->flags & CTC_ASYNC_CLOSING) &&		    (do_clocal || (info->msr & UART_MSR_DCD))) {			break;		}		if (signal_pending(current)) {			retval = -ERESTARTSYS;			break;		}#ifdef CTC_DEBUG_MODEM_OPEN		printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",		       CTC_TTY_NAME, info->line, info->count);#endif		schedule();	}	current->state = TASK_RUNNING;	remove_wait_queue(&info->open_wait, &wait);	if (!tty_hung_up_p(filp))		info->count++;	info->blocked_open--;#ifdef CTC_DEBUG_MODEM_OPEN	printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",	       CTC_TTY_NAME, info->line, info->count);#endif	if (retval)		return retval;	info->flags |= CTC_ASYNC_NORMAL_ACTIVE;	return 0;}/* * This routine is called whenever a serial port is opened.  It * enables interrupts for a serial port, linking in its async structure into * the IRQ chain.   It also performs the serial-specific * initialization for the tty structure. */static intctc_tty_open(struct tty_struct *tty, struct file *filp){	ctc_tty_info *info;	unsigned long saveflags;	int retval,	 line;	DBF_TEXT(trace, 3, __FUNCTION__);	line = tty->index;	if (line < 0 || line > CTC_TTY_MAX_DEVICES)		return -ENODEV;	info = &driver->info[line];	if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))		return -ENODEV;	if (!info->netdev)		return -ENODEV;#ifdef CTC_DEBUG_MODEM_OPEN	printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,	       info->count);#endif	spin_lock_irqsave(&ctc_tty_lock, saveflags);	info->count++;	tty->driver_data = info;	info->tty = tty;	spin_unlock_irqrestore(&ctc_tty_lock, saveflags);	/*	 * Start up serial port	 */	retval = ctc_tty_startup(info);	if (retval) {#ifdef CTC_DEBUG_MODEM_OPEN		printk(KERN_DEBUG "ctc_tty_open return after startup\n");#endif		return retval;	}	retval = ctc_tty_block_til_ready(tty, filp, info);	if (retval) {#ifdef CTC_DEBUG_MODEM_OPEN		printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");#endif		return retval;	}#ifdef CTC_DEBUG_MODEM_OPEN	printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);#endif	return 0;}static voidctc_tty_close(struct tty_struct *tty, struct file *filp){	ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;	ulong flags;	ulong timeout;	DBF_TEXT(trace, 3, __FUNCTION__);	if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))		return;	spin_lock_irqsave(&ctc_tty_lock, flags);	if (tty_hung_up_p(filp)) {		spin_unlock_irqrestore(&ctc_tty_lock, flags);#ifdef CTC_DEBUG_MODEM_OPEN		printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");#endif		return;	}	if ((tty->count == 1) && (info->count != 1)) {		/*		 * Uh, oh.  tty->count is 1, which means that the tty		 * structure will be freed.  Info->count should always		 * be one in these conditions.  If it's greater than		 * one, we've got real problems, since it means the		 * serial port won't be shutdown.		 */		printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "		       "info->count is %d\n", info->count);		info->count = 1;	}	if (--info->count < 0) {		printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",		       CTC_TTY_NAME, info->line, info->count);		info->count = 0;	}	if (info->count) {		local_irq_restore(flags);#ifdef CTC_DEBUG_MODEM_OPEN		printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");#endif		return;	}	info->flags |= CTC_ASYNC_CLOSING;	tty->closing = 1;	/*	 * At this point we stop accepting input.  To do this, we	 * disable the receive line status interrupts, and tell the	 * interrupt driver to stop checking the data ready bit in the	 * line status register.	 */	if (info->flags & CTC_ASYNC_INITIALIZED) {		tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */		/*		 * Before we drop DTR, make sure the UART transmitter		 * has completely drained; this is especially		 * important if there is a transmit FIFO!		 */		timeout = jiffies + HZ;		while (!(info->lsr & UART_LSR_TEMT)) {			spin_unlock_irqrestore(&ctc_tty_lock, flags);			msleep(500);			spin_lock_irqsave(&ctc_tty_lock, flags);			if (time_after(jiffies,timeout))				break;		}	}	ctc_tty_shutdown(info);	if (tty->driver->flush_buffer) {		skb_queue_purge(&info->tx_queue);		info->lsr |= UART_LSR_TEMT;	}	tty_ldisc_flush(tty);	info->tty = 0;	tty->closing = 0;	if (info->blocked_open) {		msleep_interruptible(500);		wake_up_interruptible(&info->open_wait);	}	info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);	wake_up_interruptible(&info->close_wait);	spin_unlock_irqrestore(&ctc_tty_lock, flags);#ifdef CTC_DEBUG_MODEM_OPEN	printk(KERN_DEBUG "ctc_tty_close normal exit\n");#endif}/* * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled. */static voidctc_tty_hangup(struct tty_struct *tty){	ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;	unsigned long saveflags;	DBF_TEXT(trace, 3, __FUNCTION__);	if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))		return;	ctc_tty_shutdown(info);	info->count = 0;	info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;	spin_lock_irqsave(&ctc_tty_lock, saveflags);	info->tty = 0;	spin_unlock_irqrestore(&ctc_tty_lock, saveflags);	wake_up_interruptible(&info->open_wait);}/* * For all online tty's, try sending data to * the lower levels. */static voidctc_tty_task(unsigned long arg){	ctc_tty_info *info = (void *)arg;	unsigned long saveflags;	int again;	DBF_TEXT(trace, 3, __FUNCTION__);	spin_lock_irqsave(&ctc_tty_lock, saveflags);	if ((!ctc_tty_shuttingdown) && info) {		again = ctc_tty_tint(info);		if (!again)			info->lsr |= UART_LSR_TEMT;		again |= ctc_tty_readmodem(info);		if (again) {			tasklet_schedule(&info->tasklet);		}	}	spin_unlock_irqrestore(&ctc_tty_lock, saveflags);}static struct tty_operations ctc_ops = {	.open = ctc_tty_open,	.close = ctc_tty_close,	.write = ctc_tty_write,	.flush_chars = ctc_tty_flush_chars,	.write_room = ctc_tty_write_room,	.chars_in_buffer = ctc_tty_chars_in_buffer,	.flush_buffer = ctc_tty_flush_buffer,	.ioctl = ctc_tty_ioctl,	.throttle = ctc_tty_throttle,	.unthrottle = ctc_tty_unthrottle,	.set_termios = ctc_tty_set_termios,	.hangup = ctc_tty_hangup,	.tiocmget = ctc_tty_tiocmget,	.tiocmset = ctc_tty_tiocmset,};intctc_tty_init(void){	int i;	ctc_tty_info *info;	struct tty_driver *device;	DBF_TEXT(trace, 2, __FUNCTION__);	driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);	if (driver == NULL) {		printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");		return -ENOMEM;	}	memset(driver, 0, sizeof(ctc_tty_driver));	device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);	if (!device) {		kfree(driver);		printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");		return -ENOMEM;	}	device->devfs_name = "ctc/" CTC_TTY_NAME;	device->name = CTC_TTY_NAME;	device->major = CTC_TTY_MAJOR;	device->minor_start = 0;	device->type = TTY_DRIVER_TYPE_SERIAL;	device->subtype = SERIAL_TYPE_NORMAL;	device->init_termios = tty_std_termios;	device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;	device->flags = TTY_DRIVER_REAL_RAW;	device->driver_name = "ctc_tty",	tty_set_operations(device, &ctc_ops);	if (tty_register_driver(device)) {		printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");		put_tty_driver(device);		kfree(driver);		return -1;	}	driver->ctc_tty_device = device;	for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {		info = &driver->info[i];		init_MUTEX(&info->write_sem);		tasklet_init(&info->tasklet, ctc_tty_task,				(unsigned long) info);		info->magic = CTC_ASYNC_MAGIC;		info->line = i;		info->tty = 0;		info->count = 0;		info->blocked_open = 0;		init_waitqueue_head(&info->open_wait);		init_waitqueue_head(&info->close_wait);		skb_queue_head_init(&info->tx_queue);		skb_queue_head_init(&info->rx_queue);		init_timer(&info->stoptimer);		info->stoptimer.function = ctc_tty_stopdev;		info->stoptimer.data = (unsigned long)info;		info->mcr = UART_MCR_RTS;	}	return 0;}intctc_tty_register_netdev(struct net_device *dev) {	int ttynum;	char *err;	char *p;	DBF_TEXT(trace, 2, __FUNCTION__);	if ((!dev) || (!dev->name)) {		printk(KERN_WARNING		       "ctc_tty_register_netdev called "		       "with NULL dev or NULL dev-name\n");		return -1;	}	/*	 *	If the name is a format string the caller wants us to	 *	do a name allocation : format string must end with %d	 */	if (strchr(dev->name, '%'))	{		int err = dev_alloc_name(dev, dev->name);	// dev->name is changed by this		if (err < 0) {			printk(KERN_DEBUG "dev_alloc returned error %d\n", err);			return err;		}	}	for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);	ttynum = simple_strtoul(p, &err, 0);	if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||	    (err && *err)) {		printk(KERN_WARNING		       "ctc_tty_register_netdev called "		       "with number in name '%s'\n", dev->name);		return -1;	}	if (driver->info[ttynum].netdev) {		printk(KERN_WARNING		       "ctc_tty_register_netdev called "		       "for already registered device '%s'\n",		       dev->name);		return -1;	}	driver->info[ttynum].netdev = dev;	return 0;}voidctc_tty_unregister_netdev(struct net_device *dev) {	int i;	unsigned long saveflags;	ctc_tty_info *info = NULL;	DBF_TEXT(trace, 2, __FUNCTION__);	spin_lock_irqsave(&ctc_tty_lock, saveflags);	for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)		if (driver->info[i].netdev == dev) {			info = &driver->info[i];			break;		}	if (info) {		info->netdev = NULL;		skb_queue_purge(&info->tx_queue);		skb_queue_purge(&info->rx_queue);	}	spin_unlock_irqrestore(&ctc_tty_lock, saveflags);}voidctc_tty_cleanup(void) {	unsigned long saveflags;		DBF_TEXT(trace, 2, __FUNCTION__);	spin_lock_irqsave(&ctc_tty_lock, saveflags);	ctc_tty_shuttingdown = 1;	spin_unlock_irqrestore(&ctc_tty_lock, saveflags);	tty_unregister_driver(driver->ctc_tty_device);	put_tty_driver(driver->ctc_tty_device);	kfree(driver);	driver = NULL;}

⌨️ 快捷键说明

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