Chinaunix首页 | 论坛 | 博客
  • 博客访问: 569777
  • 博文数量: 146
  • 博客积分: 5251
  • 博客等级: 大校
  • 技术积分: 1767
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-10 15:58
文章分类
文章存档

2010年(12)

2008年(129)

2007年(5)

我的朋友

分类: LINUX

2008-11-07 17:16:54

tty驱动包括3类,其中pty和console是内核提供的。pty驱动符合tty驱动规范,结构清晰。通过解析pty驱动程序,我们可以更好的去开发自己的串口设备驱动。

src/drivers/char/pty.c文件,代码不超过500行。包括函数如下:
static void pty_close(struct tty_struct * tty, struct file * filp)
static void pty_unthrottle(struct tty_struct * tty)

static int pty_write(struct tty_struct * tty, int from_user,
         const unsigned char *buf, int count)
static int pty_write_room(struct tty_struct *tty)

static int pty_chars_in_buffer(struct tty_struct *tty)
static int pty_set_lock(struct tty_struct *tty, int * arg) //由pty_bsd_ioctl调用
static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
    unsigned int cmd, unsigned long arg)

static void pty_flush_buffer(struct tty_struct *tty)
static int pty_open(struct tty_struct *tty, struct file * filp)
static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
int __init pty_init(void)

注:所有和 CONFIG_UNIX98_PTYS有关的,我们不考虑,这是扩展功能。
红色带下划线的函数是tty驱动必须的函数。我们逐个解析:

1。static void pty_close(struct tty_struct * tty, struct file * filp)
关闭pty设备
{
if (!tty)   //tty驱动是否初始化过
   return;
if (tty->driver.subtype == PTY_TYPE_MASTER) {
   if (tty->count > 1) //tty驱动还在使用,不能关闭
    printk("master pty_close: count = %d!!\n", tty->count);
} else {
   if (tty->count > 2)
    return;
}
wake_up_interruptible(&tty->read_wait);
wake_up_interruptible(&tty->write_wait);
tty->packet = 0;
if (!tty->link)
   return;
tty->link->packet = 0;
wake_up_interruptible(&tty->link->read_wait);
wake_up_interruptible(&tty->link->write_wait);
set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
if (tty->driver.subtype == PTY_TYPE_MASTER) {
   set_bit(TTY_OTHER_CLOSED, &tty->flags); //设置关闭标志
#ifdef CONFIG_UNIX98_PTYS
   {
    unsigned int major = MAJOR(tty->device) - UNIX98_PTY_MASTER_MAJOR;
    if ( major < UNIX98_NR_MAJORS ) {
     devpts_pty_kill( MINOR(tty->device)
     - tty->driver.minor_start + tty->driver.name_base );
    }
   }
#endif
   tty_unregister_devfs (&tty->link->driver, MINOR (tty->device)); //从devfs卸载
   tty_vhangup(tty->link);
}
}


2。static void pty_unthrottle(struct tty_struct * tty)
/*
* The unthrottle routine is called by the line discipline to signal
* that it can receive more characters. For PTY's, the TTY_THROTTLED
* flag is always set, to force the line discipline to always call the
* unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
* characters in the queue. This is necessary since each time this
* happens, we need to wake up any sleeping processes that could be
* (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
* for the pty buffer to be drained.
*/
该函数有tty_core调用,通知tty驱动,tty_core的输入缓冲区有空间可以接收新字符了。tty驱动在该函数内实现,缓冲区的填充。
{
struct tty_struct *o_tty = tty->link;

if (!o_tty)
   return;

if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
     o_tty->ldisc.write_wakeup)
   (o_tty->ldisc.write_wakeup)(o_tty);
wake_up_interruptible(&o_tty->write_wait); //唤醒写进程
set_bit(TTY_THROTTLED, &tty->flags);
}

3。static int pty_write(struct tty_struct * tty, int from_user,
         const unsigned char *buf, int count)
tty驱动里面的写函数
{
struct tty_struct *to = tty->link;
int c=0, n, room;
char *temp_buffer;

if (!to || tty->stopped)
   return 0;

if (from_user) {
   down(&tty->flip.pty_sem);
   temp_buffer = &tty->flip.char_buf[0];
   while (count > 0) {
    /* check space so we don't copy needlessly */
    n = to->ldisc.receive_room(to);
    if (n > count)
     n = count;
    if (!n) break;

    n = MIN(n, PTY_BUF_SIZE);
    n -= copy_from_user(temp_buffer, buf, n); //写的具体实现
    if (!n) {
     if (!c)
      c = -EFAULT;
     break;
    }

    /* check again in case the buffer filled up */
    room = to->ldisc.receive_room(to);
    if (n > room)
     n = room;
    if (!n) break;
    buf   += n;
    c     += n;
    count -= n;
    to->ldisc.receive_buf(to, temp_buffer, 0, n);
   }
   up(&tty->flip.pty_sem);
} else {
   c = to->ldisc.receive_room(to);
   if (c > count)
    c = count;
   to->ldisc.receive_buf(to, buf, 0, c);
}

return c;
}

