Skip to content

Instantly share code, notes, and snippets.

@creamidea
Last active December 16, 2015 05:08
Show Gist options
  • Save creamidea/5381888 to your computer and use it in GitHub Desktop.
Save creamidea/5381888 to your computer and use it in GitHub Desktop.
The Linux Kernel Module Programming 的一个Hello程序。 The Linux Kernel Module Programming Guide:http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
/* From: http://www.tldp.org/LDP/lkmpg/2.6/html/x279.html */
/*
* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2 or later]
* "GPL v2" [GNU Public License v2]
* "GPL and additional rights" [GNU Public License v2 rights and more]
* "Dual BSD/GPL" [GNU Public License v2
* or BSD license choice]
* "Dual MIT/GPL" [GNU Public License v2
* or MIT license choice]
* "Dual MPL/GPL" [GNU Public License v2
* or Mozilla license choice]
*
* The following other idents are available
*
* "Proprietary" [Non free products]
*
* There are dual licensed components, but when running with Linux it is the
* GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
* is a GPL combined work.
*
* This exists for several reasons
* 1. So modinfo can show license info for users wanting to vet their setup
* is free
* 2. So the community can ignore bug reports including proprietary modules
* 3. So vendors can do likewise based on their own policies
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#define DRIVER_AUTHOR "icecream <creamidea@gmail.com>"
#define DRIVER_DESC "A sample driver"
static int __init hello_init(void)
{
printk("Hello, icecream!\n");
// 调试信息级别
printk("Hello, icecream!\n");
printk(KERN_ERR "kernel error!\n");
printk(KERN_INFO "kernel information!\n");
printk("KERN_ERR in define kernel error!\n"); /* This is invalid */
printk("<3> num-kernel error\n");
/* printk(<3>"out num-kernel error1!\n"); /\* This is error *\/ */
printk(KERN_DEBUG "debug information!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("Exit the driver module!\n");
}
module_init(hello_init); /* enter */
module_exit(hello_exit); /* out */
/*
* You can use strings, like this:
*/
/*
* Get rid of taint message by declaring code as GPL.
*/
MODULE_LICENSE("Dual BSD/GPL");
/*
* Or with defines, like this:
*/
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
/*
* This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might
* be used in the future to help automatic configuration of modules, but is
* currently unused other than for documentation purposes.
*/
MODULE_SUPPORTED_DEVICE("Test Device");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment