xref: /freebsd/sys/compat/linuxkpi/common/include/linux/cdev.h (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2021 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef	_LINUXKPI_LINUX_CDEV_H_
30 #define	_LINUXKPI_LINUX_CDEV_H_
31 
32 #include <linux/kobject.h>
33 #include <linux/sysfs.h>
34 #include <linux/kdev_t.h>
35 #include <linux/list.h>
36 
37 #include <asm/atomic-long.h>
38 
39 struct device;
40 struct file_operations;
41 struct inode;
42 struct module;
43 
44 extern struct cdevsw linuxcdevsw;
45 extern const struct kobj_type linux_cdev_ktype;
46 extern const struct kobj_type linux_cdev_static_ktype;
47 
48 struct linux_cdev {
49 	struct kobject	kobj;
50 	struct module	*owner;
51 	struct cdev	*cdev;
52 	dev_t		dev;
53 	const struct file_operations *ops;
54 	u_int		refs;
55 	u_int		siref;
56 };
57 
58 struct linux_cdev *cdev_alloc(void);
59 
60 static inline void
61 cdev_init(struct linux_cdev *cdev, const struct file_operations *ops)
62 {
63 
64 	kobject_init(&cdev->kobj, &linux_cdev_static_ktype);
65 	cdev->ops = ops;
66 	cdev->refs = 1;
67 }
68 
69 static inline void
70 cdev_put(struct linux_cdev *p)
71 {
72 	kobject_put(&p->kobj);
73 }
74 
75 static inline int
76 cdev_add(struct linux_cdev *cdev, dev_t dev, unsigned count)
77 {
78 	struct make_dev_args args;
79 	int error;
80 
81 	if (count != 1)
82 		return (-EINVAL);
83 
84 	cdev->dev = dev;
85 
86 	/* Setup arguments for make_dev_s() */
87 	make_dev_args_init(&args);
88 	args.mda_devsw = &linuxcdevsw;
89 	args.mda_uid = 0;
90 	args.mda_gid = 0;
91 	args.mda_mode = 0700;
92 	args.mda_si_drv1 = cdev;
93 
94 	error = make_dev_s(&args, &cdev->cdev, "%s",
95 	    kobject_name(&cdev->kobj));
96 	if (error)
97 		return (-error);
98 
99 	kobject_get(cdev->kobj.parent);
100 	return (0);
101 }
102 
103 static inline int
104 cdev_add_ext(struct linux_cdev *cdev, dev_t dev, uid_t uid, gid_t gid, int mode)
105 {
106 	struct make_dev_args args;
107 	int error;
108 
109 	cdev->dev = dev;
110 
111 	/* Setup arguments for make_dev_s() */
112 	make_dev_args_init(&args);
113 	args.mda_devsw = &linuxcdevsw;
114 	args.mda_uid = uid;
115 	args.mda_gid = gid;
116 	args.mda_mode = mode;
117 	args.mda_si_drv1 = cdev;
118 
119 	error = make_dev_s(&args, &cdev->cdev, "%s/%d",
120 	    kobject_name(&cdev->kobj), MINOR(dev));
121 	if (error)
122 		return (-error);
123 
124 	kobject_get(cdev->kobj.parent);
125 	return (0);
126 }
127 
128 static inline void
129 cdev_del(struct linux_cdev *cdev)
130 {
131 	kobject_put(&cdev->kobj);
132 }
133 
134 struct linux_cdev *linux_find_cdev(const char *name, unsigned major, unsigned minor);
135 
136 int linux_cdev_device_add(struct linux_cdev *, struct device *);
137 void linux_cdev_device_del(struct linux_cdev *, struct device *);
138 
139 #define	cdev_device_add(...)		\
140   linux_cdev_device_add(__VA_ARGS__)
141 #define	cdev_device_del(...)		\
142   linux_cdev_device_del(__VA_ARGS__)
143 
144 #define	cdev	linux_cdev
145 
146 #endif	/* _LINUXKPI_LINUX_CDEV_H_ */
147