This source file includes following definitions.
- hfi1_cdev_init
- hfi1_cdev_cleanup
- class_name
- hfi1_devnode
- class_name_user
- hfi1_user_devnode
- dev_init
- dev_cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 #include <linux/cdev.h>
49 #include <linux/module.h>
50 #include <linux/device.h>
51 #include <linux/fs.h>
52
53 #include "hfi.h"
54 #include "device.h"
55
56 static struct class *class;
57 static struct class *user_class;
58 static dev_t hfi1_dev;
59
60 int hfi1_cdev_init(int minor, const char *name,
61 const struct file_operations *fops,
62 struct cdev *cdev, struct device **devp,
63 bool user_accessible,
64 struct kobject *parent)
65 {
66 const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
67 struct device *device = NULL;
68 int ret;
69
70 cdev_init(cdev, fops);
71 cdev->owner = THIS_MODULE;
72 cdev_set_parent(cdev, parent);
73 kobject_set_name(&cdev->kobj, name);
74
75 ret = cdev_add(cdev, dev, 1);
76 if (ret < 0) {
77 pr_err("Could not add cdev for minor %d, %s (err %d)\n",
78 minor, name, -ret);
79 goto done;
80 }
81
82 if (user_accessible)
83 device = device_create(user_class, NULL, dev, NULL, "%s", name);
84 else
85 device = device_create(class, NULL, dev, NULL, "%s", name);
86
87 if (IS_ERR(device)) {
88 ret = PTR_ERR(device);
89 device = NULL;
90 pr_err("Could not create device for minor %d, %s (err %d)\n",
91 minor, name, -ret);
92 cdev_del(cdev);
93 }
94 done:
95 *devp = device;
96 return ret;
97 }
98
99 void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp)
100 {
101 struct device *device = *devp;
102
103 if (device) {
104 device_unregister(device);
105 *devp = NULL;
106
107 cdev_del(cdev);
108 }
109 }
110
111 static const char *hfi1_class_name = "hfi1";
112
113 const char *class_name(void)
114 {
115 return hfi1_class_name;
116 }
117
118 static char *hfi1_devnode(struct device *dev, umode_t *mode)
119 {
120 if (mode)
121 *mode = 0600;
122 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
123 }
124
125 static const char *hfi1_class_name_user = "hfi1_user";
126 static const char *class_name_user(void)
127 {
128 return hfi1_class_name_user;
129 }
130
131 static char *hfi1_user_devnode(struct device *dev, umode_t *mode)
132 {
133 if (mode)
134 *mode = 0666;
135 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
136 }
137
138 int __init dev_init(void)
139 {
140 int ret;
141
142 ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME);
143 if (ret < 0) {
144 pr_err("Could not allocate chrdev region (err %d)\n", -ret);
145 goto done;
146 }
147
148 class = class_create(THIS_MODULE, class_name());
149 if (IS_ERR(class)) {
150 ret = PTR_ERR(class);
151 pr_err("Could not create device class (err %d)\n", -ret);
152 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
153 goto done;
154 }
155 class->devnode = hfi1_devnode;
156
157 user_class = class_create(THIS_MODULE, class_name_user());
158 if (IS_ERR(user_class)) {
159 ret = PTR_ERR(user_class);
160 pr_err("Could not create device class for user accessible files (err %d)\n",
161 -ret);
162 class_destroy(class);
163 class = NULL;
164 user_class = NULL;
165 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
166 goto done;
167 }
168 user_class->devnode = hfi1_user_devnode;
169
170 done:
171 return ret;
172 }
173
174 void dev_cleanup(void)
175 {
176 class_destroy(class);
177 class = NULL;
178
179 class_destroy(user_class);
180 user_class = NULL;
181
182 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
183 }