xref: /linux/include/linux/mdev.h (revision be709d48329a500621d2a05835283150ae137b45)
1 /*
2  * Mediated device definition
3  *
4  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5  *     Author: Neo Jia <cjia@nvidia.com>
6  *             Kirti Wankhede <kwankhede@nvidia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #ifndef MDEV_H
14 #define MDEV_H
15 
16 struct mdev_device;
17 
18 /**
19  * struct mdev_parent_ops - Structure to be registered for each parent device to
20  * register the device to mdev module.
21  *
22  * @owner:		The module owner.
23  * @dev_attr_groups:	Attributes of the parent device.
24  * @mdev_attr_groups:	Attributes of the mediated device.
25  * @supported_type_groups: Attributes to define supported types. It is mandatory
26  *			to provide supported types.
27  * @create:		Called to allocate basic resources in parent device's
28  *			driver for a particular mediated device. It is
29  *			mandatory to provide create ops.
30  *			@kobj: kobject of type for which 'create' is called.
31  *			@mdev: mdev_device structure on of mediated device
32  *			      that is being created
33  *			Returns integer: success (0) or error (< 0)
34  * @remove:		Called to free resources in parent device's driver for a
35  *			a mediated device. It is mandatory to provide 'remove'
36  *			ops.
37  *			@mdev: mdev_device device structure which is being
38  *			       destroyed
39  *			Returns integer: success (0) or error (< 0)
40  * @open:		Open mediated device.
41  *			@mdev: mediated device.
42  *			Returns integer: success (0) or error (< 0)
43  * @release:		release mediated device
44  *			@mdev: mediated device.
45  * @read:		Read emulation callback
46  *			@mdev: mediated device structure
47  *			@buf: read buffer
48  *			@count: number of bytes to read
49  *			@ppos: address.
50  *			Retuns number on bytes read on success or error.
51  * @write:		Write emulation callback
52  *			@mdev: mediated device structure
53  *			@buf: write buffer
54  *			@count: number of bytes to be written
55  *			@ppos: address.
56  *			Retuns number on bytes written on success or error.
57  * @ioctl:		IOCTL callback
58  *			@mdev: mediated device structure
59  *			@cmd: ioctl command
60  *			@arg: arguments to ioctl
61  * @mmap:		mmap callback
62  *			@mdev: mediated device structure
63  *			@vma: vma structure
64  * Parent device that support mediated device should be registered with mdev
65  * module with mdev_parent_ops structure.
66  **/
67 struct mdev_parent_ops {
68 	struct module   *owner;
69 	const struct attribute_group **dev_attr_groups;
70 	const struct attribute_group **mdev_attr_groups;
71 	struct attribute_group **supported_type_groups;
72 
73 	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
74 	int     (*remove)(struct mdev_device *mdev);
75 	int     (*open)(struct mdev_device *mdev);
76 	void    (*release)(struct mdev_device *mdev);
77 	ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
78 			size_t count, loff_t *ppos);
79 	ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
80 			 size_t count, loff_t *ppos);
81 	long	(*ioctl)(struct mdev_device *mdev, unsigned int cmd,
82 			 unsigned long arg);
83 	int	(*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
84 };
85 
86 /* interface for exporting mdev supported type attributes */
87 struct mdev_type_attribute {
88 	struct attribute attr;
89 	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
90 	ssize_t (*store)(struct kobject *kobj, struct device *dev,
91 			 const char *buf, size_t count);
92 };
93 
94 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
95 struct mdev_type_attribute mdev_type_attr_##_name =		\
96 	__ATTR(_name, _mode, _show, _store)
97 #define MDEV_TYPE_ATTR_RW(_name) \
98 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
99 #define MDEV_TYPE_ATTR_RO(_name) \
100 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
101 #define MDEV_TYPE_ATTR_WO(_name) \
102 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
103 
104 /**
105  * struct mdev_driver - Mediated device driver
106  * @name: driver name
107  * @probe: called when new device created
108  * @remove: called when device removed
109  * @driver: device driver structure
110  *
111  **/
112 struct mdev_driver {
113 	const char *name;
114 	int  (*probe)(struct device *dev);
115 	void (*remove)(struct device *dev);
116 	struct device_driver driver;
117 };
118 
119 #define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
120 
121 extern void *mdev_get_drvdata(struct mdev_device *mdev);
122 extern void mdev_set_drvdata(struct mdev_device *mdev, void *data);
123 extern const guid_t *mdev_uuid(struct mdev_device *mdev);
124 
125 extern struct bus_type mdev_bus_type;
126 
127 extern int  mdev_register_device(struct device *dev,
128 				 const struct mdev_parent_ops *ops);
129 extern void mdev_unregister_device(struct device *dev);
130 
131 extern int  mdev_register_driver(struct mdev_driver *drv, struct module *owner);
132 extern void mdev_unregister_driver(struct mdev_driver *drv);
133 
134 extern struct device *mdev_parent_dev(struct mdev_device *mdev);
135 extern struct device *mdev_dev(struct mdev_device *mdev);
136 extern struct mdev_device *mdev_from_dev(struct device *dev);
137 
138 #endif /* MDEV_H */
139