Makefile
TARGET := hello_drv
obj-m += $(TARGET).o
ROOTFS = /home/flinn/smart210-SDK/fs/drv
KERNEL = /home/flinn/smart210-SDK/linux-3.10.79
all:
make -C $(KERNEL) M=`pwd` modules
clean:
make -C $(KERNEL) M=`pwd` clean
install:
sudo cp $(TARGET).ko $(ROOTFS)
make.sh
#!/bin/bash
echo -e "\e[1;31m##make clean \e[0m"
make clean
echo -e "\e[1;31m##make \e[0m"
make
echo -e "\e[1;31m##make install \e[0m"
make install
hello_dev.c
/*
* linux-3.10.27
* arm-linux-gcc-4.5.1
*
* @ hello driver
*/
#include <linux/module.h>
#include <linux/init.h> /* module_init, ... */
#include <linux/kernel.h> /* everything */
#include <linux/cdev.h> /* cdev_init, ... */
#include <linux/fs.h> /* file_operations, */
#include <linux/device.h> /* class_create,... */
#include <linux/platform_device.h>
static void hello_release(struct device *dev)
{
/* 如果没有定义release, 加载会出错 */
}
static struct platform_device hello_dev =
{
.name = "hello",
.dev = {
.release = hello_release, /* must BE */
},
};
static int hello_dev_init(void)
{
pr_info("%s called.\n", __func__);
platform_device_register(&hello_dev);
return 0;
}
static void hello_dev_exit(void)
{
pr_info("%s called.\n", __func__);
platform_device_unregister(&hello_dev);
}
module_init(hello_dev_init);
module_exit(hello_dev_exit);
MODULE_LICENSE("GPL");
hello_drv.c
/*
* linux-3.10.27
* arm-linux-gcc-4.5.1
*
* @ hello driver
*/
#include <linux/module.h>
#include <linux/init.h> /* module_init, ... */
#include <linux/kernel.h> /* everything */
#include <linux/cdev.h> /* cdev_init, ... */
#include <linux/fs.h> /* file_operations, */
#include <linux/device.h> /* class_create,... */
#include <linux/platform_device.h>
#define DEVICE_NAME "hello"
struct priv_data
{
char *name;
int major;
dev_t dev;
struct cdev *cdev;
struct class *pri_class;
};
static int hello_probe(struct platform_device *pdev)
{
pr_info("%s called.\n", __func__);
return 0;
}
static int hello_remove(struct platform_device *pdev)
{
pr_info("%s called.\n", __func__);
return 0;
}
static struct platform_driver hello_drv =
{
.driver = {
.name = "hello",
.owner = THIS_MODULE
},
.probe = hello_probe,
.remove = hello_remove,
};
static int hello_drv_init(void)
{
pr_info("%s called.\n", __func__);
platform_driver_register(&hello_drv);
return 0;
}
static void hello_drv_exit(void)
{
pr_info("%s called.\n", __func__);
platform_driver_unregister(&hello_drv);
}
module_init(hello_drv_init);
module_exit(hello_drv_exit);
MODULE_LICENSE("GPL");