device.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/cdev.h>
  48. #include <linux/module.h>
  49. #include <linux/device.h>
  50. #include <linux/fs.h>
  51. #include "hfi.h"
  52. #include "device.h"
  53. static struct class *class;
  54. static struct class *user_class;
  55. static dev_t hfi1_dev;
  56. int hfi1_cdev_init(int minor, const char *name,
  57. const struct file_operations *fops,
  58. struct cdev *cdev, struct device **devp,
  59. bool user_accessible,
  60. struct kobject *parent)
  61. {
  62. const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
  63. struct device *device = NULL;
  64. int ret;
  65. cdev_init(cdev, fops);
  66. cdev->owner = THIS_MODULE;
  67. cdev_set_parent(cdev, parent);
  68. kobject_set_name(&cdev->kobj, name);
  69. ret = cdev_add(cdev, dev, 1);
  70. if (ret < 0) {
  71. pr_err("Could not add cdev for minor %d, %s (err %d)\n",
  72. minor, name, -ret);
  73. goto done;
  74. }
  75. if (user_accessible)
  76. device = device_create(user_class, NULL, dev, NULL, "%s", name);
  77. else
  78. device = device_create(class, NULL, dev, NULL, "%s", name);
  79. if (IS_ERR(device)) {
  80. ret = PTR_ERR(device);
  81. device = NULL;
  82. pr_err("Could not create device for minor %d, %s (err %d)\n",
  83. minor, name, -ret);
  84. cdev_del(cdev);
  85. }
  86. done:
  87. *devp = device;
  88. return ret;
  89. }
  90. void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp)
  91. {
  92. struct device *device = *devp;
  93. if (device) {
  94. device_unregister(device);
  95. *devp = NULL;
  96. cdev_del(cdev);
  97. }
  98. }
  99. static const char *hfi1_class_name = "hfi1";
  100. const char *class_name(void)
  101. {
  102. return hfi1_class_name;
  103. }
  104. static char *hfi1_devnode(struct device *dev, umode_t *mode)
  105. {
  106. if (mode)
  107. *mode = 0600;
  108. return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
  109. }
  110. static const char *hfi1_class_name_user = "hfi1_user";
  111. static const char *class_name_user(void)
  112. {
  113. return hfi1_class_name_user;
  114. }
  115. static char *hfi1_user_devnode(struct device *dev, umode_t *mode)
  116. {
  117. if (mode)
  118. *mode = 0666;
  119. return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
  120. }
  121. int __init dev_init(void)
  122. {
  123. int ret;
  124. ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME);
  125. if (ret < 0) {
  126. pr_err("Could not allocate chrdev region (err %d)\n", -ret);
  127. goto done;
  128. }
  129. class = class_create(THIS_MODULE, class_name());
  130. if (IS_ERR(class)) {
  131. ret = PTR_ERR(class);
  132. pr_err("Could not create device class (err %d)\n", -ret);
  133. unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
  134. goto done;
  135. }
  136. class->devnode = hfi1_devnode;
  137. user_class = class_create(THIS_MODULE, class_name_user());
  138. if (IS_ERR(user_class)) {
  139. ret = PTR_ERR(user_class);
  140. pr_err("Could not create device class for user accessible files (err %d)\n",
  141. -ret);
  142. class_destroy(class);
  143. class = NULL;
  144. user_class = NULL;
  145. unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
  146. goto done;
  147. }
  148. user_class->devnode = hfi1_user_devnode;
  149. done:
  150. return ret;
  151. }
  152. void dev_cleanup(void)
  153. {
  154. class_destroy(class);
  155. class = NULL;
  156. class_destroy(user_class);
  157. user_class = NULL;
  158. unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
  159. }