[Linux drivers] [Linux Driver] device driver Model related (i)--Sample code

Source: Internet
Author: User
Tags sprintf

1, the following is the sample code:

[CPP]View Plaincopy
  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>
  6. Module_license ("Dual BSD/GPL");
  7. static char *version = "2.0.1";
  8. static int My_match (struct device *dev, struct device_driver *driver)
  9. {
  10. return!strncmp (Dev->kobj.name, Driver->name, strlen (driver->name));
  11. }
  12. static void my_bus_release (struct device *dev)
  13. {
  14. PRINTK (kern_debug "My bus release\n");
  15. }
  16. struct device My_bus = {
  17. . Kobj.name = "My_bus0",
  18. . Release = My_bus_release
  19. };
  20. struct Bus_type My_bus_type = {
  21. . Name = "My_bus",
  22. . match = My_match,
  23. };
  24. Export_symbol (My_bus);
  25. Export_symbol (My_bus_type);
  26. Static ssize_t show_bus_version (struct bus_type *bus, char *buf)
  27. {
  28. return snprintf (buf, Page_size, "%s\n", Version);
  29. }
  30. Static bus_attr (version, S_irugo, show_bus_version, NULL);
  31. static int __init my_bus_init (void)
  32. {
  33. int ret;
  34. ret = Bus_register (&my_bus_type);
  35. if (ret)
  36. return ret;
  37. if (Bus_create_file (&my_bus_type, &bus_attr_version))
  38. PRINTK (Kern_notice "Fail to create version attribute!\n");
  39. ret = Device_register (&my_bus);
  40. if (ret)
  41. PRINTK (Kern_notice "Fail to register device:my_bus!\n");
  42. return ret;
  43. }
  44. static void My_bus_exit (void)
  45. {
  46. Device_unregister (&my_bus);
  47. Bus_unregister (&my_bus_type);
  48. }
  49. Module_init (My_bus_init);
  50. Module_exit (My_bus_exit);

[CPP]View Plaincopy
  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>
  6. Module_license ("Dual BSD/GPL");
  7. extern struct device my_bus;
  8. extern struct Bus_type my_bus_type;
  9. static void my_dev_release (struct device *dev)
  10. {
  11. }
  12. struct device My_dev = {
  13. . Init_name = "My_dev",
  14. . Bus = &my_bus_type,
  15. . Parent = &my_bus,
  16. . Release = My_dev_release,
  17. };
  18. Static ssize_t mydev_show (struct device *dev,struct Device_attribute *attr, char *buf)
  19. {
  20. return sprintf (buf, "%s\n", "This is my device!");
  21. }
  22. Static device_attr (Dev, S_irugo, mydev_show, NULL);
  23. static int __init my_device_init (void)
  24. {
  25. int ret = 0;
  26. ///strncpy (My_dev->kobj.name, "My_dev", strlen (My_dev->name));
  27. Device_register (&my_dev);
  28. Device_create_file (&my_dev, &dev_attr_dev);
  29. return ret;
  30. }
  31. static void My_device_exit (void)
  32. {
  33. Device_unregister (&my_dev);
  34. }
  35. Module_init (My_device_init);
  36. Module_exit (My_device_exit);



[CPP]View Plaincopy
    1. #include <linux/device.h>
    2. #include <linux/module.h>
    3. #include <linux/kernel.h>
    4. #include <linux/init.h>
    5. #include <linux/string.h>
    6. Module_license ("Dual BSD/GPL");
    7. extern struct Bus_type my_bus_type;
    8. static int my_probe (struct device *dev)
    9. {
    10. PRINTK ("Driver found device which my Driver can handle!\n");
    11. return 0;
    12. }
    13. static int my_remove (struct device *dev)
    14. {
    15. PRINTK ("Driver found device unpluged!\n");
    16. return 0;
    17. }
    18. struct Device_driver my_driver = {
    19. . Name = "My_dev",
    20. . Bus = &my_bus_type,
    21. . Probe = My_probe,
    22. . remove = My_remove,
    23. };
    24. Static ssize_t mydriver_show (struct device_driver *driver, char *buf)
    25. {
    26. return sprintf (buf, "%s\n", "This is my driver!");
    27. }
    28. Static driver_attr (DRV, S_irugo, Mydriver_show, NULL);
    29. static int __init my_driver_init (void)
    30. {
    31. int ret = 0;
    32. Driver_register (&my_driver);
    33. Driver_create_file (&my_driver, &driver_attr_drv);
    34. return ret;
    35. }
    36. static void My_driver_exit (void)
    37. {
    38. Driver_unregister (&my_driver);
    39. }
    40. Module_init (My_driver_init);
    41. Module_exit (My_driver_exit);

[Linux drivers] [Linux Driver] device driver Model related (i)--Sample code

Related Article

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.