File: device.h

package info (click to toggle)
crust-firmware 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: ansic: 19,341; yacc: 596; lex: 479; makefile: 334; asm: 215; sh: 136; python: 42
file content (94 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright © 2017-2022 The Crust Firmware Authors.
 * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
 */

#ifndef COMMON_DEVICE_H
#define COMMON_DEVICE_H

#include <stdbool.h>
#include <stdint.h>

/**
 * Default initializer for the device state pointer.
 *
 * The state pointer must be initialized for all devices. If additional mutable
 * state is needed, wrap struct device_state intrusively. Otherwise, use this
 * macro.
 */
#define DEVICE_STATE_INIT &(struct device_state) { 0 }

struct device_state;
struct driver;

struct device {
	/** A unique name for this device. */
	const char          *name;
	/** The driver for this device. */
	const struct driver *drv;
	/** Mutable state for this device. */
	struct device_state *state;
};

struct device_state {
	/** Reference count for this device. */
	uint8_t refcount;
};

struct driver {
	/** A function called to detect and initialize new devices. */
	int  (*probe)(const struct device *dev);
	/** A function called to uninitialize devices and free resources. */
	void (*release)(const struct device *dev);
};

/**
 * Determine if a device is active (if it has any outstanding references).
 *
 * @param dev A device.
 * @return    The state of the device.
 */
bool device_active(const struct device *dev);

/**
 * Get a reference to a device.
 *
 * If this is the first reference to a device, that device's driver will be
 * initialized. Otherwise, this function only updates the reference count.
 *
 * The device will remain running as long as the reference is held (that is,
 * until calling device_put()).
 *
 * If this function returns an error, do not call device_put().
 *
 * @param dev A device.
 *
 * @return Zero on success; an error code on failure.
 */
int device_get(const struct device *dev);

/**
 * Get a reference to a device.
 *
 * @return A reference to the device that was acquired, or a NULL pointer.
 */
const struct device *device_get_or_null(const struct device *dev);

/**
 * Release a reference to a device.
 *
 * @param dev A reference to a device.
 */
void device_put(const struct device *dev);

/**
 * Implementation of the device probe function that does nothing.
 */
int dummy_probe(const struct device *dev);

/**
 * Implementation of the device release function that does nothing.
 */
void dummy_release(const struct device *dev);

#endif /* COMMON_DEVICE_H */