Linux MTD (Memory technology Device) subsystem analysis-for Atheros char device

Source: Internet
Author: User
Tags fread joins

Linux MTD (Memory technology Device) subsystem analysis

For Atheros Char device

Read the Linux MTD Source code Analysis to this part of the overall understanding, combined with the existing code, analysis of how Atheros MTD is used.

Linux kernel:2.6.31.

Atheros platform:qca9890???

Refer to the Linux MTD Source code Analysis , where MTD is divided into 4 layers, from top to bottom: Character device node, character device, MTD core, FLASH hardware driver.

Character Device node:/DEV/MTD0/DEV/MTD1 and other nodes, can be used directly through fopen (). These nodes are created by the MTD subsystem, where the user operates MTD by accessing these nodes.

Character device: The MTD subsystem encapsulates MTD as a character device.

MTD Core: Manages all of the MTD devices and provides Get/add/del MTD capabilities, while a MTD device may logically be partitioned into multiple zones, each of which is treated as a MTD device, so it also provides the ability to bulk add and remove MTD devices through partition information. To obtain partition information from the kernel startup parameters, the MTD core provides the ability to analyze the partition information in the startup parameters.

Flash hardware driver: Provide flash read/write/erase and other functions. Because the Atheros partition information is obtained in the kernel startup parameters, there is also the function of analyzing the partition information of the starting parameter, and the MTD device is added to the MTD core according to the partition information.

When the system starts, the MTD core layer can be directly compiled into kernel, the Flash driver layer resolves the partition information at system startup, the MTD is added to the MTD core layer, the MTD core layer completes the device node creation, and the MTD character device registers the MTD character device information, The specific execution function can be found when the device node is accessed.

Figure 1 Atheros MTD Subsystem Architecture

Figure 1 shows the main information for each layer.

The use of MTD devices is by accessing the device node/DEV/MTD0/DEV/MTD1, the general usage is

File *f = fopen ("/dev/mtd0", "R");

Fread (void *ptr, size_t size, size_t nmemb, FILE *stream);

fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream);

Devices such as/dev/mtd0 are created by the MTD core and created by Add_mtd_device ()->device_register (). Fread/fwrite is implemented by the MTD character device layer for specific reads and writes, as to how to find the functions of the MTD character device layer, and how the device nodes are associated with these functions, need to look at the Fread and Linux driver Model (device_register/ Register_chrdev), then summarize it later.

The MTD character device layer is implemented through Register_chrdev (), which needs to provide functions in the struct file_operations that complete the read/write/erase of the specified MTD. After Register_chrdev (), when you visit MTD with fread and so on, you will find the function registered here.

static const struct File_operations Mtd_fops = {

. Owner = This_module,

. Llseek = Mtd_lseek,

. Read = Mtd_read,

. write = Mtd_write,

. IOCTL = Mtd_ioctl,

#ifdef CONFIG_COMPAT

. Compat_ioctl = Mtd_compat_ioctl,

#endif

. open = Mtd_open,

. Release = Mtd_close,

. mmap = Mtd_mmap,

#ifndef Config_mmu

. Get_unmapped_area = Mtd_get_unmapped_area,

#endif

};

static int __init Init_mtdchar (void)

{

int status;

Status = Register_chrdev (Mtd_char_major, "MTD", &mtd_fops);

if (Status < 0) {

PRINTK (kern_notice "Can ' t allocate major number%d for Memory technology devices.\n",

Mtd_char_major);

}

return status;

}

specifically to see how open/read/close is using MTD, Get/put_mtd_device has the MTD core layer provided, which will be seen in detail below. Mtd_open obtains the device number through the Inode, further obtains the mtd_info information of MTD by Get_mtd_device, and obtains the function of the specific operation MTD; the mtd_read/write and other functions can be used directly mtd- >read/mtd->write complete the specific operation.

static int Mtd_open (struct inode *inode, struct file *file)

{

int minor = Iminor (Inode);

int devnum = minor >> 1;

...

MTD = Get_mtd_device (NULL, devnum);

....

MFI = Kzalloc (sizeof (*MFI), Gfp_kernel);

MFI->MTD = MTD;

File->private_data = MFI;

Out

Unlock_kernel ();

return ret;

}/* Mtd_open */

Static ssize_t mtd_read (struct file *file, char __user *buf, size_t count,loff_t *ppos)

{

struct Mtd_file_info *mfi = file->private_data;

struct Mtd_info *MTD = mfi->mtd;

...

while (count) {

Switch (mfi->mode) {

Default

ret = Mtd->read (MTD, *ppos, Len, &retlen, kbuf);

}

}

...

return Total_retlen;

}/* Mtd_read */

static int mtd_close (struct inode *inode, struct file *file)

{

struct Mtd_file_info *mfi = file->private_data;

struct Mtd_info *MTD = mfi->mtd;

Put_mtd_device (MTD);

File->private_data = NULL;

Kfree (MFI);

return 0;

}/* Mtd_close */

The capabilities provided by the MTD core layer can be divided into two parts: an API for upper-level users and an API for the underlying user. The upper user mainly refers to the MTD character device layer, the character device layer can obtain MTD's struct mtd_info information through Get_mtd_device, then Operation MTD through Mtd->read, Mtd->write and so on. The method can be used to refer to the implementation of functions in the character device layer. The lower level user is the Flash driver layer, the Flash driver layer obtains the partition information in the kernel boot parameter through the parse_mtd_partitions, then joins each partition as a separate MTD device through the add_mtd_partitions to the MTD core layer In order to analyze the kernel startup parameters, you need to register the parser function, through Register_mtd_parser implementation, the registration of this function can be done by the Flash driver layer, can also be compiled into kernel (make menuconfig-> Device DRIVERS->MTD Support->command Line Partition table parsing).

As for the registered parser how to get kernel startup parameters, the approximate process is, bootloader through Bootargs mtdparts incoming partition information, this partition information will be treated as kernel boot parameters; kernel through __setup (" Mtdparts= ", mtdpart_setup) Register function Mtdpart_setup,mtdpart_setup Save the partition information, parser parse the saved information, reference (CMDLINEPART.C).

The implementation principles of the MTD core layer are:

Each MTD is described with a struct mtd_info, and all mtd_info are stored in the struct mtd_info *mtd_table[], Get/add/del_mtd_device are operation Mtd_table.

A MTD flash can be logically divided into a plurality of zones, each of which is a MTD, and the partition information is described by a struct mtd_partition, which needs to know the master MTD information when it joins MTD through partition information, most of the information from Master MTD , Master MTD does not join the MTD core layer, and only the partitions inside will be added to the core layer as a separate MTD.

The MTD core layer also features notifier, which is stored in the struct mtd_notifier, and all notifier are stored in a linked list.

Specifically, you can refer to Linux MTD Source code analysis.

The Flash driver layer provides a concrete implementation of read/write in Mtd_info, and if there is a logical partition, the Mtd_info is Master MTD. Then call Parser_mtd_partitions and add_mtd_partitions to add the logical partition to the MTD core layer.

Linux MTD (Memory technology Device) subsystem analysis-for Atheros char device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.