1 /* 2 * Copyright 2020, Jérôme Duval, jerome.duval@gmail.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "WMIPrivate.h" 8 9 10 WMIDevice::WMIDevice(device_node *node) 11 : 12 fNode(node), 13 fBus(NULL), 14 fBusCookie(UINT32_MAX), 15 fInitStatus(B_OK) 16 { 17 CALLED(); 18 19 { 20 device_node *parent = gDeviceManager->get_parent_node(node); 21 gDeviceManager->get_driver(parent, NULL, (void **)&fBus); 22 gDeviceManager->put_node(parent); 23 } 24 25 fInitStatus = gDeviceManager->get_attr_uint32(node, WMI_BUS_COOKIE, 26 &fBusCookie, false); 27 } 28 29 30 WMIDevice::~WMIDevice() 31 { 32 } 33 34 35 status_t 36 WMIDevice::InitCheck() 37 { 38 return fInitStatus; 39 } 40 41 42 status_t 43 WMIDevice::EvaluateMethod(uint8 instance, uint32 methodId, const acpi_data* in, 44 acpi_data* out) 45 { 46 CALLED(); 47 return fBus->EvaluateMethod(fBusCookie, instance, methodId, in, out); 48 } 49 50 51 status_t 52 WMIDevice::InstallEventHandler(const char* guidString, 53 acpi_notify_handler handler, void* context) 54 { 55 CALLED(); 56 if (guidString == NULL || handler == NULL) 57 return B_BAD_VALUE; 58 59 return fBus->InstallEventHandler(guidString, handler, context); 60 } 61 62 63 status_t 64 WMIDevice::RemoveEventHandler(const char* guidString) 65 { 66 CALLED(); 67 if (guidString == NULL) 68 return B_BAD_VALUE; 69 70 return fBus->RemoveEventHandler(guidString); 71 } 72 73 74 status_t 75 WMIDevice::GetEventData(uint32 notify, acpi_data* out) 76 { 77 CALLED(); 78 return fBus->GetEventData(notify, out); 79 } 80 81 82 const char* 83 WMIDevice::GetUid() 84 { 85 CALLED(); 86 return fBus->GetUid(fBusCookie); 87 } 88 89 90 // #pragma mark - driver module API 91 92 93 static status_t 94 wmi_init_device(device_node *node, void **_device) 95 { 96 CALLED(); 97 WMIDevice *device = new(std::nothrow) WMIDevice(node); 98 if (device == NULL) 99 return B_NO_MEMORY; 100 101 status_t result = device->InitCheck(); 102 if (result != B_OK) { 103 ERROR("failed to set up wmi device object\n"); 104 return result; 105 } 106 107 *_device = device; 108 109 return B_OK; 110 } 111 112 113 static void 114 wmi_uninit_device(void *_device) 115 { 116 CALLED(); 117 WMIDevice *device = (WMIDevice *)_device; 118 delete device; 119 } 120 121 122 static status_t 123 wmi_evaluate_method(wmi_device _device, uint8 instance, uint32 methodId, 124 const acpi_data* in, acpi_data* out) 125 { 126 WMIDevice *device = (WMIDevice *)_device; 127 return device->EvaluateMethod(instance, methodId, in, out); 128 } 129 130 131 static status_t 132 wmi_install_event_handler(wmi_device _device, const char* guidString, 133 acpi_notify_handler handler, void* context) 134 { 135 WMIDevice *device = (WMIDevice *)_device; 136 return device->InstallEventHandler(guidString, handler, context); 137 } 138 139 140 static status_t 141 wmi_remove_event_handler(wmi_device _device, const char* guidString) 142 { 143 WMIDevice *device = (WMIDevice *)_device; 144 return device->RemoveEventHandler(guidString); 145 } 146 147 148 static status_t 149 wmi_get_event_data(wmi_device _device, uint32 notify, acpi_data* out) 150 { 151 WMIDevice *device = (WMIDevice *)_device; 152 return device->GetEventData(notify, out); 153 } 154 155 156 static const char* 157 wmi_get_uid(wmi_device _device) 158 { 159 WMIDevice *device = (WMIDevice *)_device; 160 return device->GetUid(); 161 } 162 163 164 static status_t 165 std_ops(int32 op, ...) 166 { 167 switch (op) { 168 case B_MODULE_INIT: 169 case B_MODULE_UNINIT: 170 return B_OK; 171 172 default: 173 return B_ERROR; 174 } 175 } 176 177 178 wmi_device_interface gWMIDeviceModule = { 179 { 180 { 181 WMI_DEVICE_MODULE_NAME, 182 0, 183 std_ops 184 }, 185 186 NULL, // supported devices 187 NULL, // register node 188 wmi_init_device, 189 wmi_uninit_device, 190 NULL, // register child devices 191 NULL, // rescan 192 NULL, // device_removed 193 }, 194 195 wmi_evaluate_method, 196 wmi_install_event_handler, 197 wmi_remove_event_handler, 198 wmi_get_event_data, 199 wmi_get_uid, 200 }; 201