Path: blob/main/sys/compat/linuxkpi/common/include/linux/device.h
101848 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 *);94void (*coredump) (struct device *);95};9697struct device_type {98const char *name;99};100101struct device {102struct device *parent;103struct list_head irqents;104device_t bsddev;105/*106* The following flag is used to determine if the LinuxKPI is107* responsible for detaching the BSD device or not. If the108* LinuxKPI got the BSD device using devclass_get_device(), it109* must not try to detach or delete it, because it's already110* done somewhere else.111*/112bool bsddev_attached_here;113struct device_driver *driver;114struct device_type *type;115dev_t devt;116struct class *class;117void (*release)(struct device *dev);118struct kobject kobj;119void *dma_priv;120void *driver_data;121unsigned int irq;122#define LINUX_IRQ_INVALID 65535123unsigned int irq_start;124unsigned int irq_end;125const struct attribute_group **groups;126struct fwnode_handle *fwnode;127struct cdev *backlight_dev;128struct backlight_device *bd;129130spinlock_t devres_lock;131struct list_head devres_head;132133struct dev_pm_info power;134};135136extern struct device linux_root_device;137extern struct kobject linux_class_root;138extern const struct kobj_type linux_dev_ktype;139extern const struct kobj_type linux_class_ktype;140141struct class_attribute {142struct attribute attr;143ssize_t (*show)(struct class *, struct class_attribute *, char *);144ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);145const void *(*namespace)(struct class *, const struct class_attribute *);146};147148#define CLASS_ATTR(_name, _mode, _show, _store) \149struct class_attribute class_attr_##_name = \150{ { #_name, NULL, _mode }, _show, _store }151152struct device_attribute {153struct attribute attr;154ssize_t (*show)(struct device *,155struct device_attribute *, char *);156ssize_t (*store)(struct device *,157struct device_attribute *, const char *,158size_t);159};160161#define DEVICE_ATTR(_name, _mode, _show, _store) \162struct device_attribute dev_attr_##_name = \163__ATTR(_name, _mode, _show, _store)164#define DEVICE_ATTR_RO(_name) \165struct device_attribute dev_attr_##_name = __ATTR_RO(_name)166#define DEVICE_ATTR_WO(_name) \167struct device_attribute dev_attr_##_name = __ATTR_WO(_name)168#define DEVICE_ATTR_RW(_name) \169struct device_attribute dev_attr_##_name = __ATTR_RW(_name)170171/* Simple class attribute that is just a static string */172struct class_attribute_string {173struct class_attribute attr;174char *str;175};176177static inline ssize_t178show_class_attr_string(struct class *class,179struct class_attribute *attr, char *buf)180{181struct class_attribute_string *cs;182cs = container_of(attr, struct class_attribute_string, attr);183return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);184}185186/* Currently read-only only */187#define _CLASS_ATTR_STRING(_name, _mode, _str) \188{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }189#define CLASS_ATTR_STRING(_name, _mode, _str) \190struct class_attribute_string class_attr_##_name = \191_CLASS_ATTR_STRING(_name, _mode, _str)192193#define dev_printk(lvl, dev, fmt, ...) \194device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)195196#define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)197#define dev_alert(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)198#define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)199#define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)200#define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)201#define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)202#define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)203#define dev_dbg(dev, fmt, ...) do { } while (0)204205#define dev_WARN(dev, fmt, ...) \206device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__)207208#define dev_WARN_ONCE(dev, condition, fmt, ...) do { \209static bool __dev_WARN_ONCE; \210bool __ret_warn_on = (condition); \211if (unlikely(__ret_warn_on)) { \212if (!__dev_WARN_ONCE) { \213__dev_WARN_ONCE = true; \214device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \215} \216} \217} while (0)218219#define dev_info_once(dev, ...) do { \220static bool __dev_info_once; \221if (!__dev_info_once) { \222__dev_info_once = true; \223dev_info(dev, __VA_ARGS__); \224} \225} while (0)226227#define dev_warn_once(dev, ...) do { \228static bool __dev_warn_once; \229if (!__dev_warn_once) { \230__dev_warn_once = 1; \231dev_warn(dev, __VA_ARGS__); \232} \233} while (0)234235#define dev_err_once(dev, ...) do { \236static bool __dev_err_once; \237if (!__dev_err_once) { \238__dev_err_once = 1; \239dev_err(dev, __VA_ARGS__); \240} \241} while (0)242243#define dev_dbg_once(dev, ...) do { \244static bool __dev_dbg_once; \245if (!__dev_dbg_once) { \246__dev_dbg_once = 1; \247dev_dbg(dev, __VA_ARGS__); \248} \249} while (0)250251#define dev_err_ratelimited(dev, ...) do { \252static linux_ratelimit_t __ratelimited; \253if (linux_ratelimited(&__ratelimited)) \254dev_err(dev, __VA_ARGS__); \255} while (0)256257#define dev_warn_ratelimited(dev, ...) do { \258static linux_ratelimit_t __ratelimited; \259if (linux_ratelimited(&__ratelimited)) \260dev_warn(dev, __VA_ARGS__); \261} while (0)262263#define dev_dbg_ratelimited(dev, ...) do { \264static linux_ratelimit_t __ratelimited; \265if (linux_ratelimited(&__ratelimited)) \266dev_dbg(dev, __VA_ARGS__); \267} while (0)268269/* Public and LinuxKPI internal devres functions. */270void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);271void lkpi_devres_add(struct device *, void *);272void lkpi_devres_free(void *);273void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),274int (*match)(struct device *, void *, void *), void *);275int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),276int (*match)(struct device *, void *, void *), void *);277#define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g)278#define devres_add(_d, _p) lkpi_devres_add(_d, _p)279#define devres_free(_p) lkpi_devres_free(_p)280#define devres_find(_d, _rfn, _mfn, _mp) \281lkpi_devres_find(_d, _rfn, _mfn, _mp)282#define devres_destroy(_d, _rfn, _mfn, _mp) \283lkpi_devres_destroy(_d, _rfn, _mfn, _mp)284void lkpi_devres_release_free_list(struct device *);285void lkpi_devres_unlink(struct device *, void *);286void lkpi_devm_kmalloc_release(struct device *, void *);287void lkpi_devm_kfree(struct device *, const void *);288#define devm_kfree(_d, _p) lkpi_devm_kfree(_d, _p)289290static inline const char *291dev_driver_string(const struct device *dev)292{293driver_t *drv;294const char *str = "";295296if (dev->bsddev != NULL) {297drv = device_get_driver(dev->bsddev);298if (drv != NULL)299str = drv->name;300}301302return (str);303}304305static inline void *306dev_get_drvdata(const struct device *dev)307{308309return dev->driver_data;310}311312static inline void313dev_set_drvdata(struct device *dev, void *data)314{315316dev->driver_data = data;317}318319static inline struct device *320get_device(struct device *dev)321{322323if (dev)324kobject_get(&dev->kobj);325326return (dev);327}328329static inline char *330dev_name(const struct device *dev)331{332333return kobject_name(&dev->kobj);334}335336static inline bool337dev_is_removable(struct device *dev)338{339340return (false);341}342343#define dev_set_name(_dev, _fmt, ...) \344kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)345346static inline void347put_device(struct device *dev)348{349350if (dev)351kobject_put(&dev->kobj);352}353354struct class *lkpi_class_create(const char *name);355#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60400356#define class_create(name) lkpi_class_create(name)357#else358#define class_create(owner, name) lkpi_class_create(name)359#endif360361static inline int362class_register(struct class *class)363{364365class->bsdclass = devclass_create(class->name);366kobject_init(&class->kobj, &linux_class_ktype);367kobject_set_name(&class->kobj, class->name);368kobject_add(&class->kobj, &linux_class_root, class->name);369370return (0);371}372373static inline void374class_unregister(struct class *class)375{376377kobject_put(&class->kobj);378}379380static inline struct device *kobj_to_dev(struct kobject *kobj)381{382return container_of(kobj, struct device, kobj);383}384385struct device *device_create(struct class *class, struct device *parent,386dev_t devt, void *drvdata, const char *fmt, ...);387struct device *device_create_groups_vargs(struct class *class, struct device *parent,388dev_t devt, void *drvdata, const struct attribute_group **groups,389const char *fmt, va_list args);390391/*392* Devices are registered and created for exporting to sysfs. Create393* implies register and register assumes the device fields have been394* setup appropriately before being called.395*/396static inline void397device_initialize(struct device *dev)398{399device_t bsddev = NULL;400int unit = -1;401402if (dev->devt) {403unit = MINOR(dev->devt);404bsddev = devclass_get_device(dev->class->bsdclass, unit);405dev->bsddev_attached_here = false;406} else if (dev->parent == NULL) {407bsddev = devclass_get_device(dev->class->bsdclass, 0);408dev->bsddev_attached_here = false;409} else {410dev->bsddev_attached_here = true;411}412413if (bsddev == NULL && dev->parent != NULL) {414bsddev = device_add_child(dev->parent->bsddev,415dev->class->kobj.name, unit);416}417418if (bsddev != NULL)419device_set_softc(bsddev, dev);420421dev->bsddev = bsddev;422MPASS(dev->bsddev != NULL);423kobject_init(&dev->kobj, &linux_dev_ktype);424425spin_lock_init(&dev->devres_lock);426INIT_LIST_HEAD(&dev->devres_head);427}428429static inline int430device_add(struct device *dev)431{432if (dev->bsddev != NULL) {433if (dev->devt == 0)434dev->devt = makedev(0, device_get_unit(dev->bsddev));435}436kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));437438if (dev->groups)439return (sysfs_create_groups(&dev->kobj, dev->groups));440441return (0);442}443444static inline void445device_create_release(struct device *dev)446{447kfree(dev);448}449450static inline struct device *451device_create_with_groups(struct class *class,452struct device *parent, dev_t devt, void *drvdata,453const struct attribute_group **groups, const char *fmt, ...)454{455va_list vargs;456struct device *dev;457458va_start(vargs, fmt);459dev = device_create_groups_vargs(class, parent, devt, drvdata,460groups, fmt, vargs);461va_end(vargs);462return dev;463}464465static inline bool466device_is_registered(struct device *dev)467{468469return (dev->bsddev != NULL);470}471472static inline int473device_register(struct device *dev)474{475device_t bsddev = NULL;476int unit = -1;477478if (device_is_registered(dev))479goto done;480481if (dev->devt) {482unit = MINOR(dev->devt);483bsddev = devclass_get_device(dev->class->bsdclass, unit);484dev->bsddev_attached_here = false;485} else if (dev->parent == NULL) {486bsddev = devclass_get_device(dev->class->bsdclass, 0);487dev->bsddev_attached_here = false;488} else {489dev->bsddev_attached_here = true;490}491if (bsddev == NULL && dev->parent != NULL) {492bsddev = device_add_child(dev->parent->bsddev,493dev->class->kobj.name, unit);494}495if (bsddev != NULL) {496if (dev->devt == 0)497dev->devt = makedev(0, device_get_unit(bsddev));498device_set_softc(bsddev, dev);499}500dev->bsddev = bsddev;501done:502kobject_init(&dev->kobj, &linux_dev_ktype);503kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));504505sysfs_create_groups(&dev->kobj, dev->class->dev_groups);506507return (0);508}509510static inline void511device_unregister(struct device *dev)512{513device_t bsddev;514515sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);516517bsddev = dev->bsddev;518dev->bsddev = NULL;519520if (bsddev != NULL && dev->bsddev_attached_here) {521bus_topo_lock();522device_delete_child(device_get_parent(bsddev), bsddev);523bus_topo_unlock();524}525put_device(dev);526}527528static inline void529device_del(struct device *dev)530{531device_t bsddev;532533bsddev = dev->bsddev;534dev->bsddev = NULL;535536if (bsddev != NULL && dev->bsddev_attached_here) {537bus_topo_lock();538device_delete_child(device_get_parent(bsddev), bsddev);539bus_topo_unlock();540}541}542543static inline void544device_destroy(struct class *class, dev_t devt)545{546device_t bsddev;547int unit;548549unit = MINOR(devt);550bsddev = devclass_get_device(class->bsdclass, unit);551if (bsddev != NULL)552device_unregister(device_get_softc(bsddev));553}554555static inline void556device_release_driver(struct device *dev)557{558559pr_debug("%s: TODO\n", __func__);560#if 0561/* This leads to panics. Disable temporarily. Keep to rework. */562563/* We also need to cleanup LinuxKPI bits. What else? */564lkpi_devres_release_free_list(dev);565dev_set_drvdata(dev, NULL);566/* Do not call dev->release! */567568bus_topo_lock();569if (device_is_attached(dev->bsddev))570device_detach(dev->bsddev);571bus_topo_unlock();572#endif573}574575static inline int576device_reprobe(struct device *dev)577{578int error;579580device_release_driver(dev);581bus_topo_lock();582error = device_probe_and_attach(dev->bsddev);583bus_topo_unlock();584585return (-error);586}587588static inline void589device_set_wakeup_enable(struct device *dev __unused, bool enable __unused)590{591592/*593* XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which594* we currently do not support.595*/596}597598static inline int599device_wakeup_enable(struct device *dev)600{601602device_set_wakeup_enable(dev, true);603return (0);604}605606static inline bool607device_iommu_mapped(struct device *dev __unused)608{609return (false);610}611612#define dev_pm_set_driver_flags(dev, flags) do { \613} while (0)614615static inline void616linux_class_kfree(struct class *class)617{618619kfree(class);620}621622static inline void623class_destroy(struct class *class)624{625626if (class == NULL)627return;628class_unregister(class);629}630631static inline int632device_create_file(struct device *dev, const struct device_attribute *attr)633{634635if (dev)636return sysfs_create_file(&dev->kobj, &attr->attr);637return -EINVAL;638}639640static inline void641device_remove_file(struct device *dev, const struct device_attribute *attr)642{643644if (dev)645sysfs_remove_file(&dev->kobj, &attr->attr);646}647648static inline int649class_create_file(struct class *class, const struct class_attribute *attr)650{651652if (class)653return sysfs_create_file(&class->kobj, &attr->attr);654return -EINVAL;655}656657static inline void658class_remove_file(struct class *class, const struct class_attribute *attr)659{660661if (class)662sysfs_remove_file(&class->kobj, &attr->attr);663}664665#define dev_to_node(dev) linux_dev_to_node(dev)666#define of_node_to_nid(node) -1667int linux_dev_to_node(struct device *);668669char *kvasprintf(gfp_t, const char *, va_list);670char *kasprintf(gfp_t, const char *, ...);671char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);672673#define devm_kasprintf(_dev, _gfp, _fmt, ...) \674lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)675676static __inline void *677devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)678{679void *p;680681p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);682if (p != NULL)683lkpi_devres_add(dev, p);684685return (p);686}687688static inline void *689devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)690{691void *dst;692693if (len == 0)694return (NULL);695696dst = devm_kmalloc(dev, len, gfp);697if (dst != NULL)698memcpy(dst, src, len);699700return (dst);701}702703#define devm_kzalloc(_dev, _size, _gfp) \704devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)705706#define devm_kcalloc(_dev, _sizen, _size, _gfp) \707devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)708709int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data);710#define devm_add_action(dev, action, data) \711lkpi_devm_add_action(dev, action, data);712int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data);713#define devm_add_action_or_reset(dev, action, data) \714lkpi_devm_add_action_or_reset(dev, action, data)715716int lkpi_devm_device_add_group(struct device *dev, const struct attribute_group *group);717#define devm_device_add_group(dev, group) \718lkpi_devm_device_add_group(dev, group)719720#endif /* _LINUXKPI_LINUX_DEVICE_H_ */721722723