Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 */ |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 17 | struct mdev_parent { |
| 18 | struct device *dev; |
| 19 | const struct mdev_parent_ops *ops; |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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; |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 32 | struct mdev_parent *parent; |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 | /** |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 43 | * struct mdev_parent_ops - Structure to be registered for each parent device to |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 89 | * module with mdev_parent_ops structure. |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 90 | **/ |
| 91 | |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 92 | struct mdev_parent_ops { |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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, |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame^] | 162 | const struct mdev_parent_ops *ops); |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 */ |