blob: b6e048e1045f99868642b920a005d2382e6dc316 [file] [log] [blame]
Kirti Wankhede7b969532016-11-17 02:16:13 +05301/*
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
Alex Williamson99e31232016-12-30 08:13:44 -070016struct mdev_device;
Kirti Wankhede7b969532016-11-17 02:16:13 +053017
18/**
Alex Williamson42930552016-12-30 08:13:38 -070019 * struct mdev_parent_ops - Structure to be registered for each parent device to
Kirti Wankhede7b969532016-11-17 02:16:13 +053020 * 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
Alex Williamson42930552016-12-30 08:13:38 -070065 * module with mdev_parent_ops structure.
Kirti Wankhede7b969532016-11-17 02:16:13 +053066 **/
Alex Williamson42930552016-12-30 08:13:38 -070067struct mdev_parent_ops {
Kirti Wankhede7b969532016-11-17 02:16:13 +053068 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);
Paul Gortmakerc6ef7fd2017-01-04 15:08:15 -050081 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
Kirti Wankhede7b969532016-11-17 02:16:13 +053082 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 */
87struct 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) \
95struct 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 **/
112struct 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)
Kirti Wankhede7b969532016-11-17 02:16:13 +0530120
Alex Williamson99e31232016-12-30 08:13:44 -0700121extern void *mdev_get_drvdata(struct mdev_device *mdev);
122extern void mdev_set_drvdata(struct mdev_device *mdev, void *data);
123extern uuid_le mdev_uuid(struct mdev_device *mdev);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530124
125extern struct bus_type mdev_bus_type;
126
Kirti Wankhede7b969532016-11-17 02:16:13 +0530127extern int mdev_register_device(struct device *dev,
Alex Williamson42930552016-12-30 08:13:38 -0700128 const struct mdev_parent_ops *ops);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530129extern void mdev_unregister_device(struct device *dev);
130
131extern int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
132extern void mdev_unregister_driver(struct mdev_driver *drv);
133
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700134extern struct device *mdev_parent_dev(struct mdev_device *mdev);
Alex Williamson99e31232016-12-30 08:13:44 -0700135extern struct device *mdev_dev(struct mdev_device *mdev);
136extern struct mdev_device *mdev_from_dev(struct device *dev);
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700137
Kirti Wankhede7b969532016-11-17 02:16:13 +0530138#endif /* MDEV_H */