I have been reading the code. Today I am going to try it. I will take the tiny6410 LED lamp to open the knife. Although I write it myself, it is actually referring to the routine. However, the basic idea is quite clear.
Define this drive as MISC device (Miscellaneous Device Driver), including the header file miscdevice. h. In fact, the so-called miscellaneous driver is the device driver with the main device Number 10. In fact, the register_chrdev () function is called with the main device number 10 (). The MISC device automatically creates a device node in the/dev directory, instead of using mkmod.
Development Board I/O definition:
GPK Port:
# Include <Linux/miscdevice. h>
# Include <Linux/fs. h>
# Include <Linux/PCI. h>
# Include <Mach/map. h>
# Include <Mach/regs-gpio.h>
# Include <Mach/gpio-bank-k.h>
# Include "LEDs. H"
# Define device_name "tiny6410_leds"
Module_license ("GPL ");
Module_author ("pang123hui ");
Static long sbc2440_leds_ioctl (struct file * filp, unsigned int cmd, unsigned long Arg)
{
Switch (CMD ){
Unsigned TMP;
/*
Case 0:
Case 1:
If (Arg> 4)
{
// Invalid Parameter
Return-einval;
}
TMP = readl (89c64xx_gpkdat );
TMP & = ~ (1 <(3 + Arg ));
TMP | = (CMD) <(3 + Arg ));
Writel (TMP, 89c64xx_gpkdat );
Return 0;
*/
Case 1:
Case 2:
Case 3:
Case 4:
TMP = readl (89c64xx_gpkdat );
TMP = (TMP &~ (1 <(CMD-1 + 4) | (ARG <(CMD-1 + 4 ));
Writel (TMP, 89c64xx_gpkdat );
Return 0;
Default:
Return-einval;
}
}
Static struct file_operations dev_fops = {
. Owner = this_module,
. Unlocked_ioctl = sbc2440_leds_ioctl,
};
// MISC device: a miscellaneous device, that is, a special character device whose main device number is 10.
Static struct miscdevice MISC = {
// Number of sub-devices. do not conflict with the number of sub-devices in/proc/Misc.
// Misc_dynamic_minor
. Minor = misc_dynamic_minor,
// Device name
. Name = device_name,
. Fops = & dev_fops,
};
Static int _ init dev_init (void)
{
Int ret;
{
Unsigned TMP;
TMP = readl (89c64xx_gpkcon );
// Configure gpk4 to gpk7 as 0001 output
TMP = (TMP &~ (0 xffffu <16) | (0x1111u <16 );
Writel (TMP, 89c64xx_gpkcon );
// Gpkdat [7: 4] = 1
// Light off
TMP = readl (89c64xx_gpkdat );
TMP | = (0xf <4 );
Writel (TMP, 89c64xx_gpkdat );
// Prohibit pulling up/down
TMP = readl (89c64xx_gpkpud );
TMP & = (0x00 <8 );
Writel (TMP, 89c64xx_gpkpud );
}
// This function automatically creates a device node, that is, a device file.
Ret = misc_register (& MISC );
Printk (device_name "/tinitialized/N ");
Return ret;
}
Static void _ exit dev_exit (void)
{
Misc_deregister (& MISC );
}
Module_init (dev_init );
Module_exit (dev_exit );
Makefile
ifneq ($(KERNELRELEASE),)
obj-m := leds.o
else
KERNELDIR := /opt/FriendlyARM/mini6410/linux/linux-2.6.38
PWD:=$(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.ko *.o *.mod.c *.mod.o *.symvers
endif
Leds_test
# Include <stdio. h>
// Exit
# Include <stdlib. h>
# Include <sys/IOCTL. h>
Int main (INT argc, char ** argv)
{
Int num = 0;
Int led_state = 1;
Int FD = 0;
// Parameter value assignment
If (argc! = 3 | sscanf (argv [1], "% d", & num )! = 1 | sscanf (argv [2], "% d", & led_state )! = 1 | num> 4 | led_state> 1)
{
Fprintf (stderr, "error/N ");
Exit (1 );
}
Printf ("num = % d, led_state = % d/N", num, led_state );
FD = open ("/dev/tiny6410_leds", 0 );
If (FD <0 ){
FD = open ("/dev/tiny6410_leds0", 0 );
}
If (FD <0 ){
Perror ("open device LEDs ");
Exit (1 );
}
IOCTL (FD, num, led_state );
Printf ("led % d/N", num );
Close (FD );
Return 0;
}