Asynchronous notification for character Devices

Source: Internet
Author: User

Driver:
[Cpp]
# Include <linux/kernel. h>
# Include <linux/module. h>
# Include <linux/cdev. h>
# Include <linux/fs. h>
# Include <linux/device. h>
# Include <linux/syscils. h>

# Include <linux/uaccess. h>
# Include <linux/string. h>

Struct fpga_key_dev
{
Struct cdev;
Dev_t devno;
Char test [20];
Struct class * fpga_key_class;
Struct fasync_struct * async_queue;
 
Int message_cdev_open;
};

Static int fpga_key_open (struct inode * node, struct file * fd)
{
Struct fpga_key_dev * dev;

Printk ("fpga_key_open () ++ \ n ");
 
Printk ("node-> I _cdev = % x \ n", (unsigned int) node-> I _cdev );
Dev = container_of (node-> I _cdev, struct fpga_key_dev, cdev );
Printk ("dev-> cdev = % x \ n", (unsigned int) & dev-> cdev );
Printk ("dev = % x \ n", (unsigned int) dev );

If (! Dev-> message_cdev_open ){
Dev-> message_cdev_open = 1;
Fd-> private_data = dev;
}
Else {
Return-EFAULT;
}

Printk ("test [20] = % s \ n", dev-> test );

Printk ("fpga_key_open () -- \ n ");

Return 0;
}

Static ssize_t fpga_key_write (struct file * fd, const char _ user * buf, size_t len, loff_t * ptr)
{
Char temp_buffer [20];
Int print_size = 0;
Struct fpga_key_dev * dev = fd-> private_data;

Printk ("fpga_key_wirte () ++ \ n ");
If (len> 20)
Print_size = 20;
Else
Print_size = len;
Printk ("print_size = % d; len = % d \ n", print_size, len );
If (copy_from_user (temp_buffer, buf, print_size ))
Return-EFAULT;
 
If (dev-> async_queue)
Kill_fasync (& dev-> async_queue, SIGIO, POLL_IN );

Printk ("writing data: % s", temp_buffer );

Printk ("fpga_key_wirte () -- \ n ");

Return print_size;
}
Static ssize_t fpga_key_read (struct file * fd, char _ user * buf, size_t len, loff_t * ptr)
{
Char * temp_buffer = "Hello fpga_key_read! \ N ";
Int print_size = 0;

Printk ("fpga_key_read () ++ \ n ");

If (len> strnlen (temp_buffer, 20 ))
Print_size = strnlen (temp_buffer, 20 );
Else
Print_size = len;
Printk ("print_size = % d; len = % d \ n", print_size, len );
If (copy_to_user (buf, temp_buffer, print_size ))
Return-EFAULT;
Printk ("% s", temp_buffer );
Printk ("fpga_key_read () -- \ n ");

Return print_size;
}
 
Static int fpga_key_fasync (int fd, struct file * filp, int mode)
{
Struct fpga_key_dev * dev = filp-> private_data;
 
Printk ("fpga_key_fasync () ++ \ n ");

Fasync_helper (fd, filp, mode, & dev-> async_queue );
 
Printk ("fpga_key_fasync () -- \ n ");

Return 0;
}
 
Static int fpga_key_release (struct inode * node, struct file * fd)
{
Struct fpga_key_dev * dev = fd-> private_data;

Printk ("fpga_key_release () ++ \ n ");
 
Dev-> message_cdev_open = 0;
Fpga_key_fasync (-1, fd, 0 );

Printk ("fpga_key_release () -- \ n ");
Return 0;
}
 
 
Struct file_operations meassage_operatons =
{
. Owner = THIS_MODULE,
. Open = fpga_key_open,
. Write = fpga_key_write,
. Read = fpga_key_read,
. Fasync = fpga_key_fasync,
. Release = fpga_key_release,
};

Struct fpga_key_dev;
Static int _ init fpga_key_init (void)
{
Struct fpga_key_dev * dev;
Char * temp_char = "hello world \ n ";

Printk ("fpga_key_to_app_init (void) ++ \ n ");

Dev = & fpga_key_dev;
Strcpy (dev-> test, temp_char );
Printk ("% s", dev-> test );

Alloc_chrdev_region (& dev-> devno, 0, 1, "fpga_key_to_app ");
// Dev-> cdev = cdev_alloc ();
Cdev_init (& dev-> cdev, & meassage_operatons );
Cdev_add (& dev-> cdev, dev-> devno, 1 );
Printk ("dev-> cdev = % x \ n", (unsigned int) & dev-> cdev );
Printk ("dev = % x \ n", (unsigned int) dev );

Dev-> fpga_key_class = class_create (THIS_MODULE, "fpga_key_class ");
If (IS_ERR (dev-> fpga_key_class )){
Printk ("Err: failed in creating class./n ");
Return-1;
}
Device_create (dev-> fpga_key_class, NULL, dev-> devno, NULL, "fpga_key ");
Printk ("fpga_key_to_app_init (void) -- \ n ");
Return 0;
}
Static void _ exit fpga_key_exit (void)
{
Struct fpga_key_dev * dev = & fpga_key_dev;

Printk ("fpga_key_to_app_exit (void) ++ \ n ");
Device_destroy (dev-> fpga_key_class, dev-> devno );
Class_destroy (dev-> fpga_key_class );
Cdev_del (& dev-> cdev );
Unregister_chrdev_region (dev-> devno, 1 );

Printk ("fpga_key_to_app_exit (void) -- \ n ");
}
Module_init (fpga_key_init );
Module_exit (fpga_key_exit );

MODULE_LICENSE ("GPL ");
MODULE_AUTHOR ("Driver Monkey ");
MODULE_DESCRIPTION ("Test fpga_key to App ");

App code:
[Cpp]
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <sys/select. h>
# Include <unistd. h>
# Include <signal. h>
# Include <string. h>

Unsigned int flag = 0;

Void sig_handler (int sig)
{
Printf ("% s \ n" ,__ FUNCTION __);
Flag ++;
}
Int main (void)
{
Char r_buf [20];
Char * w_buf = "hello write! \ N ";
Int r_count = 0;
Int fd;
Int f_flags;
Flag ++;
Fd = open ("/dev/fpga_key", O_RDWR );
If (fd <0)
{
Perror ("open ");
Return-1;
}
 
Signal (SIGIO, sig_handler );
Fcntl (fd, F_SETOWN, getpid ());
F_flags = fcntl (fd, F_GETFL );
Fcntl (fd, F_SETFL, FASYNC | f_flags );

While (1)
{
Printf ("waiting \ n ");
Sleep (2 );
If (flag> 3)
Break;
Printf ("flag = % d \ n", flag );

Write (fd, w_buf, strlen (w_buf ));
}
 
Close (fd );

Return 0;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.