linux串口终端驱动——s3c6410平台(四)

时间:2021-07-21 12:21:18

接着上一篇的tty线路规程,这一篇主要说明tty最主要的数据结构tty_driver

1、tty_driver

我认为tty_driver结构体是tty终端设备的根本,他连接了设备和驱动,而且,特定tty设备驱动的主体工作就是填充tty_driver结构体中的成员,实现其中的成员函数,tty_driver结构体如下:



struct tty_driver {
intmagic; /* magic number for this structure */

表示给这个结构体的“幻数”,设为TTY_DRIVER_MAGIC,在alloc_tty_driver()函数中被初始化。
struct kref kref;/* Reference management */引用计数
struct cdev cdev;
struct module *owner;
const char*driver_name;

表示驱动的名字,用在/proc/tty和sysfs中
const char *name;

表示驱动的设备节点名
int name_base;/* offset of printed name */
intmajor; /* major device number */主设备号

        intminor_start;/* start of minor device number */开始次设备号

intminor_num; /* number of *possible* devices */可能的设备数量
int num;/* number of devices allocated */

在tty_register_driver()函数中,用这些注册了字符设备,并使之与以前讲的最上层的操作函数联系在了一起

static const struct file_operations tty_fops = {
.llseek = no_llseek,
.read = tty_read,
.write = tty_write,
.poll = tty_poll,
.unlocked_ioctl= tty_ioctl,
.compat_ioctl = tty_compat_ioctl,
.open = tty_open,
.release = tty_release,
.fasync = tty_fasync,
};

前面一片文章,已经列出了tty_register_driver()的源码,为了看的明白,在列出一次:

/*
 * Called by a tty driver to register itself.
 */
int tty_register_driver(struct tty_driver *driver)
{
int error;
int i;
dev_t dev;
void **p = NULL;


if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
if (!p)
return -ENOMEM;
}


if (!driver->major) {
error = alloc_chrdev_region(&dev, driver->minor_start,
driver->num, driver->name);
if (!error) {
driver->major = MAJOR(dev);
driver->minor_start = MINOR(dev);
}
} else {
dev = MKDEV(driver->major, driver->minor_start);
error = register_chrdev_region(dev, driver->num, driver->name);
}
if (error < 0) {
kfree(p);
return error;
}


if (p) {
driver->ttys = (struct tty_struct **)p;
driver->termios = (struct ktermios **)(p + driver->num);
} else {
driver->ttys = NULL;
driver->termios = NULL;
}


cdev_init(&driver->cdev, &tty_fops);
driver->cdev.owner = driver->owner;
error = cdev_add(&driver->cdev, dev, driver->num);
if (error) {
unregister_chrdev_region(dev, driver->num);
driver->ttys = NULL;
driver->termios = NULL;
kfree(p);
return error;
}



mutex_lock(&tty_mutex);
list_add(&driver->tty_drivers, &tty_drivers);
mutex_unlock(&tty_mutex);


if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
for (i = 0; i < driver->num; i++)
   tty_register_device(driver, i, NULL);
}
proc_tty_register_driver(driver);
driver->flags |= TTY_DRIVER_INSTALLED;
return 0;
}

tty_register_driver()源码到此处结束,

下面的仍然是tty_driver结构体的内容:


shorttype; /* type of tty driver */tty 驱动的类型和子类型
short subtype;/* subtype of tty driver */

struct ktermios init_termios; /* Initial termios */初始的termios
int flags;/* tty driver flags */
struct proc_dir_entry *proc_entry; /* /proc fs entry */
struct tty_driver *other; /* only used for the PTY driver */


/*
* Pointer to the tty data structures

        tty数据结构指针
*/
struct tty_struct **ttys;
struct ktermios **termios;

用于保存当前的线路设置,这些线路设置控制当前的波特率、数据大小、数据流控设置等。驱动会使用一个标准的数值初始化这个成员,它拷贝自tty_std_termios变量,

struct ktermios tty_std_termios = {/* for the benefit of tty drivers  */
.c_iflag = ICRNL | IXON,
.c_oflag = OPOST | ONLCR,
.c_cflag = B38400 | CS8 | CREAD | HUPCL,
.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  ECHOCTL | ECHOKE | IEXTEN,
.c_cc = INIT_C_CC,
.c_ispeed = 38400,
.c_ospeed = 38400
};
struct ktermios **termios_locked;
void *driver_state;

/*
* Driver methods驱动中的操作函数,其成员函数通常在特定设备tty驱动模块初始化函数中被赋值。
*/

const struct tty_operations *ops;


