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
#include
#include
#include
#include
#include
#include
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;
}