xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision a580b014940f7e913a7b12c1169208a4501ddfcb)
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$
30  */
31 #ifndef	_LINUX_DEVICE_H_
32 #define	_LINUX_DEVICE_H_
33 
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/list.h>
39 #include <linux/compiler.h>
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/workqueue.h>
43 #include <linux/sysfs.h>
44 #include <linux/kdev_t.h>
45 #include <asm/atomic.h>
46 
47 #include <sys/bus.h>
48 
49 enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
50 typedef enum irqreturn	irqreturn_t;
51 
52 struct device;
53 
54 struct class {
55 	const char	*name;
56 	struct module	*owner;
57 	struct kobject	kobj;
58 	devclass_t	bsdclass;
59 	void		(*class_release)(struct class *class);
60 	void		(*dev_release)(struct device *dev);
61 	char *		(*devnode)(struct device *dev, umode_t *mode);
62 };
63 
64 struct dev_pm_ops {
65 	int (*suspend)(struct device *dev);
66 	int (*suspend_late)(struct device *dev);
67 	int (*resume)(struct device *dev);
68 	int (*resume_early)(struct device *dev);
69 	int (*freeze)(struct device *dev);
70 	int (*freeze_late)(struct device *dev);
71 	int (*thaw)(struct device *dev);
72 	int (*thaw_early)(struct device *dev);
73 	int (*poweroff)(struct device *dev);
74 	int (*poweroff_late)(struct device *dev);
75 	int (*restore)(struct device *dev);
76 	int (*restore_early)(struct device *dev);
77 	int (*runtime_suspend)(struct device *dev);
78 	int (*runtime_resume)(struct device *dev);
79 	int (*runtime_idle)(struct device *dev);
80 };
81 
82 struct device {
83 	struct device	*parent;
84 	struct list_head irqents;
85 	device_t	bsddev;
86 	/*
87 	 * The following flag is used to determine if the LinuxKPI is
88 	 * responsible for detaching the BSD device or not. If the
89 	 * LinuxKPI got the BSD device using devclass_get_device(), it
90 	 * must not try to detach or delete it, because it's already
91 	 * done somewhere else.
92 	 */
93 	bool		bsddev_attached_here;
94 	dev_t		devt;
95 	struct class	*class;
96 	void		(*release)(struct device *dev);
97 	struct kobject	kobj;
98 	uint64_t	*dma_mask;
99 	void		*driver_data;
100 	unsigned int	irq;
101 #define	LINUX_IRQ_INVALID	65535
102 	unsigned int	msix;
103 	unsigned int	msix_max;
104 	const struct attribute_group **groups;
105 };
106 
107 extern struct device linux_root_device;
108 extern struct kobject linux_class_root;
109 extern const struct kobj_type linux_dev_ktype;
110 extern const struct kobj_type linux_class_ktype;
111 
112 struct class_attribute {
113         struct attribute attr;
114         ssize_t (*show)(struct class *, struct class_attribute *, char *);
115         ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
116         const void *(*namespace)(struct class *, const struct class_attribute *);
117 };
118 
119 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
120 	struct class_attribute class_attr_##_name =			\
121 	    { { #_name, NULL, _mode }, _show, _store }
122 
123 struct device_attribute {
124 	struct attribute	attr;
125 	ssize_t			(*show)(struct device *,
126 					struct device_attribute *, char *);
127 	ssize_t			(*store)(struct device *,
128 					struct device_attribute *, const char *,
129 					size_t);
130 };
131 
132 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
133 	struct device_attribute dev_attr_##_name =			\
134 	    { { #_name, NULL, _mode }, _show, _store }
135 
136 /* Simple class attribute that is just a static string */
137 struct class_attribute_string {
138 	struct class_attribute attr;
139 	char *str;
140 };
141 
142 static inline ssize_t
143 show_class_attr_string(struct class *class,
144 				struct class_attribute *attr, char *buf)
145 {
146 	struct class_attribute_string *cs;
147 	cs = container_of(attr, struct class_attribute_string, attr);
148 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
149 }
150 
151 /* Currently read-only only */
152 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
153 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
154 #define CLASS_ATTR_STRING(_name, _mode, _str) \
155 	struct class_attribute_string class_attr_##_name = \
156 		_CLASS_ATTR_STRING(_name, _mode, _str)
157 
158 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
159 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
160 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
161 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
162 #define	dev_printk(lvl, dev, fmt, ...)					\
163 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
164 
165 #define	dev_err_ratelimited(dev, ...) do {	\
166 	static linux_ratelimit_t __ratelimited;	\
167 	if (linux_ratelimited(&__ratelimited))	\
168 		dev_err(dev, __VA_ARGS__);	\
169 } while (0)
170 
171 #define	dev_warn_ratelimited(dev, ...) do {	\
172 	static linux_ratelimit_t __ratelimited;	\
173 	if (linux_ratelimited(&__ratelimited))	\
174 		dev_warn(dev, __VA_ARGS__);	\
175 } while (0)
176 
177 static inline void *
178 dev_get_drvdata(const struct device *dev)
179 {
180 
181 	return dev->driver_data;
182 }
183 
184 static inline void
185 dev_set_drvdata(struct device *dev, void *data)
186 {
187 
188 	dev->driver_data = data;
189 }
190 
191 static inline struct device *
192 get_device(struct device *dev)
193 {
194 
195 	if (dev)
196 		kobject_get(&dev->kobj);
197 
198 	return (dev);
199 }
200 
201 static inline char *
202 dev_name(const struct device *dev)
203 {
204 
205  	return kobject_name(&dev->kobj);
206 }
207 
208 #define	dev_set_name(_dev, _fmt, ...)					\
209 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
210 
211 static inline void
212 put_device(struct device *dev)
213 {
214 
215 	if (dev)
216 		kobject_put(&dev->kobj);
217 }
218 
219 static inline int
220 class_register(struct class *class)
221 {
222 
223 	class->bsdclass = devclass_create(class->name);
224 	kobject_init(&class->kobj, &linux_class_ktype);
225 	kobject_set_name(&class->kobj, class->name);
226 	kobject_add(&class->kobj, &linux_class_root, class->name);
227 
228 	return (0);
229 }
230 
231 static inline void
232 class_unregister(struct class *class)
233 {
234 
235 	kobject_put(&class->kobj);
236 }
237 
238 static inline struct device *kobj_to_dev(struct kobject *kobj)
239 {
240 	return container_of(kobj, struct device, kobj);
241 }
242 
243 /*
244  * Devices are registered and created for exporting to sysfs. Create
245  * implies register and register assumes the device fields have been
246  * setup appropriately before being called.
247  */
248 static inline void
249 device_initialize(struct device *dev)
250 {
251 	device_t bsddev = NULL;
252 	int unit = -1;
253 
254 	if (dev->devt) {
255 		unit = MINOR(dev->devt);
256 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
257 		dev->bsddev_attached_here = false;
258 	} else if (dev->parent == NULL) {
259 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
260 		dev->bsddev_attached_here = false;
261 	} else {
262 		dev->bsddev_attached_here = true;
263 	}
264 
265 	if (bsddev == NULL && dev->parent != NULL) {
266 		bsddev = device_add_child(dev->parent->bsddev,
267 		    dev->class->kobj.name, unit);
268 	}
269 
270 	if (bsddev != NULL)
271 		device_set_softc(bsddev, dev);
272 
273 	dev->bsddev = bsddev;
274 	MPASS(dev->bsddev != NULL);
275 	kobject_init(&dev->kobj, &linux_dev_ktype);
276 }
277 
278 static inline int
279 device_add(struct device *dev)
280 {
281 	if (dev->bsddev != NULL) {
282 		if (dev->devt == 0)
283 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
284 	}
285 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
286 	return (0);
287 }
288 
289 static inline void
290 device_create_release(struct device *dev)
291 {
292 	kfree(dev);
293 }
294 
295 static inline struct device *
296 device_create_groups_vargs(struct class *class, struct device *parent,
297     dev_t devt, void *drvdata, const struct attribute_group **groups,
298     const char *fmt, va_list args)
299 {
300 	struct device *dev = NULL;
301 	int retval = -ENODEV;
302 
303 	if (class == NULL || IS_ERR(class))
304 		goto error;
305 
306 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
307 	if (!dev) {
308 		retval = -ENOMEM;
309 		goto error;
310 	}
311 
312 	dev->devt = devt;
313 	dev->class = class;
314 	dev->parent = parent;
315 	dev->groups = groups;
316 	dev->release = device_create_release;
317 	/* device_initialize() needs the class and parent to be set */
318 	device_initialize(dev);
319 	dev_set_drvdata(dev, drvdata);
320 
321 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
322 	if (retval)
323 		goto error;
324 
325 	retval = device_add(dev);
326 	if (retval)
327 		goto error;
328 
329 	return dev;
330 
331 error:
332 	put_device(dev);
333 	return ERR_PTR(retval);
334 }
335 
336 static inline struct device *
337 device_create_with_groups(struct class *class,
338     struct device *parent, dev_t devt, void *drvdata,
339     const struct attribute_group **groups, const char *fmt, ...)
340 {
341 	va_list vargs;
342 	struct device *dev;
343 
344 	va_start(vargs, fmt);
345 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
346 	    groups, fmt, vargs);
347 	va_end(vargs);
348 	return dev;
349 }
350 
351 static inline int
352 device_register(struct device *dev)
353 {
354 	device_t bsddev = NULL;
355 	int unit = -1;
356 
357 	if (dev->bsddev != NULL)
358 		goto done;
359 
360 	if (dev->devt) {
361 		unit = MINOR(dev->devt);
362 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
363 		dev->bsddev_attached_here = false;
364 	} else if (dev->parent == NULL) {
365 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
366 		dev->bsddev_attached_here = false;
367 	} else {
368 		dev->bsddev_attached_here = true;
369 	}
370 	if (bsddev == NULL && dev->parent != NULL) {
371 		bsddev = device_add_child(dev->parent->bsddev,
372 		    dev->class->kobj.name, unit);
373 	}
374 	if (bsddev != NULL) {
375 		if (dev->devt == 0)
376 			dev->devt = makedev(0, device_get_unit(bsddev));
377 		device_set_softc(bsddev, dev);
378 	}
379 	dev->bsddev = bsddev;
380 done:
381 	kobject_init(&dev->kobj, &linux_dev_ktype);
382 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
383 
384 	return (0);
385 }
386 
387 static inline void
388 device_unregister(struct device *dev)
389 {
390 	device_t bsddev;
391 
392 	bsddev = dev->bsddev;
393 	dev->bsddev = NULL;
394 
395 	if (bsddev != NULL && dev->bsddev_attached_here) {
396 		mtx_lock(&Giant);
397 		device_delete_child(device_get_parent(bsddev), bsddev);
398 		mtx_unlock(&Giant);
399 	}
400 	put_device(dev);
401 }
402 
403 static inline void
404 device_del(struct device *dev)
405 {
406 	device_t bsddev;
407 
408 	bsddev = dev->bsddev;
409 	dev->bsddev = NULL;
410 
411 	if (bsddev != NULL && dev->bsddev_attached_here) {
412 		mtx_lock(&Giant);
413 		device_delete_child(device_get_parent(bsddev), bsddev);
414 		mtx_unlock(&Giant);
415 	}
416 }
417 
418 struct device *device_create(struct class *class, struct device *parent,
419 	    dev_t devt, void *drvdata, const char *fmt, ...);
420 
421 static inline void
422 device_destroy(struct class *class, dev_t devt)
423 {
424 	device_t bsddev;
425 	int unit;
426 
427 	unit = MINOR(devt);
428 	bsddev = devclass_get_device(class->bsdclass, unit);
429 	if (bsddev != NULL)
430 		device_unregister(device_get_softc(bsddev));
431 }
432 
433 static inline void
434 linux_class_kfree(struct class *class)
435 {
436 
437 	kfree(class);
438 }
439 
440 static inline struct class *
441 class_create(struct module *owner, const char *name)
442 {
443 	struct class *class;
444 	int error;
445 
446 	class = kzalloc(sizeof(*class), M_WAITOK);
447 	class->owner = owner;
448 	class->name = name;
449 	class->class_release = linux_class_kfree;
450 	error = class_register(class);
451 	if (error) {
452 		kfree(class);
453 		return (NULL);
454 	}
455 
456 	return (class);
457 }
458 
459 static inline void
460 class_destroy(struct class *class)
461 {
462 
463 	if (class == NULL)
464 		return;
465 	class_unregister(class);
466 }
467 
468 static inline int
469 device_create_file(struct device *dev, const struct device_attribute *attr)
470 {
471 
472 	if (dev)
473 		return sysfs_create_file(&dev->kobj, &attr->attr);
474 	return -EINVAL;
475 }
476 
477 static inline void
478 device_remove_file(struct device *dev, const struct device_attribute *attr)
479 {
480 
481 	if (dev)
482 		sysfs_remove_file(&dev->kobj, &attr->attr);
483 }
484 
485 static inline int
486 class_create_file(struct class *class, const struct class_attribute *attr)
487 {
488 
489 	if (class)
490 		return sysfs_create_file(&class->kobj, &attr->attr);
491 	return -EINVAL;
492 }
493 
494 static inline void
495 class_remove_file(struct class *class, const struct class_attribute *attr)
496 {
497 
498 	if (class)
499 		sysfs_remove_file(&class->kobj, &attr->attr);
500 }
501 
502 static inline int
503 dev_to_node(struct device *dev)
504 {
505                 return -1;
506 }
507 
508 char *kvasprintf(gfp_t, const char *, va_list);
509 char *kasprintf(gfp_t, const char *, ...);
510 
511 #endif	/* _LINUX_DEVICE_H_ */
512