[Linux Driver] character device driver learning Note (ii) ——— instance

Source: Internet
Author: User

One, register character device

[CPP]View Plaincopy
  1. #define GLOBALMEM_MAJOR 256
  2. #define Globalmem_size 0X1000//4k
  3. static int char_major=globalmem_major; Main device number
  4. struct CHARTEST_DEV
  5. {
  6. struct Cdev cdev;
  7. unsigned char mem[globalmem_size];
  8. };
  9. struct Chartest_dev Glob_char_dev; //Global device structure
  10. Static struct class *char_class=null;
  11. Static struct Class_device *char_class_dev=null;
  12. -------------------------------------device open function, match upper function call
  13. static int Char_open (struct inode *inode, struct file *filp)
  14. {
  15. Filp->private_data = &glob_char_dev;
  16. return 0;
  17. }
  18. -------------------------------------read data from the kernel to the user space copy
  19. Static ssize_t Char_read (struct file* filp,char __user *buf,size_t count,loff_t *ppos)
  20. {
  21. unsigned long p=*ppos;
  22. int ret=0;
  23. struct Chartest_dev *char_dev=filp->private_data;
  24. if (p>globalmem_size)//For cross-border inspection
  25. return 0;
  26. if (count>globalmem_size-p)//Read more data
  27. Count=globalmem_size-p;
  28. if (Copy_to_user (buf, (void*) (char_dev->mem+p), count))
  29. Ret=-efault;
  30. Else
  31. {
  32. *ppos=*ppos+count;
  33. Ret=count;
  34. PRINTK (kern_info "READ%d BYTES (S) from%d", count,p);
  35. }
  36. return ret;
  37. }
  38. -------------------------------------writing data from the user control to the kernel
  39. Static ssize_t char_write (struct file* filp,const char __user *buf,size_t count,loff_t *ppos)
  40. {
  41. unsigned long p=*ppos;
  42. int ret=0;
  43. struct Chartest_dev *char_dev=filp->private_data;
  44. if (p>globalmem_size)
  45. return 0;
  46. if (count>globalmem_size-p)
  47. Count=globalmem_size-p;
  48. if (Copy_from_user (char_dev->mem+p,buf,count))
  49. Ret=-efault;
  50. Else
  51. {
  52. *ppos=*ppos+count;
  53. Ret=count;
  54. PRINTK (Kern_info "written%d BYTES (S) from%d", count,p);
  55. }
  56. return ret;
  57. }
  58. Static loff_t Char_llseek (struct file *filp, loff_t offset, int orig)
  59. {
  60. loff_t ret = 0;
  61. switch (orig)
  62. {
  63. Case 0:
  64. if (offset < 0)
  65. {
  66. ret =-EINVAL;
  67. Break ;
  68. }
  69. if ((unsigned int) offset > globalmem_size)
  70. {
  71. ret =-EINVAL;
  72. Break ;
  73. }
  74. Filp->f_pos = (unsigned int) offset;
  75. RET = filp->f_pos;
  76. Break ;
  77. Case 1:
  78. if ((Filp->f_pos + offset) > globalmem_size)
  79. {
  80. ret =-EINVAL;
  81. Break ;
  82. }
  83. if ((Filp->f_pos + offset) < 0)
  84. {
  85. ret =-EINVAL;
  86. Break ;
  87. }
  88. Filp->f_pos + = offset;
  89. RET = filp->f_pos;
  90. Break ;
  91. Default:
  92. ret =-EINVAL;
  93. Break ;
  94. }
  95. return ret;
  96. }
  97. static const struct file_operations char_fops = {
  98. . Owner = This_module,
  99. . Llseek =char_llseek,
  100. . open = Char_open,
  101. . Read = Char_read,
  102. . write = Char_write,
  103. };
  104. static void Chartest_setup_cdev ()
  105. {
  106. int Err,devno=mkdev (char_major,0);
  107. Cdev_init (&glob_char_dev.cdev,&char_fops); / * Initialize * /
  108. Glob_char_dev.cdev.owner=this_module;
  109. Err=cdev_add (&glob_char_dev.cdev,devno,1);
  110. if (err<0)
  111. PRINTK (Kern_notice "ERROR%d ADDING chartest", err);
  112. Char_class=class_create (This_module,"chartest");
  113. Char_class_dev=device_create (Char_class,null,devno,null,"Chartest", 0);
  114. }
  115. static int char_init (void)
  116. {
  117. int result;
  118. Unsigned long Char_dev_no=mkdev (char_major,0);
  119. if (char_major) {
  120. Result=register_chrdev_region (char_dev_no,1,"chartest");
  121. }
  122. else{
  123. Result=alloc_chrdev_region (&char_dev_no,0,1,"chartest");
  124. Char_major=major (CHAR_DEV_NO);
  125. }
  126. PRINTK (Kern_alert "Hello,,we succeed\n");
  127. if (result<0)
  128. return result;
  129. Chartest_setup_cdev ();
  130. PRINTK (Kern_alert "Hello,,we succeed\n");
  131. return 0;
  132. }
  133. static void Char_exit (void)
  134. {
  135. Cdev_del (&glob_char_dev.cdev);
  136. Unregister_chrdev_region (MKDEV (char_major,0), 1); //zhu XIAO
  137. Device_unregister (Char_class_dev);
  138. Class_destroy (Char_class);
  139. PRINTK (Kern_alert "720 unregister success\n");
  140. }
  141. Module_license ("Dual BSD/GPL");
  142. Module_init (Char_init);

Second, note the point:

The parameter buff of the read and write functions is a pointer to the user space, so the kernel code cannot directly reference the contents, mainly for the following reasons:
1, user-space pointers may not be valid while kernel-mode is running
2, the user space is paged, the content involved may not be in RAM, a direct reference to the memory of the user space may cause page errors
3. Pointers are provided by user space and may be defective.

Copy_to_user () and Copy_from_user () two functions, in addition to the data copy in the kernel space and user space, will also check whether the user space pointer is valid, if the pointer is invalid, not copy, if the copy process encountered an invalid address, will only copy part of the data, In both cases, the return value is a value that also requires a copy of the amount of memory.

[Linux Driver] character device driver learning Note (ii) ——— instance

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.