免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1638 | 回复: 0
打印 上一主题 下一主题

请教关于并行端口驱动的问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-25 16:41 |只看该作者 |倒序浏览

  1. 这是代码:
  2. #include <linux/fs.h>
  3. #include <linux/cdev.h>
  4. #include <linux/parport.h>
  5. #include <asm/uaccess.h>
  6. #include <linux/platform_device.h>
  7. #define DEVICE_NAME    "led1"

  8. static dev_t dev_number;          /* Allotted device number */
  9. static struct class *led_class;   /* Class to which this device
  10.                                      belongs */
  11. struct cdev led_cdev;             /* Associated cdev */
  12. struct pardevice *pdev;           /* Parallel port device */

  13. /* LED open */
  14. int
  15. led_open(struct inode *inode, struct file *file)
  16. {
  17.   return 0;
  18. }

  19. /* Write to the LED */
  20. ssize_t
  21. led_write(struct file *file, const char *buf,
  22.           size_t count, loff_t *ppos)
  23. {
  24.   char kbuf;
  25.   if (copy_from_user(&kbuf, buf, 1)) return -EFAULT;
  26.   /* Claim the port */

  27.    parport_claim_or_block(pdev);
  28.   /* Write to the device */
  29.    parport_write_data(pdev->port, kbuf);
  30.   /* Release the port */
  31.   parport_release(pdev);
  32.   return count;
  33. }
  34. /* Release the device */
  35. int
  36. 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. /*
  48. static int
  49. led_preempt(void *handle)
  50. {
  51.   return 1;
  52. }
  53. */
  54. /* Parport attach method */
  55. static void
  56. led_attach(struct parport *port)
  57. {
  58.   /* Register the parallel LED device with parport */
  59.   pdev = parport_register_device(port, DEVICE_NAME,
  60.                                  NULL, NULL,
  61.                                  NULL, 0, NULL);
  62.   if (pdev == NULL) printk("Bad register\n");
  63. else
  64. printk("well register\n");
  65. }

  66. /* Parport detach method */
  67. static void
  68. led_detach(struct parport *port)
  69. {
  70.   /* Do nothing */
  71. }

  72. /* Parport driver operations */
  73. static struct parport_driver led_driver = {
  74.   .name   = "led1",
  75.   .attach = led_attach,
  76.   .detach = led_detach,
  77. };
  78. /* Driver Initialization */
  79. int __init
  80. led_init(void)
  81. {
  82.   /* Request dynamic allocation of a device major number */
  83.   if (alloc_chrdev_region(&dev_number, 0, 1, DEVICE_NAME)
  84.                           < 0) {
  85.     printk(KERN_DEBUG "Can't register device\n");
  86.     return -1;
  87.   }
  88.   /* Create the led class */
  89.   led_class = class_create(THIS_MODULE, DEVICE_NAME);
  90.   if (IS_ERR(led_class)) printk("Bad class create\n");

  91.   /* Connect the file operations with the cdev */
  92.   cdev_init(&led_cdev, &led_fops);

  93.   led_cdev.owner = THIS_MODULE;

  94.   /* Connect the major/minor number to the cdev */
  95.   if (cdev_add(&led_cdev, dev_number, 1)) {
  96.     printk("Bad cdev add\n");
  97.     return 1;
  98.   }


  99.   class_device_create(led_class, NULL, dev_number,
  100.                                  NULL, DEVICE_NAME);
  101.   /* Register this driver with parport */
  102.   if (parport_register_driver(&led_driver)) {
  103.     printk(KERN_ERR "Bad Parport Register\n");
  104.     return -EIO;
  105.   }

  106.   printk("LED Driver Initialized.\n");
  107.   return 0;
  108. }

  109. /* Driver Exit */
  110. void __exit
  111. led_cleanup(void)
  112. {
  113.   unregister_chrdev_region(MAJOR(dev_number), 1);
  114.   class_device_destroy(led_class, MKDEV(MAJOR(dev_number), 0));
  115.   class_destroy(led_class);
  116.   return;
  117. }

  118. module_init(led_init);
  119. module_exit(led_cleanup);

  120. MODULE_LICENSE("GPL");
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP