Linux drivers Getting Started 4

Source: Internet
Author: User

Do not endure hardship, difficult people. Efforts to provide a good living environment for the wife and children.

Http://www.cnblogs.com/nan-jing/articles/5806399.html

The above mentioned how to create a proc node. But I often see cases where the/sys node is operating. Can applications manipulate the SYS node? Ask someone else to do it. Hey. Someone else can only tell you a ballpark figure. I can only tell you to operate. Why, how to operate. Others can only
Let you see the code. No other tricks, do you want to make money? Think. Just look at the code.

The usual, put all the code. See how to create a sys node

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
//Zang Chinese New Year Honeywell
//#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/proc_fs.h>
#include <linux/sysfs.h>
#include <linux/kobject.h>

Module_author ("[email protected]");
Module_license ("Dual BSD/GPL");

#define Globalmem_size 0X1000
#define Mem_clear 0x1

#define Cj_proc_entry "Cjzang"
#define Cj_proc_node "Cj_entry"

static int globalmem_major = 0;

struct globalmem_dev{
struct Cdev Cdev;
unsigned char mem[globalmem_size];
};

struct Globalmem_dev *globalmem_devp;

struct class *globalmem_class;

struct Proc_dir_entry *proc_cj_entry;

struct Kobject *cj_sys;

static int Globalmem_open (struct inode *inode, struct file *filp) {
struct Globalmem_dev *dev;
dev = container_of (inode->i_cdev, struct globalmem_dev, Cdev);
Filp->private_data = Dev;
PRINTK (Kern_alert "globalmem_open!");
return 0;
}

static int globalmem_release (struct inode *inode, struct file *filp) {
PRINTK (Kern_alert "globalmem_release!");
return 0;
}

Static ssize_t globalmem_read (struct file *filp, char __user *buf, size_t size, loff_t *ppos) {
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct Globalmem_dev *dev = filp->private_data;

if (P >= globalmem_size) {
return 0;
}
if (Count > Globalmem_size-p) {
Count = globalmem_size-p;
}

if (Copy_to_user (buf, (void *) (Dev->mem + P), count)) {
ret =-Efault;
}else {
*ppos + = count;
ret = count;
PRINTK (kern_alert "read%u bytes from%lu \ n", count, p);
}
return ret;
}

Static ssize_t globalmem_write (struct file *filp, const char __user *buf, size_t size, loff_t *ppos) {
unsigned long p = *ppos;
unsigned int count = size;

int ret = 0;
struct Globalmem_dev *dev = filp->private_data;

if (P >= globalmem_size) {
return 0;
}

if (Count > Globalmem_size-p) {
Zang Chinese New Year Honeywell
Count = globalmem_size-p;
}
if (Copy_from_user (Dev->mem + p, buf, count)) {
ret =-Efault;
}else {
*ppos + = count;
ret = count;
PRINTK (kern_alert "write%u bytes from%lu \ n", count, p);
}

return ret;
}

