[Linux Driver] character device driver learning Note (iii) ——— advanced

Source: Internet
Author: User

One, the IOCTL use instance

IOCTL usage examples

Driver. h file memdev.h

[CPP]View Plaincopy
    1. /* Define MAGIC number */
    2. #define Memdev_ioc_magic ' k '
    3. /* Define the command */
    4. #define Memdev_iocprint _io (memdev_ioc_magic, 1)
    5. #define Memdev_iocgetdata _ior (memdev_ioc_magic, 2, int)
    6. #define Memdev_iocsetdata _iow (memdev_ioc_magic, 3, int)
    7. #define MEMDEV_IOC_MAXNR 3


Driver. c File

[CPP]View Plaincopy
  1. /*io Operation */
  2. int Memdev_ioctl (struct inode *inode, struct file *filp,
  3. unsigned int cmd, unsigned long arg)
  4. {
  5. int err = 0;
  6. int ret = 0;
  7. int ioarg = 0;
  8. / * Check the validity of the command * /
  9. if (_ioc_type (cmd)! = memdev_ioc_magic)
  10. Return-einval;
  11. if (_ioc_nr (cmd) > Memdev_ioc_maxnr)
  12. Return-einval;
  13. / * Check whether the parameter space can be accessed according to the command type * /
  14. if (_ioc_dir (cmd) & _ioc_read)
  15. Err =!ACCESS_OK (Verify_write, (void *) arg, _ioc_size (cmd));
  16. Else if (_ioc_dir (cmd) & _ioc_write)
  17. Err =!ACCESS_OK (Verify_read, (void *) arg, _ioc_size (cmd));
  18. if (err)
  19. Return-efault;
  20. / * Perform the appropriate action according to the command * /
  21. switch (cmd) {
  22. / * Print current device information * /
  23. Case Memdev_iocprint:
  24. PRINTK ("<---CMD memdev_iocprint done--->\n\n");
  25. Break ;
  26. / * Get parameters * /
  27. Case Memdev_iocgetdata:
  28. Ioarg = 1101;
  29. ret = __put_user (Ioarg, (int *) arg);
  30. Break ;
  31. / * Set parameters * /
  32. Case Memdev_iocsetdata:
  33. ret = __get_user (Ioarg, (int *) arg);
  34. PRINTK ("<---in Kernel memdev_iocsetdata ioarg =%d--->\n\n", ioarg);
  35. Break ;
  36. Default:
  37. Return-einval;
  38. }
  39. return ret;
  40. }


Application

[CPP]View Plaincopy
    1. #include "Memdev.h"
    2. cmd = Memdev_iocprint;
    3. if (IOCTL (FD, CMD, &arg) < 0)
    4. {
    5. printf ("Call cmd memdev_iocprint fail\n");
    6. return-1;
    7. }
    8. cmd = Memdev_iocsetdata;
    9. arg = 2007;
    10. if (IOCTL (FD, CMD, &arg) < 0)
    11. {
    12. printf ("Call cmd memdev_iocsetdata fail\n");
    13. return-1;
    14. }

[Linux Driver] character device driver learning Note (iii) ——— advanced

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.