device.h revision 299674
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-2016 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 * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/device.h 299674 2016-05-13 13:01:02Z hselasky $
30 */
31#ifndef	_LINUX_DEVICE_H_
32#define	_LINUX_DEVICE_H_
33
34#include <linux/types.h>
35#include <linux/kobject.h>
36#include <linux/sysfs.h>
37#include <linux/list.h>
38#include <linux/compiler.h>
39#include <linux/types.h>
40#include <linux/module.h>
41#include <linux/workqueue.h>
42#include <linux/sysfs.h>
43#include <linux/kdev_t.h>
44#include <asm/atomic.h>
45
46#include <sys/bus.h>
47
48enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
49typedef enum irqreturn	irqreturn_t;
50
51struct class {
52	const char	*name;
53	struct module	*owner;
54	struct kobject	kobj;
55	devclass_t	bsdclass;
56	void		(*class_release)(struct class *class);
57	void		(*dev_release)(struct device *dev);
58	char *		(*devnode)(struct device *dev, umode_t *mode);
59};
60
61struct device {
62	struct device	*parent;
63	struct list_head irqents;
64	device_t	bsddev;
65	dev_t		devt;
66	struct class	*class;
67	void		(*release)(struct device *dev);
68	struct kobject	kobj;
69	uint64_t	*dma_mask;
70	void		*driver_data;
71	unsigned int	irq;
72	unsigned int	msix;
73	unsigned int	msix_max;
74};
75
76extern struct device linux_root_device;
77extern struct kobject linux_class_root;
78extern const struct kobj_type linux_dev_ktype;
79extern const struct kobj_type linux_class_ktype;
80
81struct class_attribute {
82        struct attribute attr;
83        ssize_t (*show)(struct class *, struct class_attribute *, char *);
84        ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
85        const void *(*namespace)(struct class *, const struct class_attribute *);
86};
87
88#define	CLASS_ATTR(_name, _mode, _show, _store)				\
89	struct class_attribute class_attr_##_name =			\
90	    { { #_name, NULL, _mode }, _show, _store }
91
92struct device_attribute {
93	struct attribute	attr;
94	ssize_t			(*show)(struct device *,
95					struct device_attribute *, char *);
96	ssize_t			(*store)(struct device *,
97					struct device_attribute *, const char *,
98					size_t);
99};
100
101#define	DEVICE_ATTR(_name, _mode, _show, _store)			\
102	struct device_attribute dev_attr_##_name =			\
103	    { { #_name, NULL, _mode }, _show, _store }
104
105/* Simple class attribute that is just a static string */
106struct class_attribute_string {
107	struct class_attribute attr;
108	char *str;
109};
110
111static inline ssize_t
112show_class_attr_string(struct class *class,
113				struct class_attribute *attr, char *buf)
114{
115	struct class_attribute_string *cs;
116	cs = container_of(attr, struct class_attribute_string, attr);
117	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
118}
119
120/* Currently read-only only */
121#define _CLASS_ATTR_STRING(_name, _mode, _str) \
122	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
123#define CLASS_ATTR_STRING(_name, _mode, _str) \
124	struct class_attribute_string class_attr_##_name = \
125		_CLASS_ATTR_STRING(_name, _mode, _str)
126
127#define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
128#define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
129#define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
130#define	dev_printk(lvl, dev, fmt, ...)					\
131	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
132
133static inline void *
134dev_get_drvdata(struct device *dev)
135{
136
137	return dev->driver_data;
138}
139
140static inline void
141dev_set_drvdata(struct device *dev, void *data)
142{
143
144	dev->driver_data = data;
145}
146
147static inline struct device *
148get_device(struct device *dev)
149{
150
151	if (dev)
152		kobject_get(&dev->kobj);
153
154	return (dev);
155}
156
157static inline char *
158dev_name(const struct device *dev)
159{
160
161 	return kobject_name(&dev->kobj);
162}
163
164#define	dev_set_name(_dev, _fmt, ...)					\
165	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
166
167static inline void
168put_device(struct device *dev)
169{
170
171	if (dev)
172		kobject_put(&dev->kobj);
173}
174
175static inline int
176class_register(struct class *class)
177{
178
179	class->bsdclass = devclass_create(class->name);
180	kobject_init(&class->kobj, &linux_class_ktype);
181	kobject_set_name(&class->kobj, class->name);
182	kobject_add(&class->kobj, &linux_class_root, class->name);
183
184	return (0);
185}
186
187static inline void
188class_unregister(struct class *class)
189{
190
191	kobject_put(&class->kobj);
192}
193
194/*
195 * Devices are registered and created for exporting to sysfs.  create
196 * implies register and register assumes the device fields have been
197 * setup appropriately before being called.
198 */
199static inline int
200device_register(struct device *dev)
201{
202	device_t bsddev;
203	int unit;
204
205	bsddev = NULL;
206	unit = -1;
207
208	if (dev->devt) {
209		unit = MINOR(dev->devt);
210		bsddev = devclass_get_device(dev->class->bsdclass, unit);
211	} else if (dev->parent == NULL) {
212		bsddev = devclass_get_device(dev->class->bsdclass, 0);
213	}
214
215	if (bsddev == NULL)
216		bsddev = device_add_child(dev->parent->bsddev,
217		    dev->class->kobj.name, unit);
218	if (bsddev) {
219		if (dev->devt == 0)
220			dev->devt = makedev(0, device_get_unit(bsddev));
221		device_set_softc(bsddev, dev);
222	}
223	dev->bsddev = bsddev;
224	kobject_init(&dev->kobj, &linux_dev_ktype);
225	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
226
227	return (0);
228}
229
230static inline void
231device_unregister(struct device *dev)
232{
233	device_t bsddev;
234
235	bsddev = dev->bsddev;
236	mtx_lock(&Giant);
237	if (bsddev)
238		device_delete_child(device_get_parent(bsddev), bsddev);
239	mtx_unlock(&Giant);
240	put_device(dev);
241}
242
243struct device *device_create(struct class *class, struct device *parent,
244	    dev_t devt, void *drvdata, const char *fmt, ...);
245
246static inline void
247device_destroy(struct class *class, dev_t devt)
248{
249	device_t bsddev;
250	int unit;
251
252	unit = MINOR(devt);
253	bsddev = devclass_get_device(class->bsdclass, unit);
254	if (bsddev)
255		device_unregister(device_get_softc(bsddev));
256}
257
258static inline void
259linux_class_kfree(struct class *class)
260{
261
262	kfree(class);
263}
264
265static inline struct class *
266class_create(struct module *owner, const char *name)
267{
268	struct class *class;
269	int error;
270
271	class = kzalloc(sizeof(*class), M_WAITOK);
272	class->owner = owner;
273	class->name= name;
274	class->class_release = linux_class_kfree;
275	error = class_register(class);
276	if (error) {
277		kfree(class);
278		return (NULL);
279	}
280
281	return (class);
282}
283
284static inline void
285class_destroy(struct class *class)
286{
287
288	if (class == NULL)
289		return;
290	class_unregister(class);
291}
292
293static inline int
294device_create_file(struct device *dev, const struct device_attribute *attr)
295{
296
297	if (dev)
298		return sysfs_create_file(&dev->kobj, &attr->attr);
299	return -EINVAL;
300}
301
302static inline void
303device_remove_file(struct device *dev, const struct device_attribute *attr)
304{
305
306	if (dev)
307		sysfs_remove_file(&dev->kobj, &attr->attr);
308}
309
310static inline int
311class_create_file(struct class *class, const struct class_attribute *attr)
312{
313
314	if (class)
315		return sysfs_create_file(&class->kobj, &attr->attr);
316	return -EINVAL;
317}
318
319static inline void
320class_remove_file(struct class *class, const struct class_attribute *attr)
321{
322
323	if (class)
324		sysfs_remove_file(&class->kobj, &attr->attr);
325}
326
327static inline int
328dev_to_node(struct device *dev)
329{
330                return -1;
331}
332
333char *kvasprintf(gfp_t, const char *, va_list);
334char *kasprintf(gfp_t, const char *, ...);
335
336#endif	/* _LINUX_DEVICE_H_ */
337