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