struct tty_operations {
struct tty_struct * (*lookup)(struct tty_driver *driver,
struct inode *inode, int idx);
int  (*install)(struct tty_driver *driver, struct tty_struct *tty);
void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
int  (*open)(struct tty_struct * tty, struct file * filp);
void (*close)(struct tty_struct * tty, struct file * filp);
void (*shutdown)(struct tty_struct *tty);
int  (*write)(struct tty_struct * tty,
     const unsigned char *buf, int count);
int  (*put_char)(struct tty_struct *tty, unsigned char ch);
void (*flush_chars)(struct tty_struct *tty);
int  (*write_room)(struct tty_struct *tty);
int  (*chars_in_buffer)(struct tty_struct *tty);
int  (*ioctl)(struct tty_struct *tty, struct file * file,
   unsigned int cmd, unsigned long arg);
long (*compat_ioctl)(struct tty_struct *tty, struct file * file,
    unsigned int cmd, unsigned long arg);
void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
void (*throttle)(struct tty_struct * tty);
void (*unthrottle)(struct tty_struct * tty);
void (*stop)(struct tty_struct *tty);
void (*start)(struct tty_struct *tty);
void (*hangup)(struct tty_struct *tty);
int (*break_ctl)(struct tty_struct *tty, int state);
void (*flush_buffer)(struct tty_struct *tty);
void (*set_ldisc)(struct tty_struct *tty);
void (*wait_until_sent)(struct tty_struct *tty, int timeout);
void (*send_xchar)(struct tty_struct *tty, char ch);
int (*read_proc)(char *page, char **start, off_t off,
 int count, int *eof, void *data);
int (*tiocmget)(struct tty_struct *tty, struct file *file);
int (*tiocmset)(struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear);
int (*resize)(struct tty_struct *tty, struct tty_struct *real_tty,
struct winsize *ws);
int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
#ifdef CONFIG_CONSOLE_POLL
int (*poll_init)(struct tty_driver *driver, int line, char *options);
int (*poll_get_char)(struct tty_driver *driver, int line);
void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
#endif
};

struct list_head tty_drivers;
};

2、tty设备

仅有驱动不够,驱动必须依附于设备,tty_register_device()用于注册tty设备,当然,还有其他函数,如注销函数等。仅以一例说明。

struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  struct device *device)
{
char name[64];
dev_t dev = MKDEV(driver->major, driver->minor_start) + index;


if (index >= driver->num) {
printk(KERN_ERR "Attempt to register invalid tty line number "
      " (%d).\n", index);
return ERR_PTR(-EINVAL);
}


if (driver->type == TTY_DRIVER_TYPE_PTY)
pty_line_name(driver, index, name);
else
tty_line_name(driver, index, name);


return device_create(tty_class, device, dev, NULL, name);
}

直到最后调用的函数


/**
 * device_add - add device to device hierarchy.添加到设备树中,大概就是sysfs文件系统的bus,class等的绑定,把设备文件建在那个bus,父节点等,具体就不太清楚了。
 * @dev: device.
 *
 * This is part 2 of device_register(), though may be called
 * separately _iff_ device_initialize() has been called separately.
 *
 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
 * to the global and sibling lists for the device, then
 * adds it to the other relevant subsystems of the driver model.
 *
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up your
 * reference instead.
 */
int device_add(struct device *dev)
{
struct device *parent = NULL;
struct class_interface *class_intf;
int error = -EINVAL;


dev = get_device(dev);
if (!dev)
goto done;


/* Temporarily support init_name if it is set.
* It will override bus_id for now */
if (dev->init_name)
dev_set_name(dev, "%s", dev->init_name);


if (!strlen(dev->bus_id))
goto done;


pr_debug("device: '%s': %s\n", dev->bus_id, __func__);


parent = get_device(dev->parent);
setup_parent(dev, parent);


/* use parent numa_node */
if (parent)
set_dev_node(dev, dev_to_node(parent));


/* first, register with generic layer. */
error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
if (error)
goto Error;



/* notify platform of device entry */
if (platform_notify)
platform_notify(dev);


/* notify clients of device entry (new way) */
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
    BUS_NOTIFY_ADD_DEVICE, dev);



error = device_create_file(dev, &uevent_attr);
if (error)
goto attrError;



if (MAJOR(dev->devt)) {
error = device_create_file(dev, &devt_attr);
if (error)
goto ueventattrError;


error = device_create_sys_dev_entry(dev);
if (error)
goto devtattrError;

}


error = device_add_class_symlinks(dev);
if (error)
goto SymlinkError;
error = device_add_attrs(dev);
if (error)
goto AttrsError;
error = bus_add_device(dev);
if (error)
goto BusError;
error = dpm_sysfs_add(dev);
if (error)
goto DPMError;
device_pm_add(dev);
kobject_uevent(&dev->kobj, KOBJ_ADD);
bus_attach_device(dev);
if (parent)
klist_add_tail(&dev->knode_parent, &parent->klist_children);


if (dev->class) {
mutex_lock(&dev->class->p->class_mutex);
/* tie the class to the device */
klist_add_tail(&dev->knode_class,
      &dev->class->p->class_devices);


/* notify any interfaces that the device is here */
list_for_each_entry(class_intf,
   &dev->class->p->class_interfaces, node)
if (class_intf->add_dev)
class_intf->add_dev(dev, class_intf);
mutex_unlock(&dev->class->p->class_mutex);
}
done:
put_device(dev);
return error;
 DPMError:
bus_remove_device(dev);
 BusError:
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
    BUS_NOTIFY_DEL_DEVICE, dev);
device_remove_attrs(dev);
 AttrsError:
device_remove_class_symlinks(dev);
 SymlinkError:
if (MAJOR(dev->devt))
device_remove_sys_dev_entry(dev);
 devtattrError:
if (MAJOR(dev->devt))
device_remove_file(dev, &devt_attr);
 ueventattrError:
device_remove_file(dev, &uevent_attr);
 attrError:
kobject_uevent(&dev->kobj, KOBJ_REMOVE);
kobject_del(&dev->kobj);
 Error:
cleanup_device_parent(dev);
if (parent)
put_device(parent);
goto done;
}