Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3602032
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: LINUX

2012-01-08 14:38:27


========================================================
自己没有测试成功,这部分内容以后看
比较好的这方面资料:
lt
linux device driver p246 并口
精通d板
========================================================
并口编程一些重要的函数:
struct pardevice *pdev;
parport_claim_or_block(pdev);请求并口
parport_write_data(pdev->port,buf);
parport_release(pdev);释放并口

parport_register_device();
parport_register_driver();
parport_unregister_driver();

========================================================
附件: parport.rar   将rar修改为tar.bz2
  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/parport.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pci.h>
  10. #include <linux/device.h> //class

  11. #define DEVICE_NAME "led"

  12. static dev_t dev_number; /* Allotted device number */
  13. static struct class *led_class; /* Class to which this device
  14.                                    belongs */
  15. struct cdev led_cdev; /* Associated cdev */
  16. struct pardevice *pdev; /* Parallel port device */

  17. /* LED open */
  18. int led_open(struct inode *inode, struct file *file)
  19. {
  20.     return 0;
  21. }

  22. /* Write to the LED */
  23. ssize_t led_write(struct file *file, const char *buf,size_t count, loff_t *ppos)
  24. {
  25.     char kbuf;
  26.     if (copy_from_user(&kbuf, buf, 1)) return -EFAULT;
  27.     /* Claim the port */
  28.     parport_claim_or_block(pdev);
  29.     /* Write to the device */
  30.     parport_write_data(pdev->port, kbuf);
  31.     /* Release the port */
  32.     parport_release(pdev);
  33.     return count;
  34. }

  35. /* Release the device */
  36. int led_release(struct inode *inode, struct file *file)
  37. {
  38.     return 0;
  39. }

  40. /* File Operations */
  41. static struct file_operations led_fops = {
  42.     .owner = THIS_MODULE,
  43.     .open = led_open,
  44.     .write = led_write,
  45.     .release = led_release,
  46. };

  47. static int
  48. led_preempt(void *handle)
  49. {
  50.     return 1;
  51. }

  52. /* Parport attach method */
  53. static void led_attach(struct parport *port)
  54. {
  55.     /* Register the parallel LED device with parport */
  56.     pdev = parport_register_device(port, DEVICE_NAME,
  57.          led_preempt, NULL,
  58.          NULL, 0, NULL);
  59.     if (pdev == NULL) printk("Bad register\n");
  60. }

  61. /* Parport detach method */
  62. static void led_detach(struct parport *port)
  63. {
  64.     /* Do nothing */
  65. }

  66. /* Parport driver operations */
  67. static struct parport_driver led_driver = {
  68.     .name = "led",
  69.     .attach = led_attach,
  70.     .detach = led_detach,
  71. };

  72. /* Driver Initialization */
  73. int __init led_init(void)
  74. {
  75.     /* Request dynamic allocation of a device major number */
  76.     if (alloc_chrdev_region(&dev_number, 0, 1, DEVICE_NAME)< 0)
  77.     {
  78.         printk(KERN_DEBUG "Can't register device\n");
  79.         return -1;
  80.     }
  81.     printk("alloc_chrdev_region..ok.\n");

  82.     /* Create the led class */
  83.     led_class = class_create(THIS_MODULE, DEVICE_NAME);
  84.     if(IS_ERR(led_class))
  85.         printk("Bad class create\n");
  86.     else
  87.         printk("class_create ok.\n");

  88.     /* Connect the file operations with the cdev */
  89.     cdev_init(&led_cdev, &led_fops);
  90.     led_cdev.owner = THIS_MODULE;
  91.     /* Connect the major/minor number to the cdev */
  92.     if (cdev_add(&led_cdev, dev_number, 1))
  93.     {
  94.         printk("Bad cdev add\n");
  95.         return 1;
  96.     }
  97.     else
  98.         printk("cdev_add ok.\n");

  99.     //class_device_create(led_class, NULL, dev_number,
  100.     //     NULL, DEVICE_NAME);
  101.     device_create(led_class, NULL, dev_number,
  102.          NULL, DEVICE_NAME);

  103.     /* Register this driver with parport */
  104.     if(parport_register_driver(&led_driver))
  105.     {
  106.         printk(KERN_ERR "Bad Parport Register\n");
  107.         return -EIO;
  108.     }
  109.     else
  110.         printk("parport_register_driver ok\n");

  111.     printk("LED Driver Initialized.\n");
  112.     return 0;
  113. }

  114. /* Driver Exit */
  115. void __exit led_cleanup(void)
  116. {
  117.     unregister_chrdev_region(dev_number, 1);
  118.     //parport_unregister_driver();
  119.     device_destroy(led_class,dev_number);
  120.     class_destroy(led_class);
  121.     return;
  122. }

  123. module_init(led_init);
  124. module_exit(led_cleanup);
  125. MODULE_LICENSE("GPL");

阅读(5772) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~