XenevaOS
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1#ifndef __LINUX_DEVICE_H__
2#define __LINUX_DEVICE_H__
3
4/*
5 * DCL <linux/device.h> -- the class/device_create surface mem.c needs to
6 * publish /dev nodes. The bodies live in DCL/linux_cdev_shim.c (the bridge
7 * into Xeneva devfs) and DCL/dcl_va_trampoline.s (device_create is variadic,
8 * and BaseHdr/stdarg.h cannot rebuild a va_list under aarch64-unknown-windows,
9 * so the asm entry saves x0..x7 for device_create_Call).
10 */
11
12#include <linux/kernel.h> /* umode_t */
13#include <linux/mutex.h> /* struct mutex: <linux/input.h> puts one in input_dev */
14#include <linux/devres.h> /* devm_*: mainline's include for these is
15 * <linux/devres.h>, which no vendored drm
16 * .c names -- they reach device APIs through
17 * this header, so the surface rides here. */
18#include <linux/kobject.h> /* kobject_uevent_env() macro -- drm_drv.c
19 * reaches it through this header, as mainline
20 * does; see kobject.h for why it is a macro. */
21#include <linux/fs.h> /* struct device, MKDEV */
22#include <linux/sysfs.h> /* struct attribute, __ATTR */
23#include <linux/property.h> /* dev_fwnode(), property readers */
24#include <linux/fwnode.h> /* fwnode_handle_put() -- mainline keeps this
25 * in the fwnode/property pair; property.h is
26 * where DCL defines the struct and this header
27 * is where its callers reach it. */
28
30 const char* name;
31 const struct attribute_group* const* groups;
32 int (*uevent)(const struct device* dev, struct kobj_uevent_env* env);
33 char* (*devnode)(const struct device* dev, umode_t* mode);
34 void (*release)(struct device* dev);
35 const struct dev_pm_ops* pm;
36};
37
38struct class {
39 const char* name;
40 char* (*devnode)(const struct device* dev, umode_t* mode);
41};
42
43int class_register(const void* cls);
44void class_unregister(const void* cls);
45
46struct device* device_create(const void* class, const void* parent,
47 unsigned int devt, void* drvdata,
48 const char* fmt, ...);
49void device_destroy(const void* class, unsigned int devt);
50
51/*
52 * The driver-core pair drm_drv.c drives directly (device_add at :195,
53 * device_del at :226): a struct device with kobj_name + devt already set
54 * (drm_sysfs_minor_alloc() sets them) becomes a devfs node, same bridge
55 * device_create() takes. Bodies: DCL/linux_cdev_shim.c.
56 */
57int device_add(struct device* dev);
58void device_del(struct device* dev);
60int dev_set_name(struct device* dev, const char* fmt, ...);
61
62
63/*
64 * struct device_attribute and DEVICE_ATTR_RW().
65 *
66 * mainline spells device_attribute with __SYSFS_FUNCTION_ALTERNATIVE to get
67 * a const-correct show() alongside show_const(); DCL writes the plain two
68 * members, because nothing in this tree takes the address of a const variant
69 * and the alternative exists to serve a different property system. The field
70 * *names* -- attr, show, store -- are mainline's, and they matter: 8250_port.c
71 * reads the attribute back as &dev_attr_rx_trig_bytes.attr, so a differently
72 * named member would not compile rather than silently misbehave.
73 */
77 char* buf);
79 const char* buf, size_t count);
80};
81
82#define __DEVICE_ATTR_RW_MODE(_name, _mode) \
83 __ATTR(_name, _mode, _name##_show, _name##_store)
84#define __DEVICE_ATTR_RW(_name) __DEVICE_ATTR_RW_MODE(_name, 0644)
85
86/*
87 * static DEVICE_ATTR_RW(rx_trig_bytes);
88 * -> static struct device_attribute dev_attr_rx_trig_bytes =
89 * __ATTR(rx_trig_bytes, 0644, rx_trig_bytes_show,
90 * rx_trig_bytes_store);
91 *
92 * The producing macro deliberately expands to a *declaration*, not an
93 * expression: the call site is `static DEVICE_ATTR_RW(rx_trig_bytes);` with
94 * the static already written, so the expansion has to carry the struct keyword
95 * and end at the initializer. That is why this is not a value-style macro.
96 */
97#define DEVICE_ATTR_RW(_name) \
98 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RW(_name)
99
100
101/*
102 * dev_get_drvdata() / dev_set_drvdata().
103 *
104 * The input core is the first user that needs a real answer: it stores its
105 * per-device `struct virtio_input *` with input_set_drvdata() and reads it
106 * back from the LED/status callback. struct device carries the slot at
107 * offset 88 (see <linux/fs.h>), inside the 800-byte ABI blob rather than
108 * beyond it, so the accessors can be the plain mainline pair.
109 */
110static inline void* dev_get_drvdata(const struct device* dev)
111{
112 return dev ? dev->driver_data : NULL;
113}
114
115static inline void dev_set_drvdata(struct device* dev, void* data)
116{
117 if (dev)
119}
120
121#define DEVICE_ATTR_RO(_name) \
122 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
123
124/*
125 * DEVICE_ATTR(_name, _mode, _show, _store) -- the four-argument spelling
126 * mainline defines in <linux/device.h>. The three one-argument spellings
127 * above cover everything else in this tree (8250 and serial_core use only
128 * _RW/_RO); virtio_console.c:1250 is the first caller that names its own
129 * mode -- `static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);` -- so
130 * this exists for it.
131 *
132 * It is a *declaration* macro exactly like DEVICE_ATTR_RW above: the
133 * expansion has to carry the struct keyword, because the call site writes
134 * `static` and expects the expansion to start at the type. The mode comes
135 * from <linux/sysfs.h>, which this header already includes, and `store` is
136 * allowed to be NULL (it is, at that one call site) because __ATTR puts it
137 * straight into a struct member.
138 */
139#define DEVICE_ATTR(_name, _mode, _show, _store) \
140 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, \
141 _show, _store)
142
143#define DEVICE_ATTR_WO(_name) \
144 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
145
146/*
147 * get_device() -- mainline returns the pointer it was handed, as a
148 * refcount-taking trip. DCL's is in DCL/linux_cdev_shim.c:157 and does the
149 * same thing for the same reason put_device() two blocks down does nothing:
150 * devfs owns the lifetime and there is no refcount to take. Declared here
151 * because that is where mainline declares it, and because virtio_console.c:438
152 * reaches it through this header with no other one in scope.
153 */
154void* get_device(const void* dev);
155
156/*
157 * `static DEVICE_ATTR_RO(uartclk);` and its fifteen siblings at
158 * serial_core.c:3004-3017 are the tty device's attribute table -- uartclk,
159 * type, line, port, irq, flags, xmit_fifo_size, close_delay, closing_wait,
160 * custom_divisor, io_type, iomem_base, iomem_reg_shift, console. Each needs
161 * a struct device_attribute whose show function the same file defines, so
162 * this is a *declaration* macro exactly like DEVICE_ATTR_RW above: the struct
163 * keyword carries `static`, and there is no trailing semicolon in the
164 * expansion because the call site supplies it.
165 */
166
167/*
168 * put_device() is a declaration, not a definition: DCL/linux_cdev_shim.c:160
169 * already has the body (a no-op -- devfs owns the lifetime), and four call
170 * sites in serial_core.c (:2308, :2311, :2387, :2390) pair it with the
171 * get from device_find_child(). Declaring it anywhere else would be a
172 * second definition; declaring it here is where mainline puts it.
173 *
174 * The parameter type is `const void *` because that is what the shim's
175 * definition says, and the two have to agree -- otherwise the pair compiles
176 * as two unrelated symbols and one of them silently fails to link.
177 */
178void put_device(const void* dev);
179
180/*
181 * dev_name() -- the device's name as devfs set it. DCL's struct device has
182 * the kobject name as its first member (kobj_name at offset 0, the same
183 * slot mainline's dev->kobj.name occupies), and linux_cdev_shim.c:449 fills
184 * it in, so this reads a live value rather than fabricating one.
185 *
186 * The NULL arm is belt-and-braces: the one caller (serial_core.c:2490) already
187 * guards `port->dev ? dev_name(port->dev) : ""`, but `%s` handed a NULL on a
188 * formatter with no "(null)" convention is a fault, and a name that is absent
189 * is better reported as empty than as a crash.
190 */
191static inline const char* dev_name(const struct device* dev)
192{
193 if (!dev || !dev->kobj_name)
194 return "";
195 return dev->kobj_name;
196}
197
198/*
199 * device_find_child() -- search a device's children for one the predicate
200 * accepts. DCL has no child-device tree: devices are created flat by
201 * device_create() and never linked under a parent, so the answer is always
202 * "none".
203 *
204 * The consequence is visible one line later at both call sites (serial_core.c
205 * :2305 and :2383): `tty_dev` comes back NULL, so the wake-arming arm at
206 * :2306 is skipped (it tests tty_dev first) and the one at :2383 calls
207 * device_may_wakeup(NULL), which is written to tolerate it. Both paths then
208 * fall through to put_device(tty_dev) with NULL, which the shim ignores.
209 *
210 * Returning NULL rather than a fabricated device is what keeps
211 * serial_match_port() -- whose job is to compare dev_t values against a real
212 * child -- from ever being handed something it would have to make sense of.
213 */
214static inline struct device* device_find_child(
215 struct device* parent, void* data,
216 int (*match)(struct device* dev, const void* data))
217{
218 (void)parent;
219 (void)data;
220 (void)match;
221 return NULL;
222}
223
224/*
225 * ── the device-model symbols the vendored drm core links against ────────
226 *
227 * mainline keeps these across <linux/device.h>, <linux/dev_printk.h> and
228 * <linux/platform_device.h>. DCL has no dev_printk.h at all and its
229 * platform_device.h is an empty stub, while every vendored drm .c reaches
230 * this header through drm_print.h -- so this is where the surface rides,
231 * the same routing devres.h and kobject.h already got above. The bodies
232 * that cannot be static inlines are in DCL/linux_device_shim.c.
233 */
234
235/*
236 * device_is_registered() -- mainline reads kobject.state_in_sysfs, which DCL
237 * does not model (struct device is the offset-forged blob in fs.h and the
238 * kobject state bits have no named fields). The one caller,
239 * drm_minor_unregister() (drm_drv.c:220), asks whether the minor's kdev was
240 * ever added so it can skip device_del() for one that was not. What can be
241 * answered truthfully here is the shape device_add() (DCL/linux_cdev_shim.c
242 * :685) publishes: a name and a dev_t, both set by
243 * drm_sysfs_minor_alloc() before the add. device_del() tolerates a device it
244 * never saw, so this stays on the safe side of the same question.
245 */
246static inline int device_is_registered(struct device* dev)
247{
248 return dev && dev->kobj_name && dev->devt;
249}
250
251/*
252 * dev_to_node() -- mainline's answer without CONFIG_NUMA is NUMA_NO_NODE,
253 * and DCL has no NUMA at all: one memory pool and no per-node allocator for
254 * the result to select. drm_managed.c:147/:233 hand it to alloc_dr()'s node
255 * argument, where -1 (mainline's NUMA_NO_NODE) changes nothing. The real
256 * body would read dev->numa_node, a field the pinned fs.h layout does not
257 * name.
258 */
259static inline int dev_to_node(struct device* dev)
260{
261 (void)dev;
262 return -1;
263}
264
265/*
266 * dev_driver_string() -- mainline answers driver->name, or the bus name, or
267 * "". DCL attaches neither a driver nor a bus to struct device (see the
268 * dev_get_drvdata() note above), so the no-driver arm is the only one that
269 * can fire. Its users are the drm_WARN/drm_WARN_ONCE macros in
270 * <drm/drm_print.h>:780/:785, which print "%s %s: [drm] ..." with
271 * dev_driver_string() and dev_name(); a fabricated driver name would put a
272 * second, wrong identifier in every warning, and the empty string does not.
273 */
274const char* dev_driver_string(const struct device* dev);
275
276/*
277 * dev_printk() -- the formatting entry point behind DRM's device-scoped
278 * logging. drm_print.c:194/:197 call it directly with a level string, the
279 * device and a message built from a struct va_format into "%pV". mainline
280 * v7.2 spells dev_printk as a macro over _dev_printk(), but its header was
281 * never ported, so this call has been an implicit `int dev_printk()` --
282 * accepted here because -Wno-implicit-function-declaration is on, which is
283 * exactly why the declaration has to exist: the call must be checked against
284 * something. The body is in DCL/linux_device_shim.c.
285 */
286int dev_printk(const char* level, const struct device* dev, const char* fmt,
287 ...);
288
289/*
290 * dev_warn_once() -- mainline defines this per call site in
291 * <linux/dev_printk.h> so that the once-flag is the statement's own static;
292 * a function could only remember across every caller. DCL has no
293 * dev_printk.h, so it lives here. `int` rather than mainline's
294 * `bool __read_mostly` follows kernel.h:635's WARN_ON_ONCE, the in-tree
295 * precedent (DCL has no __read_mostly). The expander is drm_warn_once()
296 * (BaseHdr/drm/drm_print.h:637) at drm_plane.c:1708, drm_dumb_buffers.c:160
297 * and drm_format_helper.c:509 -- all conditions that can repeat per
298 * plane/framebuffer, which is why the flag is not decorative.
299 */
300#ifndef dev_warn_once
301#define dev_warn_once(dev, fmt, ...) \
302 do { \
303 static int __dcl_warned_once; \
304 if (!__dcl_warned_once) { \
305 __dcl_warned_once = 1; \
306 dev_warn((dev), fmt, ##__VA_ARGS__); \
307 } \
308 } while (0)
309#endif
310
311/*
312 * platform_device_unregister() -- mainline drivers/base/platform.c, declared
313 * in <linux/platform_device.h>; DCL's copy of that header is an empty stub
314 * and struct platform_device is never defined, only pointed at
315 * (drm_connector.h:1884). The one caller is drm_connector_cleanup()
316 * (drm_connector.c:770), unregistering connector->hdmi_audio.codec_pdev --
317 * which nothing in this tree assigns, because the HDMI-audio codec binding
318 * that would is not ported. The body in DCL/linux_device_shim.c is a no-op
319 * that also answers NULL.
320 *
321 * The tag is forward-declared at file scope on purpose: a struct tag first
322 * named inside a parameter list is scoped to that prototype, and two such
323 * spellings are not the same type (the trap <linux/scatterlist.h> documents
324 * for struct sg_table).
325 */
326struct platform_device;
327void platform_device_unregister(struct platform_device* pdev);
328
329/*
330 * dmem_cgroup_register_region() / dmem_cgroup_unregister_region() --
331 * mainline's dma-buf cgroup accounting, declared in <linux/cgroup_dmem.h>.
332 * DCL's cgroup_dmem.h is an empty shell (no memcg is ported), and its two
333 * callers in the vendored core -- drm_drv.c:971 (drmm_cgroup_register_region)
334 * and :954 (drmm_cg_unregister_region) -- reach this header through
335 * drm_print.h, so the declarations ride here beside the stubs in
336 * DCL/linux_device_shim.c. The region tag is mainline's opaque forward
337 * declaration; nothing in this tree dereferences it.
338 */
339struct dmem_cgroup_region;
340struct dmem_cgroup_region* dmem_cgroup_register_region(u64 size,
341 const char* name_fmt,
342 ...);
343void dmem_cgroup_unregister_region(struct dmem_cgroup_region* region);
344
345/*
346 * check_move_unevictable_folios() -- vmscan's "put these pinned folios back
347 * on the right LRU" walk, declared in mainline's <linux/swap.h> -- a header
348 * DCL does not carry. drm_gem.c:635 calls it from
349 * drm_gem_check_release_batch() on the way out of drm_gem_get_pages()/
350 * drm_gem_put_pages(), a path reached through shmem backing that is itself
351 * stubbed (DCL/drm_stubs.c). The no-op body is in DCL/linux_device_shim.c;
352 * its counterpart, __folio_batch_release(), is already a no-op there.
353 */
354struct folio_batch;
356#endif /* __LINUX_DEVICE_H__ */
int64_t ssize_t
Definition stddef.h:38
#define NULL
Definition actypes.h:561
void class_unregister(const void *cls)
void platform_device_unregister(struct platform_device *pdev)
void * get_device(const void *dev)
const char * dev_driver_string(const struct device *dev)
void put_device(const void *dev)
int device_add(struct device *dev)
struct device * device_create(const void *class, const void *parent, unsigned int devt, void *drvdata, const char *fmt,...)
void device_destroy(const void *class, unsigned int devt)
void dmem_cgroup_unregister_region(struct dmem_cgroup_region *region)
int dev_set_name(struct device *dev, const char *fmt,...)
struct dmem_cgroup_region * dmem_cgroup_register_region(u64 size, const char *name_fmt,...)
void device_initialize(struct device *dev)
int class_register(const void *cls)
void device_del(struct device *dev)
void check_move_unevictable_folios(struct folio_batch *fbatch)
int dev_printk(const char *level, const struct device *dev, const char *fmt,...)
size_t size
Definition drm_bridge.h:1299
const char * fmt
Definition drm_file.h:466
const struct device * dev
Definition drm_print.h:519
const char * level
Definition drm_print.h:515
struct kobject * parent
Definition kobject.h:91
void * data
Definition kthread.h:30
uint64_t u64
Definition kernel.h:17
unsigned int umode_t
Definition kernel.h:58
Definition sysfs.h:33
Definition sysfs.h:23
Definition device.h:38
const char * name
Definition device.h:39
Definition pm.h:71
Definition device.h:74
struct attribute attr
Definition device.h:75
ssize_t(* show)(struct device *dev, struct device_attribute *attr, char *buf)
Definition device.h:76
ssize_t(* store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Definition device.h:78
Definition device.h:29
const struct dev_pm_ops * pm
Definition device.h:35
const char * name
Definition device.h:30
int(* uevent)(const struct device *dev, struct kobj_uevent_env *env)
Definition device.h:32
const struct attribute_group *const * groups
Definition device.h:31
void(* release)(struct device *dev)
Definition device.h:34
Definition fs.h:255
unsigned int devt
Definition fs.h:269
const char * kobj_name
Definition fs.h:257
void * driver_data
Definition fs.h:263
Definition folio_batch.h:28
Definition kobject.h:122
#define mode(x)