xref: /linux-6.15/include/linux/mdev.h (revision e6486939)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Mediated device definition
4  *
5  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6  *     Author: Neo Jia <[email protected]>
7  *             Kirti Wankhede <[email protected]>
8  */
9 
10 #ifndef MDEV_H
11 #define MDEV_H
12 
13 struct mdev_type;
14 
15 struct mdev_device {
16 	struct device dev;
17 	guid_t uuid;
18 	void *driver_data;
19 	struct list_head next;
20 	struct mdev_type *type;
21 	bool active;
22 };
23 
24 static inline struct mdev_device *to_mdev_device(struct device *dev)
25 {
26 	return container_of(dev, struct mdev_device, dev);
27 }
28 
29 unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
30 unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
31 struct device *mtype_get_parent_dev(struct mdev_type *mtype);
32 
33 /**
34  * struct mdev_parent_ops - Structure to be registered for each parent device to
35  * register the device to mdev module.
36  *
37  * @owner:		The module owner.
38  * @device_driver:	Which device driver to probe() on newly created devices
39  * @mdev_attr_groups:	Attributes of the mediated device.
40  * @supported_type_groups: Attributes to define supported types. It is mandatory
41  *			to provide supported types.
42  *
43  * Parent device that support mediated device should be registered with mdev
44  * module with mdev_parent_ops structure.
45  **/
46 struct mdev_parent_ops {
47 	struct module   *owner;
48 	struct mdev_driver *device_driver;
49 	const struct attribute_group **mdev_attr_groups;
50 	struct attribute_group **supported_type_groups;
51 };
52 
53 /* interface for exporting mdev supported type attributes */
54 struct mdev_type_attribute {
55 	struct attribute attr;
56 	ssize_t (*show)(struct mdev_type *mtype,
57 			struct mdev_type_attribute *attr, char *buf);
58 	ssize_t (*store)(struct mdev_type *mtype,
59 			 struct mdev_type_attribute *attr, const char *buf,
60 			 size_t count);
61 };
62 
63 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
64 struct mdev_type_attribute mdev_type_attr_##_name =		\
65 	__ATTR(_name, _mode, _show, _store)
66 #define MDEV_TYPE_ATTR_RW(_name) \
67 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
68 #define MDEV_TYPE_ATTR_RO(_name) \
69 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
70 #define MDEV_TYPE_ATTR_WO(_name) \
71 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
72 
73 /**
74  * struct mdev_driver - Mediated device driver
75  * @probe: called when new device created
76  * @remove: called when device removed
77  * @driver: device driver structure
78  *
79  **/
80 struct mdev_driver {
81 	int (*probe)(struct mdev_device *dev);
82 	void (*remove)(struct mdev_device *dev);
83 	struct device_driver driver;
84 };
85 
86 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
87 {
88 	return mdev->driver_data;
89 }
90 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
91 {
92 	mdev->driver_data = data;
93 }
94 static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
95 {
96 	return &mdev->uuid;
97 }
98 
99 extern struct bus_type mdev_bus_type;
100 
101 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
102 void mdev_unregister_device(struct device *dev);
103 
104 int mdev_register_driver(struct mdev_driver *drv);
105 void mdev_unregister_driver(struct mdev_driver *drv);
106 
107 struct device *mdev_parent_dev(struct mdev_device *mdev);
108 static inline struct device *mdev_dev(struct mdev_device *mdev)
109 {
110 	return &mdev->dev;
111 }
112 static inline struct mdev_device *mdev_from_dev(struct device *dev)
113 {
114 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
115 }
116 
117 #endif /* MDEV_H */
118