1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Mediated device definition 4 * 5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. 6 * Author: Neo Jia <[email protected]> 7 * Kirti Wankhede <[email protected]> 8 */ 9 10 #ifndef MDEV_H 11 #define MDEV_H 12 13 #include <linux/device.h> 14 #include <linux/uuid.h> 15 16 struct mdev_type; 17 18 struct mdev_device { 19 struct device dev; 20 guid_t uuid; 21 struct list_head next; 22 struct mdev_type *type; 23 bool active; 24 }; 25 26 struct mdev_type { 27 /* set by the driver before calling mdev_register parent: */ 28 const char *sysfs_name; 29 30 /* set by the core, can be used drivers */ 31 struct mdev_parent *parent; 32 33 /* internal only */ 34 struct kobject kobj; 35 struct kobject *devices_kobj; 36 }; 37 38 /* embedded into the struct device that the mdev devices hang off */ 39 struct mdev_parent { 40 struct device *dev; 41 struct mdev_driver *mdev_driver; 42 struct kset *mdev_types_kset; 43 /* Synchronize device creation/removal with parent unregistration */ 44 struct rw_semaphore unreg_sem; 45 struct mdev_type **types; 46 unsigned int nr_types; 47 }; 48 49 static inline struct mdev_device *to_mdev_device(struct device *dev) 50 { 51 return container_of(dev, struct mdev_device, dev); 52 } 53 54 /* interface for exporting mdev supported type attributes */ 55 struct mdev_type_attribute { 56 struct attribute attr; 57 ssize_t (*show)(struct mdev_type *mtype, 58 struct mdev_type_attribute *attr, char *buf); 59 ssize_t (*store)(struct mdev_type *mtype, 60 struct mdev_type_attribute *attr, const char *buf, 61 size_t count); 62 }; 63 64 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \ 65 struct mdev_type_attribute mdev_type_attr_##_name = \ 66 __ATTR(_name, _mode, _show, _store) 67 #define MDEV_TYPE_ATTR_RW(_name) \ 68 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name) 69 #define MDEV_TYPE_ATTR_RO(_name) \ 70 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name) 71 #define MDEV_TYPE_ATTR_WO(_name) \ 72 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name) 73 74 /** 75 * struct mdev_driver - Mediated device driver 76 * @probe: called when new device created 77 * @remove: called when device removed 78 * @types_attrs: attributes to the type kobjects. 79 * @driver: device driver structure 80 **/ 81 struct mdev_driver { 82 int (*probe)(struct mdev_device *dev); 83 void (*remove)(struct mdev_device *dev); 84 const struct attribute * const *types_attrs; 85 struct device_driver driver; 86 }; 87 88 int mdev_register_parent(struct mdev_parent *parent, struct device *dev, 89 struct mdev_driver *mdev_driver, struct mdev_type **types, 90 unsigned int nr_types); 91 void mdev_unregister_parent(struct mdev_parent *parent); 92 93 int mdev_register_driver(struct mdev_driver *drv); 94 void mdev_unregister_driver(struct mdev_driver *drv); 95 96 static inline struct device *mdev_dev(struct mdev_device *mdev) 97 { 98 return &mdev->dev; 99 } 100 101 #endif /* MDEV_H */ 102