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 <cjia@nvidia.com>
7  *             Kirti Wankhede <kwankhede@nvidia.com>
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 	struct list_head next;
19 	struct mdev_type *type;
20 	bool active;
21 };
22 
to_mdev_device(struct device * dev)23 static inline struct mdev_device *to_mdev_device(struct device *dev)
24 {
25 	return container_of(dev, struct mdev_device, dev);
26 }
27 
28 unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
29 unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
30 struct device *mtype_get_parent_dev(struct mdev_type *mtype);
31 
32 /* interface for exporting mdev supported type attributes */
33 struct mdev_type_attribute {
34 	struct attribute attr;
35 	ssize_t (*show)(struct mdev_type *mtype,
36 			struct mdev_type_attribute *attr, char *buf);
37 	ssize_t (*store)(struct mdev_type *mtype,
38 			 struct mdev_type_attribute *attr, const char *buf,
39 			 size_t count);
40 };
41 
42 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
43 struct mdev_type_attribute mdev_type_attr_##_name =		\
44 	__ATTR(_name, _mode, _show, _store)
45 #define MDEV_TYPE_ATTR_RW(_name) \
46 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
47 #define MDEV_TYPE_ATTR_RO(_name) \
48 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
49 #define MDEV_TYPE_ATTR_WO(_name) \
50 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
51 
52 /**
53  * struct mdev_driver - Mediated device driver
54  * @probe: called when new device created
55  * @remove: called when device removed
56  * @supported_type_groups: Attributes to define supported types. It is mandatory
57  *			to provide supported types.
58  * @driver: device driver structure
59  *
60  **/
61 struct mdev_driver {
62 	int (*probe)(struct mdev_device *dev);
63 	void (*remove)(struct mdev_device *dev);
64 	struct attribute_group **supported_type_groups;
65 	struct device_driver driver;
66 };
67 
68 extern struct bus_type mdev_bus_type;
69 
70 int mdev_register_device(struct device *dev, struct mdev_driver *mdev_driver);
71 void mdev_unregister_device(struct device *dev);
72 
73 int mdev_register_driver(struct mdev_driver *drv);
74 void mdev_unregister_driver(struct mdev_driver *drv);
75 
76 struct device *mdev_parent_dev(struct mdev_device *mdev);
mdev_dev(struct mdev_device * mdev)77 static inline struct device *mdev_dev(struct mdev_device *mdev)
78 {
79 	return &mdev->dev;
80 }
mdev_from_dev(struct device * dev)81 static inline struct mdev_device *mdev_from_dev(struct device *dev)
82 {
83 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
84 }
85 
86 #endif /* MDEV_H */
87