San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <arpa/inet.h> |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 24 | |
| 25 | #include <cutils/properties.h> |
| 26 | #define LOG_TAG "TiwlanWifiController" |
| 27 | #include <cutils/log.h> |
| 28 | |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 29 | #include "PropertyManager.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 30 | #include "TiwlanWifiController.h" |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 31 | #include "TiwlanEventListener.h" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 32 | |
| 33 | #define DRIVER_PROP_NAME "wlan.driver.status" |
| 34 | |
| 35 | extern "C" int sched_yield(void); |
| 36 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame] | 37 | TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr, |
| 38 | IControllerHandler *handlers, |
| 39 | char *modpath, char *modname, |
| 40 | char *modargs) : |
| 41 | WifiController(propmngr, handlers, modpath, modname, |
| 42 | modargs) { |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 43 | mEventListener = NULL; |
| 44 | mListenerSock = -1; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int TiwlanWifiController::powerUp() { |
| 48 | return 0; // Powerup is currently done when the driver is loaded |
| 49 | } |
| 50 | |
| 51 | int TiwlanWifiController::powerDown() { |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 52 | if (mEventListener) { |
| 53 | delete mEventListener; |
| 54 | close(mListenerSock); |
| 55 | mListenerSock = -1; |
| 56 | mEventListener = NULL; |
| 57 | } |
| 58 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 59 | return 0; // Powerdown is currently done when the driver is unloaded |
| 60 | } |
| 61 | |
| 62 | bool TiwlanWifiController::isPoweredUp() { |
| 63 | return isKernelModuleLoaded(getModuleName()); |
| 64 | } |
| 65 | |
| 66 | int TiwlanWifiController::loadFirmware() { |
| 67 | char driver_status[PROPERTY_VALUE_MAX]; |
| 68 | int count = 100; |
| 69 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 70 | property_set("ctl.start", "wlan_loader"); |
| 71 | sched_yield(); |
| 72 | |
| 73 | // Wait for driver to be ready |
| 74 | while (count-- > 0) { |
| 75 | if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) { |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 76 | if (!strcmp(driver_status, "ok")) { |
| 77 | LOGD("Firmware loaded OK"); |
| 78 | |
| 79 | if (startDriverEventListener()) { |
| 80 | LOGW("Failed to start driver event listener"); |
| 81 | } |
| 82 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 83 | return 0; |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 84 | } else if (!strcmp(DRIVER_PROP_NAME, "failed")) { |
| 85 | LOGE("Firmware load failed"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 86 | return -1; |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 87 | } |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 88 | } |
| 89 | usleep(200000); |
| 90 | } |
| 91 | property_set(DRIVER_PROP_NAME, "timeout"); |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 92 | LOGE("Firmware load timed out"); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 93 | return -1; |
| 94 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 95 | |
San Mehat | 5fc4129 | 2009-06-16 10:50:06 -0700 | [diff] [blame] | 96 | int TiwlanWifiController::startDriverEventListener() { |
| 97 | struct sockaddr_in addr; |
| 98 | int s; |
| 99 | |
| 100 | if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 101 | return -1; |
| 102 | |
| 103 | memset(&addr, 0, sizeof(addr)); |
| 104 | addr.sin_family = AF_INET; |
| 105 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 106 | addr.sin_port = htons(TI_DRIVER_MSG_PORT); |
| 107 | |
| 108 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 109 | close(s); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | mEventListener = new TiwlanEventListener(s); |
| 114 | |
| 115 | if (mEventListener->startListener()) { |
| 116 | LOGE("Error starting driver listener (%s)", strerror(errno)); |
| 117 | delete mEventListener; |
| 118 | mEventListener = NULL; |
| 119 | close(s); |
| 120 | return -1; |
| 121 | } |
| 122 | mListenerSock = s; |
| 123 | return 0; |
| 124 | } |
| 125 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 126 | bool TiwlanWifiController::isFirmwareLoaded() { |
San Mehat | 69772dc | 2009-05-10 09:27:07 -0700 | [diff] [blame] | 127 | // Always load the firmware |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |