Changeset 8a64320e in mainline for uspace/drv/nic/ar9271/htc.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/htc.c
r09044cb r8a64320e 36 36 #include <byteorder.h> 37 37 #include <errno.h> 38 39 38 #include "wmi.h" 40 39 #include "htc.h" … … 42 41 #include "ar9271.h" 43 42 44 /** 45 * HTC download pipes mapping. 46 * 43 /** HTC download pipes mapping. 44 * 47 45 * @param service_id Identification of WMI service. 48 * 46 * 49 47 * @return Number of pipe used for this service. 48 * 50 49 */ 51 50 static inline uint8_t wmi_service_to_download_pipe(wmi_services_t service_id) … … 54 53 } 55 54 56 /** 57 * HTC upload pipes mapping. 58 * 55 /** HTC upload pipes mapping. 56 * 59 57 * @param service_id Identification of WMI service. 60 * 58 * 61 59 * @return Number of pipe used for this service. 60 * 62 61 */ 63 62 static inline uint8_t wmi_service_to_upload_pipe(wmi_services_t service_id) … … 75 74 76 75 nic_address_t addr; 77 nic_t *nic = 78 nic_get_from_ddf_dev( 79 ieee80211_get_ddf_dev(htc_device->ieee80211_dev) 80 ); 76 nic_t *nic = 77 nic_get_from_ddf_dev(ieee80211_get_ddf_dev(htc_device->ieee80211_dev)); 81 78 nic_query_address(nic, &addr); 82 79 … … 84 81 memcpy(&sta_msg.addr, &addr.address, ETH_ADDR); 85 82 86 ieee80211_operating_mode_t op_mode = 87 88 89 switch (op_mode) {90 91 92 93 94 95 96 97 98 99 100 101 83 ieee80211_operating_mode_t op_mode = 84 ieee80211_query_current_op_mode(htc_device->ieee80211_dev); 85 86 switch (op_mode) { 87 case IEEE80211_OPMODE_ADHOC: 88 vif_msg.op_mode = HTC_OPMODE_ADHOC; 89 break; 90 case IEEE80211_OPMODE_AP: 91 vif_msg.op_mode = HTC_OPMODE_AP; 92 break; 93 case IEEE80211_OPMODE_MESH: 94 vif_msg.op_mode = HTC_OPMODE_MESH; 95 break; 96 case IEEE80211_OPMODE_STATION: 97 vif_msg.op_mode = HTC_OPMODE_STATION; 98 break; 102 99 } 103 100 … … 105 102 vif_msg.rts_thres = host2uint16_t_be(HTC_RTS_THRESHOLD); 106 103 107 wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg, 108 104 wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg, 105 sizeof(vif_msg), NULL); 109 106 110 107 sta_msg.is_vif_sta = 1; … … 113 110 sta_msg.vif_index = 0; 114 111 115 wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg, 116 112 wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg, 113 sizeof(sta_msg), NULL); 117 114 118 115 /* Write first 4 bytes of MAC address. */ … … 121 118 id0 = host2uint32_t_le(id0); 122 119 wmi_reg_write(htc_device, AR9271_STATION_ID0, id0); 123 120 124 121 /* Write last 2 bytes of MAC address (and preserve existing data). */ 125 122 uint32_t id1; 126 123 wmi_reg_read(htc_device, AR9271_STATION_ID1, &id1); 127 124 128 125 uint16_t id1_addr; 129 126 memcpy(&id1_addr, &addr.address[4], 2); … … 134 131 } 135 132 136 static void htc_config_frame_header(htc_frame_header_t *header, 137 133 static void htc_config_frame_header(htc_frame_header_t *header, 134 size_t buffer_size, uint8_t endpoint_id) 138 135 { 139 136 memset(header, 0, sizeof(htc_frame_header_t)); 140 137 141 138 header->endpoint_id = endpoint_id; 142 header->payload_length = 143 host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t)); 144 } 145 146 /** 147 * Send control HTC message to USB device. 148 * 149 * @param htc_device HTC device structure. 150 * @param buffer Buffer with data to be sent to USB device (without HTC frame 151 * header). 139 header->payload_length = 140 host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t)); 141 } 142 143 /** Send control HTC message to USB device. 144 * 145 * @param htc_device HTC device structure. 146 * @param buffer Buffer with data to be sent to USB device 147 * (without HTC frame header). 152 148 * @param buffer_size Size of buffer (including HTC frame header). 153 149 * @param endpoint_id Destination endpoint. 154 * 155 * @return EOK if succeed, negative error code otherwise. 156 */ 157 int htc_send_control_message(htc_device_t *htc_device, void *buffer, 158 size_t buffer_size, uint8_t endpoint_id) 150 * 151 * @return EOK if succeed, negative error code otherwise. 152 * 153 */ 154 int htc_send_control_message(htc_device_t *htc_device, void *buffer, 155 size_t buffer_size, uint8_t endpoint_id) 159 156 { 160 157 htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size, 161 158 endpoint_id); 162 159 163 160 ath_t *ath_device = htc_device->ath_device; 164 161 165 return ath_device->ops->send_ctrl_message(ath_device, buffer, 166 buffer_size); 167 } 168 169 /** 170 * Send data HTC message to USB device. 171 * 172 * @param htc_device HTC device structure. 173 * @param buffer Buffer with data to be sent to USB device (without HTC frame 174 * header). 162 return ath_device->ops->send_ctrl_message(ath_device, buffer, 163 buffer_size); 164 } 165 166 /** Send data HTC message to USB device. 167 * 168 * @param htc_device HTC device structure. 169 * @param buffer Buffer with data to be sent to USB device 170 * (without HTC frame header). 175 171 * @param buffer_size Size of buffer (including HTC frame header). 176 172 * @param endpoint_id Destination endpoint. 177 * 178 * @return EOK if succeed, negative error code otherwise. 179 */ 180 int htc_send_data_message(htc_device_t *htc_device, void *buffer, 181 size_t buffer_size, uint8_t endpoint_id) 173 * 174 * @return EOK if succeed, negative error code otherwise. 175 * 176 */ 177 int htc_send_data_message(htc_device_t *htc_device, void *buffer, 178 size_t buffer_size, uint8_t endpoint_id) 182 179 { 183 180 htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size, 184 181 endpoint_id); 185 182 186 183 ath_t *ath_device = htc_device->ath_device; 187 184 188 return ath_device->ops->send_data_message(ath_device, buffer, 189 190 } 191 192 /** 193 * Read HTC data message from USB device.194 * 195 * @param htc_device HTC device structure.196 * @param buffer Buffer where data from USB devicewill be stored.197 * @param buffer_size Size of buffer.185 return ath_device->ops->send_data_message(ath_device, buffer, 186 buffer_size); 187 } 188 189 /** Read HTC data message from USB device. 190 * 191 * @param htc_device HTC device structure. 192 * @param buffer Buffer where data from USB device 193 * will be stored. 194 * @param buffer_size Size of buffer. 198 195 * @param transferred_size Real size of read data. 199 * 200 * @return EOK if succeed, negative error code otherwise. 201 */ 202 int htc_read_data_message(htc_device_t *htc_device, void *buffer, 203 size_t buffer_size, size_t *transferred_size) 196 * 197 * @return EOK if succeed, negative error code otherwise. 198 * 199 */ 200 int htc_read_data_message(htc_device_t *htc_device, void *buffer, 201 size_t buffer_size, size_t *transferred_size) 204 202 { 205 203 ath_t *ath_device = htc_device->ath_device; 206 204 207 return ath_device->ops->read_data_message(ath_device, buffer, 208 209 } 210 211 /** 212 * Read HTC control message from USB device.213 * 214 * @param htc_device HTC device structure.215 * @param buffer Buffer where data from USB devicewill be stored.216 * @param buffer_size Size of buffer.205 return ath_device->ops->read_data_message(ath_device, buffer, 206 buffer_size, transferred_size); 207 } 208 209 /** Read HTC control message from USB device. 210 * 211 * @param htc_device HTC device structure. 212 * @param buffer Buffer where data from USB device 213 * will be stored. 214 * @param buffer_size Size of buffer. 217 215 * @param transferred_size Real size of read data. 218 * 219 * @return EOK if succeed, negative error code otherwise. 220 */ 221 int htc_read_control_message(htc_device_t *htc_device, void *buffer, 222 size_t buffer_size, size_t *transferred_size) 216 * 217 * @return EOK if succeed, negative error code otherwise. 218 * 219 */ 220 int htc_read_control_message(htc_device_t *htc_device, void *buffer, 221 size_t buffer_size, size_t *transferred_size) 223 222 { 224 223 ath_t *ath_device = htc_device->ath_device; 225 224 226 return ath_device->ops->read_ctrl_message(ath_device, buffer, 227 buffer_size, transferred_size); 228 } 229 230 /** 231 * Initialize HTC service. 232 * 233 * @param htc_device HTC device structure. 234 * @param service_id Identification of WMI service. 235 * @param response_endpoint_no HTC endpoint to be used for this service. 236 * 237 * @return EOK if succeed, EINVAL when failed to connect service, 238 * negative error code otherwise. 239 */ 240 static int htc_connect_service(htc_device_t *htc_device, 225 return ath_device->ops->read_ctrl_message(ath_device, buffer, 226 buffer_size, transferred_size); 227 } 228 229 /** Initialize HTC service. 230 * 231 * @param htc_device HTC device structure. 232 * @param service_id Identification of WMI service. 233 * @param response_endpoint_no HTC endpoint to be used for 234 * this service. 235 * 236 * @return EOK if succeed, EINVAL when failed to connect service, 237 * negative error code otherwise. 238 * 239 */ 240 static int htc_connect_service(htc_device_t *htc_device, 241 241 wmi_services_t service_id, int *response_endpoint_no) 242 242 { 243 size_t buffer_size = sizeof(htc_frame_header_t) + 244 243 size_t buffer_size = sizeof(htc_frame_header_t) + 244 sizeof(htc_service_msg_t); 245 245 void *buffer = malloc(buffer_size); 246 246 memset(buffer, 0, buffer_size); … … 248 248 /* Fill service message structure. */ 249 249 htc_service_msg_t *service_message = (htc_service_msg_t *) 250 251 service_message->service_id = 252 253 service_message->message_id = 254 255 service_message->download_pipe_id = 256 257 service_message->upload_pipe_id = 258 250 ((void *) buffer + sizeof(htc_frame_header_t)); 251 service_message->service_id = 252 host2uint16_t_be(service_id); 253 service_message->message_id = 254 host2uint16_t_be(HTC_MESSAGE_CONNECT_SERVICE); 255 service_message->download_pipe_id = 256 wmi_service_to_download_pipe(service_id); 257 service_message->upload_pipe_id = 258 wmi_service_to_upload_pipe(service_id); 259 259 service_message->connection_flags = 0; 260 260 261 261 /* Send HTC message. */ 262 262 int rc = htc_send_control_message(htc_device, buffer, buffer_size, 263 264 if (rc != EOK) {263 htc_device->endpoints.ctrl_endpoint); 264 if (rc != EOK) { 265 265 free(buffer); 266 266 usb_log_error("Failed to send HTC message. Error: %d\n", rc); … … 275 275 /* Read response from device. */ 276 276 rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL); 277 if (rc != EOK) {277 if (rc != EOK) { 278 278 free(buffer); 279 279 usb_log_error("Failed to receive HTC service connect response. " 280 280 "Error: %d\n", rc); 281 281 return rc; 282 282 } 283 283 284 284 htc_service_resp_msg_t *response_message = (htc_service_resp_msg_t *) 285 ((void *) buffer + sizeof(htc_frame_header_t)); 286 287 /* If service was successfully connected, write down HTC endpoint number 288 * that will be used for communication. */ 289 if(response_message->status == HTC_SERVICE_SUCCESS) { 285 ((void *) buffer + sizeof(htc_frame_header_t)); 286 287 /* 288 * If service was successfully connected, 289 * write down HTC endpoint number that will 290 * be used for communication. 291 */ 292 if (response_message->status == HTC_SERVICE_SUCCESS) { 290 293 *response_endpoint_no = response_message->endpoint_id; 291 294 rc = EOK; 292 295 } else { 293 296 usb_log_error("Failed to connect HTC service. " 294 297 "Message status: %d\n", response_message->status); 295 298 rc = EINVAL; 296 299 } 297 300 298 301 free(buffer); … … 301 304 } 302 305 303 /** 304 * HTC credits initialization message. 305 * 306 /** HTC credits initialization message. 307 * 306 308 * @param htc_device HTC device structure. 307 * 308 * @return EOK if succeed, negative error code otherwise. 309 * 310 * @return EOK if succeed, negative error code otherwise. 311 * 309 312 */ 310 313 static int htc_config_credits(htc_device_t *htc_device) 311 314 { 312 size_t buffer_size = sizeof(htc_frame_header_t) + 313 315 size_t buffer_size = sizeof(htc_frame_header_t) + 316 sizeof(htc_config_msg_t); 314 317 void *buffer = malloc(buffer_size); 315 318 htc_config_msg_t *config_message = (htc_config_msg_t *) 316 319 ((void *) buffer + sizeof(htc_frame_header_t)); 317 320 318 321 config_message->message_id = 319 322 host2uint16_t_be(HTC_MESSAGE_CONFIG); 320 323 /* Magic number to initialize device. */ 321 324 config_message->credits = 33; 322 325 config_message->pipe_id = 1; 323 326 324 327 /* Send HTC message. */ 325 328 int rc = htc_send_control_message(htc_device, buffer, buffer_size, 326 327 if (rc != EOK) {329 htc_device->endpoints.ctrl_endpoint); 330 if (rc != EOK) { 328 331 free(buffer); 329 332 usb_log_error("Failed to send HTC config message. " 330 333 "Error: %d\n", rc); 331 334 return rc; 332 335 } … … 336 339 buffer_size = htc_device->ath_device->ctrl_response_length; 337 340 buffer = malloc(buffer_size); 338 341 339 342 /* Check response from device. */ 340 343 rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL); 341 if (rc != EOK) {344 if (rc != EOK) { 342 345 usb_log_error("Failed to receive HTC config response message. " 343 346 "Error: %d\n", rc); 344 347 } 345 348 346 349 free(buffer); 347 350 348 351 return rc; 349 352 } 350 353 351 /** 352 * HTC setup complete confirmation message. 353 * 354 /** HTC setup complete confirmation message. 355 * 354 356 * @param htc_device HTC device structure. 355 * 356 * @return EOK if succeed, negative error code otherwise. 357 * 358 * @return EOK if succeed, negative error code otherwise. 359 * 357 360 */ 358 361 static int htc_complete_setup(htc_device_t *htc_device) 359 362 { 360 size_t buffer_size = sizeof(htc_frame_header_t) + 361 363 size_t buffer_size = sizeof(htc_frame_header_t) + 364 sizeof(htc_setup_complete_msg_t); 362 365 void *buffer = malloc(buffer_size); 363 htc_setup_complete_msg_t *complete_message = 364 365 366 367 complete_message->message_id = 368 369 366 htc_setup_complete_msg_t *complete_message = 367 (htc_setup_complete_msg_t *) 368 ((void *) buffer + sizeof(htc_frame_header_t)); 369 370 complete_message->message_id = 371 host2uint16_t_be(HTC_MESSAGE_SETUP_COMPLETE); 372 370 373 /* Send HTC message. */ 371 int rc = htc_send_control_message(htc_device, buffer, buffer_size, 372 373 if (rc != EOK) {374 int rc = htc_send_control_message(htc_device, buffer, buffer_size, 375 htc_device->endpoints.ctrl_endpoint); 376 if (rc != EOK) 374 377 usb_log_error("Failed to send HTC setup complete message. " 375 "Error: %d\n", rc); 376 } 378 "Error: %d\n", rc); 377 379 378 380 free(buffer); 379 381 380 382 return rc; 381 383 } 382 384 383 /** 384 * Try to fetch ready message from device.385 /** Try to fetch ready message from device. 386 * 385 387 * Checks that firmware was successfully loaded on device side. 386 * 388 * 387 389 * @param htc_device HTC device structure. 388 * 389 * @return EOK if succeed, EINVAL if response error, negative error code 390 * otherwise. 390 * 391 * @return EOK if succeed, EINVAL if response error, 392 * negative error code otherwise. 393 * 391 394 */ 392 395 static int htc_check_ready(htc_device_t *htc_device) … … 394 397 size_t buffer_size = htc_device->ath_device->ctrl_response_length; 395 398 void *buffer = malloc(buffer_size); 396 399 397 400 /* Read response from device. */ 398 int rc = htc_read_control_message(htc_device, buffer, buffer_size, 399 400 if (rc != EOK) {401 int rc = htc_read_control_message(htc_device, buffer, buffer_size, 402 NULL); 403 if (rc != EOK) { 401 404 free(buffer); 402 405 usb_log_error("Failed to receive HTC check ready message. " 403 404 return rc; 405 } 406 407 uint16_t *message_id = (uint16_t *) ((void *) buffer + 408 409 if (uint16_t_be2host(*message_id) == HTC_MESSAGE_READY) {406 "Error: %d\n", rc); 407 return rc; 408 } 409 410 uint16_t *message_id = (uint16_t *) ((void *) buffer + 411 sizeof(htc_frame_header_t)); 412 if (uint16_t_be2host(*message_id) == HTC_MESSAGE_READY) 410 413 rc = EOK; 411 } else {414 else 412 415 rc = EINVAL; 413 }414 416 415 417 free(buffer); … … 418 420 } 419 421 420 /** 421 * Initialize HTC device structure.422 * 423 * @param ath_device Atheros WiFi device connectedwith this HTC device.422 /** Initialize HTC device structure. 423 * 424 * @param ath_device Atheros WiFi device connected 425 * with this HTC device. 424 426 * @param htc_device HTC device structure to be initialized. 425 * 426 * @return EOK if succeed, negative error code otherwise. 427 */ 428 int htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev, 429 htc_device_t *htc_device) 427 * 428 * @return EOK if succeed, negative error code otherwise. 429 * 430 */ 431 int htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev, 432 htc_device_t *htc_device) 430 433 { 431 434 fibril_mutex_initialize(&htc_device->rx_lock); … … 440 443 } 441 444 442 /** 443 * HTC communication initalization. 444 * 445 /** HTC communication initalization. 446 * 445 447 * @param htc_device HTC device structure. 446 * 447 * @return EOK if succeed, negative error code otherwise. 448 * 449 * @return EOK if succeed, negative error code otherwise. 450 * 448 451 */ 449 452 int htc_init(htc_device_t *htc_device) … … 451 454 /* First check ready message in device. */ 452 455 int rc = htc_check_ready(htc_device); 453 if (rc != EOK) {456 if (rc != EOK) { 454 457 usb_log_error("Device is not in ready state after loading " 455 456 return rc; 457 } 458 459 /* 458 "firmware.\n"); 459 return rc; 460 } 461 462 /* 460 463 * HTC services initialization start. 461 *462 464 */ 463 464 465 rc = htc_connect_service(htc_device, WMI_CONTROL_SERVICE, 465 466 &htc_device->endpoints.wmi_endpoint); 466 if (rc != EOK) {467 if (rc != EOK) { 467 468 usb_log_error("Error while initalizing WMI service.\n"); 468 469 return rc; 469 470 } 470 471 471 472 rc = htc_connect_service(htc_device, WMI_BEACON_SERVICE, 472 473 &htc_device->endpoints.beacon_endpoint); 473 if (rc != EOK) {474 if (rc != EOK) { 474 475 usb_log_error("Error while initalizing beacon service.\n"); 475 476 return rc; 476 477 } 477 478 478 479 rc = htc_connect_service(htc_device, WMI_CAB_SERVICE, 479 480 &htc_device->endpoints.cab_endpoint); 480 if (rc != EOK) {481 if (rc != EOK) { 481 482 usb_log_error("Error while initalizing CAB service.\n"); 482 483 return rc; 483 484 } 484 485 485 486 rc = htc_connect_service(htc_device, WMI_UAPSD_SERVICE, 486 487 &htc_device->endpoints.uapsd_endpoint); 487 if (rc != EOK) {488 if (rc != EOK) { 488 489 usb_log_error("Error while initalizing UAPSD service.\n"); 489 490 return rc; 490 491 } 491 492 492 493 rc = htc_connect_service(htc_device, WMI_MGMT_SERVICE, 493 494 &htc_device->endpoints.mgmt_endpoint); 494 if (rc != EOK) {495 if (rc != EOK) { 495 496 usb_log_error("Error while initalizing MGMT service.\n"); 496 497 return rc; … … 499 500 rc = htc_connect_service(htc_device, WMI_DATA_BE_SERVICE, 500 501 &htc_device->endpoints.data_be_endpoint); 501 if (rc != EOK) {502 if (rc != EOK) { 502 503 usb_log_error("Error while initalizing data best effort " 503 504 "service.\n"); 504 505 return rc; 505 506 } 506 507 507 508 rc = htc_connect_service(htc_device, WMI_DATA_BK_SERVICE, 508 509 &htc_device->endpoints.data_bk_endpoint); 509 if (rc != EOK) {510 if (rc != EOK) { 510 511 usb_log_error("Error while initalizing data background " 511 512 "service.\n"); 512 513 return rc; 513 514 } 514 515 515 516 rc = htc_connect_service(htc_device, WMI_DATA_VIDEO_SERVICE, 516 517 &htc_device->endpoints.data_video_endpoint); 517 if (rc != EOK) {518 if (rc != EOK) { 518 519 usb_log_error("Error while initalizing data video service.\n"); 519 520 return rc; … … 522 523 rc = htc_connect_service(htc_device, WMI_DATA_VOICE_SERVICE, 523 524 &htc_device->endpoints.data_voice_endpoint); 524 if (rc != EOK) {525 if (rc != EOK) { 525 526 usb_log_error("Error while initalizing data voice service.\n"); 526 527 return rc; 527 528 } 528 529 /* 529 530 /* 530 531 * HTC services initialization end. 531 *532 532 */ 533 533 534 534 /* Credits initialization message. */ 535 535 rc = htc_config_credits(htc_device); 536 if (rc != EOK) {536 if (rc != EOK) { 537 537 usb_log_error("Failed to send HTC config message.\n"); 538 538 return rc; 539 539 } 540 540 541 541 /* HTC setup complete confirmation message. */ 542 542 rc = htc_complete_setup(htc_device); 543 if (rc != EOK) {543 if (rc != EOK) { 544 544 usb_log_error("Failed to send HTC complete setup message.\n"); 545 545 return rc; 546 546 } 547 547 548 548 usb_log_info("HTC services initialization finished successfully.\n"); 549 549
Note:
See TracChangeset
for help on using the changeset viewer.