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