xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision d819ddba9aa458f6e91df059426d9a2f1ac0b92d)
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	_LINUXKPI_LINUX_DEVICE_H_
32 #define	_LINUXKPI_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/module.h>
41 #include <linux/workqueue.h>
42 #include <linux/kdev_t.h>
43 #include <linux/backlight.h>
44 #include <linux/pm.h>
45 #include <linux/idr.h>
46 #include <linux/ratelimit.h>	/* via linux/dev_printk.h */
47 #include <asm/atomic.h>
48 
49 #include <sys/bus.h>
50 #include <sys/backlight.h>
51 
52 struct device;
53 struct fwnode_handle;
54 
55 struct class {
56 	const char	*name;
57 	struct module	*owner;
58 	struct kobject	kobj;
59 	devclass_t	bsdclass;
60 	const struct dev_pm_ops *pm;
61 	const struct attribute_group **dev_groups;
62 	void		(*class_release)(struct class *class);
63 	void		(*dev_release)(struct device *dev);
64 	char *		(*devnode)(struct device *dev, umode_t *mode);
65 };
66 
67 struct dev_pm_ops {
68 	int (*prepare)(struct device *dev);
69 	int (*suspend)(struct device *dev);
70 	int (*suspend_late)(struct device *dev);
71 	int (*resume)(struct device *dev);
72 	int (*resume_early)(struct device *dev);
73 	int (*freeze)(struct device *dev);
74 	int (*freeze_late)(struct device *dev);
75 	int (*thaw)(struct device *dev);
76 	int (*thaw_early)(struct device *dev);
77 	int (*poweroff)(struct device *dev);
78 	int (*poweroff_late)(struct device *dev);
79 	int (*restore)(struct device *dev);
80 	int (*restore_early)(struct device *dev);
81 	int (*runtime_suspend)(struct device *dev);
82 	int (*runtime_resume)(struct device *dev);
83 	int (*runtime_idle)(struct device *dev);
84 };
85 
86 struct device_driver {
87 	const char	*name;
88 	const struct dev_pm_ops *pm;
89 };
90 
91 struct device_type {
92 	const char	*name;
93 };
94 
95 struct device {
96 	struct device	*parent;
97 	struct list_head irqents;
98 	device_t	bsddev;
99 	/*
100 	 * The following flag is used to determine if the LinuxKPI is
101 	 * responsible for detaching the BSD device or not. If the
102 	 * LinuxKPI got the BSD device using devclass_get_device(), it
103 	 * must not try to detach or delete it, because it's already
104 	 * done somewhere else.
105 	 */
106 	bool		bsddev_attached_here;
107 	struct device_driver *driver;
108 	struct device_type *type;
109 	dev_t		devt;
110 	struct class	*class;
111 	void		(*release)(struct device *dev);
112 	struct kobject	kobj;
113 	void		*dma_priv;
114 	void		*driver_data;
115 	unsigned int	irq;
116 #define	LINUX_IRQ_INVALID	65535
117 	unsigned int	irq_start;
118 	unsigned int	irq_end;
119 	const struct attribute_group **groups;
120 	struct fwnode_handle *fwnode;
121 	struct cdev	*backlight_dev;
122 	struct backlight_device	*bd;
123 
124 	spinlock_t	devres_lock;
125 	struct list_head devres_head;
126 };
127 
128 extern struct device linux_root_device;
129 extern struct kobject linux_class_root;
130 extern const struct kobj_type linux_dev_ktype;
131 extern const struct kobj_type linux_class_ktype;
132 
133 struct class_attribute {
134 	struct attribute attr;
135 	ssize_t (*show)(struct class *, struct class_attribute *, char *);
136 	ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
137 	const void *(*namespace)(struct class *, const struct class_attribute *);
138 };
139 
140 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
141 	struct class_attribute class_attr_##_name =			\
142 	    { { #_name, NULL, _mode }, _show, _store }
143 
144 struct device_attribute {
145 	struct attribute	attr;
146 	ssize_t			(*show)(struct device *,
147 					struct device_attribute *, char *);
148 	ssize_t			(*store)(struct device *,
149 					struct device_attribute *, const char *,
150 					size_t);
151 };
152 
153 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
154 	struct device_attribute dev_attr_##_name =			\
155 	    __ATTR(_name, _mode, _show, _store)
156 #define	DEVICE_ATTR_RO(_name)						\
157 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
158 #define	DEVICE_ATTR_WO(_name)						\
159 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
160 #define	DEVICE_ATTR_RW(_name)						\
161 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
162 
163 /* Simple class attribute that is just a static string */
164 struct class_attribute_string {
165 	struct class_attribute attr;
166 	char *str;
167 };
168 
169 static inline ssize_t
170 show_class_attr_string(struct class *class,
171 				struct class_attribute *attr, char *buf)
172 {
173 	struct class_attribute_string *cs;
174 	cs = container_of(attr, struct class_attribute_string, attr);
175 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
176 }
177 
178 /* Currently read-only only */
179 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
180 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
181 #define CLASS_ATTR_STRING(_name, _mode, _str) \
182 	struct class_attribute_string class_attr_##_name = \
183 		_CLASS_ATTR_STRING(_name, _mode, _str)
184 
185 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186 #define	dev_crit(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
188 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
189 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
190 #define	dev_emerg(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
191 #define	dev_dbg(dev, fmt, ...)	do { } while (0)
192 #define	dev_printk(lvl, dev, fmt, ...)					\
193 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
194 
195 #define dev_info_once(dev, ...) do {		\
196 	static bool __dev_info_once;		\
197 	if (!__dev_info_once) {			\
198 	__dev_info_once = true;			\
199 	dev_info(dev, __VA_ARGS__);		\
200 	}					\
201 } while (0)
202 
203 #define	dev_err_once(dev, ...) do {		\
204 	static bool __dev_err_once;		\
205 	if (!__dev_err_once) {			\
206 		__dev_err_once = 1;		\
207 		dev_err(dev, __VA_ARGS__);	\
208 	}					\
209 } while (0)
210 
211 #define	dev_err_ratelimited(dev, ...) do {	\
212 	static linux_ratelimit_t __ratelimited;	\
213 	if (linux_ratelimited(&__ratelimited))	\
214 		dev_err(dev, __VA_ARGS__);	\
215 } while (0)
216 
217 #define	dev_warn_ratelimited(dev, ...) do {	\
218 	static linux_ratelimit_t __ratelimited;	\
219 	if (linux_ratelimited(&__ratelimited))	\
220 		dev_warn(dev, __VA_ARGS__);	\
221 } while (0)
222 
223 /* Public and LinuxKPI internal devres functions. */
224 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);
225 void lkpi_devres_add(struct device *, void *);
226 void lkpi_devres_free(void *);
227 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),
228     int (*match)(struct device *, void *, void *), void *);
229 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),
230     int (*match)(struct device *, void *, void *), void *);
231 #define	devres_alloc(_r, _s, _g)	lkpi_devres_alloc(_r, _s, _g)
232 #define	devres_add(_d, _p)		lkpi_devres_add(_d, _p)
233 #define	devres_free(_p)			lkpi_devres_free(_p)
234 #define	devres_find(_d, _rfn, _mfn, _mp) \
235 					lkpi_devres_find(_d, _rfn, _mfn, _mp)
236 #define	devres_destroy(_d, _rfn, _mfn, _mp) \
237 					lkpi_devres_destroy(_d, _rfn, _mfn, _mp)
238 void lkpi_devres_release_free_list(struct device *);
239 void lkpi_devres_unlink(struct device *, void *);
240 void lkpi_devm_kmalloc_release(struct device *, void *);
241 
242 static inline const char *
243 dev_driver_string(const struct device *dev)
244 {
245 	driver_t *drv;
246 	const char *str = "";
247 
248 	if (dev->bsddev != NULL) {
249 		drv = device_get_driver(dev->bsddev);
250 		if (drv != NULL)
251 			str = drv->name;
252 	}
253 
254 	return (str);
255 }
256 
257 static inline void *
258 dev_get_drvdata(const struct device *dev)
259 {
260 
261 	return dev->driver_data;
262 }
263 
264 static inline void
265 dev_set_drvdata(struct device *dev, void *data)
266 {
267 
268 	dev->driver_data = data;
269 }
270 
271 static inline struct device *
272 get_device(struct device *dev)
273 {
274 
275 	if (dev)
276 		kobject_get(&dev->kobj);
277 
278 	return (dev);
279 }
280 
281 static inline char *
282 dev_name(const struct device *dev)
283 {
284 
285 	return kobject_name(&dev->kobj);
286 }
287 
288 #define	dev_set_name(_dev, _fmt, ...)					\
289 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
290 
291 static inline void
292 put_device(struct device *dev)
293 {
294 
295 	if (dev)
296 		kobject_put(&dev->kobj);
297 }
298 
299 struct class *class_create(struct module *owner, const char *name);
300 
301 static inline int
302 class_register(struct class *class)
303 {
304 
305 	class->bsdclass = devclass_create(class->name);
306 	kobject_init(&class->kobj, &linux_class_ktype);
307 	kobject_set_name(&class->kobj, class->name);
308 	kobject_add(&class->kobj, &linux_class_root, class->name);
309 
310 	return (0);
311 }
312 
313 static inline void
314 class_unregister(struct class *class)
315 {
316 
317 	kobject_put(&class->kobj);
318 }
319 
320 static inline struct device *kobj_to_dev(struct kobject *kobj)
321 {
322 	return container_of(kobj, struct device, kobj);
323 }
324 
325 struct device *device_create(struct class *class, struct device *parent,
326 	    dev_t devt, void *drvdata, const char *fmt, ...);
327 struct device *device_create_groups_vargs(struct class *class, struct device *parent,
328     dev_t devt, void *drvdata, const struct attribute_group **groups,
329     const char *fmt, va_list args);
330 
331 /*
332  * Devices are registered and created for exporting to sysfs. Create
333  * implies register and register assumes the device fields have been
334  * setup appropriately before being called.
335  */
336 static inline void
337 device_initialize(struct device *dev)
338 {
339 	device_t bsddev = NULL;
340 	int unit = -1;
341 
342 	if (dev->devt) {
343 		unit = MINOR(dev->devt);
344 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
345 		dev->bsddev_attached_here = false;
346 	} else if (dev->parent == NULL) {
347 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
348 		dev->bsddev_attached_here = false;
349 	} else {
350 		dev->bsddev_attached_here = true;
351 	}
352 
353 	if (bsddev == NULL && dev->parent != NULL) {
354 		bsddev = device_add_child(dev->parent->bsddev,
355 		    dev->class->kobj.name, unit);
356 	}
357 
358 	if (bsddev != NULL)
359 		device_set_softc(bsddev, dev);
360 
361 	dev->bsddev = bsddev;
362 	MPASS(dev->bsddev != NULL);
363 	kobject_init(&dev->kobj, &linux_dev_ktype);
364 
365 	spin_lock_init(&dev->devres_lock);
366 	INIT_LIST_HEAD(&dev->devres_head);
367 }
368 
369 static inline int
370 device_add(struct device *dev)
371 {
372 	if (dev->bsddev != NULL) {
373 		if (dev->devt == 0)
374 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
375 	}
376 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
377 
378 	if (dev->groups)
379 		return (sysfs_create_groups(&dev->kobj, dev->groups));
380 
381 	return (0);
382 }
383 
384 static inline void
385 device_create_release(struct device *dev)
386 {
387 	kfree(dev);
388 }
389 
390 static inline struct device *
391 device_create_with_groups(struct class *class,
392     struct device *parent, dev_t devt, void *drvdata,
393     const struct attribute_group **groups, const char *fmt, ...)
394 {
395 	va_list vargs;
396 	struct device *dev;
397 
398 	va_start(vargs, fmt);
399 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
400 	    groups, fmt, vargs);
401 	va_end(vargs);
402 	return dev;
403 }
404 
405 static inline bool
406 device_is_registered(struct device *dev)
407 {
408 
409 	return (dev->bsddev != NULL);
410 }
411 
412 static inline int
413 device_register(struct device *dev)
414 {
415 	device_t bsddev = NULL;
416 	int unit = -1;
417 
418 	if (device_is_registered(dev))
419 		goto done;
420 
421 	if (dev->devt) {
422 		unit = MINOR(dev->devt);
423 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
424 		dev->bsddev_attached_here = false;
425 	} else if (dev->parent == NULL) {
426 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
427 		dev->bsddev_attached_here = false;
428 	} else {
429 		dev->bsddev_attached_here = true;
430 	}
431 	if (bsddev == NULL && dev->parent != NULL) {
432 		bsddev = device_add_child(dev->parent->bsddev,
433 		    dev->class->kobj.name, unit);
434 	}
435 	if (bsddev != NULL) {
436 		if (dev->devt == 0)
437 			dev->devt = makedev(0, device_get_unit(bsddev));
438 		device_set_softc(bsddev, dev);
439 	}
440 	dev->bsddev = bsddev;
441 done:
442 	kobject_init(&dev->kobj, &linux_dev_ktype);
443 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
444 
445 	sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
446 
447 	return (0);
448 }
449 
450 static inline void
451 device_unregister(struct device *dev)
452 {
453 	device_t bsddev;
454 
455 	sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
456 
457 	bsddev = dev->bsddev;
458 	dev->bsddev = NULL;
459 
460 	if (bsddev != NULL && dev->bsddev_attached_here) {
461 		bus_topo_lock();
462 		device_delete_child(device_get_parent(bsddev), bsddev);
463 		bus_topo_unlock();
464 	}
465 	put_device(dev);
466 }
467 
468 static inline void
469 device_del(struct device *dev)
470 {
471 	device_t bsddev;
472 
473 	bsddev = dev->bsddev;
474 	dev->bsddev = NULL;
475 
476 	if (bsddev != NULL && dev->bsddev_attached_here) {
477 		bus_topo_lock();
478 		device_delete_child(device_get_parent(bsddev), bsddev);
479 		bus_topo_unlock();
480 	}
481 }
482 
483 static inline void
484 device_destroy(struct class *class, dev_t devt)
485 {
486 	device_t bsddev;
487 	int unit;
488 
489 	unit = MINOR(devt);
490 	bsddev = devclass_get_device(class->bsdclass, unit);
491 	if (bsddev != NULL)
492 		device_unregister(device_get_softc(bsddev));
493 }
494 
495 static inline void
496 device_release_driver(struct device *dev)
497 {
498 
499 #if 0
500 	/* This leads to panics. Disable temporarily. Keep to rework. */
501 
502 	/* We also need to cleanup LinuxKPI bits. What else? */
503 	lkpi_devres_release_free_list(dev);
504 	dev_set_drvdata(dev, NULL);
505 	/* Do not call dev->release! */
506 
507 	bus_topo_lock();
508 	if (device_is_attached(dev->bsddev))
509 		device_detach(dev->bsddev);
510 	bus_topo_unlock();
511 #endif
512 }
513 
514 static inline int
515 device_reprobe(struct device *dev)
516 {
517 	int error;
518 
519 	device_release_driver(dev);
520 	bus_topo_lock();
521 	error = device_probe_and_attach(dev->bsddev);
522 	bus_topo_unlock();
523 
524 	return (-error);
525 }
526 
527 #define	dev_pm_set_driver_flags(dev, flags) do { \
528 } while (0)
529 
530 static inline void
531 linux_class_kfree(struct class *class)
532 {
533 
534 	kfree(class);
535 }
536 
537 static inline void
538 class_destroy(struct class *class)
539 {
540 
541 	if (class == NULL)
542 		return;
543 	class_unregister(class);
544 }
545 
546 static inline int
547 device_create_file(struct device *dev, const struct device_attribute *attr)
548 {
549 
550 	if (dev)
551 		return sysfs_create_file(&dev->kobj, &attr->attr);
552 	return -EINVAL;
553 }
554 
555 static inline void
556 device_remove_file(struct device *dev, const struct device_attribute *attr)
557 {
558 
559 	if (dev)
560 		sysfs_remove_file(&dev->kobj, &attr->attr);
561 }
562 
563 static inline int
564 class_create_file(struct class *class, const struct class_attribute *attr)
565 {
566 
567 	if (class)
568 		return sysfs_create_file(&class->kobj, &attr->attr);
569 	return -EINVAL;
570 }
571 
572 static inline void
573 class_remove_file(struct class *class, const struct class_attribute *attr)
574 {
575 
576 	if (class)
577 		sysfs_remove_file(&class->kobj, &attr->attr);
578 }
579 
580 #define	dev_to_node(dev) linux_dev_to_node(dev)
581 #define	of_node_to_nid(node) -1
582 int linux_dev_to_node(struct device *);
583 
584 char *kvasprintf(gfp_t, const char *, va_list);
585 char *kasprintf(gfp_t, const char *, ...);
586 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);
587 
588 #define	devm_kasprintf(_dev, _gfp, _fmt, ...)			\
589     lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)
590 
591 static __inline void *
592 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
593 {
594 	void *p;
595 
596 	p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);
597 	if (p != NULL)
598 		lkpi_devres_add(dev, p);
599 
600 	return (p);
601 }
602 
603 #define	devm_kzalloc(_dev, _size, _gfp)				\
604     devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)
605 
606 #define	devm_kcalloc(_dev, _sizen, _size, _gfp)			\
607     devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)
608 
609 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
610