2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
25 #include <cutils/properties.h>
26 #define LOG_TAG "TiwlanWifiController"
27 #include <cutils/log.h>
29 #include "PropertyManager.h"
30 #include "TiwlanWifiController.h"
31 #include "TiwlanEventListener.h"
33 #define DRIVER_PROP_NAME "wlan.driver.status"
35 extern "C" int sched_yield(void);
37 TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr,
38 IControllerHandler *handlers,
39 char *modpath, char *modname,
41 WifiController(propmngr, handlers, modpath, modname,
43 mEventListener = NULL;
47 int TiwlanWifiController::powerUp() {
48 return 0; // Powerup is currently done when the driver is loaded
51 int TiwlanWifiController::powerDown() {
53 delete mEventListener;
54 shutdown(mListenerSock, SHUT_RDWR);
57 mEventListener = NULL;
60 return 0; // Powerdown is currently done when the driver is unloaded
63 bool TiwlanWifiController::isPoweredUp() {
64 return isKernelModuleLoaded(getModuleName());
67 int TiwlanWifiController::loadFirmware() {
68 char driver_status[PROPERTY_VALUE_MAX];
71 property_set("ctl.start", "wlan_loader");
74 // Wait for driver to be ready
76 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
77 if (!strcmp(driver_status, "ok")) {
78 LOGD("Firmware loaded OK");
80 if (startDriverEventListener()) {
81 LOGW("Failed to start driver event listener (%s)",
86 } else if (!strcmp(DRIVER_PROP_NAME, "failed")) {
87 LOGE("Firmware load failed");
93 property_set(DRIVER_PROP_NAME, "timeout");
94 LOGE("Firmware load timed out");
98 int TiwlanWifiController::startDriverEventListener() {
99 struct sockaddr_in addr;
101 if (mListenerSock != -1) {
102 LOGE("Listener already started!");
107 if ((mListenerSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
108 LOGE("socket failed (%s)", strerror(errno));
112 memset(&addr, 0, sizeof(addr));
113 addr.sin_family = AF_INET;
114 addr.sin_addr.s_addr = htonl(INADDR_ANY);
115 addr.sin_port = htons(TI_DRIVER_MSG_PORT);
117 if (bind(mListenerSock,
118 (struct sockaddr *) &addr,
120 LOGE("bind failed (%s)", strerror(errno));
124 mEventListener = new TiwlanEventListener(mListenerSock);
126 if (mEventListener->startListener()) {
127 LOGE("Error starting driver listener (%s)", strerror(errno));
132 if (mEventListener) {
133 delete mEventListener;
134 mEventListener = NULL;
136 if (mListenerSock != -1) {
137 shutdown(mListenerSock, SHUT_RDWR);
138 close(mListenerSock);
144 bool TiwlanWifiController::isFirmwareLoaded() {
145 // Always load the firmware