xref: /linux/drivers/infiniband/hw/hfi1/device.c (revision 0ad53fe3ae82443c74ff8cfd7bd13377cc1134a3)
1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2 /*
3  * Copyright(c) 2015, 2016 Intel Corporation.
4  */
5 
6 #include <linux/cdev.h>
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/fs.h>
10 
11 #include "hfi.h"
12 #include "device.h"
13 
14 static struct class *class;
15 static struct class *user_class;
16 static dev_t hfi1_dev;
17 
18 int hfi1_cdev_init(int minor, const char *name,
19 		   const struct file_operations *fops,
20 		   struct cdev *cdev, struct device **devp,
21 		   bool user_accessible,
22 		   struct kobject *parent)
23 {
24 	const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
25 	struct device *device = NULL;
26 	int ret;
27 
28 	cdev_init(cdev, fops);
29 	cdev->owner = THIS_MODULE;
30 	cdev_set_parent(cdev, parent);
31 	kobject_set_name(&cdev->kobj, name);
32 
33 	ret = cdev_add(cdev, dev, 1);
34 	if (ret < 0) {
35 		pr_err("Could not add cdev for minor %d, %s (err %d)\n",
36 		       minor, name, -ret);
37 		goto done;
38 	}
39 
40 	if (user_accessible)
41 		device = device_create(user_class, NULL, dev, NULL, "%s", name);
42 	else
43 		device = device_create(class, NULL, dev, NULL, "%s", name);
44 
45 	if (IS_ERR(device)) {
46 		ret = PTR_ERR(device);
47 		device = NULL;
48 		pr_err("Could not create device for minor %d, %s (err %d)\n",
49 			minor, name, -ret);
50 		cdev_del(cdev);
51 	}
52 done:
53 	*devp = device;
54 	return ret;
55 }
56 
57 void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp)
58 {
59 	struct device *device = *devp;
60 
61 	if (device) {
62 		device_unregister(device);
63 		*devp = NULL;
64 
65 		cdev_del(cdev);
66 	}
67 }
68 
69 static const char *hfi1_class_name = "hfi1";
70 
71 const char *class_name(void)
72 {
73 	return hfi1_class_name;
74 }
75 
76 static char *hfi1_devnode(struct device *dev, umode_t *mode)
77 {
78 	if (mode)
79 		*mode = 0600;
80 	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
81 }
82 
83 static const char *hfi1_class_name_user = "hfi1_user";
84 static const char *class_name_user(void)
85 {
86 	return hfi1_class_name_user;
87 }
88 
89 static char *hfi1_user_devnode(struct device *dev, umode_t *mode)
90 {
91 	if (mode)
92 		*mode = 0666;
93 	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
94 }
95 
96 int __init dev_init(void)
97 {
98 	int ret;
99 
100 	ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME);
101 	if (ret < 0) {
102 		pr_err("Could not allocate chrdev region (err %d)\n", -ret);
103 		goto done;
104 	}
105 
106 	class = class_create(THIS_MODULE, class_name());
107 	if (IS_ERR(class)) {
108 		ret = PTR_ERR(class);
109 		pr_err("Could not create device class (err %d)\n", -ret);
110 		unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
111 		goto done;
112 	}
113 	class->devnode = hfi1_devnode;
114 
115 	user_class = class_create(THIS_MODULE, class_name_user());
116 	if (IS_ERR(user_class)) {
117 		ret = PTR_ERR(user_class);
118 		pr_err("Could not create device class for user accessible files (err %d)\n",
119 		       -ret);
120 		class_destroy(class);
121 		class = NULL;
122 		user_class = NULL;
123 		unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
124 		goto done;
125 	}
126 	user_class->devnode = hfi1_user_devnode;
127 
128 done:
129 	return ret;
130 }
131 
132 void dev_cleanup(void)
133 {
134 	class_destroy(class);
135 	class = NULL;
136 
137 	class_destroy(user_class);
138 	user_class = NULL;
139 
140 	unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
141 }
142