Enforce the customized style for example code

Instead of using tab for indention, the style defaults to 4 spaces for
the sake of compact layout.
This commit is contained in:
Jim Huang 2021-07-22 06:58:13 +08:00
parent 936d82fad7
commit 50b8dfe6c2
33 changed files with 421 additions and 422 deletions

15
examples/.clang-format Normal file
View File

@ -0,0 +1,15 @@
BasedOnStyle: Chromium
Language: Cpp
MaxEmptyLinesToKeep: 3
IndentCaseLabels: false
AllowShortIfStatementsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
DerivePointerAlignment: false
PointerAlignment: Right
SpaceAfterCStyleCast: true
TabWidth: 4
UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Linux
AccessModifierOffset: -4

View File

@ -9,26 +9,22 @@
* Press one button to turn on a LED and another to turn it off
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int button_irqs[] = { -1, -1 };
static int button_irqs[] = {-1, -1};
/* Define GPIOs for LEDs.
Change the numbers for the GPIO on your board. */
static struct gpio leds[] = {
{ 4, GPIOF_OUT_INIT_LOW, "LED 1" }
};
static struct gpio leds[] = {{4, GPIOF_OUT_INIT_LOW, "LED 1"}};
/* Define GPIOs for BUTTONS
Change the numbers for the GPIO on your board. */
static struct gpio buttons[] = {
{ 17, GPIOF_IN, "LED 1 ON BUTTON" },
{ 18, GPIOF_IN, "LED 1 OFF BUTTON" }
};
static struct gpio buttons[] = {{17, GPIOF_IN, "LED 1 ON BUTTON"},
{18, GPIOF_IN, "LED 1 OFF BUTTON"}};
/* Tasklet containing some non-trivial amount of processing */
static void bottomhalf_tasklet_fn(unsigned long data)
@ -48,9 +44,9 @@ static irqreturn_t button_isr(int irq, void *data)
{
/* Do something quickly right now */
if (irq == button_irqs[0] && !gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 1);
else if(irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 0);
gpio_set_value(leds[0].gpio, 1);
else if (irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 0);
/* Do the rest at leisure via the scheduler */
tasklet_schedule(&buttontask);
@ -80,8 +76,7 @@ int init_module()
goto fail1;
}
pr_info("Current button1 value: %d\n",
gpio_get_value(buttons[0].gpio));
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
ret = gpio_to_irq(buttons[0].gpio);
@ -92,8 +87,7 @@ int init_module()
button_irqs[0] = ret;
pr_info("Successfully requested BUTTON1 IRQ # %d\n",
button_irqs[0]);
pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
ret = request_irq(button_irqs[0], button_isr,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
@ -114,8 +108,7 @@ int init_module()
button_irqs[1] = ret;
pr_info("Successfully requested BUTTON2 IRQ # %d\n",
button_irqs[1]);
pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
ret = request_irq(button_irqs[1], button_isr,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,

View File

@ -3,18 +3,18 @@
* you've read from the dev file
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/cdev.h>
/*
* Prototypes - this would normally go in a .h file
@ -27,27 +27,25 @@ static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct class *cls;
static struct file_operations chardev_fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
static struct file_operations chardev_fops = {.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release};
/*
* This function is called when the module is loaded
@ -113,7 +111,7 @@ static int device_open(struct inode *inode, struct file *file)
*/
static int device_release(struct inode *inode, struct file *file)
{
Device_Open--; /* We're now ready for our next caller */
Device_Open--; /* We're now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
@ -128,10 +126,10 @@ static int device_release(struct inode *inode, struct file *file)
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t *offset)
{
/*
* Number of bytes actually written to the buffer
@ -149,7 +147,6 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
* Actually put the data into the buffer
*/
while (length && *msg_Ptr) {
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
@ -174,7 +171,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
static ssize_t device_write(struct file *filp,
const char *buff,
size_t len,
loff_t * off)
loff_t *off)
{
pr_alert("Sorry, this operation isn't supported.\n");
return -EINVAL;

View File

@ -2,18 +2,18 @@
* chardev2.c - Create an input/output character device
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/poll.h>
#include <linux/cdev.h>
#include "chardev.h"
#define SUCCESS 0
@ -38,7 +38,7 @@ static char Message[BUF_LEN];
*/
static char *Message_Ptr;
static int Major; /* Major number assigned to our device driver */
static int Major; /* Major number assigned to our device driver */
static struct class *cls;
/*
@ -47,7 +47,7 @@ static struct class *cls;
static int device_open(struct inode *inode, struct file *file)
{
#ifdef DEBUG
pr_info("device_open(%p)\n", file);
pr_info("device_open(%p)\n", file);
#endif
/*
@ -85,10 +85,10 @@ static int device_release(struct inode *inode, struct file *file)
* device file attempts to read from it.
*/
static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
char __user * buffer, /* buffer to be
* filled with data */
char __user *buffer, /* buffer to be
* filled with data */
size_t length, /* length of the buffer */
loff_t * offset)
loff_t *offset)
{
/*
* Number of bytes actually written to the buffer
@ -110,18 +110,17 @@ static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
* Actually put the data into the buffer
*/
while (length && *Message_Ptr) {
/*
* Because the buffer is in the user data segment,
* not the kernel data segment, assignment wouldn't
* work. Instead, we have to use put_user which
* copies data from the kernel data segment to the
* user data segment.
*/
put_user(*(Message_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Because the buffer is in the user data segment,
* not the kernel data segment, assignment wouldn't
* work. Instead, we have to use put_user which
* copies data from the kernel data segment to the
* user data segment.
*/
put_user(*(Message_Ptr++), buffer++);
length--;
bytes_read++;
}
#ifdef DEBUG
pr_info("Read %d bytes, %d left\n", bytes_read, length);
@ -138,9 +137,10 @@ static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
* This function is called when somebody tries to
* write into our device file.
*/
static ssize_t
device_write(struct file *file,
const char __user * buffer, size_t length, loff_t * offset)
static ssize_t device_write(struct file *file,
const char __user *buffer,
size_t length,
loff_t *offset)
{
int i;
@ -169,8 +169,8 @@ device_write(struct file *file,
* calling process), the ioctl call returns the output of this function.
*
*/
long device_ioctl(struct file *file, /* ditto */
unsigned int ioctl_num, /* number and param for ioctl */
long device_ioctl(struct file *file, /* ditto */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param)
{
int i;
@ -187,30 +187,30 @@ long device_ioctl(struct file *file, /* ditto */
* to be the device's message. Get the parameter given to
* ioctl by the process.
*/
temp = (char *)ioctl_param;
temp = (char *) ioctl_param;
/*
* Find the length of the message
*/
get_user(ch, temp);
for (i = 0; ch && i < BUF_LEN; i++, temp++)
get_user(ch, temp);
/*
* Find the length of the message
*/
get_user(ch, temp);
for (i = 0; ch && i < BUF_LEN; i++, temp++)
get_user(ch, temp);
device_write(file, (char *)ioctl_param, i, 0);
break;
device_write(file, (char *) ioctl_param, i, 0);
break;
case IOCTL_GET_MSG:
/*
* Give the current message to the calling process -
* the parameter we got is a pointer, fill it.
*/
i = device_read(file, (char *)ioctl_param, 99, 0);
i = device_read(file, (char *) ioctl_param, 99, 0);
/*
* Put a zero at the end of the buffer, so it will be
* properly terminated
*/
put_user('\0', (char *)ioctl_param + i);
put_user('\0', (char *) ioctl_param + i);
break;
case IOCTL_GET_NTH_BYTE:
@ -235,11 +235,11 @@ long device_ioctl(struct file *file, /* ditto */
* init_module. NULL is for unimplemented functions.
*/
struct file_operations Fops = {
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
};
/*

View File

@ -1,15 +1,15 @@
#include <linux/completion.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/module.h>
static struct {
struct completion crank_comp;
struct completion flywheel_comp;
} machine;
static int machine_crank_thread(void* arg)
static int machine_crank_thread(void *arg)
{
pr_info("Turn the crank\n");
@ -17,7 +17,7 @@ static int machine_crank_thread(void* arg)
complete_and_exit(&machine.crank_comp, 0);
}
static int machine_flywheel_spinup_thread(void* arg)
static int machine_flywheel_spinup_thread(void *arg)
{
wait_for_completion(&machine.crank_comp);
@ -29,23 +29,20 @@ static int machine_flywheel_spinup_thread(void* arg)
static int completions_init(void)
{
struct task_struct* crank_thread;
struct task_struct* flywheel_thread;
struct task_struct *crank_thread;
struct task_struct *flywheel_thread;
pr_info("completions example\n");
init_completion(&machine.crank_comp);
init_completion(&machine.flywheel_comp);
crank_thread =
kthread_create(machine_crank_thread,
NULL, "KThread Crank");
crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank");
if (IS_ERR(crank_thread))
goto ERROR_THREAD_1;
flywheel_thread =
kthread_create(machine_flywheel_spinup_thread,
NULL, "KThread Flywheel");
flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
"KThread Flywheel");
if (IS_ERR(flywheel_thread))
goto ERROR_THREAD_2;

View File

@ -1,23 +1,23 @@
#include <linux/module.h>
#include <crypto/internal/hash.h>
#include <linux/module.h>
#define SHA256_LENGTH 32
static void show_hash_result(char * plaintext, char * hash_sha256)
static void show_hash_result(char *plaintext, char *hash_sha256)
{
int i;
char str[SHA256_LENGTH*2 + 1];
char str[SHA256_LENGTH * 2 + 1];
pr_info("sha256 test for string: \"%s\"\n", plaintext);
for (i = 0; i < SHA256_LENGTH ; i++)
sprintf(&str[i*2],"%02x", (unsigned char)hash_sha256[i]);
str[i*2] = 0;
for (i = 0; i < SHA256_LENGTH; i++)
sprintf(&str[i * 2], "%02x", (unsigned char) hash_sha256[i]);
str[i * 2] = 0;
pr_info("%s\n", str);
}
int cryptosha256_init(void)
{
char * plaintext = "This is a test";
char *plaintext = "This is a test";
char hash_sha256[SHA256_LENGTH];
struct crypto_shash *sha256;
struct shash_desc *shash;
@ -26,9 +26,8 @@ int cryptosha256_init(void)
if (IS_ERR(sha256))
return -1;
shash =
kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(sha256),
GFP_KERNEL);
shash = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(sha256),
GFP_KERNEL);
if (!shash)
return -ENOMEM;
@ -51,9 +50,7 @@ int cryptosha256_init(void)
return 0;
}
void cryptosha256_exit(void)
{
}
void cryptosha256_exit(void) {}
module_init(cryptosha256_init);
module_exit(cryptosha256_exit);

View File

@ -1,9 +1,9 @@
#include <crypto/internal/skcipher.h>
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/module.h>
#define SYMMETRIC_KEY_LENGTH 32
#define CIPHER_BLOCK_SIZE 16
#define CIPHER_BLOCK_SIZE 16
struct tcrypt_result {
struct completion completion;
@ -12,17 +12,17 @@ struct tcrypt_result {
struct skcipher_def {
struct scatterlist sg;
struct crypto_skcipher * tfm;
struct skcipher_request * req;
struct crypto_skcipher *tfm;
struct skcipher_request *req;
struct tcrypt_result result;
char * scratchpad;
char * ciphertext;
char * ivdata;
char *scratchpad;
char *ciphertext;
char *ivdata;
};
static struct skcipher_def sk;
static void test_skcipher_finish(struct skcipher_def * sk)
static void test_skcipher_finish(struct skcipher_def *sk)
{
if (sk->tfm)
crypto_free_skcipher(sk->tfm);
@ -36,24 +36,23 @@ static void test_skcipher_finish(struct skcipher_def * sk)
kfree(sk->ciphertext);
}
static int test_skcipher_result(struct skcipher_def * sk, int rc)
static int test_skcipher_result(struct skcipher_def *sk, int rc)
{
switch (rc) {
case 0:
break;
case -EINPROGRESS || -EBUSY:
rc = wait_for_completion_interruptible(
&sk->result.completion);
rc = wait_for_completion_interruptible(&sk->result.completion);
if (!rc && !sk->result.err) {
reinit_completion(&sk->result.completion);
break;
}
pr_info("skcipher encrypt returned with %d result %d\n",
rc, sk->result.err);
pr_info("skcipher encrypt returned with %d result %d\n", rc,
sk->result.err);
break;
default:
pr_info("skcipher encrypt returned with %d result %d\n",
rc, sk->result.err);
pr_info("skcipher encrypt returned with %d result %d\n", rc,
sk->result.err);
break;
}
@ -90,8 +89,9 @@ static void test_skcipher_callback(struct crypto_async_request *req, int error)
*/
}
static int test_skcipher_encrypt(char * plaintext, char * password,
struct skcipher_def * sk)
static int test_skcipher_encrypt(char *plaintext,
char *password,
struct skcipher_def *sk)
{
int ret = -EFAULT;
unsigned char key[SYMMETRIC_KEY_LENGTH];
@ -114,14 +114,13 @@ static int test_skcipher_encrypt(char * plaintext, char * password,
}
skcipher_request_set_callback(sk->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
test_skcipher_callback,
&sk->result);
test_skcipher_callback, &sk->result);
/* clear the key */
memset((void*)key,'\0',SYMMETRIC_KEY_LENGTH);
memset((void *) key, '\0', SYMMETRIC_KEY_LENGTH);
/* Use the world's favourite password */
sprintf((char*)key,"%s",password);
sprintf((char *) key, "%s", password);
/* AES 256 with given symmetric key */
if (crypto_skcipher_setkey(sk->tfm, key, SYMMETRIC_KEY_LENGTH)) {
@ -150,11 +149,11 @@ static int test_skcipher_encrypt(char * plaintext, char * password,
goto out;
}
}
sprintf((char*)sk->scratchpad,"%s",plaintext);
sprintf((char *) sk->scratchpad, "%s", plaintext);
sg_init_one(&sk->sg, sk->scratchpad, CIPHER_BLOCK_SIZE);
skcipher_request_set_crypt(sk->req, &sk->sg, &sk->sg,
CIPHER_BLOCK_SIZE, sk->ivdata);
skcipher_request_set_crypt(sk->req, &sk->sg, &sk->sg, CIPHER_BLOCK_SIZE,
sk->ivdata);
init_completion(&sk->result.completion);
/* encrypt data */
@ -172,7 +171,7 @@ out:
int cryptoapi_init(void)
{
/* The world's favourite password */
char * password = "password123";
char *password = "password123";
sk.tfm = NULL;
sk.req = NULL;

View File

@ -4,12 +4,13 @@
struct devicemodel_data {
char *greeting;
int number;
int number;
};
static int devicemodel_probe(struct platform_device *dev)
{
struct devicemodel_data *pd = (struct devicemodel_data *)(dev->dev.platform_data);
struct devicemodel_data *pd =
(struct devicemodel_data *) (dev->dev.platform_data);
pr_info("devicemodel probe\n");
pr_info("devicemodel greeting: %s; %d\n", pd->greeting, pd->number);
@ -46,24 +47,23 @@ static int devicemodel_resume(struct device *dev)
return 0;
}
static const struct dev_pm_ops devicemodel_pm_ops =
{
static const struct dev_pm_ops devicemodel_pm_ops = {
.suspend = devicemodel_suspend,
.resume = devicemodel_resume,
.poweroff = devicemodel_suspend,
.freeze = devicemodel_suspend,
.thaw = devicemodel_resume,
.restore = devicemodel_resume
};
.restore = devicemodel_resume};
static struct platform_driver devicemodel_driver = {
.driver = {
.name = "devicemodel_example",
.owner = THIS_MODULE,
.pm = &devicemodel_pm_ops,
},
.probe = devicemodel_probe,
.remove = devicemodel_remove,
.driver =
{
.name = "devicemodel_example",
.owner = THIS_MODULE,
.pm = &devicemodel_pm_ops,
},
.probe = devicemodel_probe,
.remove = devicemodel_remove,
};
static int devicemodel_init(void)

View File

@ -1,17 +1,13 @@
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')
static void atomic_add_subtract(void)
{
@ -28,29 +24,29 @@ static void atomic_add_subtract(void)
/* add one */
atomic_inc(&debbie);
pr_info("chris: %d, debbie: %d\n",
atomic_read(&chris), atomic_read(&debbie));
pr_info("chris: %d, debbie: %d\n", atomic_read(&chris),
atomic_read(&debbie));
}
static void atomic_bitwise(void)
{
unsigned long word = 0;
pr_info("Bits 0: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 0: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
set_bit(3, &word);
set_bit(5, &word);
pr_info("Bits 1: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 1: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
clear_bit(5, &word);
pr_info("Bits 2: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 2: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
change_bit(3, &word);
pr_info("Bits 3: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 3: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
if (test_and_set_bit(3, &word))
pr_info("wrong\n");
pr_info("Bits 4: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 4: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
word = 255;
pr_info("Bits 5: "BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
pr_info("Bits 5: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
}
static int example_atomic_init(void)

View File

@ -1,6 +1,6 @@
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mutex.h>
DEFINE_MUTEX(mymutex);
@ -20,8 +20,7 @@ static int example_mutex_init(void)
mutex_unlock(&mymutex);
pr_info("mutex is unlocked\n");
}
else
} else
pr_info("Failed to lock\n");
return 0;

View File

@ -1,6 +1,6 @@
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
DEFINE_RWLOCK(myrwlock);

View File

@ -1,8 +1,8 @@
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
DEFINE_SPINLOCK(sl_static);
spinlock_t sl_dynamic;

View File

@ -1,7 +1,7 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
static void tasklet_fn(unsigned long data)
{

View File

@ -1,8 +1,8 @@
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/module.h> /* Needed by all modules */
int init_module(void)
{

View File

@ -2,9 +2,9 @@
* hello-2.c - Demonstrating the module_init() and module_exit() macros.
* This is preferred over using init_module() and cleanup_module().
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/module.h> /* Needed by all modules */
static int __init hello_2_init(void)
{

View File

@ -1,9 +1,9 @@
/*
* hello-3.c - Illustrating the __init, __initdata and __exit macros.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/module.h> /* Needed by all modules */
static int hello3_data __initdata = 3;

View File

@ -1,9 +1,9 @@
/*
* hello-4.c - Demonstrates module documentation.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/module.h> /* Needed by all modules */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bob Mottram");

View File

@ -1,10 +1,10 @@
/*
* hello-5.c - Demonstrates command line argument passing to a module.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
MODULE_LICENSE("GPL");
@ -14,7 +14,7 @@ static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { -1, -1 };
static int myintArray[2] = {-1, -1};
static int arr_argc = 0;
/*
@ -54,7 +54,7 @@ static int __init hello_5_init(void)
pr_info("mylong is a long integer: %ld\n", mylong);
pr_info("mystring is a string: %s\n", mystring);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
for (i = 0; i < (sizeof myintArray / sizeof(int)); i++)
pr_info("myintArray[%d] = %d\n", i, myintArray[i]);
pr_info("got %d arguments for myintArray.\n", arr_argc);

View File

@ -2,12 +2,12 @@
* hello-sysfs.c sysfs example
*/
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/sysfs.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bob Mottram");
@ -26,7 +26,8 @@ static ssize_t myvariable_show(struct kobject *kobj,
static ssize_t myvariable_store(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf, size_t count)
char *buf,
size_t count)
{
sscanf(buf, "%du", &myvariable);
return count;
@ -34,30 +35,29 @@ static ssize_t myvariable_store(struct kobject *kobj,
static struct kobj_attribute myvariable_attribute =
__ATTR(myvariable, 0660, myvariable_show,
(void*)myvariable_store);
__ATTR(myvariable, 0660, myvariable_show, (void *) myvariable_store);
static int __init mymodule_init (void)
static int __init mymodule_init(void)
{
int error = 0;
pr_info("mymodule: initialised\n");
mymodule =
kobject_create_and_add("mymodule", kernel_kobj);
mymodule = kobject_create_and_add("mymodule", kernel_kobj);
if (!mymodule)
return -ENOMEM;
error = sysfs_create_file(mymodule, &myvariable_attribute.attr);
if (error) {
pr_info("failed to create the myvariable file " \
"in /sys/kernel/mymodule\n");
pr_info(
"failed to create the myvariable file "
"in /sys/kernel/mymodule\n");
}
return error;
}
static void __exit mymodule_exit (void)
static void __exit mymodule_exit(void)
{
pr_info("mymodule: Exit success\n");
kobject_put(mymodule);

View File

@ -9,25 +9,21 @@
* Press one button to turn on a LED and another to turn it off
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int button_irqs[] = { -1, -1 };
static int button_irqs[] = {-1, -1};
/* Define GPIOs for LEDs.
Change the numbers for the GPIO on your board. */
static struct gpio leds[] = {
{ 4, GPIOF_OUT_INIT_LOW, "LED 1" }
};
static struct gpio leds[] = {{4, GPIOF_OUT_INIT_LOW, "LED 1"}};
/* Define GPIOs for BUTTONS
Change the numbers for the GPIO on your board. */
static struct gpio buttons[] = {
{ 17, GPIOF_IN, "LED 1 ON BUTTON" },
{ 18, GPIOF_IN, "LED 1 OFF BUTTON" }
};
static struct gpio buttons[] = {{17, GPIOF_IN, "LED 1 ON BUTTON"},
{18, GPIOF_IN, "LED 1 OFF BUTTON"}};
/*
* interrupt function triggered when a button is pressed
@ -36,10 +32,10 @@ static irqreturn_t button_isr(int irq, void *data)
{
/* first button */
if (irq == button_irqs[0] && !gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 1);
gpio_set_value(leds[0].gpio, 1);
/* second button */
else if(irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 0);
else if (irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
gpio_set_value(leds[0].gpio, 0);
return IRQ_HANDLED;
}
@ -66,8 +62,7 @@ int init_module()
goto fail1;
}
pr_info("Current button1 value: %d\n",
gpio_get_value(buttons[0].gpio));
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
ret = gpio_to_irq(buttons[0].gpio);
@ -78,8 +73,7 @@ int init_module()
button_irqs[0] = ret;
pr_info("Successfully requested BUTTON1 IRQ # %d\n",
button_irqs[0]);
pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
ret = request_irq(button_irqs[0], button_isr,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
@ -100,8 +94,7 @@ int init_module()
button_irqs[1] = ret;
pr_info("Successfully requested BUTTON2 IRQ # %d\n",
button_irqs[1]);
pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
ret = request_irq(button_irqs[1], button_isr,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,

View File

@ -1,8 +1,8 @@
#include <linux/ioctl.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/ioctl.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
@ -14,10 +14,10 @@ struct ioctl_arg {
/* Documentation/ioctl/ioctl-number.txt */
#define IOC_MAGIC '\x66'
#define IOCTL_VALSET _IOW(IOC_MAGIC, 0, struct ioctl_arg)
#define IOCTL_VALGET _IOR(IOC_MAGIC, 1, struct ioctl_arg)
#define IOCTL_VALGET_NUM _IOR(IOC_MAGIC, 2, int)
#define IOCTL_VALSET_NUM _IOW(IOC_MAGIC, 3, int)
#define IOCTL_VALSET _IOW(IOC_MAGIC, 0, struct ioctl_arg)
#define IOCTL_VALGET _IOR(IOC_MAGIC, 1, struct ioctl_arg)
#define IOCTL_VALGET_NUM _IOR(IOC_MAGIC, 2, int)
#define IOCTL_VALSET_NUM _IOW(IOC_MAGIC, 3, int)
#define IOCTL_VAL_MAXNR 3
#define DRIVER_NAME "ioctltest"
@ -32,8 +32,11 @@ struct test_ioctl_data {
rwlock_t lock;
};
static long test_ioctl_ioctl(struct file* filp, unsigned int cmd, unsigned long arg) {
struct test_ioctl_data* ioctl_data = filp->private_data;
static long test_ioctl_ioctl(struct file *filp,
unsigned int cmd,
unsigned long arg)
{
struct test_ioctl_data *ioctl_data = filp->private_data;
int retval = 0;
unsigned char val;
struct ioctl_arg data;
@ -52,7 +55,7 @@ static long test_ioctl_ioctl(struct file* filp, unsigned int cmd, unsigned long
goto done;
}
*/
if (copy_from_user(&data, (int __user*)arg, sizeof(data))) {
if (copy_from_user(&data, (int __user *) arg, sizeof(data))) {
retval = -EFAULT;
goto done;
}
@ -75,7 +78,7 @@ static long test_ioctl_ioctl(struct file* filp, unsigned int cmd, unsigned long
read_unlock(&ioctl_data->lock);
data.val = val;
if (copy_to_user((int __user*)arg, &data, sizeof(data))) {
if (copy_to_user((int __user *) arg, &data, sizeof(data))) {
retval = -EFAULT;
goto done;
}
@ -83,7 +86,7 @@ static long test_ioctl_ioctl(struct file* filp, unsigned int cmd, unsigned long
break;
case IOCTL_VALGET_NUM:
retval = __put_user(ioctl_num, (int __user*)arg);
retval = __put_user(ioctl_num, (int __user *) arg);
break;
case IOCTL_VALSET_NUM:
@ -102,8 +105,12 @@ done:
return retval;
}
ssize_t test_ioctl_read(struct file* filp, char __user* buf, size_t count, loff_t* f_pos) {
struct test_ioctl_data* ioctl_data = filp->private_data;
ssize_t test_ioctl_read(struct file *filp,
char __user *buf,
size_t count,
loff_t *f_pos)
{
struct test_ioctl_data *ioctl_data = filp->private_data;
unsigned char val;
int retval;
int i = 0;
@ -111,7 +118,7 @@ ssize_t test_ioctl_read(struct file* filp, char __user* buf, size_t count, loff_
val = ioctl_data->val;
read_unlock(&ioctl_data->lock);
for (; i < count ; i++) {
for (; i < count; i++) {
if (copy_to_user(&buf[i], &val, 1)) {
retval = -EFAULT;
goto out;
@ -123,7 +130,8 @@ out:
return retval;
}
static int test_ioctl_close(struct inode* inode, struct file* filp) {
static int test_ioctl_close(struct inode *inode, struct file *filp)
{
pr_alert("%s call.\n", __func__);
if (filp->private_data) {
@ -134,8 +142,9 @@ static int test_ioctl_close(struct inode* inode, struct file* filp) {
return 0;
}
static int test_ioctl_open(struct inode* inode, struct file* filp) {
struct test_ioctl_data* ioctl_data;
static int test_ioctl_open(struct inode *inode, struct file *filp)
{
struct test_ioctl_data *ioctl_data;
pr_alert("%s call.\n", __func__);
ioctl_data = kmalloc(sizeof(struct test_ioctl_data), GFP_KERNEL);
@ -157,7 +166,8 @@ struct file_operations fops = {
.unlocked_ioctl = test_ioctl_ioctl,
};
static int ioctl_init(void) {
static int ioctl_init(void)
{
dev_t dev = MKDEV(test_ioctl_major, 0);
int alloc_ret = 0;
int cdev_ret = 0;
@ -175,7 +185,8 @@ static int ioctl_init(void) {
goto error;
}
pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME, test_ioctl_major);
pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME,
test_ioctl_major);
return 0;
error:
@ -190,7 +201,8 @@ error:
return -1;
}
static void ioctl_exit(void) {
static void ioctl_exit(void)
{
dev_t dev = MKDEV(test_ioctl_major, 0);
cdev_del(&test_ioctl_cdev);
unregister_chrdev_region(dev, num_of_dev);

View File

@ -2,13 +2,13 @@
* kbleds.c - Blink keyboard leds until the module is unloaded.
*/
#include <linux/module.h>
#include <linux/console_struct.h> /* For vc_cons */
#include <linux/init.h>
#include <linux/vt_kern.h> /* for fg_console */
#include <linux/tty.h> /* For fg_console, MAX_NR_CONSOLES */
#include <linux/kd.h> /* For KDSETLED */
#include <linux/kd.h> /* For KDSETLED */
#include <linux/module.h>
#include <linux/tty.h> /* For fg_console, MAX_NR_CONSOLES */
#include <linux/vt.h>
#include <linux/console_struct.h> /* For vc_cons */
#include <linux/vt_kern.h> /* for fg_console */
MODULE_DESCRIPTION("Example module illustrating the use of Keyboard LEDs.");
MODULE_AUTHOR("Daniele Paolo Scarpazza");
@ -18,9 +18,9 @@ struct timer_list my_timer;
struct tty_driver *my_driver;
char kbledstatus = 0;
#define BLINK_DELAY HZ/5
#define ALL_LEDS_ON 0x07
#define RESTORE_LEDS 0xFF
#define BLINK_DELAY HZ / 5
#define ALL_LEDS_ON 0x07
#define RESTORE_LEDS 0xFF
/*
* Function my_timer_func blinks the keyboard LEDs periodically by invoking
@ -39,15 +39,15 @@ char kbledstatus = 0;
static void my_timer_func(unsigned long ptr)
{
unsigned long *pstatus = (unsigned long *)ptr;
struct tty_struct* t = vc_cons[fg_console].d->port.tty;
unsigned long *pstatus = (unsigned long *) ptr;
struct tty_struct *t = vc_cons[fg_console].d->port.tty;
if (*pstatus == ALL_LEDS_ON)
*pstatus = RESTORE_LEDS;
else
*pstatus = ALL_LEDS_ON;
(my_driver->ops->ioctl) (t, KDSETLED, *pstatus);
(my_driver->ops->ioctl)(t, KDSETLED, *pstatus);
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
@ -62,9 +62,8 @@ static int __init kbleds_init(void)
for (i = 0; i < MAX_NR_CONSOLES; i++) {
if (!vc_cons[i].d)
break;
pr_info("poet_atkm: console[%i/%i] #%i, tty %lx\n", i,
MAX_NR_CONSOLES, vc_cons[i].d->vc_num,
(unsigned long)vc_cons[i].d->port.tty);
pr_info("poet_atkm: console[%i/%i] #%i, tty %lx\n", i, MAX_NR_CONSOLES,
vc_cons[i].d->vc_num, (unsigned long) vc_cons[i].d->port.tty);
}
pr_info("kbleds: finished scanning consoles\n");
@ -74,7 +73,8 @@ static int __init kbleds_init(void)
/*
* Set up the LED blink timer the first time
*/
timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus);
timer_setup(&my_timer, (void *) &my_timer_func,
(unsigned long) &kbledstatus);
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
@ -85,8 +85,8 @@ static void __exit kbleds_cleanup(void)
{
pr_info("kbleds: unloading...\n");
del_timer(&my_timer);
(my_driver->ops->ioctl) (vc_cons[fg_console].d->port.tty,
KDSETLED, RESTORE_LEDS);
(my_driver->ops->ioctl)(vc_cons[fg_console].d->port.tty, KDSETLED,
RESTORE_LEDS);
}
module_init(kbleds_init);

View File

@ -2,20 +2,20 @@
* wait for input */
/* Copyright (C) 1998 by Ori Pomerantz */
#include <stdio.h> /* standard I/O */
#include <fcntl.h> /* for open */
#include <unistd.h> /* for read */
#include <stdlib.h> /* for exit */
#include <errno.h> /* for errno */
#include <errno.h> /* for errno */
#include <fcntl.h> /* for open */
#include <stdio.h> /* standard I/O */
#include <stdlib.h> /* for exit */
#include <unistd.h> /* for read */
#define MAX_BYTES 1024*4
#define MAX_BYTES 1024 * 4
int main(int argc, char *argv[])
{
int fd; /* The file descriptor for the file to read */
size_t bytes; /* The number of bytes read */
char buffer[MAX_BYTES]; /* The buffer for the bytes */
int fd; /* The file descriptor for the file to read */
size_t bytes; /* The number of bytes read */
char buffer[MAX_BYTES]; /* The buffer for the bytes */
/* Usage */
@ -55,7 +55,7 @@ int main(int argc, char *argv[])
/* Print the characters */
if (bytes > 0) {
for(i=0; i<bytes; i++)
for (i = 0; i < bytes; i++)
putchar(buffer[i]);
}

View File

@ -3,12 +3,12 @@
* through X11, telnet, etc. We do this by printing the string to the tty
* associated with the current task.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h> /* For current */
#include <linux/tty.h> /* For the tty declarations */
#include <linux/version.h> /* For LINUX_VERSION_CODE */
#include <linux/sched.h> /* For current */
#include <linux/tty.h> /* For the tty declarations */
#include <linux/version.h> /* For LINUX_VERSION_CODE */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
@ -21,7 +21,7 @@ static void print_string(char *str)
/*
* tty struct went into signal struct in 2.6.6
*/
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,5) )
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 5))
/*
* The tty for the current task
*/
@ -39,7 +39,6 @@ static void print_string(char *str)
* (ie, if it's a daemon). If so, there's nothing we can do.
*/
if (my_tty != NULL) {
/*
* my_tty->driver is a struct which holds the tty's functions,
* one of which (write) is used to write strings to the tty.
@ -62,13 +61,13 @@ static void print_string(char *str)
* is described in section 2 of
* linux/Documentation/SubmittingPatches
*/
(ttyops->write) (my_tty, /* The tty itself */
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )
0, /* Don't take the string
from user space */
(ttyops->write)(my_tty, /* The tty itself */
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 9))
0, /* Don't take the string
from user space */
#endif
str, /* String */
strlen(str)); /* Length */
str, /* String */
strlen(str)); /* Length */
/*
* ttys were originally hardware devices, which (usually)
@ -85,10 +84,10 @@ static void print_string(char *str)
* and therefore a newline requirs both a LF and a CR.
*/
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )
(ttyops->write) (my_tty, 0, "\015\012", 2);
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 9))
(ttyops->write)(my_tty, 0, "\015\012", 2);
#else
(ttyops->write) (my_tty, "\015\012", 2);
(ttyops->write)(my_tty, "\015\012", 2);
#endif
}
}

View File

@ -2,8 +2,8 @@
procfs1.c
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
@ -12,29 +12,30 @@
struct proc_dir_entry *Our_Proc_File;
ssize_t procfile_read(struct file *filePointer,char *buffer,
size_t buffer_length, loff_t * offset)
ssize_t procfile_read(struct file *filePointer,
char *buffer,
size_t buffer_length,
loff_t *offset)
{
int ret=0;
if(strlen(buffer) ==0) {
pr_info("procfile read %s\n",filePointer->f_path.dentry->d_name.name);
ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n"));
ret=sizeof("HelloWorld!\n");
int ret = 0;
if (strlen(buffer) == 0) {
pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
ret = copy_to_user(buffer, "HelloWorld!\n", sizeof("HelloWorld!\n"));
ret = sizeof("HelloWorld!\n");
}
return ret;
}
static const struct proc_ops proc_file_fops = {
.proc_read = procfile_read,
.proc_read = procfile_read,
};
int init_module()
{
Our_Proc_File = proc_create(procfs_name,0644,NULL,&proc_file_fops);
if(NULL==Our_Proc_File) {
Our_Proc_File = proc_create(procfs_name, 0644, NULL, &proc_file_fops);
if (NULL == Our_Proc_File) {
proc_remove(Our_Proc_File);
pr_alert("Error:Could not initialize /proc/%s\n",procfs_name);
pr_alert("Error:Could not initialize /proc/%s\n", procfs_name);
return -ENOMEM;
}

View File

@ -3,13 +3,13 @@
*
*/
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#include <linux/uaccess.h> /* for copy_from_user */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#include <linux/uaccess.h> /* for copy_from_user */
#define PROCFS_MAX_SIZE 1024
#define PROCFS_NAME "buffer1k"
#define PROCFS_MAX_SIZE 1024
#define PROCFS_NAME "buffer1k"
/**
* This structure hold information about the /proc file
@ -33,14 +33,16 @@ static unsigned long procfs_buffer_size = 0;
* This function is called then the /proc file is read
*
*/
ssize_t procfile_read(struct file *filePointer,char *buffer,
size_t buffer_length, loff_t * offset)
ssize_t procfile_read(struct file *filePointer,
char *buffer,
size_t buffer_length,
loff_t *offset)
{
int ret=0;
if(strlen(buffer) ==0) {
pr_info("procfile read %s\n",filePointer->f_path.dentry->d_name.name);
ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n"));
ret=sizeof("HelloWorld!\n");
int ret = 0;
if (strlen(buffer) == 0) {
pr_info("procfile read %s\n", filePointer->f_path.dentry->d_name.name);
ret = copy_to_user(buffer, "HelloWorld!\n", sizeof("HelloWorld!\n"));
ret = sizeof("HelloWorld!\n");
}
return ret;
}
@ -50,8 +52,10 @@ ssize_t procfile_read(struct file *filePointer,char *buffer,
* This function is called with the /proc file is written
*
*/
static ssize_t procfile_write(struct file *file, const char *buff,
size_t len, loff_t *off)
static ssize_t procfile_write(struct file *file,
const char *buff,
size_t len,
loff_t *off)
{
procfs_buffer_size = len;
if (procfs_buffer_size > PROCFS_MAX_SIZE)
@ -65,8 +69,8 @@ static ssize_t procfile_write(struct file *file, const char *buff,
}
static const struct proc_ops proc_file_fops = {
.proc_read = procfile_read,
.proc_write = procfile_write,
.proc_read = procfile_read,
.proc_write = procfile_write,
};
/**
@ -75,10 +79,10 @@ static const struct proc_ops proc_file_fops = {
*/
int init_module()
{
Our_Proc_File = proc_create(PROCFS_NAME,0644,NULL,&proc_file_fops);
if(NULL==Our_Proc_File) {
Our_Proc_File = proc_create(PROCFS_NAME, 0644, NULL, &proc_file_fops);
if (NULL == Our_Proc_File) {
proc_remove(Our_Proc_File);
pr_alert("Error:Could not initialize /proc/%s\n",PROCFS_NAME);
pr_alert("Error:Could not initialize /proc/%s\n", PROCFS_NAME);
return -ENOMEM;
}

View File

@ -8,37 +8,40 @@
#include <linux/sched.h>
#include <linux/uaccess.h>
#define PROCFS_MAX_SIZE 2048
#define PROCFS_ENTRY_FILENAME "buffer2k"
#define PROCFS_MAX_SIZE 2048
#define PROCFS_ENTRY_FILENAME "buffer2k"
struct proc_dir_entry *Our_Proc_File;
static char procfs_buffer[PROCFS_MAX_SIZE];
static unsigned long procfs_buffer_size = 0;
static ssize_t procfs_read(struct file *filp, char *buffer,
size_t length, loff_t *offset)
static ssize_t procfs_read(struct file *filp,
char *buffer,
size_t length,
loff_t *offset)
{
static int finished = 0;
if(finished)
{
if (finished) {
pr_debug("procfs_read: END\n");
finished = 0;
return 0;
}
finished = 1;
if(copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size))
return -EFAULT;
pr_debug("procfs_read: read %lu bytes\n", procfs_buffer_size);
return procfs_buffer_size;
}
static ssize_t procfs_write(struct file *file, const char *buffer,
size_t len, loff_t *off)
static ssize_t procfs_write(struct file *file,
const char *buffer,
size_t len,
loff_t *off)
{
if(len>PROCFS_MAX_SIZE)
if (len > PROCFS_MAX_SIZE)
procfs_buffer_size = PROCFS_MAX_SIZE;
else
procfs_buffer_size = len;
if(copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
if (copy_from_user(procfs_buffer, buffer, procfs_buffer_size))
return -EFAULT;
pr_debug("procfs_write: write %lu bytes\n", procfs_buffer_size);
return procfs_buffer_size;
@ -55,23 +58,24 @@ int procfs_close(struct inode *inode, struct file *file)
}
static struct proc_ops File_Ops_4_Our_Proc_File = {
.proc_read = procfs_read,
.proc_write = procfs_write,
.proc_open = procfs_open,
.proc_release = procfs_close,
.proc_read = procfs_read,
.proc_write = procfs_write,
.proc_open = procfs_open,
.proc_release = procfs_close,
};
int init_module()
{
Our_Proc_File = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL,&File_Ops_4_Our_Proc_File);
if(Our_Proc_File == NULL)
{
Our_Proc_File = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL,
&File_Ops_4_Our_Proc_File);
if (Our_Proc_File == NULL) {
remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL);
pr_debug("Error: Could not initialize /proc/%s\n", PROCFS_ENTRY_FILENAME);
pr_debug("Error: Could not initialize /proc/%s\n",
PROCFS_ENTRY_FILENAME);
return -ENOMEM;
}
proc_set_size(Our_Proc_File, 80);
proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
pr_debug("/proc/%s created\n", PROCFS_ENTRY_FILENAME);
return 0;

View File

@ -4,12 +4,12 @@
*
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/seq_file.h> /* for seq_file */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/seq_file.h> /* for seq_file */
#define PROC_NAME "iter"
#define PROC_NAME "iter"
MODULE_AUTHOR("Philippe Reynes");
MODULE_LICENSE("GPL");
@ -26,11 +26,10 @@ static void *my_seq_start(struct seq_file *s, loff_t *pos)
static unsigned long counter = 0;
/* beginning a new sequence ? */
if ( *pos == 0 ) {
if (*pos == 0) {
/* yes => return a non null value to begin the sequence */
return &counter;
}
else {
} else {
/* no => it's the end of the sequence, return end to stop reading */
*pos = 0;
return NULL;
@ -44,7 +43,7 @@ static void *my_seq_start(struct seq_file *s, loff_t *pos)
*/
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
unsigned long *tmp_v = (unsigned long *)v;
unsigned long *tmp_v = (unsigned long *) v;
(*tmp_v)++;
(*pos)++;
return NULL;
@ -75,12 +74,10 @@ static int my_seq_show(struct seq_file *s, void *v)
* This structure gather "function" to manage the sequence
*
*/
static struct seq_operations my_seq_ops = {
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show
};
static struct seq_operations my_seq_ops = {.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show};
/**
* This function is called when the /proc file is open.
@ -95,12 +92,10 @@ static int my_open(struct inode *inode, struct file *file)
* This structure gather "function" that manage the /proc file
*
*/
static struct proc_ops my_file_ops = {
.proc_open = my_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = seq_release
};
static struct proc_ops my_file_ops = {.proc_open = my_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = seq_release};
/**
@ -112,8 +107,7 @@ int init_module(void)
struct proc_dir_entry *entry;
entry = proc_create(PROC_NAME, 0, NULL, &my_file_ops);
if(entry == NULL)
{
if (entry == NULL) {
remove_proc_entry(PROC_NAME, NULL);
pr_debug("Error: Could not initialize /proc/%s\n", PROC_NAME);
return -ENOMEM;

View File

@ -1,13 +1,13 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/workqueue.h>
static struct workqueue_struct *queue=NULL;
static struct workqueue_struct *queue = NULL;
static struct work_struct work;
static void work_handler(struct work_struct *data)
{
pr_info ("work handler function.\n");
pr_info("work handler function.\n");
}
int init_module()

View File

@ -3,12 +3,12 @@
* the same time, put all but one to sleep
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/sched.h> /* For putting processes to sleep and
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
#include <linux/sched.h> /* For putting processes to sleep and
waking them up */
#include <linux/uaccess.h> /* for get_user and put_user */
#include <linux/uaccess.h> /* for get_user and put_user */
/*
* The module's file functions
@ -30,10 +30,10 @@ static struct proc_dir_entry *Our_Proc_File;
* function
*/
static ssize_t module_output(struct file *file, /* see include/linux/fs.h */
char *buf, /* The buffer to put data to
(in the user segment) */
char *buf, /* The buffer to put data to
(in the user segment) */
size_t len, /* The length of the buffer */
loff_t * offset)
loff_t *offset)
{
static int finished = 0;
int i;
@ -57,17 +57,17 @@ static ssize_t module_output(struct file *file, /* see include/linux/fs.h */
put_user(message[i], buf + i);
finished = 1;
return i; /* Return the number of bytes "read" */
return i; /* Return the number of bytes "read" */
}
/*
* This function receives input from the user when the user writes to the /proc
* file.
*/
static ssize_t module_input(struct file *file, /* The file itself */
const char *buf, /* The buffer with input */
size_t length, /* The buffer's length */
loff_t * offset) /* offset to file - ignore */
static ssize_t module_input(struct file *file, /* The file itself */
const char *buf, /* The buffer with input */
size_t length, /* The buffer's length */
loff_t *offset) /* offset to file - ignore */
{
int i;
@ -144,20 +144,19 @@ static int module_open(struct inode *inode, struct file *file)
/*
* Emmanuel Papirakis:
*
* This is a little update to work with 2.2.*. Signals now are contained in
* two words (64 bits) and are stored in a structure that contains an array of
* two unsigned longs. We now have to make 2 checks in our if.
* This is a little update to work with 2.2.*. Signals now are
* contained in two words (64 bits) and are stored in a structure that
* contains an array of two unsigned longs. We now have to make 2
* checks in our if.
*
* Ori Pomerantz:
*
* Nobody promised me they'll never use more than 64 bits, or that this book
* won't be used for a version of Linux with a word size of 16 bits. This code
* would work in any case.
* Nobody promised me they'll never use more than 64 bits, or that this
* book won't be used for a version of Linux with a word size of 16
* bits. This code would work in any case.
*/
for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
is_sig =
current->pending.signal.sig[i] & ~current->
blocked.sig[i];
is_sig = current->pending.signal.sig[i] & ~current->blocked.sig[i];
if (is_sig) {
/*
@ -183,7 +182,7 @@ static int module_open(struct inode *inode, struct file *file)
* Open the file
*/
Already_Open = 1;
return 0; /* Allow the access */
return 0; /* Allow the access */
}
/*
@ -207,7 +206,7 @@ int module_close(struct inode *inode, struct file *file)
module_put(THIS_MODULE);
return 0; /* success */
return 0; /* success */
}
/*
@ -221,10 +220,10 @@ int module_close(struct inode *inode, struct file *file)
* means we don't want to deal with something.
*/
static struct proc_ops File_Ops_4_Our_Proc_File = {
.proc_read = module_output, /* "read" from the file */
.proc_write = module_input, /* "write" to the file */
.proc_open = module_open, /* called when the /proc file is opened */
.proc_release = module_close, /* called when it's closed */
.proc_read = module_output, /* "read" from the file */
.proc_write = module_input, /* "write" to the file */
.proc_open = module_open, /* called when the /proc file is opened */
.proc_release = module_close, /* called when it's closed */
};
/*
@ -237,15 +236,15 @@ static struct proc_ops File_Ops_4_Our_Proc_File = {
int init_module()
{
Our_Proc_File = proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File);
if(Our_Proc_File == NULL)
{
Our_Proc_File =
proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File);
if (Our_Proc_File == NULL) {
remove_proc_entry(PROC_ENTRY_FILENAME, NULL);
pr_debug("Error: Could not initialize /proc/%s\n", PROC_ENTRY_FILENAME);
return -ENOMEM;
}
proc_set_size(Our_Proc_File, 80);
proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
proc_set_user(Our_Proc_File, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID);
pr_info("/proc/test created\n");

View File

@ -2,8 +2,8 @@
* start.c - Illustration of multi filed modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
int init_module(void)
{

View File

@ -2,8 +2,8 @@
* stop.c - Illustration of multi filed modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
void cleanup_module()
{

View File

@ -10,13 +10,13 @@
* https://bbs.archlinux.org/viewtopic.php?id=139406
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/delay.h>
#include <asm/paravirt.h>
#include <linux/moduleparam.h> /* which will have params */
#include <linux/unistd.h> /* The list of system calls */
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h> /* which will have params */
#include <linux/syscalls.h>
#include <linux/unistd.h> /* The list of system calls */
/*
* For the current (process) structure, we need
@ -48,7 +48,7 @@ module_param(uid, int, 0644);
* Another reason for this is that we can't get sys_open.
* It's a static variable, so it is not exported.
*/
asmlinkage int (*original_call) (const char *, int, int);
asmlinkage int (*original_call)(const char *, int, int);
/*
* The function we'll replace sys_open (the function
@ -93,7 +93,7 @@ static unsigned long **aquire_sys_call_table(void)
unsigned long **sct;
while (offset < ULLONG_MAX) {
sct = (unsigned long **)offset;
sct = (unsigned long **) offset;
if (sct[__NR_close] == (unsigned long *) ksys_close)
return sct;
@ -106,7 +106,7 @@ static unsigned long **aquire_sys_call_table(void)
static int __init syscall_start(void)
{
if(!(sys_call_table = aquire_sys_call_table()))
if (!(sys_call_table = aquire_sys_call_table()))
return -1;
original_cr0 = read_cr0();
@ -114,10 +114,10 @@ static int __init syscall_start(void)
write_cr0(original_cr0 & ~0x00010000);
/* keep track of the original open function */
original_call = (void*)sys_call_table[__NR_open];
original_call = (void *) sys_call_table[__NR_open];
/* use our open function instead */
sys_call_table[__NR_open] = (unsigned long *)our_sys_open;
sys_call_table[__NR_open] = (unsigned long *) our_sys_open;
write_cr0(original_cr0);
@ -128,14 +128,14 @@ static int __init syscall_start(void)
static void __exit syscall_end(void)
{
if(!sys_call_table) {
if (!sys_call_table) {
return;
}
/*
* Return the system call back to normal
*/
if (sys_call_table[__NR_open] != (unsigned long *)our_sys_open) {
if (sys_call_table[__NR_open] != (unsigned long *) our_sys_open) {
pr_alert("Somebody else also played with the ");
pr_alert("open system call\n");
pr_alert("The system may be left in ");
@ -143,7 +143,7 @@ static void __exit syscall_end(void)
}
write_cr0(original_cr0 & ~0x00010000);
sys_call_table[__NR_open] = (unsigned long *)original_call;
sys_call_table[__NR_open] = (unsigned long *) original_call;
write_cr0(original_cr0);
msleep(2000);