Linux character driver skeleton

Source: Internet
Author: User

Driver file:

  1. /*
  2. * Simplechrdriver. c
  3. *
  4. * Copyright (c) 2008 breathomn <breathomn@sohu.com>
  5. *
  6. */
  7. # Include <Linux/module. h>
  8. # Include <Linux/kernel. h>
  9. # Include <Linux/fs. h>
  10. # Include <Linux/init. h>
  11. # Include <ASM/uaccess. h>/* Copy _ * _ User */
  12. StaticUnsigned int major_num = 0;
  13. StaticInt global_var = 0;
  14. StaticSsize_t simplechr_read (struct file * filp, char * Buf, ssize_t size, loff_t * Off)
  15. {
  16. If (copy_to_user (buf, & global_var, sizeof (int )))
  17. Return-EFAULT;
  18. Return sizeof (int );
  19. }
  20. StaticSsize_t simplechr_write (struct file * filp, const char * buf, ssize_t size, loff_t * off)
  21. {
  22. If (copy_from_user (& global_var, buf, sizeof (int )))
  23. Return-EFAULT;
  24. Return sizeof (int );
  25. }
  26. StaticStruct file_operations simplechrdev_ops =
  27. {
  28. . Owner = THIS_MODULE,
  29. . Read = simplechr_read,
  30. . Write = simplechr_write,
  31. };
  32. StaticInt _ init simplechr_init (void)
  33. {
  34. Int ret;
  35. If (ret = register_chrdev (major_num, "simplechrdev", & simplechrdev_ops) <0)
  36. Printk (KERN_ALERT "simplechrdev register failed! /N ");
  37. Else
  38. {
  39. Major_num = ret;
  40. Printk (KERN_ALERT "simplechrdev register success! /N ");
  41. }
  42. Return ret;
  43. }
  44. StaticVoid _ exit simplechr_exit (void)
  45. {
  46. Int ret;
  47. If (ret = unregister_chrdev (major_num, "simplechrdev ")))
  48. Printk (kern_alert "simplechrdev unregister failed! /N ");
  49. Else
  50. Printk (kern_alert "simplechrdev unregister success! /N ");
  51. }
  52. Module_init (simplechr_init );
  53. Module_exit (simplechr_exit );

User File:

  1. /*
  2. * Simplechrsrc. c
  3. *
  4. * Copyright (c) 2008 breathomn <breathomn@sohu.com>
  5. *
  6. */
  7. # Include <sys/types. h>
  8. # Include <sys/stat. h>
  9. # Include <stdio. h>
  10. # Include <fcntl. h>
  11. IntMain ()
  12. {
  13. Int fd, num;
  14. If (fd = open ("/dev/simplechrdev", O_RDWR, S_IRUSR | S_IWUSR ))! =-1)
  15. {
  16. Read (fd, & num, sizeof (int ));
  17. Printf ("Read: % d/n", num );
  18. Printf ("Write a num :");
  19. Scanf ("% d", & num );
  20. Write (FD, & num, sizeof (INT ));
  21. Read (FD, & num, sizeof (INT ));
  22. Printf ("RAED again: % d/N", num );
  23. Close (FD );
  24. }
  25. Else
  26. Printf ("open device failed! /N ");
  27. }

MAKEFILE file:

 

OBJ-M: = simplechrdriver. o

Kerneldir? =/Lib/modules/$ (shell uname-R)/build

PWD: = $ (shell PWD)

ALL:
$ (Make)-C $ (kerneldir) M = $ (PWD)

Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

 

Compile our driver into kernel:
(1) create a new directory named simplechrdev in "$ (kernelsource)/Drivers/char" directory.
(2) copy the driver file "simplechrdriver. c" to the new directory.
(3) create a kconfig file in "simplechrdev" directory with contents below:
Config simplechrdev
Tristate "simplechrdev"
(4) modify the kconfig file of its parent directory "char", add contents:
Source "Drivers/Char/simplechrdev/kconfig"
Then we can find a item named "simplechrdev" in "Device Driver-> character devices" when we use "make menuconfig/xconfig ".
(5) create a MAKEFILE file in "simplechrdev" directory with contents below:
OBJ-$ (config_simplechrdev) + = simplechrdriver. o
(6) modify the MAKEFILE file of its parent directory "char", add contents:
OBJ-$ (config_simplechrdev) + = simplechrdev/
All things done! Remake kernel and reboot computer, the driver simplechrdriver works probably. input the command "cat/proc/devices", our device simplechrdev is in the character devices list.
(7) To load our module automatically
We can choose to compile our driver as a module, but sometimes the kernel can not load our module automatically. we can find a solution that add some contents to "/etc/rc. d/RC. sysinit "file to the problem.
# Simplechrdev
If [-D/lib/modules/$ unamer/kernel/Drivers/Char/simplechrdev]; then
For module in/lib/modules/$ unamer/kernel/Drivers/Char/simplechrdev/*; do
Module =$ {module ##*/}
Module =$ {module %. Ko}
Modprobe $ module>/dev/null 2> & 1
Done
Fi

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.