Linux并口设备驱动程序设置

来源:互联网 发布:跟单软件免费 编辑:程序博客网 时间:2024/05/26 19:14

原文地址::http://blog.csdn.net/bg2bkk/article/details/8946424


参考地址:http://oss.org.cn/kernel-book/ldd3/ch09s03.html

并口的管脚

13脚接led的正输入,25脚接led的负极


驱动代码:

parport_drv.c

[cpp] view plain copy
  1. #include<linux init="" h="">  
  2. #include<linux module="" h="">  
  3. #include<linux kernel="" h="">  
  4. #include<linux fs="" h="">  
  5. #include<linux cdev="" h="">  
  6. #include<linux types="" h="">  
  7. #include<linux uaccess="" h="">  
  8.   
  9. #include"parport_drv.h"  
  10.   
  11. #define Drv_major 240  
  12. #define Drv_name  "parport_drv"  
  13. #define Drv_read_addr 0x379  
  14. #define Drv_write_addr 0x378  
  15.   
  16. int parport_open(struct inode *inode, struct file *filp)  
  17. {  
  18.     printk(KERN_ALERT "open the parport_dev\n");  
  19.     return 0;  
  20. }  
  21.   
  22. ssize_t parport_write(struct file *filp, const char *buf, size_t count, loff_t *f_ops)  
  23. {  
  24.     unsigned char status;  
  25.     int loop;  
  26.     for(loop = 0; loop < count; loop++)  
  27.     {  
  28.         get_user(status, (char *)buf);  
  29.         outb(status, Drv_write_addr);  
  30.     }  
  31.     return count;  
  32. }  
  33.   
  34. ssize_t parport_read(struct file *filp, char *buf, size_t count, loff_t *f_ops)  
  35. {  
  36.     unsigned char status;  
  37.     int loop;  
  38.     for(loop = 0; loop < count; loop++)  
  39.     {  
  40.         status = inb(Drv_read_addr);  
  41.         put_user(status, (char *) &buf[loop]);  
  42.     }  
  43.     return count;  
  44. }  
  45.   
  46. long    parport_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)  
  47. {  
  48.     int loop;  
  49.     struct dat data;  
  50.     switch(cmd)  
  51.     {  
  52.         case PARPORT_WRITE:  
  53. //          outb(status, Drv_write_addr);  
  54.             copy_from_user(&data, (struct dat *)arg, sizeof(data));           
  55.             printk(KERN_ALERT "out put %d\n",data.loop);  
  56.             for(loop = 0; loop < data.loop; loop ++)  
  57.             {  
  58.                 printk(KERN_ALERT "the %dth loop, write %d\n",loop,data.buf[loop]);  
  59.                 outb(data.buf[loop], Drv_write_addr);  
  60.                 wmb();  
  61.             }  
  62.             break;  
  63.   
  64.         case PARPORT_CLOSE:  
  65.             outb(0x00, Drv_write_addr);  
  66.             wmb();  
  67.             break;  
  68.     }  
  69.     return 0;  
  70. }  
  71.   
  72. int parport_release(struct inode *inode, struct file *filp)  
  73. {  
  74.     printk(KERN_ALERT "close the module parport_dev\n");  
  75.     return 0;  
  76. }  
  77.   
  78. struct file_operations parport_fops = {  
  79.     .owner  =   THIS_MODULE,  
  80.     .write  =   parport_write,  
  81.     .read   =   parport_read,  
  82.     .open   =   parport_open,  
  83.     .unlocked_ioctl =   parport_ioctl,  
  84.     .release=   parport_release,  
  85. };  
  86.   
  87. int parport_init(void)  
  88. {  
  89.     int result;  
  90.   
  91.     result = register_chrdev(Drv_major, Drv_name, &parport_fops);  
  92.     if(result < 0)  
  93.         return result;  
  94.     printk(KERN_ALERT "hello the module parport_dev\n");  
  95.   
  96.     return 0;  
  97. }  
  98.   
  99. void parport_exit(void)  
  100. {  
  101.     printk(KERN_ALERT "exit the module parport_drv\n");  
  102.     unregister_chrdev(Drv_major, Drv_name);  
  103.       
  104. }  
  105.   
  106. module_init(parport_init);  
  107. module_exit(parport_exit);  
  108. </linux></linux></linux></linux></linux></linux></linux>  
[cpp] view plain copy
  1. <linux init="" h=""><linux module="" h=""><linux kernel="" h=""><linux fs="" h=""><linux cdev="" h=""><linux types="" h=""><linux uaccess="" h="">  
  2. </linux></linux></linux></linux></linux></linux></linux>  
[cpp] view plain copy
  1. <linux init="" h=""><linux module="" h=""><linux kernel="" h=""><linux fs="" h=""><linux cdev="" h=""><linux types="" h=""><linux uaccess="" h="">parport_drv.h  
  2. </linux></linux></linux></linux></linux></linux></linux>  
[cpp] view plain copy
  1. #ifndef<span style="white-space:pre">   </span>_PARPORT_DRV_H  
  2. #define _PARPORT_DRV_H  
  3.   
  4.   
  5. #define<span style="white-space:pre">   </span>PARPORT_WRITE<span style="white-space:pre">  </span>1  
  6. #define<span style="white-space:pre">   </span>PARPORT_CLOSE<span style="white-space:pre">  </span>2  
  7.   
  8.   
  9. struct dat{  
  10. <span style="white-space:pre">  </span>int loop;  
  11. <span style="white-space:pre">  </span>unsigned char *buf;  
  12. };  
  13.   
  14.   
  15. #endif  

测试代码par_test.c


[cpp] view plain copy
  1. #include <stdio h="">  
  2. #include <stdlib h="">  
  3. #include <sys types="" h="">  
  4. #include <sys stat="" h="">  
  5. #include <fcntl h="">  
  6. #include "parport_drv.h"  
  7. #define DEVICE_NAME "/dev/parport_dev"  
  8.   
  9. int main()  
  10. {  
  11.     int fd;  
  12.     char buf[128];  
  13.     int loop;  
  14.   
  15.     fd = open(DEVICE_NAME, O_RDWR | O_NDELAY);  
  16.   
  17.     if(fd < 0)  
  18.     {  
  19.         perror("open device");  
  20.         exit(1);  
  21.     }  
  22.     else  
  23.     {  
  24. //      printf("waiting for input ...\n");  
  25. //  
  26. //      while(1)  
  27. //      {  
  28. //          if(read(fd, buf, 1) == 1)  
  29. //          {  
  30. //              printf("read data [%2X]\n", buf[0] & 0xFF);  
  31. //              if(!(buf[0] & 0x10))  
  32. //                  break;  
  33. //          }  
  34. //          sleep(1);  
  35. //      }  
  36. //      printf("input ok ... \n");  
  37. //      printf("led begins to flash ... \n");  
  38. //      for(loop = 0; loop < 5; loop++)  
  39. //      {  
  40. //          buf[0] = 0xff;  
  41. //          write(fd, buf, 1);  
  42. //          sleep(1);  
  43. //          buf[0] = 0x00;  
  44. //          write(fd, buf, 1);  
  45. //          sleep(1);  
  46. //      }  
  47.         int i;  
  48.         int arg=0x99;  
  49.         unsigned char buf[255];  
  50.         struct dat da;  
  51.         da.loop = 4;  
  52.         da.buf = (unsigned char *)malloc(5 * sizeof(unsigned char));  
  53.         for(i = 0;i< da.loop; i++)  
  54.             da.buf[i] = i*2+1;  
  55.         for(i=0;i<da.loop;i++)  
  56.             printf("test:%d\n", da.buf[i]);  
  57.         ioctl(fd, PARPORT_WRITE,&da);  
  58.         sleep(1);  
  59.         ioctl(fd, PARPORT_CLOSE);  
  60.         sleep(1);  
  61.         close(fd);  
  62.     }  
  63.   
  64.     return 0;  
  65. }  
  66.     </fcntl></sys></sys></stdlib></stdio>  

1 load_rdwrdev
[cpp] view plain copy
  1. #!/bin/sh  
  2.   
  3. insmod parport_drv.ko  
  4. mknod /dev/parport_dev c 240 0  
2 unload_rdwrdev
[cpp] view plain copy
  1. #!/bin/sh  
  2.   
  3. rmmod parport_drv.ko  
  4. rm /dev/parport_dev  


0 0
原创粉丝点击