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