static loff_t globalmem_llseek (struct file *filp, loff_t offset, int orig) {
loff_t ret = 0;
switch (orig) {
case 0:
if (Offset < 0) {
ret =-efault;
break;

if ((unsigned int) offset > globalmem_size) {
ret =-efault;
break;

filp->f_pos = (unsigned int) offset ;
ret = Filp->f_pos;
break;

Case 1:
if ((Filp->f_pos + offset) > globalmem_size) {
ret =-Efault;
Break
}

if ((Filp->f_pos + offset) < 0) {
ret =-Efault;
Break
}
Filp->f_pos + = offset;
RET = filp->f_pos;
Break

Default
ret =-Efault;
Break
}

return ret;

}

Static long globalmem_ioctl (struct file *flip, unsigned int cmd, unsigned long arg) {
PRINTK (Kern_alert "Globalmem_ioctl");
return 10L;
}

static const struct File_operations Globalmem_fops = {
. Owner = This_module,
. Llseek = Globalmem_llseek,
. Read = globalmem_read,//Zang Spring Festival Honeywell
. write = Globalmem_write,
. Unlocked_ioctl = Globalmem_ioctl,
. open = Globalmem_open,
. Release = Globalmem_release,
};

static int proc_show (struct seq_file *file, void *v) {
PRINTK (kern_alert "proc driver show!!!");
seq_printf (file, "proc Driver:%s\n", "proc_proc_test");
return 0;
}

static int Proc_open (struct inode *inode, struct file *file) {
PRINTK (kern_alert "proc driver open!!!");
Single_open (file, proc_show, NULL);
return 0;
}

Static ssize_t proc_cj_write (struct file *file, const char __user *buffer, size_t count, loff_t *pos) {
PRINTK (kern_alert "proc driver wirte!!!");
return 100;
}

static struct File_operations Proc_file_ops = {
. Owner = This_module,
. open = Proc_open,
. Read = Seq_read,
. write = Proc_cj_write,
. Release = Single_release,
Zang Chinese New Year Honeywell
};

static void Globalmem_setup_cdev (struct globalmem_dev *dev, int index) {
Span style= "color: #0000ff;" >int err, Devno = MKDEV (globalmem_major, index);
cdev_init (& (Dev->cdev), &globalmem_fops);
dev->cdev.owner = This_module;
err = Cdev_add (&dev->cdev, Devno, 1);
if (err) {
printk (Kern_alert "Cdev_add Failed! ");
"
}
sta TIC int Cj_proc_read (char *page, Char **start, off_t off, int count, int *eof, void *data) {
return;

static int cj_proc_write (struct file *file, const char *buffer, unsigned long count, void *data) {
return 100;
}

static int create_cjproc_entry (void) {
Proc_cj_entry = Proc_mkdir (Cj_proc_entry, NULL);
if (proc_cj_entry = = NULL) {
PRINTK (kern_alert "create/proc/%s failed \ n", cj_proc_entry);
return-1;
}

printk (kern_alert "create/proc/%s success \ n", cj_proc_entry);
struct proc_dir_entry *pt_entry = proc_create (Cj_proc_node, 0666, Proc_cj_entry, &proc_file_ops);
if (Pt_entry = = NULL) {
RETURN-1;

//pt_entry->read_proc = Cj_proc_ Read;
//pt_entry->write_proc = Cj_proc_write;

return 0;

Static ssize_t cj_sys_read (struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
PRINTK (Kern_alert "Cj_sys_read");
return 19;
Zang Chinese New Year Honeywell
}

Static ssize_t cj_sys_write (struct kobject *kobj, struct kobj_attribute *attr, const char *BUF, size_t count) {
PRINTK (Kern_alert "Cj_sys_read");
return 10;
}

static struct Kobj_attribute Cj_attribute = __attr (cj_attr, 0444, Cj_sys_read, cj_sys_write);

static int create_cjsys_entry (void) {
PRINTK (kern_alert "SYS CJ created!!!");
Cj_sys = Kobject_create_and_add ("Cjsys", kernel_kobj);
Sysfs_create_file (Cj_sys, &cj_attribute);
Zang Chinese New Year Honeywell
return 0;
}

static void Create_dev_node (int index) {
Device_create (Globalmem_class, NULL, MKDEV (globalmem_major, index), NULL, "globalmem_cj1");
Device_create (Globalmem_class, NULL, MKDEV (globalmem_major, index + 1), NULL, "GLOBALMEM_CJ2");
}

static int __init globalmem_init (void) {
int result;
dev_t devno = MKDEV (globalmem_major, 0);
if (globalmem_major) {
result = Register _chrdev_region (Devno, 2, "Globalmem");
}else {
result = Alloc_chrdev_region ( &devno,0, 2, "Globalmem");
globalmem_major = major (Devno);

printk (Kern_alert" Major-------------->%d \ n ", globalmem_major);
if (Result < 0) {
return result;

Globalmem_class = Class_create (This_module, "globalmem_cls");
create_dev_node (0);

GLOBALMEM_DEVP = Kmalloc (2 * sizeof (struct globalmem_dev), gfp_kernel);
if (! Globalmem_devp) {
result =-enomem;
goto Fail_malloc;
"
memset (GLOBALMEM_DEVP, 0, 2 * sizeof ( struct globalmem_dev));
globalmem_setup_cdev (&globalmem_devp[0], 0);
globalmem_setup_cdev (&globalmem_devp[1], 1);
//Zang Spring Festival Honeywell
create_cjsys_entry ();
create_cjproc_entry ();

return 0;

Fail_malloc:
Unregister_chrdev_region (Devno, 1);
return result;
}


static void __exit globalmem_exit (void) {
Cdev_del (&globalmem_devp[0].cdev);

Cdev_del (&globalmem_devp[1].cdev);
Kfree (GLOBALMEM_DEVP);
Unregister_chrdev_region (MKDEV (globalmem_major, 0), 2);
Class_destroy (Globalmem_class);
Remove_proc_entry (Cj_proc_entry, NULL);
Remove_proc_entry (Cj_proc_node, proc_cj_entry);
}

Zang Chinese New Year Honeywell
Module_param (globalmem_major, int, 0555);
Module_init (Globalmem_init);
Module_exit (Globalmem_exit);

Explain:

The key is this function
static int create_cjsys_entry (void) {
PRINTK (kern_alert "SYS CJ created!!!");
Cj_sys = Kobject_create_and_add ("Cjsys", kernel_kobj);
Sysfs_create_file (Cj_sys, &cj_attribute);
return 0;
}

Sysfs_create_file with this parameter kobject, attribute structure contains the device node read and write method.

I'm having a problem here.
static struct Kobj_attribute Cj_attribute = __attr (cj_attr, 0444, Cj_sys_read, cj_sys_write);

The privilege in this sentence is 0444. Can be changed to 0555. Other words. Zang can only read the Spring Festival. Can't write. To 0666 and compile, but. This is weird. Why this node can only read and not write.

A little introduction, a little learning, the key is to insist. to accumulate constantly.

Write code, play with women. Program Yes 2 great hobbies.

Linux drivers Getting Started 4

Related Article

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.