KPP. c
---------------------------------------------
# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/fs. h>
# Include <Linux/slab. h>
# Include <Linux/init. h>
# Include <Linux/list. h>
# Include <Linux/ioport. h>
# Include <Linux/IO. h>
# Include <Linux/interrupt. h>
# Include <Linux/input. h>
# Include <Linux/fs. h>
# Include <Linux/cdev. h>
# Include <Linux/poll. h>
# Include <Linux/IRQ. h>
# Include <Linux/delay. h>
# Include <Linux/workqueue. h>
# Include <Linux/jiffies. h>
# Include <ASM/uaccess. h>
# Include <ASM/IRQ. h>
# Include <ASM/arch-Magus/hardware. h>
# Include <ASM/arch-Magus/irqs. h>
# Include <ASM/gpio. h>
# Include <ASM/Param. h>
# Define kppmajor 240
# Define module_name "KPP"
# Define hold_irq magus_gpio_hold
# Define kpp_get _ ior ('F', 2, INT)
// <Global variable
Struct kpp_dev {
Struct cdev;
Struct fasync_struct * async_queue;
};
Struct kpp_dev * kpp_devp;
// Work queue struct
Struct work_struct hold_wq;
Void hold_do_work (void );
Static int gvalue;
Static struct class * hold_class;
Static int number;
Void hold_do_work (void)
{
Int state;
Struct kpp_dev * Dev = kpp_devp;
State = gpio_get_value (hold_irq );
If (State = 1 ){
Gvalue = 10;
Number ++;
If (number = 1 ){
If (Dev-> async_queue)
Kill_fasync (& Dev-> async_queue, sigio, poll_in );
}
Schedule_work (& hold_wq );
} Else {
Gvalue =-10;
Number = 0;
Enable_irq (gpio_to_irq (hold_irq ));
If (Dev-> async_queue)
Kill_fasync (& Dev-> async_queue, sigio, poll_in );
}
Msleep (100 );
}
Static irqreturn_t hold_isr (int irq, void * dev_id)
{
Int state;
Mdelay (5 );
State = gpio_get_value (hold_irq );
If (State = 1 ){
Disable_irq (IRQ );
Schedule_work (& hold_wq );
}
Return irq_handled;
}
Static int kpp_fasync (int fd, struct file * filp, int Mode)
{
Struct kpp_dev * Dev = filp-> private_data;
Return fasync_helper (FD, filp, mode, & Dev-> async_queue );
}
Static int kpp_release (struct inode * inode, struct file * filp)
{
Kpp_fasync (-1, filp, 0 );
Return 0;
}
Static int kpp_open (struct inode * inode, struct file * filp)
{
Filp-> private_data = kpp_devp;
Return 0;
}
Static int kpp_ioctl (struct inode * inode, struct file * filp, unsigned
Int cmd, unsigned long Arg)
{
Int value = gvalue;
Switch (CMD ){
Case kpp_get:
Put_user (value, (INT _ User *) Arg );
Break;
Default:
Return-enotty;
}
Return 0;
}
Static struct file_operations g_kpp_fops = {
. Owner = this_module,
. IOCTL = kpp_ioctl,
. Fasync = kpp_fasync,
. Open = kpp_open,
. Release = kpp_release,
};
Static void kpp_setup_cdev (struct kpp_dev * Dev, int index)
{
Int devno = mkdev (kppmajor, index );
Cdev_init (& Dev-> cdev, & g_kpp_fops );
Dev-> cdev. Owner = this_module;
Dev-> cdev. Ops = & g_kpp_fops;
Cdev_add (& Dev-> cdev, devno, 1 );
}
Static dev_t devno;
Static void register_kpp (void)
{
Devno = mkdev (kppmajor, 0 );
Register_chrdev_region (devno, 1, module_name );
Kpp_devp = kmalloc (sizeof (struct kpp_dev), gfp_kernel );
Memset (kpp_devp, 0, sizeof (struct kpp_dev ));
Kpp_setup_cdev (kpp_devp, 0 );
Hold_class = class_create (this_module, "hold_class ");
If (is_err (hold_class )){
Printk ("<1> Create class error/N ");
Return-1;
}
Class_device_create (hold_class, null, devno, null, "hold ");
}
Static int _ init kpp_init (void)
{
Int hold_irq;
Register_kpp ();
/* Register hold-key IRQ interrupt */
Hold_irq = gpio_to_irq (hold_irq );
Set_irq_type (hold_irq, irq_type_edge_rising );
If (request_irq (hold_irq, hold_isr, ir1__sample_random, "Hold-key", null )){
Printk (kern_err "request hold IRQ error! /N ");
Return-einval;
}
/* Init work queue for hold IRQ */
Init_work (& hold_wq, (void (*) (void *) hold_do_work );
Printk ("Solomon mongoech limited/N ");
Return 0;
}
Static void _ exit kpp_cleanup (void)
{
Int hold_irq = gpio_to_irq (hold_irq );
Free_irq (hold_irq, null );
Cdev_del (& kpp_devp-> cdev );
Unregister_chrdev_region (devno, 1 );
Class_device_destroy (hold_class, devno );
Class_destroy (hold_class );
}
Module_init (kpp_init );
Module_exit (kpp_cleanup );
Module_description ("Magus keypad driver ");
Module_license ("GPL ");
Kpptest. c
---------------------------------------------
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <stdlib. h>
# Include <Linux/IOCTL. h>
# Include <stdio. h>
# Include <signal. h>
# Include <unistd. h>
# Define kpp_get _ ior ('F', 2, INT)
Static int FD;
Void gpiovol_handler (INT sigum)
{
Int value;
IOCTL (FD, kpp_get, & value );
Printf ("the value = % d./N", value );
}
Int main ()
{
Int oflags;
FD = open ("/dev/hold", o_rdwr );
If (FD <0 ){
Return-1;
}
// When the hold key is pressed, the hold driver sends a sigio signal to the process.
Signal (sigio, gpiovol_handler );
// F_setown: Set the ID of the process that receives sigio and sigurg signals or the ID of the Process Group. Positive
// Arg specifies a process ID. Negative Arg indicates a process group ID that is equal to the absolute value of Arg.
Fcntl (FD, f_setown, getpid ());
Oflags = fcntl (FD, f_getfl );
Fcntl (FD, f_setfl, oflags | fasync); // (1)
While (1 );
Return 0;
}
(1) When the fasync flag of a file changes (call the fcntl () function to set the fasync file flag), the device-driven fasync () corresponding to the file () the API is called.
The driver sends a signal to the user program.
---------------------------------------------
When an I/O event occurs on the device, a mechanism is in place to ensure that a signal is sent to the application process. Obviously, the device driver plays an important role, standard implementations of actual terminals such as TTY and network socket have included support for real-time signal drivers. Therefore, they can be directly used in Linux as described above. However, some device drivers are not yet supported, so you need to customize the device drivers. The following two APIs should be a standard interface that shields all trivial operations (similar to send_sig:
IntFasync_helper(Int fd, struct file * filp, int mode, struct fasync_struct ** FA );
VoidKill_fasync(Struct fasync_struct ** FA, int Sig, int band );
To support the asynchronous notification mechanism, the following device structure requires an asynchronous event notification Queue (which should be similar to the sleep Queue) and adds fasync () interface implementation (this function registers the process to async_queue ). When the fasync flag of an open file changes (when the fcntl () function is called and the fasync file flag is set), The fasync () interface is called.
Struct kpp_dev {
Struct cdev;
Struct fasync_struct * async_queue;
};
Static int kpp_fasync (int fd, struct file * filp, int Mode)
{
Struct kpp_dev * Dev = filp-> private_data;
Return fasync_helper (FD, filp, mode, & Dev-> async_queue );
}
The time when the event occurs is to interrupt the service program or call kill_fasync () in the corresponding Soft Interrupt ():
If (Dev-> async_queue)
Kill_fasync (& Dev-> async_queue, sigio, poll_in );
For a write operation, it is poll_out. Note: No matter what signal the user process sets, sigio is generally sent in this step. Note that when the device file is closed (release method), execute fasync () so that the operation of this file is detached from the device asynchronous event waiting for the linked list.
Static int kpp_release (struct inode * inode, struct file * filp)
{
Kpp_fasync (-1, filp, 0 );
Return 0;
}
|