mdev.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef MDEV_H
  10. #define MDEV_H
  11. #include <linux/device.h>
  12. #include <linux/uuid.h>
  13. struct mdev_type;
  14. struct mdev_device {
  15. struct device dev;
  16. guid_t uuid;
  17. struct list_head next;
  18. struct mdev_type *type;
  19. bool active;
  20. };
  21. struct mdev_type {
  22. /* set by the driver before calling mdev_register parent: */
  23. const char *sysfs_name;
  24. const char *pretty_name;
  25. /* set by the core, can be used drivers */
  26. struct mdev_parent *parent;
  27. /* internal only */
  28. struct kobject kobj;
  29. struct kobject *devices_kobj;
  30. };
  31. /* embedded into the struct device that the mdev devices hang off */
  32. struct mdev_parent {
  33. struct device *dev;
  34. struct mdev_driver *mdev_driver;
  35. struct kset *mdev_types_kset;
  36. /* Synchronize device creation/removal with parent unregistration */
  37. struct rw_semaphore unreg_sem;
  38. struct mdev_type **types;
  39. unsigned int nr_types;
  40. atomic_t available_instances;
  41. };
  42. static inline struct mdev_device *to_mdev_device(struct device *dev)
  43. {
  44. return container_of(dev, struct mdev_device, dev);
  45. }
  46. /**
  47. * struct mdev_driver - Mediated device driver
  48. * @device_api: string to return for the device_api sysfs
  49. * @max_instances: maximum number of instances supported (optional)
  50. * @probe: called when new device created
  51. * @remove: called when device removed
  52. * @get_available: Return the max number of instances that can be created
  53. * @show_description: Print a description of the mtype
  54. * @driver: device driver structure
  55. **/
  56. struct mdev_driver {
  57. const char *device_api;
  58. unsigned int max_instances;
  59. int (*probe)(struct mdev_device *dev);
  60. void (*remove)(struct mdev_device *dev);
  61. unsigned int (*get_available)(struct mdev_type *mtype);
  62. ssize_t (*show_description)(struct mdev_type *mtype, char *buf);
  63. struct device_driver driver;
  64. };
  65. int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
  66. struct mdev_driver *mdev_driver, struct mdev_type **types,
  67. unsigned int nr_types);
  68. void mdev_unregister_parent(struct mdev_parent *parent);
  69. int mdev_register_driver(struct mdev_driver *drv);
  70. void mdev_unregister_driver(struct mdev_driver *drv);
  71. static inline struct device *mdev_dev(struct mdev_device *mdev)
  72. {
  73. return &mdev->dev;
  74. }
  75. #endif /* MDEV_H */