Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
7b96953b KW |
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> | |
7b96953b KW |
8 | */ |
9 | ||
10 | #ifndef MDEV_H | |
11 | #define MDEV_H | |
12 | ||
417fd5bf JG |
13 | struct mdev_type; |
14 | ||
2a3d15f2 JG |
15 | struct mdev_device { |
16 | struct device dev; | |
2a3d15f2 JG |
17 | guid_t uuid; |
18 | void *driver_data; | |
19 | struct list_head next; | |
417fd5bf | 20 | struct mdev_type *type; |
2a3d15f2 JG |
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 | } | |
7b96953b | 28 | |
15fcc44b | 29 | unsigned int mdev_get_type_group_id(struct mdev_device *mdev); |
9169cff1 JG |
30 | unsigned int mtype_get_type_group_id(struct mdev_type *mtype); |
31 | struct device *mtype_get_parent_dev(struct mdev_type *mtype); | |
15fcc44b | 32 | |
7b96953b | 33 | /** |
42930553 | 34 | * struct mdev_parent_ops - Structure to be registered for each parent device to |
7b96953b KW |
35 | * register the device to mdev module. |
36 | * | |
37 | * @owner: The module owner. | |
88a21f26 | 38 | * @device_driver: Which device driver to probe() on newly created devices |
7b96953b KW |
39 | * @dev_attr_groups: Attributes of the parent device. |
40 | * @mdev_attr_groups: Attributes of the mediated device. | |
41 | * @supported_type_groups: Attributes to define supported types. It is mandatory | |
42 | * to provide supported types. | |
43 | * @create: Called to allocate basic resources in parent device's | |
44 | * driver for a particular mediated device. It is | |
45 | * mandatory to provide create ops. | |
7b96953b KW |
46 | * @mdev: mdev_device structure on of mediated device |
47 | * that is being created | |
48 | * Returns integer: success (0) or error (< 0) | |
df54714f | 49 | * @remove: Called to free resources in parent device's driver for |
7b96953b KW |
50 | * a mediated device. It is mandatory to provide 'remove' |
51 | * ops. | |
52 | * @mdev: mdev_device device structure which is being | |
53 | * destroyed | |
54 | * Returns integer: success (0) or error (< 0) | |
7b96953b KW |
55 | * @read: Read emulation callback |
56 | * @mdev: mediated device structure | |
57 | * @buf: read buffer | |
58 | * @count: number of bytes to read | |
59 | * @ppos: address. | |
60 | * Retuns number on bytes read on success or error. | |
61 | * @write: Write emulation callback | |
62 | * @mdev: mediated device structure | |
63 | * @buf: write buffer | |
64 | * @count: number of bytes to be written | |
65 | * @ppos: address. | |
66 | * Retuns number on bytes written on success or error. | |
67 | * @ioctl: IOCTL callback | |
68 | * @mdev: mediated device structure | |
69 | * @cmd: ioctl command | |
70 | * @arg: arguments to ioctl | |
71 | * @mmap: mmap callback | |
72 | * @mdev: mediated device structure | |
73 | * @vma: vma structure | |
a15ac665 EF |
74 | * @request: request callback to release device |
75 | * @mdev: mediated device structure | |
76 | * @count: request sequence number | |
7b96953b | 77 | * Parent device that support mediated device should be registered with mdev |
42930553 | 78 | * module with mdev_parent_ops structure. |
7b96953b | 79 | **/ |
42930553 | 80 | struct mdev_parent_ops { |
7b96953b | 81 | struct module *owner; |
88a21f26 | 82 | struct mdev_driver *device_driver; |
7b96953b KW |
83 | const struct attribute_group **dev_attr_groups; |
84 | const struct attribute_group **mdev_attr_groups; | |
85 | struct attribute_group **supported_type_groups; | |
86 | ||
c2ef2f50 | 87 | int (*create)(struct mdev_device *mdev); |
7b96953b | 88 | int (*remove)(struct mdev_device *mdev); |
2fd585f4 JG |
89 | int (*open_device)(struct mdev_device *mdev); |
90 | void (*close_device)(struct mdev_device *mdev); | |
7b96953b KW |
91 | ssize_t (*read)(struct mdev_device *mdev, char __user *buf, |
92 | size_t count, loff_t *ppos); | |
93 | ssize_t (*write)(struct mdev_device *mdev, const char __user *buf, | |
94 | size_t count, loff_t *ppos); | |
c6ef7fd4 | 95 | long (*ioctl)(struct mdev_device *mdev, unsigned int cmd, |
7b96953b KW |
96 | unsigned long arg); |
97 | int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma); | |
a15ac665 | 98 | void (*request)(struct mdev_device *mdev, unsigned int count); |
7b96953b KW |
99 | }; |
100 | ||
101 | /* interface for exporting mdev supported type attributes */ | |
102 | struct mdev_type_attribute { | |
103 | struct attribute attr; | |
9169cff1 JG |
104 | ssize_t (*show)(struct mdev_type *mtype, |
105 | struct mdev_type_attribute *attr, char *buf); | |
106 | ssize_t (*store)(struct mdev_type *mtype, | |
107 | struct mdev_type_attribute *attr, const char *buf, | |
108 | size_t count); | |
7b96953b KW |
109 | }; |
110 | ||
111 | #define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \ | |
112 | struct mdev_type_attribute mdev_type_attr_##_name = \ | |
113 | __ATTR(_name, _mode, _show, _store) | |
114 | #define MDEV_TYPE_ATTR_RW(_name) \ | |
115 | struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name) | |
116 | #define MDEV_TYPE_ATTR_RO(_name) \ | |
117 | struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name) | |
118 | #define MDEV_TYPE_ATTR_WO(_name) \ | |
119 | struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name) | |
120 | ||
121 | /** | |
122 | * struct mdev_driver - Mediated device driver | |
7b96953b KW |
123 | * @probe: called when new device created |
124 | * @remove: called when device removed | |
125 | * @driver: device driver structure | |
126 | * | |
127 | **/ | |
128 | struct mdev_driver { | |
2a3d15f2 JG |
129 | int (*probe)(struct mdev_device *dev); |
130 | void (*remove)(struct mdev_device *dev); | |
7b96953b KW |
131 | struct device_driver driver; |
132 | }; | |
133 | ||
2a3d15f2 JG |
134 | static inline void *mdev_get_drvdata(struct mdev_device *mdev) |
135 | { | |
136 | return mdev->driver_data; | |
137 | } | |
138 | static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data) | |
139 | { | |
140 | mdev->driver_data = data; | |
141 | } | |
142 | static inline const guid_t *mdev_uuid(struct mdev_device *mdev) | |
143 | { | |
144 | return &mdev->uuid; | |
145 | } | |
7b96953b KW |
146 | |
147 | extern struct bus_type mdev_bus_type; | |
148 | ||
50732af3 PP |
149 | int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops); |
150 | void mdev_unregister_device(struct device *dev); | |
7b96953b | 151 | |
91b9969d | 152 | int mdev_register_driver(struct mdev_driver *drv); |
50732af3 | 153 | void mdev_unregister_driver(struct mdev_driver *drv); |
7b96953b | 154 | |
50732af3 | 155 | struct device *mdev_parent_dev(struct mdev_device *mdev); |
2a3d15f2 JG |
156 | static inline struct device *mdev_dev(struct mdev_device *mdev) |
157 | { | |
158 | return &mdev->dev; | |
159 | } | |
160 | static inline struct mdev_device *mdev_from_dev(struct device *dev) | |
161 | { | |
162 | return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL; | |
163 | } | |
9372e6fe | 164 | |
7b96953b | 165 | #endif /* MDEV_H */ |