1 /* SPDX-License-Identifier: MIT */
2 
3 #ifndef DRM_MODULE_H
4 #define DRM_MODULE_H
5 
6 #include <linux/pci.h>
7 #include <linux/platform_device.h>
8 
9 #include <drm/drm_drv.h>
10 
11 /**
12  * DOC: overview
13  *
14  * This library provides helpers registering DRM drivers during module
15  * initialization and shutdown. The provided helpers act like bus-specific
16  * module helpers, such as module_pci_driver(), but respect additional
17  * parameters that control DRM driver registration.
18  *
19  * Below is an example of initializing a DRM driver for a device on the
20  * PCI bus.
21  *
22  * .. code-block:: c
23  *
24  *	struct pci_driver my_pci_drv = {
25  *	};
26  *
27  *	drm_module_pci_driver(my_pci_drv);
28  *
29  * The generated code will test if DRM drivers are enabled and register
30  * the PCI driver my_pci_drv. For more complex module initialization, you
31  * can still use module_init() and module_exit() in your driver.
32  */
33 
34 /*
35  * PCI drivers
36  */
37 
drm_pci_register_driver(struct pci_driver * pci_drv)38 static inline int __init drm_pci_register_driver(struct pci_driver *pci_drv)
39 {
40 	if (drm_firmware_drivers_only())
41 		return -ENODEV;
42 
43 	return pci_register_driver(pci_drv);
44 }
45 
46 /**
47  * drm_module_pci_driver - Register a DRM driver for PCI-based devices
48  * @__pci_drv: the PCI driver structure
49  *
50  * Registers a DRM driver for devices on the PCI bus. The helper
51  * macro behaves like module_pci_driver() but tests the state of
52  * drm_firmware_drivers_only(). For more complex module initialization,
53  * use module_init() and module_exit() directly.
54  *
55  * Each module may only use this macro once. Calling it replaces
56  * module_init() and module_exit().
57  */
58 #define drm_module_pci_driver(__pci_drv) \
59 	module_driver(__pci_drv, drm_pci_register_driver, pci_unregister_driver)
60 
61 static inline int __init
drm_pci_register_driver_if_modeset(struct pci_driver * pci_drv,int modeset)62 drm_pci_register_driver_if_modeset(struct pci_driver *pci_drv, int modeset)
63 {
64 	if (drm_firmware_drivers_only() && modeset == -1)
65 		return -ENODEV;
66 	if (modeset == 0)
67 		return -ENODEV;
68 
69 	return pci_register_driver(pci_drv);
70 }
71 
72 static inline void __exit
drm_pci_unregister_driver_if_modeset(struct pci_driver * pci_drv,int modeset)73 drm_pci_unregister_driver_if_modeset(struct pci_driver *pci_drv, int modeset)
74 {
75 	pci_unregister_driver(pci_drv);
76 }
77 
78 /**
79  * drm_module_pci_driver_if_modeset - Register a DRM driver for PCI-based devices
80  * @__pci_drv: the PCI driver structure
81  * @__modeset: an additional parameter that disables the driver
82  *
83  * This macro is deprecated and only provided for existing drivers. For
84  * new drivers, use drm_module_pci_driver().
85  *
86  * Registers a DRM driver for devices on the PCI bus. The helper macro
87  * behaves like drm_module_pci_driver() with an additional driver-specific
88  * flag. If __modeset is 0, the driver has been disabled, if __modeset is
89  * -1 the driver state depends on the global DRM state. For all other
90  * values, the PCI driver has been enabled. The default should be -1.
91  */
92 #define drm_module_pci_driver_if_modeset(__pci_drv, __modeset) \
93 	module_driver(__pci_drv, drm_pci_register_driver_if_modeset, \
94 		      drm_pci_unregister_driver_if_modeset, __modeset)
95 
96 /*
97  * Platform drivers
98  */
99 
100 static inline int __init
drm_platform_driver_register(struct platform_driver * platform_drv)101 drm_platform_driver_register(struct platform_driver *platform_drv)
102 {
103 	if (drm_firmware_drivers_only())
104 		return -ENODEV;
105 
106 	return platform_driver_register(platform_drv);
107 }
108 
109 /**
110  * drm_module_platform_driver - Register a DRM driver for platform devices
111  * @__platform_drv: the platform driver structure
112  *
113  * Registers a DRM driver for devices on the platform bus. The helper
114  * macro behaves like module_platform_driver() but tests the state of
115  * drm_firmware_drivers_only(). For more complex module initialization,
116  * use module_init() and module_exit() directly.
117  *
118  * Each module may only use this macro once. Calling it replaces
119  * module_init() and module_exit().
120  */
121 #define drm_module_platform_driver(__platform_drv) \
122 	module_driver(__platform_drv, drm_platform_driver_register, \
123 		      platform_driver_unregister)
124 
125 #endif
126