linux driver development 1

linux drivers into three categories: character devices, block devices, network devices.

Character devices: the same device can be like a stream of bytes accessed, accessed via a character device files in / dev.

Block device: according to access data in units of blocks, such as a 512KB, is accessed through a file system node under / dev it is. The difference between character devices are different interfaces, in addition to providing the same with the character device interfaces, but also provide specifically for a block device interface, block device must support mount file systems, applications typically to access content on the block device through the file system rather than dealing directly with the block device.

Network device: does not use / dev in the file node to operate, but operated by separate network interface eth0, eth1 like. Kernel interfaces and network equipment in a different character and block devices, a set of specialized functions.


Operation command module:

1 insmod load modules

   #sudo insmod ./scull.ko

2 rmmod Unloads the module

3 lsmod lists the modules currently used by the kernel, or check / proc / modules file

4 depmod Scan / lib / modules / <kernel version> / directory for all kernel modules, thereby generating dependencies / lib / modules / module to the kernel <kernel version> /modules.dep

The probe 5 modprobe depmod modules.dep generated and loaded kernel modules, module name are given only to find a suitable automatic document module, the module needs to be given the insmod path

Basic information 6 modinfo View module file

   #sudo modinfo scull.ko


Method of compiling the kernel module:

First, to get the kernel source, and after successfully compiled kernel has been compiled source tree, i.e. performed successfully make uImage kernel source tree. Then after the write driver code, if the compiled kernel source tree is this driving need to run the kernel, you can write Makefile to compile the module code modules:

ifeq($(KERNELRELEASE),)

  KERNELDIR ?= /work/sysbuild/linux-2.6.22.6

  PWD:=$(shell pwd)

  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

else

  obj-m: = hello.o

endif

Execution can make

Wherein the compiled kernel source directory tree: /work/sysbuild/linux-2.6.22.6, module obj is hello.o, the compiler generates hello.ko


The simplest example of module:

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

stataic int hello_init(void)

{

  printk("hello world\r\n");

  return 0;

}

static void hello_exit(void)

{

  printk("goodbye,cruel world\r\n");

}

module_init(hello_init);

module_exit(hello_exit);


When the load module parameters can be passed, use the macro module_param (variable name, variable type, permissions) declare parameters during module operation parameters of variables will appear as a file in / sys directory, for example #ls / sys / module / hello / parameters / -l

Variable types: short, ushort, int, uint, long, ulong, charp, bool

Instructions:

static char *chp="test_char_p";

static int num=1;

module_param(num, int, S_IRUGO|S_IWUSR);

module_param(chp, charp, S_IRUGO);

#insmod hello.ko num=3 chp="test"


The module is integrated into the kernel

1 Make sure the module is working properly

2 hello.c module file copied to the specified directory of the kernel, e.g. / drivers / char

3 Kconfig modify the specified directory and Makefile

   Kconfig:  config HELLO tristate "New Hello"

   Makefile:  obj -$(CONFIG_HELLO) += hello.o

4 reconfigure the kernel function selected will be compiled into the kernel, instead of the compiled module

5 recompile the kernel to get the new kernel

6 test new kernel, kernel modules to ensure successful integration


View equipment system:

1 View system character device driver, and the block device driver major number

   #cat /proc/devices

2 View startup information system

  #dmesg

3 View device IO physical memory address

  #cat /proc/iomem

4 View interrupt number being used

  #cat /proc/interrupts


Character device drivers experience:

#insmod ./scull.ko

#cat /proc/devices|grep scull

252 scull

#sudo mknod scull0 c 252 0

#sudo chmod 666 scull0

#cat scull0

#sudo echo test>>scull0

#cat scull0

After looking for major number of the device after loading scull.ko, create the device file device (c for a character device, 0 represents the first slave number), change the file attribute manipulation device



Published 30 original articles · won praise 21 · Views 140,000 +

Guess you like

Origin blog.csdn.net/oushaojun2/article/details/62871412