4。static int pty_write_room(struct tty_struct *tty)
获取写缓冲区的大小
{
struct tty_struct *to = tty->link;

if (!to || tty->stopped)
   return 0;

return to->ldisc.receive_room(to);
}

5。static int pty_chars_in_buffer(struct tty_struct *tty)
获取缓冲区内,已用空间的大小
{
struct tty_struct *to = tty->link;
int count;

if (!to || !to->ldisc.chars_in_buffer)
   return 0;

/* The ldisc must report 0 if no characters available to be read */
count = to->ldisc.chars_in_buffer(to);

if (tty->driver.subtype == PTY_TYPE_SLAVE) return count;

/* Master side driver ... if the other side's read buffer is less than
* half full, return 0 to allow writers to proceed; otherwise return
* the count. This leaves a comfortable margin to avoid overflow,
* and still allows half a buffer's worth of typed-ahead commands.
*/
return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
}

6。static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
    unsigned int cmd, unsigned long arg)
ioctl函数,做驱动的都知道是干什么用的;)
{
if (!tty) {
   printk("pty_ioctl called with NULL tty!\n");
   return -EIO;
}
switch(cmd) {
case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
   return pty_set_lock(tty, (int *) arg);
}
return -ENOIOCTLCMD;
}

7。static void pty_flush_buffer(struct tty_struct *tty)
//清空缓冲区数据
{
struct tty_struct *to = tty->link;

if (!to)
   return;

if (to->ldisc.flush_buffer)
   to->ldisc.flush_buffer(to);

if (to->packet) {
   tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
   wake_up_interruptible(&to->read_wait);
}
}

8。static int pty_open(struct tty_struct *tty, struct file * filp)
打开设备
{
int retval;
int line;
struct pty_struct *pty;

retval = -ENODEV;
if (!tty || !tty->link)
   goto out;
line = MINOR(tty->device) - tty->driver.minor_start;
if ((line < 0) || (line >= NR_PTYS))
   goto out;
pty = (struct pty_struct *)(tty->driver.driver_state) + line;
tty->driver_data = pty;

retval = -EIO;
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
   goto out;
if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
   goto out;
if (tty->link->count != 1)
   goto out;

clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
wake_up_interruptible(&pty->open_wait);
set_bit(TTY_THROTTLED, &tty->flags);
/* Register a slave for the master */ 注册从设备,为什么我也不清楚。
或许是pty驱动模型决定的吧。
if (tty->driver.major == PTY_MASTER_MAJOR)
   tty_register_devfs(&tty->link->driver,
       DEVFS_FL_CURRENT_OWNER | DEVFS_FL_WAIT,
       tty->link->driver.minor_start +
       MINOR(tty->device)-tty->driver.minor_start);
retval = 0;
out:
return retval;
}

9。static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
终端设置
{
        tty->termios->c_cflag &= ~(CSIZE | PARENB);
        tty->termios->c_cflag |= (CS8 | CREAD);
}

