mod.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/types.h>
  5. #include <linux/cdev.h>
  6. #include <linux/fs.h>
  7. #include <linux/slab.h>
  8. #include "mod.h"
  9. #include "dev.h"
  10. #include "debug.h"
  11. #include "sysfs.h"
  12. MODULE_LICENSE("GPL v2");
  13. MODULE_AUTHOR("Suren A. Chilingaryan <csa@suren.me>");
  14. MODULE_DESCRIPTION("Testing module");
  15. MODULE_VERSION("0.0.1");
  16. int test_minors = TEST_MINORS;
  17. module_param(test_minors, int, S_IRUGO);
  18. static dev_t test_devno; /**< major number */
  19. static test_dev_t **test_devs = NULL; /**< per-device context */
  20. static spinlock_t test_devs_lock; /**< lock protecting creation/destruction of devices */
  21. static struct class *test_class; /**< device class */
  22. static void test_module_destroy_cdev(test_dev_t *test)
  23. {
  24. if (test->dev) {
  25. test_sysfs_free(test);
  26. device_destroy(test_class, test->devno);
  27. }
  28. cdev_del(&test->cdev);
  29. kfree(test);
  30. }
  31. static int test_module_setup_cdev(void)
  32. {
  33. int i;
  34. int err = 0;
  35. dev_t devno;
  36. test_dev_t *test;
  37. test = kmalloc(sizeof(test_dev_t), GFP_KERNEL);
  38. if (!test) {
  39. mod_info("Couldn't allocate memory. Device is not created.\n");
  40. return -ENOMEM;
  41. }
  42. cdev_init(&test->cdev, test_get_fops());
  43. test->cdev.owner = THIS_MODULE;
  44. test->cdev.ops = test_get_fops();
  45. spin_lock(&test_devs_lock);
  46. for (i = 0; i < test_minors; i++) {
  47. if (!test_devs[i])
  48. break;
  49. }
  50. if (i == test_minors) {
  51. mod_info("No free minor numbers left");
  52. err = -EBUSY;
  53. goto fail;
  54. }
  55. devno = MKDEV(MAJOR(test_devno), MINOR(test_devno) + i);
  56. test->devno = devno;
  57. err = cdev_add(&test->cdev, devno, 1);
  58. if (err) {
  59. mod_info("Error %d adding device kmm%d", err, i);
  60. goto fail;
  61. }
  62. test->dev = device_create(test_class, NULL, devno, test, TEST_NODE_FMT, MINOR(test_devno) + i);
  63. if (!test->dev) {
  64. mod_info("Error creating /dev/%s%d\n", TEST_NODE, MINOR(test_devno) + i);
  65. goto fail;
  66. }
  67. dev_set_drvdata(test->dev, test);
  68. test_devs[i] = test;
  69. spin_unlock(&test_devs_lock);
  70. err = test_sysfs_init(test);
  71. if (err) goto fail_sysfs;
  72. mod_info("Device /dev/%s%d added\n", TEST_NODE, MINOR(test_devno) + i);
  73. return 0;
  74. fail:
  75. spin_unlock(&test_devs_lock);
  76. fail_sysfs:
  77. test_module_destroy_cdev(test);
  78. return err;
  79. }
  80. static void test_module_cleanup(void)
  81. {
  82. int i;
  83. if (test_devs) {
  84. for (i = 0; i < test_minors; i++) {
  85. if (test_devs[i])
  86. test_module_destroy_cdev(test_devs[i]);
  87. }
  88. kfree(test_devs);
  89. }
  90. if (test_class)
  91. class_destroy(test_class);
  92. unregister_chrdev_region(test_devno, test_minors);
  93. }
  94. static int __init test_module_init(void)
  95. {
  96. int err;
  97. spin_lock_init(&test_devs_lock);
  98. if ((err = alloc_chrdev_region(&test_devno, 0, test_minors, TEST_NODE))) {
  99. mod_info("Couldn't allocate chrdev region. Module not loaded.\n");
  100. goto alloc_chrdev_fail;
  101. }
  102. test_class = class_create(THIS_MODULE, TEST_NODE);
  103. if (IS_ERR(test_class)) {
  104. mod_info("No sysfs support. Module not loaded.\n");
  105. goto fail;
  106. }
  107. test_devs = kmalloc(test_minors * sizeof(test_dev_t*), GFP_KERNEL);
  108. if (!test_devs) {
  109. mod_info("Couldn't allocate memory. Module not loaded.\n");
  110. err = -ENOMEM;
  111. }
  112. memset(test_devs, 0, test_minors * sizeof(test_dev_t*));
  113. mod_info("Major %d allocated to node '%s'\n", MAJOR(test_devno), TEST_NODE);
  114. err = test_module_setup_cdev();
  115. if (err) goto fail;
  116. return 0;
  117. fail:
  118. test_module_cleanup();
  119. alloc_chrdev_fail:
  120. return err;
  121. }
  122. static void __exit test_module_exit(void)
  123. {
  124. test_module_cleanup();
  125. }
  126. module_init(test_module_init);
  127. module_exit(test_module_exit);