注意事项:char *和char[]的不同
内核版本:Linux 2.6.38
开发板: Mini 6410
驱动程序:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/timer.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/fs.h>
/*
Kernel Version: Linux 2.6.38
Arm Version: Mini 6410
*/
#define MyPrintk printk
struct my_data_structure
{
unsigned int number;
char name[20];
unsigned long count;
};
static dev_t Leds_Major ;
static char * DEVICE_NAME = "DataTransfemission";
static struct class *leds_class;
static struct my_data_structure my_own_data[] = {
{1,"Jack",0},
{2,"Tom",0},
{3,"Kobe",0},
{4,"Xiaohong",0}
};
static ssize_t data_read(struct file *file, char __user * buffer, size_t count, loff_t *pos)
{
/*struct my_data_structure temp_data=
{6,"Jack",0};*/
int ret = copy_to_user(buffer,(void *)&my_own_data[1], sizeof(struct my_data_structure));
if (ret != 0){
MyPrintk("read error\n");
}
return 0;
}
static ssize_t data_write(struct file *flie, const char __user * buffer, size_t count, loff_t *pos)
{
struct my_data_structure user_data;
int ret = copy_from_user((void *)&user_data, buffer, count);
if (ret != 0){
MyPrintk("write error\n");
}
MyPrintk (KERN_EMERG "Kernel from user data: %i, %s, %lu\n", user_data.number,
user_data.name, user_data.count);
return 0;
}
static int data_open(struct file *file, struct node *nodes)
{
return 0;
}
static struct file_operations s3c64XX_leds_fops = {
.owner = THIS_MODULE,
.read = data_read,
.write = data_write,
.open = data_open,
};
static int myleds_init(void)
{
Leds_Major = register_chrdev(Leds_Major,DEVICE_NAME , &s3c64XX_leds_fops);
if(Leds_Major < 0){
MyPrintk (KERN_EMERG "Sorry, Can not register the data trsanmission device!\n");
}
MyPrintk (KERN_EMERG " Register the data trsanmission leds device\n");
leds_class = class_create(THIS_MODULE, DEVICE_NAME);
device_create(leds_class, NULL , MKDEV(Leds_Major, 0), NULL,DEVICE_NAME);
return 0;
}
static void myleds_exit(void)
{
unregister_chrdev(Leds_Major, DEVICE_NAME);
device_destroy(leds_class, MKDEV(Leds_Major, 0));
class_destroy(leds_class);
MyPrintk (KERN_EMERG "Data transmission Linux Byebye\n");
}
module_init(myleds_init);
module_exit(myleds_exit);
MODULE_LICENSE("GPL");
应用程序:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include <stdlib.h>
struct my_data_structure
{
unsigned int number;
char name[20];
unsigned long count;
};
int main(int argc, char **argv)
{
int fd;
int ret;
fd = open("/dev/DataTransfemission", O_RDWR);
if (fd < 0){
printf("Sorry, can't open!\n");
}
struct my_data_structure my_write_data = {6,"weipeng",1};
printf("struct sizeof %d, %d\n", sizeof(my_write_data ), sizeof(struct my_data_structure ));
ret = write(fd, &my_write_data , sizeof(my_write_data));
if(ret < 0){
printf("Sorry, write2 error!\n");
}
struct my_data_structure *my_write_data2 = (struct my_data_structure*)malloc(sizeof(struct my_data_structure));
printf("struct* sizeof %d, %d\n", sizeof(my_write_data2), sizeof(struct my_data_structure ));
my_write_data2->number = 5;
strcpy(my_write_data2->name ,"xiangwei");
my_write_data2->count = 2;
ret = write(fd, my_write_data2 , sizeof(struct my_data_structure));
if(ret < 0){
printf("Sorry, write2 error!\n");
}
struct my_data_structure my_read_data;
ret = read(fd, &my_read_data , sizeof(my_read_data));
if(ret < 0){
printf("Sorry, read1 error!\n");
}
printf("my_read_data: %u, %s, %u\n", my_read_data.number, my_read_data.name, my_read_data.count);
struct my_data_structure* my_read_data3 =(struct my_data_structure*)malloc(sizeof(struct my_data_structure));
ret = read(fd, my_read_data3 , sizeof(struct my_data_structure));
if(ret < 0){
printf("Sorry, read3 error!\n");
}
printf("my_read_data3: %u, %s, %u\n", my_read_data3->number, my_read_data3->name, my_read_data3->count);
free(my_write_data2);
free(my_read_data3);
return 0;
}