Path: blob/main/sys/compat/linuxkpi/common/include/linux/device.h
39604 views
/*-1* Copyright (c) 2010 Isilon Systems, Inc.2* Copyright (c) 2010 iX Systems, Inc.3* Copyright (c) 2010 Panasas, Inc.4* Copyright (c) 2013-2016 Mellanox Technologies, Ltd.5* All rights reserved.6* Copyright (c) 2021-2025 The FreeBSD Foundation7*8* Portions of this software were developed by Björn Zeeb9* under sponsorship from the FreeBSD Foundation.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice unmodified, this list of conditions, and the following16* disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR22* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES23* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.24* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,25* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT26* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF30* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/32#ifndef _LINUXKPI_LINUX_DEVICE_H_33#define _LINUXKPI_LINUX_DEVICE_H_3435#include <linux/err.h>36#include <linux/types.h>37#include <linux/kobject.h>38#include <linux/sysfs.h>39#include <linux/list.h>40#include <linux/compiler.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/overflow.h>48#include <linux/ratelimit.h> /* via linux/dev_printk.h */49#include <linux/fwnode.h>50#include <asm/atomic.h>5152#include <sys/bus.h>53#include <sys/backlight.h>5455struct device;5657struct class {58const char *name;59struct kobject kobj;60devclass_t bsdclass;61const struct dev_pm_ops *pm;62const struct attribute_group **dev_groups;63void (*class_release)(struct class *class);64void (*dev_release)(struct device *dev);65char * (*devnode)(struct device *dev, umode_t *mode);66};6768struct dev_pm_ops {69int (*prepare)(struct device *dev);70void (*complete)(struct device *dev);71int (*suspend)(struct device *dev);72int (*suspend_late)(struct device *dev);73int (*resume)(struct device *dev);74int (*resume_early)(struct device *dev);75int (*freeze)(struct device *dev);76int (*freeze_late)(struct device *dev);77int (*thaw)(struct device *dev);78int (*thaw_early)(struct device *dev);79int (*poweroff)(struct device *dev);80int (*poweroff_late)(struct device *dev);81int (*restore)(struct device *dev);82int (*restore_early)(struct device *dev);83int (*suspend_noirq)(struct device *dev);84int (*runtime_suspend)(struct device *dev);85int (*runtime_resume)(struct device *dev);86int (*runtime_idle)(struct device *dev);87};8889struct device_driver {90const char *name;91const struct dev_pm_ops *pm;9293void (*shutdown) (struct device *);94};9596struct device_type {97const char *name;98};99100struct device {101struct device *parent;102struct list_head irqents;103device_t bsddev;104/*105* The following flag is used to determine if the LinuxKPI is106* responsible for detaching the BSD device or not. If the107* LinuxKPI got the BSD device using devclass_get_device(), it108* must not try to detach or delete it, because it's already109* done somewhere else.110*/111bool bsddev_attached_here;112struct device_driver *driver;113struct device_type *type;114dev_t devt;115struct class *class;116void (*release)(struct device *dev);117struct kobject kobj;118void *dma_priv;119void *driver_data;120unsigned int irq;121#define LINUX_IRQ_INVALID 65535122unsigned int irq_start;123unsigned int irq_end;124const struct attribute_group **groups;125struct fwnode_handle *fwnode;126struct cdev *backlight_dev;127struct backlight_device *bd;128129spinlock_t devres_lock;130struct list_head devres_head;131132struct dev_pm_info power;133};134135extern struct device linux_root_device;136extern struct kobject linux_class_root;137extern const struct kobj_type linux_dev_ktype;138extern const struct kobj_type linux_class_ktype;139140struct class_attribute {141struct attribute attr;142ssize_t (*show)(struct class *, struct class_attribute *, char *);143ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);144const void *(*namespace)(struct class *, const struct class_attribute *);145};146147#define CLASS_ATTR(_name, _mode, _show, _store) \148struct class_attribute class_attr_##_name = \149{ { #_name, NULL, _mode }, _show, _store }150151struct device_attribute {152struct attribute attr;153ssize_t (*show)(struct device *,154struct device_attribute *, char *);155ssize_t (*store)(struct device *,156struct device_attribute *, const char *,157size_t);158};159160#define DEVICE_ATTR(_name, _mode, _show, _store) \161struct device_attribute dev_attr_##_name = \162__ATTR(_name, _mode, _show, _store)163#define DEVICE_ATTR_RO(_name) \164struct device_attribute dev_attr_##_name = __ATTR_RO(_name)165#define DEVICE_ATTR_WO(_name) \166struct device_attribute dev_attr_##_name = __ATTR_WO(_name)167#define DEVICE_ATTR_RW(_name) \168struct device_attribute dev_attr_##_name = __ATTR_RW(_name)169170/* Simple class attribute that is just a static string */171struct class_attribute_string {172struct class_attribute attr;173char *str;174};175176static inline ssize_t177show_class_attr_string(struct class *class,178struct class_attribute *attr, char *buf)179{180struct class_attribute_string *cs;181cs = container_of(attr, struct class_attribute_string, attr);182return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);183}184185/* Currently read-only only */186#define _CLASS_ATTR_STRING(_name, _mode, _str) \187{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }188#define CLASS_ATTR_STRING(_name, _mode, _str) \189struct class_attribute_string class_attr_##_name = \190_CLASS_ATTR_STRING(_name, _mode, _str)191192#define dev_printk(lvl, dev, fmt, ...) \193device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)194195#define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)196#define dev_alert(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)197#define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)198#define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)199#define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)200#define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)201#define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)202#define dev_dbg(dev, fmt, ...) do { } while (0)203204#define dev_WARN(dev, fmt, ...) \205device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__)206207#define dev_WARN_ONCE(dev, condition, fmt, ...) do { \208static bool __dev_WARN_ONCE; \209bool __ret_warn_on = (condition); \210if (unlikely(__ret_warn_on)) { \211if (!__dev_WARN_ONCE) { \212__dev_WARN_ONCE = true; \213device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \214} \215} \216} while (0)217218#define dev_info_once(dev, ...) do { \219static bool __dev_info_once; \220if (!__dev_info_once) { \221__dev_info_once = true; \222dev_info(dev, __VA_ARGS__); \223} \224} while (0)225226#define dev_warn_once(dev, ...) do { \227static bool __dev_warn_once; \228if (!__dev_warn_once) { \229__dev_warn_once = 1; \230dev_warn(dev, __VA_ARGS__); \231} \232} while (0)233234#define dev_err_once(dev, ...) do { \235static bool __dev_err_once; \236if (!__dev_err_once) { \237__dev_err_once = 1; \238dev_err(dev, __VA_ARGS__); \239} \240} while (0)241242#define dev_dbg_once(dev, ...) do { \243static bool __dev_dbg_once; \244if (!__dev_dbg_once) { \245__dev_dbg_once = 1; \246dev_dbg(dev, __VA_ARGS__); \247} \248} while (0)249250#define dev_err_ratelimited(dev, ...) do { \251static linux_ratelimit_t __ratelimited; \252if (linux_ratelimited(&__ratelimited)) \253dev_err(dev, __VA_ARGS__); \254} while (0)255256#define dev_warn_ratelimited(dev, ...) do { \257static linux_ratelimit_t __ratelimited; \258if (linux_ratelimited(&__ratelimited)) \259dev_warn(dev, __VA_ARGS__); \260} while (0)261262#define dev_dbg_ratelimited(dev, ...) do { \263static linux_ratelimit_t __ratelimited; \264if (linux_ratelimited(&__ratelimited)) \265dev_dbg(dev, __VA_ARGS__); \266} while (0)267268/* Public and LinuxKPI internal devres functions. */269void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);270void lkpi_devres_add(struct device *, void *);271void lkpi_devres_free(void *);272void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),273int (*match)(struct device *, void *, void *), void *);274int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),275int (*match)(struct device *, void *, void *), void *);276#define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g)277#define devres_add(_d, _p) lkpi_devres_add(_d, _p)278#define devres_free(_p) lkpi_devres_free(_p)279#define devres_find(_d, _rfn, _mfn, _mp) \280lkpi_devres_find(_d, _rfn, _mfn, _mp)281#define devres_destroy(_d, _rfn, _mfn, _mp) \282lkpi_devres_destroy(_d, _rfn, _mfn, _mp)283void lkpi_devres_release_free_list(struct device *);284void lkpi_devres_unlink(struct device *, void *);285void lkpi_devm_kmalloc_release(struct device *, void *);286void lkpi_devm_kfree(struct device *, const void *);287#define devm_kfree(_d, _p) lkpi_devm_kfree(_d, _p)288289static inline const char *290dev_driver_string(const struct device *dev)291{292driver_t *drv;293const char *str = "";294295if (dev->bsddev != NULL) {296drv = device_get_driver(dev->bsddev);297if (drv != NULL)298str = drv->name;299}300301return (str);302}303304static inline void *305dev_get_drvdata(const struct device *dev)306{307308return dev->driver_data;309}310311static inline void312dev_set_drvdata(struct device *dev, void *data)313{314315dev->driver_data = data;316}317318static inline struct device *319get_device(struct device *dev)320{321322if (dev)323kobject_get(&dev->kobj);324325return (dev);326}327328static inline char *329dev_name(const struct device *dev)330{331332return kobject_name(&dev->kobj);333}334335static inline bool336dev_is_removable(struct device *dev)337{338339return (false);340}341342#define dev_set_name(_dev, _fmt, ...) \343kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)344345static inline void346put_device(struct device *dev)347{348349if (dev)350kobject_put(&dev->kobj);351}352353struct class *lkpi_class_create(const char *name);354#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60400355#define class_create(name) lkpi_class_create(name)356#else357#define class_create(owner, name) lkpi_class_create(name)358#endif359360static inline int361class_register(struct class *class)362{363364class->bsdclass = devclass_create(class->name);365kobject_init(&class->kobj, &linux_class_ktype);366kobject_set_name(&class->kobj, class->name);367kobject_add(&class->kobj, &linux_class_root, class->name);368369return (0);370}371372static inline void373class_unregister(struct class *class)374{375376kobject_put(&class->kobj);377}378379static inline struct device *kobj_to_dev(struct kobject *kobj)380{381return container_of(kobj, struct device, kobj);382}383384struct device *device_create(struct class *class, struct device *parent,385dev_t devt, void *drvdata, const char *fmt, ...);386struct device *device_create_groups_vargs(struct class *class, struct device *parent,387dev_t devt, void *drvdata, const struct attribute_group **groups,388const char *fmt, va_list args);389390/*391* Devices are registered and created for exporting to sysfs. Create392* implies register and register assumes the device fields have been393* setup appropriately before being called.394*/395static inline void396device_initialize(struct device *dev)397{398device_t bsddev = NULL;399int unit = -1;400401if (dev->devt) {402unit = MINOR(dev->devt);403bsddev = devclass_get_device(dev->class->bsdclass, unit);404dev->bsddev_attached_here = false;405} else if (dev->parent == NULL) {406bsddev = devclass_get_device(dev->class->bsdclass, 0);407dev->bsddev_attached_here = false;408} else {409dev->bsddev_attached_here = true;410}411412if (bsddev == NULL && dev->parent != NULL) {413bsddev = device_add_child(dev->parent->bsddev,414dev->class->kobj.name, unit);415}416417if (bsddev != NULL)418device_set_softc(bsddev, dev);419420dev->bsddev = bsddev;421MPASS(dev->bsddev != NULL);422kobject_init(&dev->kobj, &linux_dev_ktype);423424spin_lock_init(&dev->devres_lock);425INIT_LIST_HEAD(&dev->devres_head);426}427428static inline int429device_add(struct device *dev)430{431if (dev->bsddev != NULL) {432if (dev->devt == 0)433dev->devt = makedev(0, device_get_unit(dev->bsddev));434}435kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));436437if (dev->groups)438return (sysfs_create_groups(&dev->kobj, dev->groups));439440return (0);441}442443static inline void444device_create_release(struct device *dev)445{446kfree(dev);447}448449static inline struct device *450device_create_with_groups(struct class *class,451struct device *parent, dev_t devt, void *drvdata,452const struct attribute_group **groups, const char *fmt, ...)453{454va_list vargs;455struct device *dev;456457va_start(vargs, fmt);458dev = device_create_groups_vargs(class, parent, devt, drvdata,459groups, fmt, vargs);460va_end(vargs);461return dev;462}463464static inline bool465device_is_registered(struct device *dev)466{467468return (dev->bsddev != NULL);469}470471static inline int472device_register(struct device *dev)473{474device_t bsddev = NULL;475int unit = -1;476477if (device_is_registered(dev))478goto done;479480if (dev->devt) {481unit = MINOR(dev->devt);482bsddev = devclass_get_device(dev->class->bsdclass, unit);483dev->bsddev_attached_here = false;484} else if (dev->parent == NULL) {485bsddev = devclass_get_device(dev->class->bsdclass, 0);486dev->bsddev_attached_here = false;487} else {488dev->bsddev_attached_here = true;489}490if (bsddev == NULL && dev->parent != NULL) {491bsddev = device_add_child(dev->parent->bsddev,492dev->class->kobj.name, unit);493}494if (bsddev != NULL) {495if (dev->devt == 0)496dev->devt = makedev(0, device_get_unit(bsddev));497device_set_softc(bsddev, dev);498}499dev->bsddev = bsddev;500done:501kobject_init(&dev->kobj, &linux_dev_ktype);502kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));503504sysfs_create_groups(&dev->kobj, dev->class->dev_groups);505506return (0);507}508509static inline void510device_unregister(struct device *dev)511{512device_t bsddev;513514sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);515516bsddev = dev->bsddev;517dev->bsddev = NULL;518519if (bsddev != NULL && dev->bsddev_attached_here) {520bus_topo_lock();521device_delete_child(device_get_parent(bsddev), bsddev);522bus_topo_unlock();523}524put_device(dev);525}526527static inline void528device_del(struct device *dev)529{530device_t bsddev;531532bsddev = dev->bsddev;533dev->bsddev = NULL;534535if (bsddev != NULL && dev->bsddev_attached_here) {536bus_topo_lock();537device_delete_child(device_get_parent(bsddev), bsddev);538bus_topo_unlock();539}540}541542static inline void543device_destroy(struct class *class, dev_t devt)544{545device_t bsddev;546int unit;547548unit = MINOR(devt);549bsddev = devclass_get_device(class->bsdclass, unit);550if (bsddev != NULL)551device_unregister(device_get_softc(bsddev));552}553554static inline void555device_release_driver(struct device *dev)556{557558#if 0559/* This leads to panics. Disable temporarily. Keep to rework. */560561/* We also need to cleanup LinuxKPI bits. What else? */562lkpi_devres_release_free_list(dev);563dev_set_drvdata(dev, NULL);564/* Do not call dev->release! */565566bus_topo_lock();567if (device_is_attached(dev->bsddev))568device_detach(dev->bsddev);569bus_topo_unlock();570#endif571}572573static inline int574device_reprobe(struct device *dev)575{576int error;577578device_release_driver(dev);579bus_topo_lock();580error = device_probe_and_attach(dev->bsddev);581bus_topo_unlock();582583return (-error);584}585586static inline void587device_set_wakeup_enable(struct device *dev __unused, bool enable __unused)588{589590/*591* XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which592* we currently do not support.593*/594}595596static inline int597device_wakeup_enable(struct device *dev)598{599600device_set_wakeup_enable(dev, true);601return (0);602}603604static inline bool605device_iommu_mapped(struct device *dev __unused)606{607return (false);608}609610#define dev_pm_set_driver_flags(dev, flags) do { \611} while (0)612613static inline void614linux_class_kfree(struct class *class)615{616617kfree(class);618}619620static inline void621class_destroy(struct class *class)622{623624if (class == NULL)625return;626class_unregister(class);627}628629static inline int630device_create_file(struct device *dev, const struct device_attribute *attr)631{632633if (dev)634return sysfs_create_file(&dev->kobj, &attr->attr);635return -EINVAL;636}637638static inline void639device_remove_file(struct device *dev, const struct device_attribute *attr)640{641642if (dev)643sysfs_remove_file(&dev->kobj, &attr->attr);644}645646static inline int647class_create_file(struct class *class, const struct class_attribute *attr)648{649650if (class)651return sysfs_create_file(&class->kobj, &attr->attr);652return -EINVAL;653}654655static inline void656class_remove_file(struct class *class, const struct class_attribute *attr)657{658659if (class)660sysfs_remove_file(&class->kobj, &attr->attr);661}662663#define dev_to_node(dev) linux_dev_to_node(dev)664#define of_node_to_nid(node) -1665int linux_dev_to_node(struct device *);666667char *kvasprintf(gfp_t, const char *, va_list);668char *kasprintf(gfp_t, const char *, ...);669char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);670671#define devm_kasprintf(_dev, _gfp, _fmt, ...) \672lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)673674static __inline void *675devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)676{677void *p;678679p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);680if (p != NULL)681lkpi_devres_add(dev, p);682683return (p);684}685686static inline void *687devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)688{689void *dst;690691if (len == 0)692return (NULL);693694dst = devm_kmalloc(dev, len, gfp);695if (dst != NULL)696memcpy(dst, src, len);697698return (dst);699}700701#define devm_kzalloc(_dev, _size, _gfp) \702devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)703704#define devm_kcalloc(_dev, _sizen, _size, _gfp) \705devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)706707int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data);708#define devm_add_action(dev, action, data) \709lkpi_devm_add_action(dev, action, data);710int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data);711#define devm_add_action_or_reset(dev, action, data) \712lkpi_devm_add_action_or_reset(dev, action, data)713714int lkpi_devm_device_add_group(struct device *dev, const struct attribute_group *group);715#define devm_device_add_group(dev, group) \716lkpi_devm_device_add_group(dev, group)717718#endif /* _LINUXKPI_LINUX_DEVICE_H_ */719720721