Merge tag 'nfsd-4.6' of git://linux-nfs.org/~bfields/linux
[deliverable/linux.git] / drivers / staging / rdma / hfi1 / device.c
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
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 {
65 const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
66 struct device *device = NULL;
67 int ret;
68
69 cdev_init(cdev, fops);
70 cdev->owner = THIS_MODULE;
71 kobject_set_name(&cdev->kobj, name);
72
73 ret = cdev_add(cdev, dev, 1);
74 if (ret < 0) {
75 pr_err("Could not add cdev for minor %d, %s (err %d)\n",
76 minor, name, -ret);
77 goto done;
78 }
79
80 if (user_accessible)
81 device = device_create(user_class, NULL, dev, NULL, "%s", name);
82 else
83 device = device_create(class, NULL, dev, NULL, "%s", name);
84
85 if (!IS_ERR(device))
86 goto done;
87 ret = PTR_ERR(device);
88 device = NULL;
89 pr_err("Could not create device for minor %d, %s (err %d)\n",
90 minor, name, -ret);
91 cdev_del(cdev);
92 done:
93 *devp = device;
94 return ret;
95 }
96
97 void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp)
98 {
99 struct device *device = *devp;
100
101 if (device) {
102 device_unregister(device);
103 *devp = NULL;
104
105 cdev_del(cdev);
106 }
107 }
108
109 static const char *hfi1_class_name = "hfi1";
110
111 const char *class_name(void)
112 {
113 return hfi1_class_name;
114 }
115
116 static char *hfi1_devnode(struct device *dev, umode_t *mode)
117 {
118 if (mode)
119 *mode = 0600;
120 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
121 }
122
123 static const char *hfi1_class_name_user = "hfi1_user";
124 static const char *class_name_user(void)
125 {
126 return hfi1_class_name_user;
127 }
128
129 static char *hfi1_user_devnode(struct device *dev, umode_t *mode)
130 {
131 if (mode)
132 *mode = 0666;
133 return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
134 }
135
136 int __init dev_init(void)
137 {
138 int ret;
139
140 ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME);
141 if (ret < 0) {
142 pr_err("Could not allocate chrdev region (err %d)\n", -ret);
143 goto done;
144 }
145
146 class = class_create(THIS_MODULE, class_name());
147 if (IS_ERR(class)) {
148 ret = PTR_ERR(class);
149 pr_err("Could not create device class (err %d)\n", -ret);
150 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
151 goto done;
152 }
153 class->devnode = hfi1_devnode;
154
155 user_class = class_create(THIS_MODULE, class_name_user());
156 if (IS_ERR(user_class)) {
157 ret = PTR_ERR(user_class);
158 pr_err("Could not create device class for user accessible files (err %d)\n",
159 -ret);
160 class_destroy(class);
161 class = NULL;
162 user_class = NULL;
163 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
164 goto done;
165 }
166 user_class->devnode = hfi1_user_devnode;
167
168 done:
169 return ret;
170 }
171
172 void dev_cleanup(void)
173 {
174 class_destroy(class);
175 class = NULL;
176
177 class_destroy(user_class);
178 user_class = NULL;
179
180 unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
181 }
This page took 0.036011 seconds and 6 git commands to generate.