Changeset 8a64320e in mainline for uspace/drv/nic/ar9271/wmi.c
- Timestamp:
- 2015-04-23T23:40:14Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- dcba819
- Parents:
- 09044cb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/ar9271/wmi.c
r09044cb r8a64320e 34 34 35 35 #include <usb/debug.h> 36 37 36 #include <malloc.h> 38 37 #include <mem.h> 39 38 #include <byteorder.h> 40 41 39 #include "wmi.h" 42 40 43 /** 44 * WMI registry read. 45 * 41 /** WMI registry read. 42 * 46 43 * @param htc_device HTC device structure. 47 44 * @param reg_offset Registry offset (address) to be read. 48 * @param res Stored result. 49 * 50 * @return EOK if succeed, negative error code otherwise. 45 * @param res Stored result. 46 * 47 * @return EOK if succeed, negative error code otherwise. 48 * 51 49 */ 52 50 int wmi_reg_read(htc_device_t *htc_device, uint32_t reg_offset, uint32_t *res) … … 54 52 uint32_t cmd_value = host2uint32_t_be(reg_offset); 55 53 56 void *resp_buffer = 57 58 59 int rc = wmi_send_command(htc_device, WMI_REG_READ, 60 61 62 if (rc != EOK) {54 void *resp_buffer = 55 malloc(htc_device->ath_device->ctrl_response_length); 56 57 int rc = wmi_send_command(htc_device, WMI_REG_READ, 58 (uint8_t *) &cmd_value, sizeof(cmd_value), resp_buffer); 59 60 if (rc != EOK) { 63 61 usb_log_error("Failed to read registry value.\n"); 64 62 return rc; 65 63 } 66 64 67 uint32_t *resp_value = (uint32_t *) ((void*) resp_buffer + 68 sizeof(htc_frame_header_t) + 69 sizeof(wmi_command_header_t)); 65 uint32_t *resp_value = (uint32_t *) ((void *) resp_buffer + 66 sizeof(htc_frame_header_t) + sizeof(wmi_command_header_t)); 70 67 71 68 *res = uint32_t_be2host(*resp_value); … … 74 71 } 75 72 76 /** 77 * WMI registry write.78 * 79 * @param htc_device HTC device structure.80 * @param reg_offset Registry offset (address) to be written.81 * @param val Value to be written82 * 83 * @return EOK if succeed, negative error code otherwise.73 /** WMI registry write. 74 * 75 * @param htc_device HTC device structure. 76 * @param reg_offset Registry offset (address) to be written. 77 * @param val Value to be written 78 * 79 * @return EOK if succeed, negative error code otherwise. 80 * 84 81 */ 85 82 int wmi_reg_write(htc_device_t *htc_device, uint32_t reg_offset, uint32_t val) 86 83 { 87 84 uint32_t cmd_buffer[] = { 88 89 85 host2uint32_t_be(reg_offset), 86 host2uint32_t_be(val) 90 87 }; 91 88 92 void *resp_buffer = 93 94 95 int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 96 89 void *resp_buffer = 90 malloc(htc_device->ath_device->ctrl_response_length); 91 92 int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 93 (uint8_t *) &cmd_buffer, sizeof(cmd_buffer), resp_buffer); 97 94 98 95 free(resp_buffer); 99 96 100 if (rc != EOK) {97 if (rc != EOK) { 101 98 usb_log_error("Failed to write registry value.\n"); 102 99 return rc; … … 106 103 } 107 104 108 /** 109 * WMI registry set or clear specified bits.110 * 111 * @param htc_device HTC device structure.112 * @param reg_offset Registry offset (address) to be written.113 * @param set_bit Bit to be set.114 * @param clear_bit Bit to be cleared.115 * 116 * @return EOK if succeed, negative error code otherwise.117 */ 118 int wmi_reg_set_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 119 105 /** WMI registry set or clear specified bits. 106 * 107 * @param htc_device HTC device structure. 108 * @param reg_offset Registry offset (address) to be written. 109 * @param set_bit Bit to be set. 110 * @param clear_bit Bit to be cleared. 111 * 112 * @return EOK if succeed, negative error code otherwise. 113 * 114 */ 115 int wmi_reg_set_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 116 uint32_t set_bit, uint32_t clear_bit) 120 117 { 121 118 uint32_t value; 122 119 123 120 int rc = wmi_reg_read(htc_device, reg_offset, &value); 124 if (rc != EOK) {121 if (rc != EOK) { 125 122 usb_log_error("Failed to read registry value in RMW " 126 123 "function.\n"); 127 124 return rc; 128 125 } … … 132 129 133 130 rc = wmi_reg_write(htc_device, reg_offset, value); 134 if (rc != EOK) {131 if (rc != EOK) { 135 132 usb_log_error("Failed to write registry value in RMW " 136 137 return rc; 138 } 139 140 return rc; 141 } 142 143 /** 144 * WMI registry set specified bit.145 * 146 * @param htc_device HTC device structure.147 * @param reg_offset Registry offset (address) to be written.148 * @param set_bit Bit to be set.149 * 150 * @return EOK if succeed, negative error code otherwise.151 */ 152 int wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset, 153 133 "function.\n"); 134 return rc; 135 } 136 137 return rc; 138 } 139 140 /** WMI registry set specified bit. 141 * 142 * @param htc_device HTC device structure. 143 * @param reg_offset Registry offset (address) to be written. 144 * @param set_bit Bit to be set. 145 * 146 * @return EOK if succeed, negative error code otherwise. 147 * 148 */ 149 int wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset, 150 uint32_t set_bit) 154 151 { 155 152 return wmi_reg_set_clear_bit(htc_device, reg_offset, set_bit, 0); 156 153 } 157 154 158 /** 159 * WMI registry clear specified bit.160 * 161 * @param htc_device HTC device structure.162 * @param reg_offset Registry offset (address) to be written.163 * @param clear_bit Bit to be cleared.164 * 165 * @return EOK if succeed, negative error code otherwise.166 */ 167 int wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 168 155 /** WMI registry clear specified bit. 156 * 157 * @param htc_device HTC device structure. 158 * @param reg_offset Registry offset (address) to be written. 159 * @param clear_bit Bit to be cleared. 160 * 161 * @return EOK if succeed, negative error code otherwise. 162 * 163 */ 164 int wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 165 uint32_t clear_bit) 169 166 { 170 167 return wmi_reg_set_clear_bit(htc_device, reg_offset, 0, clear_bit); 171 168 } 172 169 173 /** 174 * WMI multi registry write. 175 * 170 /** WMI multi registry write. 171 * 176 172 * @param htc_device HTC device structure. 177 173 * @param reg_buffer Array of registry values to be written. 178 * @param elements Number of elements in array. 179 * 180 * @return EOK if succeed, negative error code otherwise. 174 * @param elements Number of elements in array. 175 * 176 * @return EOK if succeed, negative error code otherwise. 177 * 181 178 */ 182 179 int wmi_reg_buffer_write(htc_device_t *htc_device, wmi_reg_t *reg_buffer, 183 180 size_t elements) 184 181 { 185 182 size_t buffer_size = sizeof(wmi_reg_t) * elements; 186 183 void *buffer = malloc(buffer_size); 187 void *resp_buffer = 188 184 void *resp_buffer = 185 malloc(htc_device->ath_device->ctrl_response_length); 189 186 190 187 /* Convert values to correct endianness. */ 191 for (size_t i = 0; i < elements; i++) {188 for (size_t i = 0; i < elements; i++) { 192 189 wmi_reg_t *buffer_element = ®_buffer[i]; 193 190 wmi_reg_t *buffer_it = (wmi_reg_t *) 194 ((void *) buffer + i*sizeof(wmi_reg_t));195 buffer_it->offset = 196 191 ((void *) buffer + i * sizeof(wmi_reg_t)); 192 buffer_it->offset = 193 host2uint32_t_be(buffer_element->offset); 197 194 buffer_it->value = 198 199 } 200 201 int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 202 195 host2uint32_t_be(buffer_element->value); 196 } 197 198 int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 199 (uint8_t *) buffer, buffer_size, resp_buffer); 203 200 204 201 free(buffer); 205 202 free(resp_buffer); 206 203 207 if (rc != EOK) {204 if (rc != EOK) { 208 205 usb_log_error("Failed to write multi registry value.\n"); 209 206 return rc; … … 213 210 } 214 211 215 /** 216 * Send WMI message to HTC device. 217 * 218 * @param htc_device HTC device structure. 219 * @param command_id Command identification. 220 * @param command_buffer Buffer with command data. 221 * @param command_length Length of command data. 212 /** Send WMI message to HTC device. 213 * 214 * @param htc_device HTC device structure. 215 * @param command_id Command identification. 216 * @param command_buffer Buffer with command data. 217 * @param command_length Length of command data. 222 218 * @param response_buffer Buffer with response data. 223 * 224 * @return EOK if succeed, negative error code otherwise. 225 */ 226 int wmi_send_command(htc_device_t *htc_device, wmi_command_t command_id, 227 uint8_t *command_buffer, uint32_t command_length, void *response_buffer) 228 { 229 size_t header_size = sizeof(wmi_command_header_t) + 230 sizeof(htc_frame_header_t); 219 * 220 * @return EOK if succeed, negative error code otherwise. 221 * 222 */ 223 int wmi_send_command(htc_device_t *htc_device, wmi_command_t command_id, 224 uint8_t *command_buffer, uint32_t command_length, void *response_buffer) 225 { 226 size_t header_size = sizeof(wmi_command_header_t) + 227 sizeof(htc_frame_header_t); 231 228 size_t buffer_size = header_size + command_length; 232 229 void *buffer = malloc(buffer_size); 233 230 234 if (command_buffer != NULL) {231 if (command_buffer != NULL) 235 232 memcpy(buffer+header_size, command_buffer, command_length); 236 }237 233 238 234 /* Set up WMI header */ 239 235 wmi_command_header_t *wmi_header = (wmi_command_header_t *) 240 ((void *) buffer + sizeof(htc_frame_header_t)); 241 wmi_header->command_id = 242 host2uint16_t_be(command_id); 243 wmi_header->sequence_number = 244 host2uint16_t_be(++htc_device->sequence_number); 236 ((void *) buffer + sizeof(htc_frame_header_t)); 237 wmi_header->command_id = host2uint16_t_be(command_id); 238 wmi_header->sequence_number = 239 host2uint16_t_be(++htc_device->sequence_number); 245 240 246 241 /* Send message. */ 247 242 int rc = htc_send_control_message(htc_device, buffer, buffer_size, 248 249 if (rc != EOK) {243 htc_device->endpoints.wmi_endpoint); 244 if (rc != EOK) { 250 245 free(buffer); 251 246 usb_log_error("Failed to send WMI message. Error: %d\n", rc); … … 256 251 257 252 bool clean_resp_buffer = false; 258 size_t response_buffer_size = 259 260 if (response_buffer == NULL) {253 size_t response_buffer_size = 254 htc_device->ath_device->ctrl_response_length; 255 if (response_buffer == NULL) { 261 256 response_buffer = malloc(response_buffer_size); 262 257 clean_resp_buffer = true; … … 267 262 uint16_t cmd_id; 268 263 do { 269 rc = htc_read_control_message(htc_device, response_buffer, 270 271 if (rc != EOK) {264 rc = htc_read_control_message(htc_device, response_buffer, 265 response_buffer_size, NULL); 266 if (rc != EOK) { 272 267 free(buffer); 273 268 usb_log_error("Failed to receive WMI message response. " … … 276 271 } 277 272 278 if (response_buffer_size < sizeof(htc_frame_header_t) +279 273 if (response_buffer_size < sizeof(htc_frame_header_t) + 274 sizeof(wmi_command_header_t)) { 280 275 free(buffer); 281 276 usb_log_error("Corrupted response received.\n"); … … 283 278 } 284 279 285 wmi_command_header_t *wmi_hdr = (wmi_command_header_t *) 286 ((void*) response_buffer + sizeof(htc_frame_header_t));280 wmi_command_header_t *wmi_hdr = (wmi_command_header_t *) 281 ((void *) response_buffer + sizeof(htc_frame_header_t)); 287 282 cmd_id = uint16_t_be2host(wmi_hdr->command_id); 288 283 } while(cmd_id & WMI_MGMT_CMD_MASK); 289 284 290 if (clean_resp_buffer) {285 if (clean_resp_buffer) 291 286 free(response_buffer); 292 } 293 294 return rc; 295 } 287 288 return rc; 289 }
Note:
See TracChangeset
for help on using the changeset viewer.