Main Page | Data Structures | Directories | File List | Data Fields | Globals

ieee80211_regdomain.c

Go to the documentation of this file.
00001 /*      $OpenBSD: ieee80211_regdomain.c,v 1.4 2005/02/17 23:52:05 reyk Exp $    */
00002 
00003 /*
00004  * Copyright (c) 2004, 2005 Reyk Floeter <reyk@vantronix.net>
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 /*
00020  * Basic regulation domain extensions for the IEEE 802.11 stack
00021  */
00022 
00023 #include <linux/config.h>
00024 #include <linux/version.h>
00025 #include <linux/module.h>
00026 #include <linux/init.h>
00027 #include <linux/netdevice.h>
00028 #include <linux/random.h>
00029 #include <linux/cache.h>
00030 #include <linux/if_arp.h>
00031 #include "_ieee80211.h"
00032 #include "ieee80211_regdomain.h"
00033 
00034 
00035 int      ieee80211_regdomain_compare_cn(const void *, const void *);
00036 int      ieee80211_regdomain_compare_rn(const void *, const void *);
00037 
00038 static const struct ieee80211_regdomainname
00039 ieee80211_r_names[] = IEEE80211_REGDOMAIN_NAMES;
00040 
00041 static const struct ieee80211_regdomainmap
00042 ieee80211_r_map[] = IEEE80211_REGDOMAIN_MAP;
00043 
00044 static const struct ieee80211_countryname
00045 ieee80211_r_ctry[] = IEEE80211_REGDOMAIN_COUNTRY_NAMES;
00046 
00047 #ifndef bsearch
00048 const void *bsearch(const void *, const void *, size_t, size_t,
00049     int (*)(const void *, const void *));
00050 
00051 const void *
00052 bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
00053     int (*compar)(const void *, const void *))
00054 {
00055         const char *base = base0;
00056         int lim, cmp;
00057         const void *p;
00058         
00059         for (lim = nmemb; lim != 0; lim >>= 1) {
00060                 p = base + (lim >> 1) * size;
00061                 cmp = (*compar)(key, p);
00062                 if (cmp == 0)
00063                         return ((const void *)p);
00064                 if (cmp > 0) {  /* key > p: move right */
00065                         base = (const char *)p + size;
00066                         lim--;
00067                 } /* else move left */
00068         }
00069         return (0);
00070 }
00071 #endif
00072 
00073 int
00074 ieee80211_regdomain_compare_cn(const void *a, const void *b)
00075 {
00076         return(strcmp(((const struct ieee80211_countryname*)a)->cn_name, 
00077                    ((const struct ieee80211_countryname*)b)->cn_name));
00078 }
00079 
00080 int
00081 ieee80211_regdomain_compare_rn(const void *a, const void *b)
00082 {
00083         return(strcmp(((const struct ieee80211_regdomainname*)a)->rn_name, 
00084                    ((const struct ieee80211_regdomainname*)b)->rn_name));
00085 }
00086 
00087 u_int16_t
00088 ieee80211_name2countrycode(const char *name)
00089 {
00090         const struct ieee80211_countryname key = { CTRY_DEFAULT, name }, *value;
00091 
00092         if((value = bsearch(&key, &ieee80211_r_ctry,
00093                 sizeof(ieee80211_r_ctry) / sizeof(ieee80211_r_ctry[0]),
00094                 sizeof(struct ieee80211_countryname),
00095                 ieee80211_regdomain_compare_cn)) != 0)
00096                 return(value->cn_code);
00097 
00098         return(CTRY_DEFAULT);
00099 }
00100 
00101 u_int32_t
00102 ieee80211_name2regdomain(const char *name)
00103 {
00104         const struct ieee80211_regdomainname key = { DMN_DEFAULT, name }, *value;
00105 
00106         if((value = bsearch(&key, &ieee80211_r_names,
00107                 sizeof(ieee80211_r_names) / sizeof(ieee80211_r_names[0]),
00108                 sizeof(struct ieee80211_regdomainname),
00109                 ieee80211_regdomain_compare_rn)) != 0)
00110                 return((u_int32_t)value->rn_domain);
00111 
00112         return((u_int32_t)DMN_DEFAULT);
00113 }
00114 
00115 const char *
00116 ieee80211_countrycode2name(u_int16_t code)
00117 {
00118         int i;
00119 
00120         /* Linear search over the table */
00121         for(i = 0; i < (sizeof(ieee80211_r_ctry) / sizeof(ieee80211_r_ctry[0])); i++)
00122                 if(ieee80211_r_ctry[i].cn_code == code)
00123                         return(ieee80211_r_ctry[i].cn_name);
00124 
00125         return(0);
00126 }
00127 
00128 const char *
00129 ieee80211_regdomain2name(u_int32_t regdomain)
00130 {
00131         int i;
00132 
00133         /* Linear search over the table */
00134         for(i = 0; i < (sizeof(ieee80211_r_names) /
00135                 sizeof(ieee80211_r_names[0])); i++)
00136                 if(ieee80211_r_names[i].rn_domain == regdomain)
00137                         return(ieee80211_r_names[i].rn_name);
00138 
00139         return(ieee80211_r_names[0].rn_name);
00140 }
00141 
00142 u_int32_t
00143 ieee80211_regdomain2flag(u_int16_t regdomain, u_int16_t mhz)
00144 {
00145         int i;
00146         
00147         for(i = 0; i < (sizeof(ieee80211_r_map) / 
00148                 sizeof(ieee80211_r_map[0])); i++) {
00149                 if(ieee80211_r_map[i].rm_domain == regdomain) {
00150                         if(mhz >= 2000 && mhz <= 3000)
00151                                 return((u_int32_t)ieee80211_r_map[i].rm_domain_2ghz);
00152                         if(mhz >= IEEE80211_CHANNELS_5GHZ_MIN && 
00153                             mhz <= IEEE80211_CHANNELS_5GHZ_MAX)
00154                                 return((u_int32_t)ieee80211_r_map[i].rm_domain_5ghz);
00155                 }
00156         }
00157 
00158         return((u_int32_t)DMN_DEBUG);
00159 }
00160 
00161 u_int32_t
00162 ieee80211_countrycode2regdomain(u_int16_t code)
00163 {
00164         int i;
00165 
00166         for (i = 0;
00167              i < (sizeof(ieee80211_r_ctry) / sizeof(ieee80211_r_ctry[0])); i++)
00168                 if (ieee80211_r_ctry[i].cn_code == code)
00169                         return (ieee80211_r_ctry[i].cn_domain);
00170 
00171         return((u_int32_t)DMN_DEFAULT);
00172 }

Generated on Mon Nov 21 15:58:11 2005 for openwifi by  doxygen 1.4.1