xref: /openbmc/linux/drivers/net/wireless/intel/ipw2x00/libipw_geo.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*c891f3b9SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2367a1092SKalle Valo /******************************************************************************
3367a1092SKalle Valo 
4367a1092SKalle Valo   Copyright(c) 2005 Intel Corporation. All rights reserved.
5367a1092SKalle Valo 
6367a1092SKalle Valo 
7367a1092SKalle Valo   Contact Information:
8367a1092SKalle Valo   Intel Linux Wireless <ilw@linux.intel.com>
9367a1092SKalle Valo   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
10367a1092SKalle Valo 
11367a1092SKalle Valo ******************************************************************************/
12367a1092SKalle Valo #include <linux/compiler.h>
13367a1092SKalle Valo #include <linux/errno.h>
14367a1092SKalle Valo #include <linux/if_arp.h>
15367a1092SKalle Valo #include <linux/in6.h>
16367a1092SKalle Valo #include <linux/in.h>
17367a1092SKalle Valo #include <linux/ip.h>
18367a1092SKalle Valo #include <linux/kernel.h>
19367a1092SKalle Valo #include <linux/module.h>
20367a1092SKalle Valo #include <linux/netdevice.h>
21367a1092SKalle Valo #include <linux/proc_fs.h>
22367a1092SKalle Valo #include <linux/skbuff.h>
23367a1092SKalle Valo #include <linux/tcp.h>
24367a1092SKalle Valo #include <linux/types.h>
25367a1092SKalle Valo #include <linux/wireless.h>
26367a1092SKalle Valo #include <linux/etherdevice.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
28367a1092SKalle Valo 
29367a1092SKalle Valo #include "libipw.h"
30367a1092SKalle Valo 
libipw_is_valid_channel(struct libipw_device * ieee,u8 channel)31367a1092SKalle Valo int libipw_is_valid_channel(struct libipw_device *ieee, u8 channel)
32367a1092SKalle Valo {
33367a1092SKalle Valo 	int i;
34367a1092SKalle Valo 
35367a1092SKalle Valo 	/* Driver needs to initialize the geography map before using
36367a1092SKalle Valo 	 * these helper functions */
37367a1092SKalle Valo 	if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
38367a1092SKalle Valo 		return 0;
39367a1092SKalle Valo 
40367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_24GHZ_BAND)
41367a1092SKalle Valo 		for (i = 0; i < ieee->geo.bg_channels; i++)
42367a1092SKalle Valo 			/* NOTE: If G mode is currently supported but
43367a1092SKalle Valo 			 * this is a B only channel, we don't see it
44367a1092SKalle Valo 			 * as valid. */
45367a1092SKalle Valo 			if ((ieee->geo.bg[i].channel == channel) &&
46367a1092SKalle Valo 			    !(ieee->geo.bg[i].flags & LIBIPW_CH_INVALID) &&
47367a1092SKalle Valo 			    (!(ieee->mode & IEEE_G) ||
48367a1092SKalle Valo 			     !(ieee->geo.bg[i].flags & LIBIPW_CH_B_ONLY)))
49367a1092SKalle Valo 				return LIBIPW_24GHZ_BAND;
50367a1092SKalle Valo 
51367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_52GHZ_BAND)
52367a1092SKalle Valo 		for (i = 0; i < ieee->geo.a_channels; i++)
53367a1092SKalle Valo 			if ((ieee->geo.a[i].channel == channel) &&
54367a1092SKalle Valo 			    !(ieee->geo.a[i].flags & LIBIPW_CH_INVALID))
55367a1092SKalle Valo 				return LIBIPW_52GHZ_BAND;
56367a1092SKalle Valo 
57367a1092SKalle Valo 	return 0;
58367a1092SKalle Valo }
59367a1092SKalle Valo 
libipw_channel_to_index(struct libipw_device * ieee,u8 channel)60367a1092SKalle Valo int libipw_channel_to_index(struct libipw_device *ieee, u8 channel)
61367a1092SKalle Valo {
62367a1092SKalle Valo 	int i;
63367a1092SKalle Valo 
64367a1092SKalle Valo 	/* Driver needs to initialize the geography map before using
65367a1092SKalle Valo 	 * these helper functions */
66367a1092SKalle Valo 	if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
67367a1092SKalle Valo 		return -1;
68367a1092SKalle Valo 
69367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_24GHZ_BAND)
70367a1092SKalle Valo 		for (i = 0; i < ieee->geo.bg_channels; i++)
71367a1092SKalle Valo 			if (ieee->geo.bg[i].channel == channel)
72367a1092SKalle Valo 				return i;
73367a1092SKalle Valo 
74367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_52GHZ_BAND)
75367a1092SKalle Valo 		for (i = 0; i < ieee->geo.a_channels; i++)
76367a1092SKalle Valo 			if (ieee->geo.a[i].channel == channel)
77367a1092SKalle Valo 				return i;
78367a1092SKalle Valo 
79367a1092SKalle Valo 	return -1;
80367a1092SKalle Valo }
81367a1092SKalle Valo 
libipw_channel_to_freq(struct libipw_device * ieee,u8 channel)82367a1092SKalle Valo u32 libipw_channel_to_freq(struct libipw_device * ieee, u8 channel)
83367a1092SKalle Valo {
84367a1092SKalle Valo 	const struct libipw_channel * ch;
85367a1092SKalle Valo 
86367a1092SKalle Valo 	/* Driver needs to initialize the geography map before using
87367a1092SKalle Valo 	 * these helper functions */
88367a1092SKalle Valo 	if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
89367a1092SKalle Valo 		return 0;
90367a1092SKalle Valo 
91367a1092SKalle Valo 	ch = libipw_get_channel(ieee, channel);
92367a1092SKalle Valo 	if (!ch->channel)
93367a1092SKalle Valo 		return 0;
94367a1092SKalle Valo 	return ch->freq;
95367a1092SKalle Valo }
96367a1092SKalle Valo 
libipw_freq_to_channel(struct libipw_device * ieee,u32 freq)97367a1092SKalle Valo u8 libipw_freq_to_channel(struct libipw_device * ieee, u32 freq)
98367a1092SKalle Valo {
99367a1092SKalle Valo 	int i;
100367a1092SKalle Valo 
101367a1092SKalle Valo 	/* Driver needs to initialize the geography map before using
102367a1092SKalle Valo 	 * these helper functions */
103367a1092SKalle Valo 	if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
104367a1092SKalle Valo 		return 0;
105367a1092SKalle Valo 
106367a1092SKalle Valo 	freq /= 100000;
107367a1092SKalle Valo 
108367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_24GHZ_BAND)
109367a1092SKalle Valo 		for (i = 0; i < ieee->geo.bg_channels; i++)
110367a1092SKalle Valo 			if (ieee->geo.bg[i].freq == freq)
111367a1092SKalle Valo 				return ieee->geo.bg[i].channel;
112367a1092SKalle Valo 
113367a1092SKalle Valo 	if (ieee->freq_band & LIBIPW_52GHZ_BAND)
114367a1092SKalle Valo 		for (i = 0; i < ieee->geo.a_channels; i++)
115367a1092SKalle Valo 			if (ieee->geo.a[i].freq == freq)
116367a1092SKalle Valo 				return ieee->geo.a[i].channel;
117367a1092SKalle Valo 
118367a1092SKalle Valo 	return 0;
119367a1092SKalle Valo }
120367a1092SKalle Valo 
libipw_set_geo(struct libipw_device * ieee,const struct libipw_geo * geo)121367a1092SKalle Valo void libipw_set_geo(struct libipw_device *ieee,
122367a1092SKalle Valo 		      const struct libipw_geo *geo)
123367a1092SKalle Valo {
124367a1092SKalle Valo 	memcpy(ieee->geo.name, geo->name, 3);
125367a1092SKalle Valo 	ieee->geo.name[3] = '\0';
126367a1092SKalle Valo 	ieee->geo.bg_channels = geo->bg_channels;
127367a1092SKalle Valo 	ieee->geo.a_channels = geo->a_channels;
128367a1092SKalle Valo 	memcpy(ieee->geo.bg, geo->bg, geo->bg_channels *
129367a1092SKalle Valo 	       sizeof(struct libipw_channel));
130367a1092SKalle Valo 	memcpy(ieee->geo.a, geo->a, ieee->geo.a_channels *
131367a1092SKalle Valo 	       sizeof(struct libipw_channel));
132367a1092SKalle Valo }
133367a1092SKalle Valo 
libipw_get_geo(struct libipw_device * ieee)134367a1092SKalle Valo const struct libipw_geo *libipw_get_geo(struct libipw_device *ieee)
135367a1092SKalle Valo {
136367a1092SKalle Valo 	return &ieee->geo;
137367a1092SKalle Valo }
138367a1092SKalle Valo 
libipw_get_channel_flags(struct libipw_device * ieee,u8 channel)139367a1092SKalle Valo u8 libipw_get_channel_flags(struct libipw_device * ieee, u8 channel)
140367a1092SKalle Valo {
141367a1092SKalle Valo 	int index = libipw_channel_to_index(ieee, channel);
142367a1092SKalle Valo 
143367a1092SKalle Valo 	if (index == -1)
144367a1092SKalle Valo 		return LIBIPW_CH_INVALID;
145367a1092SKalle Valo 
146367a1092SKalle Valo 	if (channel <= LIBIPW_24GHZ_CHANNELS)
147367a1092SKalle Valo 		return ieee->geo.bg[index].flags;
148367a1092SKalle Valo 
149367a1092SKalle Valo 	return ieee->geo.a[index].flags;
150367a1092SKalle Valo }
151367a1092SKalle Valo 
152367a1092SKalle Valo static const struct libipw_channel bad_channel = {
153367a1092SKalle Valo 	.channel = 0,
154367a1092SKalle Valo 	.flags = LIBIPW_CH_INVALID,
155367a1092SKalle Valo 	.max_power = 0,
156367a1092SKalle Valo };
157367a1092SKalle Valo 
libipw_get_channel(struct libipw_device * ieee,u8 channel)158367a1092SKalle Valo const struct libipw_channel *libipw_get_channel(struct libipw_device
159367a1092SKalle Valo 						      *ieee, u8 channel)
160367a1092SKalle Valo {
161367a1092SKalle Valo 	int index = libipw_channel_to_index(ieee, channel);
162367a1092SKalle Valo 
163367a1092SKalle Valo 	if (index == -1)
164367a1092SKalle Valo 		return &bad_channel;
165367a1092SKalle Valo 
166367a1092SKalle Valo 	if (channel <= LIBIPW_24GHZ_CHANNELS)
167367a1092SKalle Valo 		return &ieee->geo.bg[index];
168367a1092SKalle Valo 
169367a1092SKalle Valo 	return &ieee->geo.a[index];
170367a1092SKalle Valo }
171367a1092SKalle Valo 
172367a1092SKalle Valo EXPORT_SYMBOL(libipw_get_channel);
173367a1092SKalle Valo EXPORT_SYMBOL(libipw_get_channel_flags);
174367a1092SKalle Valo EXPORT_SYMBOL(libipw_is_valid_channel);
175367a1092SKalle Valo EXPORT_SYMBOL(libipw_freq_to_channel);
176367a1092SKalle Valo EXPORT_SYMBOL(libipw_channel_to_freq);
177367a1092SKalle Valo EXPORT_SYMBOL(libipw_channel_to_index);
178367a1092SKalle Valo EXPORT_SYMBOL(libipw_set_geo);
179367a1092SKalle Valo EXPORT_SYMBOL(libipw_get_geo);
180