device.h revision 289644
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, 2014 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/ofed/include/linux/device.h 289644 2015-10-20 19:08:26Z 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/list.h>
37#include <linux/compiler.h>
38#include <linux/types.h>
39#include <linux/module.h>
40#include <linux/workqueue.h>
41#include <linux/sysfs.h>
42#include <linux/kdev_t.h>
43#include <asm/atomic.h>
44
45#include <sys/bus.h>
46
47enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
48typedef enum irqreturn	irqreturn_t;
49
50struct class {
51	const char	*name;
52	struct module	*owner;
53	struct kobject	kobj;
54	devclass_t	bsdclass;
55	void		(*class_release)(struct class *class);
56	void		(*dev_release)(struct device *dev);
57	char *		(*devnode)(struct device *dev, umode_t *mode);
58};
59
60struct device {
61	struct device	*parent;
62	struct list_head irqents;
63	device_t	bsddev;
64	dev_t		devt;
65	struct class	*class;
66	void		(*release)(struct device *dev);
67	struct kobject	kobj;
68	uint64_t	*dma_mask;
69	void		*driver_data;
70	unsigned int	irq;
71	unsigned int	msix;
72	unsigned int	msix_max;
73};
74
75extern struct device linux_rootdev;
76extern struct kobject class_root;
77
78struct class_attribute {
79        struct attribute attr;
80        ssize_t (*show)(struct class *, struct class_attribute *, char *);
81        ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
82        const void *(*namespace)(struct class *, const struct class_attribute *);
83};
84
85#define	CLASS_ATTR(_name, _mode, _show, _store)				\
86	struct class_attribute class_attr_##_name =			\
87	    { { #_name, NULL, _mode }, _show, _store }
88
89struct device_attribute {
90	struct attribute	attr;
91	ssize_t			(*show)(struct device *,
92					struct device_attribute *, char *);
93	ssize_t			(*store)(struct device *,
94					struct device_attribute *, const char *,
95					size_t);
96};
97
98#define	DEVICE_ATTR(_name, _mode, _show, _store)			\
99	struct device_attribute dev_attr_##_name =			\
100	    { { #_name, NULL, _mode }, _show, _store }
101
102/* Simple class attribute that is just a static string */
103struct class_attribute_string {
104	struct class_attribute attr;
105	char *str;
106};
107
108static inline ssize_t
109show_class_attr_string(struct class *class,
110				struct class_attribute *attr, char *buf)
111{
112	struct class_attribute_string *cs;
113	cs = container_of(attr, struct class_attribute_string, attr);
114	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
115}
116
117/* Currently read-only only */
118#define _CLASS_ATTR_STRING(_name, _mode, _str) \
119	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
120#define CLASS_ATTR_STRING(_name, _mode, _str) \
121	struct class_attribute_string class_attr_##_name = \
122		_CLASS_ATTR_STRING(_name, _mode, _str)
123
124#define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
125#define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
126#define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
127#define	dev_printk(lvl, dev, fmt, ...)					\
128	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
129
130static inline void *
131dev_get_drvdata(struct device *dev)
132{
133
134	return dev->driver_data;
135}
136
137static inline void
138dev_set_drvdata(struct device *dev, void *data)
139{
140
141	dev->driver_data = data;
142}
143
144static inline struct device *
145get_device(struct device *dev)
146{
147
148	if (dev)
149		kobject_get(&dev->kobj);
150
151	return (dev);
152}
153
154static inline char *
155dev_name(const struct device *dev)
156{
157
158 	return kobject_name(&dev->kobj);
159}
160
161#define	dev_set_name(_dev, _fmt, ...)					\
162	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
163
164static inline void
165put_device(struct device *dev)
166{
167
168	if (dev)
169		kobject_put(&dev->kobj);
170}
171
172static inline ssize_t
173class_show(struct kobject *kobj, struct attribute *attr, char *buf)
174{
175	struct class_attribute *dattr;
176	ssize_t error;
177
178	dattr = container_of(attr, struct class_attribute, attr);
179	error = -EIO;
180	if (dattr->show)
181		error = dattr->show(container_of(kobj, struct class, kobj),
182		    dattr, buf);
183	return (error);
184}
185
186static inline ssize_t
187class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
188    size_t count)
189{
190	struct class_attribute *dattr;
191	ssize_t error;
192
193	dattr = container_of(attr, struct class_attribute, attr);
194	error = -EIO;
195	if (dattr->store)
196		error = dattr->store(container_of(kobj, struct class, kobj),
197		    dattr, buf, count);
198	return (error);
199}
200
201static inline void
202class_release(struct kobject *kobj)
203{
204	struct class *class;
205
206	class = container_of(kobj, struct class, kobj);
207	if (class->class_release)
208		class->class_release(class);
209}
210
211static struct sysfs_ops class_sysfs = {
212	.show  = class_show,
213	.store = class_store,
214};
215static struct kobj_type class_ktype = {
216	.release = class_release,
217	.sysfs_ops = &class_sysfs
218};
219
220static inline int
221class_register(struct class *class)
222{
223
224	class->bsdclass = devclass_create(class->name);
225	kobject_init(&class->kobj, &class_ktype);
226	kobject_set_name(&class->kobj, class->name);
227	kobject_add(&class->kobj, &class_root, class->name);
228
229	return (0);
230}
231
232static inline void
233class_unregister(struct class *class)
234{
235
236	kobject_put(&class->kobj);
237}
238
239static inline void
240device_release(struct kobject *kobj)
241{
242	struct device *dev;
243
244	dev = container_of(kobj, struct device, kobj);
245	/* This is the precedence defined by linux. */
246	if (dev->release)
247		dev->release(dev);
248	else if (dev->class && dev->class->dev_release)
249		dev->class->dev_release(dev);
250}
251
252static inline ssize_t
253dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
254{
255	struct device_attribute *dattr;
256	ssize_t error;
257
258	dattr = container_of(attr, struct device_attribute, attr);
259	error = -EIO;
260	if (dattr->show)
261		error = dattr->show(container_of(kobj, struct device, kobj),
262		    dattr, buf);
263	return (error);
264}
265
266static inline ssize_t
267dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
268    size_t count)
269{
270	struct device_attribute *dattr;
271	ssize_t error;
272
273	dattr = container_of(attr, struct device_attribute, attr);
274	error = -EIO;
275	if (dattr->store)
276		error = dattr->store(container_of(kobj, struct device, kobj),
277		    dattr, buf, count);
278	return (error);
279}
280
281static struct sysfs_ops dev_sysfs = { .show  = dev_show, .store = dev_store, };
282static struct kobj_type dev_ktype = {
283	.release = device_release,
284	.sysfs_ops = &dev_sysfs
285};
286
287/*
288 * Devices are registered and created for exporting to sysfs.  create
289 * implies register and register assumes the device fields have been
290 * setup appropriately before being called.
291 */
292static inline int
293device_register(struct device *dev)
294{
295	device_t bsddev;
296	int unit;
297
298	bsddev = NULL;
299	if (dev->devt) {
300		unit = MINOR(dev->devt);
301		bsddev = devclass_get_device(dev->class->bsdclass, unit);
302	} else
303		unit = -1;
304	if (bsddev == NULL)
305		bsddev = device_add_child(dev->parent->bsddev,
306		    dev->class->kobj.name, unit);
307	if (bsddev) {
308		if (dev->devt == 0)
309			dev->devt = makedev(0, device_get_unit(bsddev));
310		device_set_softc(bsddev, dev);
311	}
312	dev->bsddev = bsddev;
313	kobject_init(&dev->kobj, &dev_ktype);
314	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
315
316	return (0);
317}
318
319static inline void
320device_unregister(struct device *dev)
321{
322	device_t bsddev;
323
324	bsddev = dev->bsddev;
325	mtx_lock(&Giant);
326	if (bsddev)
327		device_delete_child(device_get_parent(bsddev), bsddev);
328	mtx_unlock(&Giant);
329	put_device(dev);
330}
331
332struct device *device_create(struct class *class, struct device *parent,
333	    dev_t devt, void *drvdata, const char *fmt, ...);
334
335static inline void
336device_destroy(struct class *class, dev_t devt)
337{
338	device_t bsddev;
339	int unit;
340
341	unit = MINOR(devt);
342	bsddev = devclass_get_device(class->bsdclass, unit);
343	if (bsddev)
344		device_unregister(device_get_softc(bsddev));
345}
346
347static inline void
348class_kfree(struct class *class)
349{
350
351	kfree(class);
352}
353
354static inline struct class *
355class_create(struct module *owner, const char *name)
356{
357	struct class *class;
358	int error;
359
360	class = kzalloc(sizeof(*class), M_WAITOK);
361	class->owner = owner;
362	class->name= name;
363	class->class_release = class_kfree;
364	error = class_register(class);
365	if (error) {
366		kfree(class);
367		return (NULL);
368	}
369
370	return (class);
371}
372
373static inline void
374class_destroy(struct class *class)
375{
376
377	if (class == NULL)
378		return;
379	class_unregister(class);
380}
381
382static inline int
383device_create_file(struct device *dev, const struct device_attribute *attr)
384{
385
386	if (dev)
387		return sysfs_create_file(&dev->kobj, &attr->attr);
388	return -EINVAL;
389}
390
391static inline void
392device_remove_file(struct device *dev, const struct device_attribute *attr)
393{
394
395	if (dev)
396		sysfs_remove_file(&dev->kobj, &attr->attr);
397}
398
399static inline int
400class_create_file(struct class *class, const struct class_attribute *attr)
401{
402
403	if (class)
404		return sysfs_create_file(&class->kobj, &attr->attr);
405	return -EINVAL;
406}
407
408static inline void
409class_remove_file(struct class *class, const struct class_attribute *attr)
410{
411
412	if (class)
413		sysfs_remove_file(&class->kobj, &attr->attr);
414}
415
416static inline int dev_to_node(struct device *dev)
417{
418                return -1;
419}
420
421char *kvasprintf(gfp_t, const char *, va_list);
422char *kasprintf(gfp_t, const char *, ...);
423
424#endif	/* _LINUX_DEVICE_H_ */
425