blob: 016c790747aa9b24fe8456431ac86ec38ca27679 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
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 Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#include <stdlib.h>
18#include <fcntl.h>
19#include <errno.h>
20#include <string.h>
San Mehat5fc41292009-06-16 10:50:06 -070021#include <sys/types.h>
22#include <sys/socket.h>
23#include <arpa/inet.h>
San Mehatdc266072009-05-06 11:16:52 -070024
25#include <cutils/properties.h>
26#define LOG_TAG "TiwlanWifiController"
27#include <cutils/log.h>
28
San Mehat3c5a6f02009-05-22 15:36:13 -070029#include "PropertyManager.h"
San Mehatdc266072009-05-06 11:16:52 -070030#include "TiwlanWifiController.h"
San Mehat5fc41292009-06-16 10:50:06 -070031#include "TiwlanEventListener.h"
San Mehatdc266072009-05-06 11:16:52 -070032
33#define DRIVER_PROP_NAME "wlan.driver.status"
34
35extern "C" int sched_yield(void);
36
San Mehat3aff2d12009-06-15 14:10:44 -070037TiwlanWifiController::TiwlanWifiController(PropertyManager *propmngr,
38 IControllerHandler *handlers,
39 char *modpath, char *modname,
40 char *modargs) :
41 WifiController(propmngr, handlers, modpath, modname,
42 modargs) {
San Mehat5fc41292009-06-16 10:50:06 -070043 mEventListener = NULL;
44 mListenerSock = -1;
San Mehatdc266072009-05-06 11:16:52 -070045}
46
47int TiwlanWifiController::powerUp() {
48 return 0; // Powerup is currently done when the driver is loaded
49}
50
51int TiwlanWifiController::powerDown() {
San Mehat5fc41292009-06-16 10:50:06 -070052 if (mEventListener) {
53 delete mEventListener;
San Mehatc4a895b2009-06-23 21:10:57 -070054 shutdown(mListenerSock, SHUT_RDWR);
San Mehat5fc41292009-06-16 10:50:06 -070055 close(mListenerSock);
56 mListenerSock = -1;
57 mEventListener = NULL;
58 }
59
San Mehatdc266072009-05-06 11:16:52 -070060 return 0; // Powerdown is currently done when the driver is unloaded
61}
62
63bool TiwlanWifiController::isPoweredUp() {
64 return isKernelModuleLoaded(getModuleName());
65}
66
67int TiwlanWifiController::loadFirmware() {
68 char driver_status[PROPERTY_VALUE_MAX];
69 int count = 100;
70
San Mehatdc266072009-05-06 11:16:52 -070071 property_set("ctl.start", "wlan_loader");
72 sched_yield();
73
74 // Wait for driver to be ready
75 while (count-- > 0) {
76 if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
San Mehat5fc41292009-06-16 10:50:06 -070077 if (!strcmp(driver_status, "ok")) {
78 LOGD("Firmware loaded OK");
79
80 if (startDriverEventListener()) {
San Mehatc4a895b2009-06-23 21:10:57 -070081 LOGW("Failed to start driver event listener (%s)",
82 strerror(errno));
San Mehat5fc41292009-06-16 10:50:06 -070083 }
84
San Mehatdc266072009-05-06 11:16:52 -070085 return 0;
San Mehat5fc41292009-06-16 10:50:06 -070086 } else if (!strcmp(DRIVER_PROP_NAME, "failed")) {
87 LOGE("Firmware load failed");
San Mehatdc266072009-05-06 11:16:52 -070088 return -1;
San Mehat5fc41292009-06-16 10:50:06 -070089 }
San Mehatdc266072009-05-06 11:16:52 -070090 }
91 usleep(200000);
92 }
93 property_set(DRIVER_PROP_NAME, "timeout");
San Mehat5fc41292009-06-16 10:50:06 -070094 LOGE("Firmware load timed out");
San Mehatdc266072009-05-06 11:16:52 -070095 return -1;
96}
San Mehat1441e762009-05-07 11:37:10 -070097
San Mehat5fc41292009-06-16 10:50:06 -070098int TiwlanWifiController::startDriverEventListener() {
99 struct sockaddr_in addr;
San Mehat5fc41292009-06-16 10:50:06 -0700100
San Mehatc4a895b2009-06-23 21:10:57 -0700101 if (mListenerSock != -1) {
102 LOGE("Listener already started!");
103 errno = EBUSY;
San Mehat5fc41292009-06-16 10:50:06 -0700104 return -1;
San Mehatc4a895b2009-06-23 21:10:57 -0700105 }
106
107 if ((mListenerSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
108 LOGE("socket failed (%s)", strerror(errno));
109 return -1;
110 }
San Mehat5fc41292009-06-16 10:50:06 -0700111
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);
116
San Mehatc4a895b2009-06-23 21:10:57 -0700117 if (bind(mListenerSock,
118 (struct sockaddr *) &addr,
119 sizeof(addr)) < 0) {
120 LOGE("bind failed (%s)", strerror(errno));
121 goto out_err;
San Mehat5fc41292009-06-16 10:50:06 -0700122 }
123
San Mehatc4a895b2009-06-23 21:10:57 -0700124 mEventListener = new TiwlanEventListener(mListenerSock);
San Mehat5fc41292009-06-16 10:50:06 -0700125
126 if (mEventListener->startListener()) {
127 LOGE("Error starting driver listener (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -0700128 goto out_err;
129 }
130 return 0;
131out_err:
132 if (mEventListener) {
San Mehat5fc41292009-06-16 10:50:06 -0700133 delete mEventListener;
134 mEventListener = NULL;
San Mehat5fc41292009-06-16 10:50:06 -0700135 }
San Mehatc4a895b2009-06-23 21:10:57 -0700136 if (mListenerSock != -1) {
137 shutdown(mListenerSock, SHUT_RDWR);
138 close(mListenerSock);
139 mListenerSock = -1;
140 }
141 return -1;
San Mehat5fc41292009-06-16 10:50:06 -0700142}
143
San Mehat1441e762009-05-07 11:37:10 -0700144bool TiwlanWifiController::isFirmwareLoaded() {
San Mehat69772dc2009-05-10 09:27:07 -0700145 // Always load the firmware
San Mehat1441e762009-05-07 11:37:10 -0700146 return false;
147}