10。int __init pty_init(void)
驱动程序加载时调用的初始化代码
{
int i;

/* Traditional BSD devices */

memset(&pty_state, 0, sizeof(pty_state));
for (i = 0; i < NR_PTYS; i++)
   init_waitqueue_head(&pty_state[i].open_wait);
memset(&pty_driver, 0, sizeof(struct tty_driver));
pty_driver.magic = TTY_DRIVER_MAGIC;
pty_driver.driver_name = "pty_master"; //主设备驱动
#ifdef CONFIG_DEVFS_FS
pty_driver.name = "pty/m%d";
#else
pty_driver.name = "pty";
#endif
pty_driver.major = PTY_MASTER_MAJOR;
pty_driver.minor_start = 0;
pty_driver.num = NR_PTYS;   //pty个数
pty_driver.type = TTY_DRIVER_TYPE_PTY;
pty_driver.subtype = PTY_TYPE_MASTER;
pty_driver.init_termios = tty_std_termios;
pty_driver.init_termios.c_iflag = 0;
pty_driver.init_termios.c_oflag = 0;
pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
pty_driver.init_termios.c_lflag = 0;
pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
pty_driver.refcount = &pty_refcount;
pty_driver.table = pty_table;
pty_driver.termios = pty_termios;
pty_driver.termios_locked = pty_termios_locked;
pty_driver.driver_state = pty_state;
pty_driver.other = &pty_slave_driver;

pty_driver.open = pty_open;   //打开
pty_driver.close = pty_close; //关闭
pty_driver.write = pty_write;    //写
pty_driver.write_room = pty_write_room; //读
pty_driver.flush_buffer = pty_flush_buffer; //
pty_driver.chars_in_buffer = pty_chars_in_buffer;//
pty_driver.unthrottle = pty_unthrottle; //
pty_driver.set_termios = pty_set_termios; //

pty_slave_driver = pty_driver;   //从设备和主设备共用驱动
pty_slave_driver.driver_name = "pty_slave";
pty_slave_driver.proc_entry = 0;
#ifdef CONFIG_DEVFS_FS
pty_slave_driver.name = "pty/s%d";
#else
pty_slave_driver.name = "ttyp";
#endif
pty_slave_driver.subtype = PTY_TYPE_SLAVE;
pty_slave_driver.major = PTY_SLAVE_MAJOR;
pty_slave_driver.minor_start = 0;
pty_slave_driver.init_termios = tty_std_termios;
pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
/* Slave ptys are registered when their corresponding master pty
* is opened, and unregistered when the pair is closed.
*/
pty_slave_driver.flags |= TTY_DRIVER_NO_DEVFS;
pty_slave_driver.table = ttyp_table;
pty_slave_driver.termios = ttyp_termios;
pty_slave_driver.termios_locked = ttyp_termios_locked;
pty_slave_driver.driver_state = pty_state;
pty_slave_driver.other = &pty_driver;

if (tty_register_driver(&pty_driver))   //注册tty驱动(主设备)
   panic("Couldn't register pty driver");
if (tty_register_driver(&pty_slave_driver)) //注册tty驱动(从设备)
   panic("Couldn't register pty slave driver");

/*
* only the master pty gets this ioctl (which is why we
* assign it here, instead of up with the rest of the
* pty_driver initialization. <>
*/
pty_driver.ioctl = pty_bsd_ioctl;    //ioctl函数

/* Unix98 devices */
#ifdef CONFIG_UNIX98_PTYS
devfs_mk_dir (NULL, "pts", NULL);
printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS*NR_PTYS);
for ( i = 0 ; i < UNIX98_NR_MAJORS ; i++ ) {
   int j;

   ptm_driver[i] = pty_driver;
   ptm_driver[i].name = "ptm";
   ptm_driver[i].proc_entry = 0;
   ptm_driver[i].major = UNIX98_PTY_MASTER_MAJOR+i;
   ptm_driver[i].minor_start = 0;
   ptm_driver[i].name_base = i*NR_PTYS;
   ptm_driver[i].num = NR_PTYS;
   ptm_driver[i].other = &pts_driver[i];
   ptm_driver[i].flags |= TTY_DRIVER_NO_DEVFS;
   ptm_driver[i].table = ptm_table[i];
   ptm_driver[i].termios = ptm_termios[i];
   ptm_driver[i].termios_locked = ptm_termios_locked[i];
   ptm_driver[i].driver_state = ptm_state[i];

   for (j = 0; j < NR_PTYS; j++)
    init_waitqueue_head(&ptm_state[i][j].open_wait);
  
   pts_driver[i] = pty_slave_driver;
#ifdef CONFIG_DEVFS_FS
   pts_driver[i].name = "pts/%d";
#else
   pts_driver[i].name = "pts";
#endif
   pts_driver[i].proc_entry = 0;
   pts_driver[i].major = UNIX98_PTY_SLAVE_MAJOR+i;
   pts_driver[i].minor_start = 0;
   pts_driver[i].name_base = i*NR_PTYS;
   pts_driver[i].num = ptm_driver[i].num;
   pts_driver[i].other = &ptm_driver[i];
   pts_driver[i].table = pts_table[i];
   pts_driver[i].termios = pts_termios[i];
   pts_driver[i].termios_locked = pts_termios_locked[i];
   pts_driver[i].driver_state = ptm_state[i];
  
   ptm_driver[i].ioctl = pty_unix98_ioctl;
  
   if (tty_register_driver(&ptm_driver[i]))
    panic("Couldn't register Unix98 ptm driver major %d",
         ptm_driver[i].major);
   if (tty_register_driver(&pts_driver[i]))
    panic("Couldn't register Unix98 pts driver major %d",
         pts_driver[i].major);
}
#endif    //这部分不关心
return 0;
}

    事实上,pty这个虚拟设备是什么?在内核里面起到什么作用等等,我并不是很清楚。同时,解析pty驱动的目的也不是为了弄清pty是如何实现的,比如pty为什么会存在MASTER和SLAVE。
   PTY驱动提供的是tty驱动的最小系统,他为我们开发串口驱动提供了参考。在这里有一点十分重要,那就是pty没有提供read的实现!这个在串口驱动里面是不允许的。或者pty实现的read不在该文件内。
   根据tty驱动规范,read函数不需要显示提供。因为tty设备在接收到数据以后,会马上发送给tty_core。所有的输入是由tty_core完成 的。在真正的串口设备驱动程序里面,read的实现是通过串口接收中断处理函数(rx_intterupt)完成。

阅读(2622) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~