mtd_debug 命令源码
时间: 2023-08-03 16:05:07 浏览: 503
mtd_debug 命令是用于调试MTD驱动的命令,其源码可以在Linux内核源码中找到。具体路径为:drivers/mtd/tests/mtd_debug.c。
以下是 mtd_debug 命令的源码:
```
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/vmalloc.h>
#include <linux/mutex.h>
#include <linux/ctype.h>
static char mtd_buf[4096];
static int mtd_buf_size;
static ssize_t mtd_debug_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
int ret;
if (*ppos >= mtd_buf_size)
return 0;
if (count > mtd_buf_size - *ppos)
count = mtd_buf_size - *ppos;
ret = copy_to_user(buf, mtd_buf + *ppos, count);
if (ret)
return -EFAULT;
*ppos += count;
return count;
}
static ssize_t mtd_debug_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
int ret;
if (count >= sizeof(mtd_buf))
return -EINVAL;
ret = copy_from_user(mtd_buf, buf, count);
if (ret)
return -EFAULT;
mtd_buf_size = count;
return count;
}
static const struct file_operations mtd_debug_fops = {
.owner = THIS_MODULE,
.read = mtd_debug_read,
.write = mtd_debug_write,
};
static struct tty_driver *mtd_debug_tty_driver = NULL;
static int mtd_debug_open(struct inode *inode, struct file *file)
{
if (!try_module_get(THIS_MODULE))
return -ENODEV;
file->private_data = mtd_debug_tty_driver->driver_state;
return tty_open(inode, file);
}
static void mtd_debug_close(struct tty_struct *tty, struct file *file)
{
tty_driver_flush_buffer(tty);
module_put(THIS_MODULE);
tty_close(tty, file);
}
static int mtd_debug_write_room(struct tty_struct *tty)
{
return 65536;
}
static int mtd_debug_put_char(struct tty_struct *tty, unsigned char ch)
{
if (mtd_buf_size >= sizeof(mtd_buf))
return 0;
mtd_buf[mtd_buf_size++] = ch;
return 1;
}
static struct tty_operations mtd_debug_tty_ops = {
.open = mtd_debug_open,
.close = mtd_debug_close,
.write_room = mtd_debug_write_room,
.put_char = mtd_debug_put_char,
};
static struct tty_driver *mtd_debug_init_tty_driver(void)
{
struct tty_driver *driver;
driver = alloc_tty_driver(1);
if (!driver)
return NULL;
driver->owner = THIS_MODULE;
driver->driver_name = "mtd_debug";
driver->name = "mtd_debug";
driver->type = TTY_DRIVER_TYPE_SERIAL;
driver->subtype = SERIAL_TYPE_NORMAL;
driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
tty_set_operations(driver, &mtd_debug_tty_ops);
driver->init_termios = tty_std_termios;
driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
tty_register_driver(driver);
return driver;
}
static void mtd_debug_exit_tty_driver(struct tty_driver *driver)
{
tty_unregister_driver(driver);
put_tty_driver(driver);
}
static int __init mtd_debug_init(void)
{
dev_t dev;
int ret;
dev = MKDEV(0, 0);
ret = register_chrdev_region(dev, 1, "mtd_debug");
if (ret) {
printk(KERN_ERR "mtd_debug: failed to register chrdev region\n");
return ret;
}
mtd_debug_tty_driver = mtd_debug_init_tty_driver();
if (!mtd_debug_tty_driver) {
printk(KERN_ERR "mtd_debug: failed to init tty driver\n");
unregister_chrdev_region(dev, 1);
return -ENOMEM;
}
cdev_init(&mtd_debug_tty_driver->cdev, &mtd_debug_fops);
mtd_debug_tty_driver->cdev.owner = THIS_MODULE;
ret = cdev_add(&mtd_debug_tty_driver->cdev, dev, 1);
if (ret) {
printk(KERN_ERR "mtd_debug: failed to add cdev\n");
mtd_debug_exit_tty_driver(mtd_debug_tty_driver);
unregister_chrdev_region(dev, 1);
return ret;
}
printk(KERN_INFO "mtd_debug: tty driver registered\n");
return 0;
}
static void __exit mtd_debug_exit(void)
{
dev_t dev;
dev = MKDEV(0, 0);
cdev_del(&mtd_debug_tty_driver->cdev);
mtd_debug_exit_tty_driver(mtd_debug_tty_driver);
unregister_chrdev_region(dev, 1);
printk(KERN_INFO "mtd_debug: tty driver unregistered\n");
}
module_init(mtd_debug_init);
module_exit(mtd_debug_exit);
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
MODULE_DESCRIPTION("MTD debug console");
MODULE_LICENSE("GPL");
```
该代码定义了一个字符设备,用于在调试MTD驱动时,查看和修改MTD缓冲区中的数据。同时,该代码还定义了一个 tty 驱动,将缓冲区中的数据输出到终端。用户可以通过在终端中输入和输出数据,对 MTd 缓冲区进行读写,从而进行调试。
阅读全文