[PATCH] Allow full 250mw (24dBm) on WRT54GL and related with wl legacy driver on...
[openwrt.git] / package / iwinfo / src / iwinfo_utils.c
1 /*
2 * iwinfo - Wireless Information Library - Shared utility routines
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17 *
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
21 */
22
23 #include "iwinfo/utils.h"
24
25
26 static int ioctl_socket = -1;
27
28 static int iwinfo_ioctl_socket(void)
29 {
30 /* Prepare socket */
31 if( ioctl_socket == -1 )
32 {
33 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
34 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
35 }
36
37 return ioctl_socket;
38 }
39
40 int iwinfo_ioctl(int cmd, void *ifr)
41 {
42 int s = iwinfo_ioctl_socket();
43 return ioctl(s, cmd, ifr);
44 }
45
46 int iwinfo_dbm2mw(int in)
47 {
48 double res = 1.0;
49 int ip = in / 10;
50 int fp = in % 10;
51 int k;
52
53 for(k = 0; k < ip; k++) res *= 10;
54 for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
55
56 return (int)res;
57 }
58
59 int iwinfo_mw2dbm(int in)
60 {
61 double fin = (double) in;
62 int res = 0;
63
64 while(fin > 10.0)
65 {
66 res += 10;
67 fin /= 10.0;
68 }
69
70 while(fin > 1.000001)
71 {
72 res += 1;
73 fin /= LOG10_MAGIC;
74 }
75
76 return (int)res;
77 }
78
79 int iwinfo_ifup(const char *ifname)
80 {
81 struct ifreq ifr;
82
83 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
84
85 if( iwinfo_ioctl(SIOCGIFFLAGS, &ifr) )
86 return 0;
87
88 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
89
90 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
91 }
92
93 int iwinfo_ifdown(const char *ifname)
94 {
95 struct ifreq ifr;
96
97 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
98
99 if( iwinfo_ioctl(SIOCGIFFLAGS, &ifr) )
100 return 0;
101
102 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
103
104 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
105 }
106
107 int iwinfo_ifmac(const char *ifname)
108 {
109 struct ifreq ifr;
110
111 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
112
113 if( iwinfo_ioctl(SIOCGIFHWADDR, &ifr) )
114 return 0;
115
116 ifr.ifr_hwaddr.sa_data[1]++;
117 ifr.ifr_hwaddr.sa_data[2]++;
118
119 return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
120 }
121
122 void iwinfo_close(void)
123 {
124 if( ioctl_socket > -1 )
125 close(ioctl_socket);
126 }
127
128 struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
129 {
130 const struct iwinfo_hardware_entry *e;
131
132 for (e = IWINFO_HARDWARE_ENTRIES; e->vendor_name; e++)
133 {
134 if ((e->vendor_id != 0xffff) && (e->vendor_id != id->vendor_id))
135 continue;
136
137 if ((e->device_id != 0xffff) && (e->device_id != id->device_id))
138 continue;
139
140 if ((e->subsystem_vendor_id != 0xffff) &&
141 (e->subsystem_vendor_id != id->subsystem_vendor_id))
142 continue;
143
144 if ((e->subsystem_device_id != 0xffff) &&
145 (e->subsystem_device_id != id->subsystem_device_id))
146 continue;
147
148 return e;
149 }
150
151 return NULL;
152 }
153
154 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
155 {
156 FILE *mtd;
157 uint16_t *bc;
158
159 int fd, len, off;
160 char buf[128];
161
162 if (!(mtd = fopen("/proc/mtd", "r")))
163 return -1;
164
165 while (fgets(buf, sizeof(buf), mtd) > 0)
166 {
167 if (fscanf(mtd, "mtd%d: %*x %x %127s", &off, &len, buf) < 3 ||
168 (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"")))
169 {
170 off = -1;
171 continue;
172 }
173
174 break;
175 }
176
177 fclose(mtd);
178
179 if (off < 0)
180 return -1;
181
182 snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
183
184 if ((fd = open(buf, O_RDONLY)) < 0)
185 return -1;
186
187 bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
188
189 if ((void *)bc != MAP_FAILED)
190 {
191 id->vendor_id = 0;
192 id->device_id = 0;
193
194 for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
195 {
196 /* AR531X board data magic */
197 if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
198 {
199 id->vendor_id = bc[off + 0x7d];
200 id->device_id = bc[off + 0x7c];
201 id->subsystem_vendor_id = bc[off + 0x84];
202 id->subsystem_device_id = bc[off + 0x83];
203 break;
204 }
205
206 /* AR5416 EEPROM magic */
207 else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
208 {
209 id->vendor_id = bc[off + 0x0D];
210 id->device_id = bc[off + 0x0E];
211 id->subsystem_vendor_id = bc[off + 0x13];
212 id->subsystem_device_id = bc[off + 0x14];
213 break;
214 }
215 }
216
217 munmap(bc, len);
218 }
219
220 close(fd);
221
222 return (id->vendor_id && id->device_id) ? 0 : -1;
223 }
This page took 0.051581 seconds and 5 git commands to generate.