📄 ctctty.c
字号:
ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info){ DECLARE_WAITQUEUE(wait, NULL); int do_clocal = 0; unsigned long flags; int retval; /* * If the device is in the middle of being closed, then block * until it's done, and then try again. */ if (tty_hung_up_p(filp) || (info->flags & CTC_ASYNC_CLOSING)) { if (info->flags & CTC_ASYNC_CLOSING) interruptible_sleep_on(&info->close_wait);#ifdef MODEM_DO_RESTART if (info->flags & CTC_ASYNC_HUP_NOTIFY) return -EAGAIN; else 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 save_flags(flags); cli(); if (!(tty_hung_up_p(filp))) info->count--; restore_flags(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; line = MINOR(tty->device) - tty->driver.minor_start; if (line < 0 || line > CTC_TTY_MAX_DEVICES) return -ENODEV; info = &driver->info[line]; if (ctc_tty_paranoia_check(info, tty->device, "ctc_tty_open")) return -ENODEV; if (!info->netdev) return -ENODEV;#ifdef CTC_DEBUG_MODEM_OPEN printk(KERN_DEBUG "ctc_tty_open %s%d, count = %d\n", tty->driver.name, info->line, 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; } if ((info->count == 1) && (info->flags & CTC_ASYNC_SPLIT_TERMIOS)) { *tty->termios = info->normal_termios; ctc_tty_change_speed(info); }#ifdef CTC_DEBUG_MODEM_OPEN printk(KERN_DEBUG "ctc_tty_open %s%d successful...\n", CTC_TTY_NAME, info->line);#endif return 0;}static voidctc_tty_close(struct tty_struct *tty, struct file *filp){ ctc_tty_info *info = (ctc_tty_info *) tty->driver_data; unsigned long saveflags; ulong flags; ulong timeout; if (!info || ctc_tty_paranoia_check(info, tty->device, "ctc_tty_close")) return; save_flags(flags); cli(); if (tty_hung_up_p(filp)) { restore_flags(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) { restore_flags(flags);#ifdef CTC_DEBUG_MODEM_OPEN printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");#endif return; } info->flags |= CTC_ASYNC_CLOSING; /* * Save the termios structure, since this port may have * separate termios for callout and dialin. */ if (info->flags & CTC_ASYNC_NORMAL_ACTIVE) info->normal_termios = *tty->termios; 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, 3000); /* 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)) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(20); if (time_after(jiffies,timeout)) break; } } ctc_tty_shutdown(info); if (tty->driver.flush_buffer) tty->driver.flush_buffer(tty); if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty); spin_lock_irqsave(&ctc_tty_lock, saveflags); info->tty = 0; spin_unlock_irqrestore(&ctc_tty_lock, saveflags); tty->closing = 0; if (info->blocked_open) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(50); wake_up_interruptible(&info->open_wait); } info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING); wake_up_interruptible(&info->close_wait); restore_flags(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; if (ctc_tty_paranoia_check(info, tty->device, "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(ctc_tty_info *info){ unsigned long saveflags; int again; 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) { queue_task(&info->tq, &tq_immediate); mark_bh(IMMEDIATE_BH); } } spin_unlock_irqrestore(&ctc_tty_lock, saveflags);}intctc_tty_init(void){ int i; ctc_tty_info *info; struct tty_driver *device; 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 = &driver->ctc_tty_device; device->magic = TTY_DRIVER_MAGIC; device->name = ctc_ttyname; device->major = CTC_TTY_MAJOR; device->minor_start = 0; device->num = CTC_TTY_MAX_DEVICES; device->type = TTY_DRIVER_TYPE_SERIAL; device->subtype = CTC_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->refcount = &driver->refcount; device->table = driver->modem_table; device->termios = driver->modem_termios; device->termios_locked = driver->modem_termios_locked; device->open = ctc_tty_open; device->close = ctc_tty_close; device->write = ctc_tty_write; device->put_char = NULL; device->flush_chars = ctc_tty_flush_chars; device->write_room = ctc_tty_write_room; device->chars_in_buffer = ctc_tty_chars_in_buffer; device->flush_buffer = ctc_tty_flush_buffer; device->ioctl = ctc_tty_ioctl; device->throttle = ctc_tty_throttle; device->unthrottle = ctc_tty_unthrottle; device->set_termios = ctc_tty_set_termios; device->stop = NULL; device->start = NULL; device->hangup = ctc_tty_hangup; device->driver_name = "ctc_tty"; if (tty_register_driver(device)) { printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n"); kfree(driver); return -1; } for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) { info = &driver->info[i]; init_MUTEX(&info->write_sem);#if LINUX_VERSION_CODE >= 0x020400 INIT_LIST_HEAD(&info->tq.list);#else info->tq.next = NULL;#endif info->tq.sync = 0; info->tq.routine = (void *)(void *)ctc_tty_task; info->tq.data = info; info->magic = CTC_ASYNC_MAGIC; info->line = i; info->tty = 0; info->count = 0; info->blocked_open = 0; info->normal_termios = device->init_termios; 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(net_device *dev) { int ttynum; char *err; char *p; if ((!dev) || (!dev->name)) { printk(KERN_WARNING "ctc_tty_register_netdev called " "with NULL dev or NULL dev-name\n"); return -1; } 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(net_device *dev) { int i; unsigned long saveflags; ctc_tty_info *info = NULL; 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(int final) { unsigned long saveflags; spin_lock_irqsave(&ctc_tty_lock, saveflags); ctc_tty_shuttingdown = 1; if (final) { kfree(driver); driver = NULL; } else { int i; for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) driver->info[i].tq.routine = NULL; tty_unregister_driver(&driver->ctc_tty_device); } spin_unlock_irqrestore(&ctc_tty_lock, saveflags);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -