]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - include/linux/mdev.h
Merge tag 'befs-v4.10-rc1' of git://github.com/luisbg/linux-befs
[mirror_ubuntu-zesty-kernel.git] / include / linux / mdev.h
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 /* Parent device */
17 struct parent_device {
18 struct device *dev;
19 const struct parent_ops *ops;
20
21 /* internal */
22 struct kref ref;
23 struct mutex lock;
24 struct list_head next;
25 struct kset *mdev_types_kset;
26 struct list_head type_list;
27 };
28
29 /* Mediated device */
30 struct mdev_device {
31 struct device dev;
32 struct parent_device *parent;
33 uuid_le uuid;
34 void *driver_data;
35
36 /* internal */
37 struct kref ref;
38 struct list_head next;
39 struct kobject *type_kobj;
40 };
41
42 /**
43 * struct parent_ops - Structure to be registered for each parent device to
44 * register the device to mdev module.
45 *
46 * @owner: The module owner.
47 * @dev_attr_groups: Attributes of the parent device.
48 * @mdev_attr_groups: Attributes of the mediated device.
49 * @supported_type_groups: Attributes to define supported types. It is mandatory
50 * to provide supported types.
51 * @create: Called to allocate basic resources in parent device's
52 * driver for a particular mediated device. It is
53 * mandatory to provide create ops.
54 * @kobj: kobject of type for which 'create' is called.
55 * @mdev: mdev_device structure on of mediated device
56 * that is being created
57 * Returns integer: success (0) or error (< 0)
58 * @remove: Called to free resources in parent device's driver for a
59 * a mediated device. It is mandatory to provide 'remove'
60 * ops.
61 * @mdev: mdev_device device structure which is being
62 * destroyed
63 * Returns integer: success (0) or error (< 0)
64 * @open: Open mediated device.
65 * @mdev: mediated device.
66 * Returns integer: success (0) or error (< 0)
67 * @release: release mediated device
68 * @mdev: mediated device.
69 * @read: Read emulation callback
70 * @mdev: mediated device structure
71 * @buf: read buffer
72 * @count: number of bytes to read
73 * @ppos: address.
74 * Retuns number on bytes read on success or error.
75 * @write: Write emulation callback
76 * @mdev: mediated device structure
77 * @buf: write buffer
78 * @count: number of bytes to be written
79 * @ppos: address.
80 * Retuns number on bytes written on success or error.
81 * @ioctl: IOCTL callback
82 * @mdev: mediated device structure
83 * @cmd: ioctl command
84 * @arg: arguments to ioctl
85 * @mmap: mmap callback
86 * @mdev: mediated device structure
87 * @vma: vma structure
88 * Parent device that support mediated device should be registered with mdev
89 * module with parent_ops structure.
90 **/
91
92 struct parent_ops {
93 struct module *owner;
94 const struct attribute_group **dev_attr_groups;
95 const struct attribute_group **mdev_attr_groups;
96 struct attribute_group **supported_type_groups;
97
98 int (*create)(struct kobject *kobj, struct mdev_device *mdev);
99 int (*remove)(struct mdev_device *mdev);
100 int (*open)(struct mdev_device *mdev);
101 void (*release)(struct mdev_device *mdev);
102 ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
103 size_t count, loff_t *ppos);
104 ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
105 size_t count, loff_t *ppos);
106 ssize_t (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
107 unsigned long arg);
108 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
109 };
110
111 /* interface for exporting mdev supported type attributes */
112 struct mdev_type_attribute {
113 struct attribute attr;
114 ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
115 ssize_t (*store)(struct kobject *kobj, struct device *dev,
116 const char *buf, size_t count);
117 };
118
119 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \
120 struct mdev_type_attribute mdev_type_attr_##_name = \
121 __ATTR(_name, _mode, _show, _store)
122 #define MDEV_TYPE_ATTR_RW(_name) \
123 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
124 #define MDEV_TYPE_ATTR_RO(_name) \
125 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
126 #define MDEV_TYPE_ATTR_WO(_name) \
127 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
128
129 /**
130 * struct mdev_driver - Mediated device driver
131 * @name: driver name
132 * @probe: called when new device created
133 * @remove: called when device removed
134 * @driver: device driver structure
135 *
136 **/
137 struct mdev_driver {
138 const char *name;
139 int (*probe)(struct device *dev);
140 void (*remove)(struct device *dev);
141 struct device_driver driver;
142 };
143
144 #define to_mdev_driver(drv) container_of(drv, struct mdev_driver, driver)
145 #define to_mdev_device(dev) container_of(dev, struct mdev_device, dev)
146
147 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
148 {
149 return mdev->driver_data;
150 }
151
152 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
153 {
154 mdev->driver_data = data;
155 }
156
157 extern struct bus_type mdev_bus_type;
158
159 #define dev_is_mdev(d) ((d)->bus == &mdev_bus_type)
160
161 extern int mdev_register_device(struct device *dev,
162 const struct parent_ops *ops);
163 extern void mdev_unregister_device(struct device *dev);
164
165 extern int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
166 extern void mdev_unregister_driver(struct mdev_driver *drv);
167
168 #endif /* MDEV_H */