mod.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <linux/semaphore.h>
  9. #include "mod.h"
  10. #include "dev.h"
  11. #include "debug.h"
  12. #include "sysfs.h"
  13. MODULE_LICENSE("GPL v2");
  14. MODULE_AUTHOR("Suren A. Chilingaryan <csa@suren.me>");
  15. MODULE_DESCRIPTION("Testing module");
  16. MODULE_VERSION("0.0.1");
  17. int test_minors = TEST_MINORS;
  18. module_param(test_minors, int, S_IRUGO);
  19. static dev_t test_devno; /**< major number */
  20. static test_dev_t **test_devs = NULL; /**< per-device context */
  21. static DEFINE_SEMAPHORE(test_devs_mtx); /**< mutex protecting creation/destruction of devices */
  22. static struct class *test_class; /**< device class */
  23. static void test_module_destroy_cdev(test_dev_t *test)
  24. {
  25. if (test->dev) {
  26. test_sysfs_free(test);
  27. device_destroy(test_class, test->devno);
  28. }
  29. cdev_del(&test->cdev);
  30. kfree(test);
  31. }
  32. static int test_module_setup_cdev(void)
  33. {
  34. int i;
  35. int err = 0;
  36. dev_t devno;
  37. test_dev_t *test;
  38. test = kmalloc(sizeof(test_dev_t), GFP_KERNEL);
  39. if (!test) {
  40. mod_info("Couldn't allocate memory. Device is not created.\n");
  41. return -ENOMEM;
  42. }
  43. cdev_init(&test->cdev, test_get_fops());
  44. test->cdev.owner = THIS_MODULE;
  45. test->cdev.ops = test_get_fops();
  46. down(&test_devs_mtx);
  47. for (i = 0; i < test_minors; i++) {
  48. if (!test_devs[i])
  49. break;
  50. }
  51. if (i == test_minors) {
  52. mod_info("No free minor numbers left");
  53. err = -EBUSY;
  54. goto fail;
  55. }
  56. devno = MKDEV(MAJOR(test_devno), MINOR(test_devno) + i);
  57. test->devno = devno;
  58. err = cdev_add(&test->cdev, devno, 1);
  59. if (err) {
  60. mod_info("Error %d adding device kmm%d", err, i);
  61. goto fail;
  62. }
  63. test->dev = device_create(test_class, NULL, devno, test, TEST_NODE_FMT, MINOR(test_devno) + i);
  64. if (!test->dev) {
  65. mod_info("Error creating /dev/%s%d\n", TEST_NODE, MINOR(test_devno) + i);
  66. goto fail;
  67. }
  68. dev_set_drvdata(test->dev, test);
  69. test_devs[i] = test;
  70. up(&test_devs_mtx);
  71. err = test_sysfs_init(test);
  72. if (err) goto fail_sysfs;
  73. mod_info("Device /dev/%s%d added\n", TEST_NODE, MINOR(test_devno) + i);
  74. return 0;
  75. fail:
  76. up(&test_devs_mtx);
  77. fail_sysfs:
  78. test_module_destroy_cdev(test);
  79. return err;
  80. }
  81. static void test_module_cleanup(void)
  82. {
  83. int i;
  84. if (test_devs) {
  85. for (i = 0; i < test_minors; i++) {
  86. if (test_devs[i])
  87. test_module_destroy_cdev(test_devs[i]);
  88. }
  89. kfree(test_devs);
  90. }
  91. if (test_class)
  92. class_destroy(test_class);
  93. unregister_chrdev_region(test_devno, test_minors);
  94. }
  95. static int __init test_module_init(void)
  96. {
  97. int err;
  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);