68328serial.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,628 行 · 第 1/3 页
C
1,628 行
copy_to_user((struct m68k_serial *) arg, info, sizeof(struct m68k_serial)); return 0; default: return -ENOIOCTLCMD; } return 0;}static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios){ struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; if (tty->termios->c_cflag == old_termios->c_cflag) return; change_speed(info); if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) { tty->hw_stopped = 0; rs_start(tty); } }/* * ------------------------------------------------------------ * rs_close() * * This routine is called when the serial port gets closed. First, we * wait for the last remaining data to be sent. Then, we unlink its * S structure from the interrupt chain if necessary, and we free * that IRQ if nothing is left in the chain. * ------------------------------------------------------------ */static void rs_close(struct tty_struct *tty, struct file * filp){ struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; m68328_uart *uart = &uart_addr[info->line]; unsigned long flags; if (!info || serial_paranoia_check(info, tty->name, "rs_close")) return; save_flags(flags); cli(); if (tty_hung_up_p(filp)) { restore_flags(flags); 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("rs_close: bad serial port count; tty->count is 1, " "info->count is %d\n", info->count); info->count = 1; } if (--info->count < 0) { printk("rs_close: bad serial port count for ttyS%d: %d\n", info->line, info->count); info->count = 0; } if (info->count) { restore_flags(flags); return; } info->flags |= S_CLOSING; /* * Now we wait for the transmit buffer to clear; and we notify * the line discipline to only process XON/XOFF characters. */ tty->closing = 1; if (info->closing_wait != S_CLOSING_WAIT_NONE) tty_wait_until_sent(tty, info->closing_wait); /* * 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. */ uart->ustcnt &= ~USTCNT_RXEN; uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK); shutdown(info); if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty); tty_ldisc_flush(tty); tty->closing = 0; info->event = 0; info->tty = 0;#warning "This is not and has never been valid so fix it" #if 0 if (tty->ldisc.num != ldiscs[N_TTY].num) { if (tty->ldisc.close) (tty->ldisc.close)(tty); tty->ldisc = ldiscs[N_TTY]; tty->termios->c_line = N_TTY; if (tty->ldisc.open) (tty->ldisc.open)(tty); }#endif if (info->blocked_open) { if (info->close_delay) { current->state = TASK_INTERRUPTIBLE; schedule_timeout(info->close_delay); } wake_up_interruptible(&info->open_wait); } info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING); wake_up_interruptible(&info->close_wait); restore_flags(flags);}/* * rs_hangup() --- called by tty_hangup() when a hangup is signaled. */void rs_hangup(struct tty_struct *tty){ struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; if (serial_paranoia_check(info, tty->name, "rs_hangup")) return; rs_flush_buffer(tty); shutdown(info); info->event = 0; info->count = 0; info->flags &= ~S_NORMAL_ACTIVE; info->tty = 0; wake_up_interruptible(&info->open_wait);}/* * ------------------------------------------------------------ * rs_open() and friends * ------------------------------------------------------------ */static int block_til_ready(struct tty_struct *tty, struct file * filp, struct m68k_serial *info){ DECLARE_WAITQUEUE(wait, current); int retval; int do_clocal = 0; /* * If the device is in the middle of being closed, then block * until it's done, and then try again. */ if (info->flags & S_CLOSING) { interruptible_sleep_on(&info->close_wait);#ifdef SERIAL_DO_RESTART if (info->flags & S_HUP_NOTIFY) return -EAGAIN; else return -ERESTARTSYS;#else return -EAGAIN;#endif } /* * If non-blocking mode is set, or the port is not enabled, * then make the check up front and then exit. */ if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) { info->flags |= S_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 * rs_close() knows when to free things. We restore it upon * exit, either normal or abnormal. */ retval = 0; add_wait_queue(&info->open_wait, &wait); info->count--; info->blocked_open++; while (1) { cli(); m68k_rtsdtr(info, 1); sti(); current->state = TASK_INTERRUPTIBLE; if (tty_hung_up_p(filp) || !(info->flags & S_INITIALIZED)) {#ifdef SERIAL_DO_RESTART if (info->flags & S_HUP_NOTIFY) retval = -EAGAIN; else retval = -ERESTARTSYS; #else retval = -EAGAIN;#endif break; } if (!(info->flags & S_CLOSING) && do_clocal) break; if (signal_pending(current)) { retval = -ERESTARTSYS; break; } schedule(); } current->state = TASK_RUNNING; remove_wait_queue(&info->open_wait, &wait); if (!tty_hung_up_p(filp)) info->count++; info->blocked_open--; if (retval) return retval; info->flags |= S_NORMAL_ACTIVE; return 0;} /* * This routine is called whenever a serial port is opened. It * enables interrupts for a serial port, linking in its S structure into * the IRQ chain. It also performs the serial-specific * initialization for the tty structure. */int rs_open(struct tty_struct *tty, struct file * filp){ struct m68k_serial *info; int retval, line; line = tty->index; if (line >= NR_PORTS || line < 0) /* we have exactly one */ return -ENODEV; info = &m68k_soft[line]; if (serial_paranoia_check(info, tty->name, "rs_open")) return -ENODEV; info->count++; tty->driver_data = info; info->tty = tty; /* * Start up serial port */ retval = startup(info); if (retval) return retval; return block_til_ready(tty, filp, info);}/* Finally, routines used to initialize the serial driver. */static void show_serial_version(void){ printk("MC68328 serial driver version 1.00\n");}#ifdef CONFIG_PM/* Serial Power management * The console (currently fixed at line 0) is a special case for power * management because the kernel is so chatty. The console will be * explicitly disabled my our power manager as the last minute, so we won't * mess with it here. */static struct pm_dev *serial_pm[NR_PORTS];static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data){ struct m68k_serial *info = (struct m68k_serial *)dev->data; if(info == NULL) return -1; /* special case for line 0 - pm restores it */ if(info->line == 0) return 0; switch (request) { case PM_SUSPEND: shutdown(info); break; case PM_RESUME: startup(info); break; } return 0;}void shutdown_console(void){ struct m68k_serial *info = &m68k_soft[0]; /* HACK: wait a bit for any pending printk's to be dumped */ { int i = 10000; while(i--); } shutdown(info);}void startup_console(void){ struct m68k_serial *info = &m68k_soft[0]; startup(info);}#endifstatic struct tty_operations rs_ops = { .open = rs_open, .close = rs_close, .write = rs_write, .flush_chars = rs_flush_chars, .write_room = rs_write_room, .chars_in_buffer = rs_chars_in_buffer, .flush_buffer = rs_flush_buffer, .ioctl = rs_ioctl, .throttle = rs_throttle, .unthrottle = rs_unthrottle, .set_termios = rs_set_termios, .stop = rs_stop, .start = rs_start, .hangup = rs_hangup, .set_ldisc = rs_set_ldisc,};/* rs_init inits the driver */static int __initrs68328_init(void){ int flags, i; struct m68k_serial *info; serial_driver = alloc_tty_driver(NR_PORTS); if (!serial_driver) return -ENOMEM; show_serial_version(); /* Initialize the tty_driver structure */ /* SPARC: Not all of this is exactly right for us. */ serial_driver->name = "ttyS"; serial_driver->major = TTY_MAJOR; serial_driver->minor_start = 64; serial_driver->type = TTY_DRIVER_TYPE_SERIAL; serial_driver->subtype = SERIAL_TYPE_NORMAL; serial_driver->init_termios = tty_std_termios; serial_driver->init_termios.c_cflag = m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL; serial_driver->flags = TTY_DRIVER_REAL_RAW; tty_set_operations(serial_driver, &rs_ops); if (tty_register_driver(serial_driver)) { put_tty_driver(serial_driver); printk(KERN_ERR "Couldn't register serial driver\n"); return -ENOMEM; } save_flags(flags); cli(); for(i=0;i<NR_PORTS;i++) { info = &m68k_soft[i]; info->magic = SERIAL_MAGIC; info->port = (int) &uart_addr[i]; info->tty = 0; info->irq = uart_irqs[i]; info->custom_divisor = 16; info->close_delay = 50; info->closing_wait = 3000; info->x_char = 0; info->event = 0; info->count = 0; info->blocked_open = 0; INIT_WORK(&info->tqueue, do_softint, info); INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info); init_waitqueue_head(&info->open_wait); init_waitqueue_head(&info->close_wait); info->line = i; info->is_cons = 1; /* Means shortcuts work */ printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line, info->port, info->irq); printk(" is a builtin MC68328 UART\n"); IRQ_ports[info->irq] = info; /* waste of space */#ifdef CONFIG_M68VZ328 if (i > 0 ) PJSEL &= 0xCF; /* PSW enable second port output */#endif if (request_irq(uart_irqs[i], rs_interrupt, IRQ_FLG_STD, "M68328_UART", NULL)) panic("Unable to attach 68328 serial interrupt\n");#ifdef CONFIG_PM serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback); if (serial_pm[i]) serial_pm[i]->data = info;#endif } restore_flags(flags); return 0;}/* * register_serial and unregister_serial allows for serial ports to be * configured at run-time, to support PCMCIA modems. *//* SPARC: Unused at this time, just here to make things link. */int register_serial(struct serial_struct *req){ return -1;}void unregister_serial(int line){ return;} module_init(rs68328_init);static void m68328_set_baud(void){ unsigned short ustcnt; int i; ustcnt = USTCNT; USTCNT = ustcnt & ~USTCNT_TXEN;again: for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++) if (baud_table[i] == m68328_console_baud) break; if (i >= sizeof(baud_table) / sizeof(baud_table[0])) { m68328_console_baud = 9600; goto again; } UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7); ustcnt |= USTCNT_8_7; ustcnt |= USTCNT_TXEN; USTCNT = ustcnt; m68328_console_initted = 1; return;}int m68328_console_setup(struct console *cp, char *arg){ int i, n = CONSOLE_BAUD_RATE; if (!cp) return(-1); if (arg) n = simple_strtoul(arg,NULL,0); for (i = 0; i < BAUD_TABLE_SIZE; i++) if (baud_table[i] == n) break; if (i < BAUD_TABLE_SIZE) { m68328_console_baud = n; m68328_console_cbaud = 0; if (i > 15) { m68328_console_cbaud |= CBAUDEX; i -= 15; } m68328_console_cbaud |= i; } m68328_set_baud(); /* make sure baud rate changes */ return(0);}static struct tty_driver *m68328_console_device(struct console *c, int *index){ *index = c->index; return serial_driver;}void m68328_console_write (struct console *co, const char *str, unsigned int count){ if (!m68328_console_initted) m68328_set_baud(); while (count--) { if (*str == '\n') rs_put_char('\r'); rs_put_char( *str++ ); }}static struct console m68328_driver = { .name = "ttyS", .write = m68328_console_write, .device = m68328_console_device, .setup = m68328_console_setup, .flags = CON_PRINTBUFFER, .index = -1,};static int __init m68328_console_init(void){ register_console(&m68328_driver); return 0;}console_initcall(m68328_console_init);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?