ps3_gelic_wireless.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  1. /*
  2. * PS3 gelic network driver.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 Sony Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/tcp.h>
  30. #include <linux/wireless.h>
  31. #include <linux/ieee80211.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/ctype.h>
  34. #include <linux/string.h>
  35. #include <net/iw_handler.h>
  36. #include <linux/dma-mapping.h>
  37. #include <net/checksum.h>
  38. #include <asm/firmware.h>
  39. #include <asm/ps3.h>
  40. #include <asm/lv1call.h>
  41. #include "ps3_gelic_net.h"
  42. #include "ps3_gelic_wireless.h"
  43. static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
  44. u8 *essid, size_t essid_len);
  45. static int gelic_wl_try_associate(struct net_device *netdev);
  46. /*
  47. * tables
  48. */
  49. /* 802.11b/g channel to freq in MHz */
  50. static const int channel_freq[] = {
  51. 2412, 2417, 2422, 2427, 2432,
  52. 2437, 2442, 2447, 2452, 2457,
  53. 2462, 2467, 2472, 2484
  54. };
  55. #define NUM_CHANNELS ARRAY_SIZE(channel_freq)
  56. /* in bps */
  57. static const int bitrate_list[] = {
  58. 1000000,
  59. 2000000,
  60. 5500000,
  61. 11000000,
  62. 6000000,
  63. 9000000,
  64. 12000000,
  65. 18000000,
  66. 24000000,
  67. 36000000,
  68. 48000000,
  69. 54000000
  70. };
  71. #define NUM_BITRATES ARRAY_SIZE(bitrate_list)
  72. /*
  73. * wpa2 support requires the hypervisor version 2.0 or later
  74. */
  75. static inline int wpa2_capable(void)
  76. {
  77. return 0 <= ps3_compare_firmware_version(2, 0, 0);
  78. }
  79. static inline int precise_ie(void)
  80. {
  81. return 0 <= ps3_compare_firmware_version(2, 2, 0);
  82. }
  83. /*
  84. * post_eurus_cmd helpers
  85. */
  86. struct eurus_cmd_arg_info {
  87. int pre_arg; /* command requires arg1, arg2 at POST COMMAND */
  88. int post_arg; /* command requires arg1, arg2 at GET_RESULT */
  89. };
  90. static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
  91. [GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
  92. [GELIC_EURUS_CMD_SET_WEP_CFG] = { .pre_arg = 1},
  93. [GELIC_EURUS_CMD_SET_WPA_CFG] = { .pre_arg = 1},
  94. [GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
  95. [GELIC_EURUS_CMD_GET_WEP_CFG] = { .post_arg = 1},
  96. [GELIC_EURUS_CMD_GET_WPA_CFG] = { .post_arg = 1},
  97. [GELIC_EURUS_CMD_GET_RSSI_CFG] = { .post_arg = 1},
  98. [GELIC_EURUS_CMD_START_SCAN] = { .pre_arg = 1},
  99. [GELIC_EURUS_CMD_GET_SCAN] = { .post_arg = 1},
  100. };
  101. #ifdef DEBUG
  102. static const char *cmdstr(enum gelic_eurus_command ix)
  103. {
  104. switch (ix) {
  105. case GELIC_EURUS_CMD_ASSOC:
  106. return "ASSOC";
  107. case GELIC_EURUS_CMD_DISASSOC:
  108. return "DISASSOC";
  109. case GELIC_EURUS_CMD_START_SCAN:
  110. return "SCAN";
  111. case GELIC_EURUS_CMD_GET_SCAN:
  112. return "GET SCAN";
  113. case GELIC_EURUS_CMD_SET_COMMON_CFG:
  114. return "SET_COMMON_CFG";
  115. case GELIC_EURUS_CMD_GET_COMMON_CFG:
  116. return "GET_COMMON_CFG";
  117. case GELIC_EURUS_CMD_SET_WEP_CFG:
  118. return "SET_WEP_CFG";
  119. case GELIC_EURUS_CMD_GET_WEP_CFG:
  120. return "GET_WEP_CFG";
  121. case GELIC_EURUS_CMD_SET_WPA_CFG:
  122. return "SET_WPA_CFG";
  123. case GELIC_EURUS_CMD_GET_WPA_CFG:
  124. return "GET_WPA_CFG";
  125. case GELIC_EURUS_CMD_GET_RSSI_CFG:
  126. return "GET_RSSI";
  127. default:
  128. break;
  129. }
  130. return "";
  131. };
  132. #else
  133. static inline const char *cmdstr(enum gelic_eurus_command ix)
  134. {
  135. return "";
  136. }
  137. #endif
  138. /* synchronously do eurus commands */
  139. static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
  140. {
  141. struct gelic_eurus_cmd *cmd;
  142. struct gelic_card *card;
  143. struct gelic_wl_info *wl;
  144. u64 arg1, arg2;
  145. pr_debug("%s: <-\n", __func__);
  146. cmd = container_of(work, struct gelic_eurus_cmd, work);
  147. BUG_ON(cmd_info[cmd->cmd].pre_arg &&
  148. cmd_info[cmd->cmd].post_arg);
  149. wl = cmd->wl;
  150. card = port_to_card(wl_port(wl));
  151. if (cmd_info[cmd->cmd].pre_arg) {
  152. arg1 = (cmd->buffer) ?
  153. ps3_mm_phys_to_lpar(__pa(cmd->buffer)) :
  154. 0;
  155. arg2 = cmd->buf_size;
  156. } else {
  157. arg1 = 0;
  158. arg2 = 0;
  159. }
  160. init_completion(&wl->cmd_done_intr);
  161. pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
  162. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  163. GELIC_LV1_POST_WLAN_CMD,
  164. cmd->cmd, arg1, arg2,
  165. &cmd->tag, &cmd->size);
  166. if (cmd->status) {
  167. complete(&cmd->done);
  168. pr_info("%s: cmd issue failed\n", __func__);
  169. return;
  170. }
  171. wait_for_completion(&wl->cmd_done_intr);
  172. if (cmd_info[cmd->cmd].post_arg) {
  173. arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
  174. arg2 = cmd->buf_size;
  175. } else {
  176. arg1 = 0;
  177. arg2 = 0;
  178. }
  179. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  180. GELIC_LV1_GET_WLAN_CMD_RESULT,
  181. cmd->tag, arg1, arg2,
  182. &cmd->cmd_status, &cmd->size);
  183. #ifdef DEBUG
  184. if (cmd->status || cmd->cmd_status) {
  185. pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
  186. cmd->tag, arg1, arg2);
  187. pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
  188. __func__, cmd->status, cmd->cmd_status, cmd->size);
  189. }
  190. #endif
  191. complete(&cmd->done);
  192. pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
  193. }
  194. static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
  195. unsigned int eurus_cmd,
  196. void *buffer,
  197. unsigned int buf_size)
  198. {
  199. struct gelic_eurus_cmd *cmd;
  200. /* allocate cmd */
  201. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  202. if (!cmd)
  203. return NULL;
  204. /* initialize members */
  205. cmd->cmd = eurus_cmd;
  206. cmd->buffer = buffer;
  207. cmd->buf_size = buf_size;
  208. cmd->wl = wl;
  209. INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
  210. init_completion(&cmd->done);
  211. queue_work(wl->eurus_cmd_queue, &cmd->work);
  212. /* wait for command completion */
  213. wait_for_completion(&cmd->done);
  214. return cmd;
  215. }
  216. static u32 gelic_wl_get_link(struct net_device *netdev)
  217. {
  218. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  219. u32 ret;
  220. pr_debug("%s: <-\n", __func__);
  221. mutex_lock(&wl->assoc_stat_lock);
  222. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  223. ret = 1;
  224. else
  225. ret = 0;
  226. mutex_unlock(&wl->assoc_stat_lock);
  227. pr_debug("%s: ->\n", __func__);
  228. return ret;
  229. }
  230. static void gelic_wl_send_iwap_event(struct gelic_wl_info *wl, u8 *bssid)
  231. {
  232. union iwreq_data data;
  233. memset(&data, 0, sizeof(data));
  234. if (bssid)
  235. memcpy(data.ap_addr.sa_data, bssid, ETH_ALEN);
  236. data.ap_addr.sa_family = ARPHRD_ETHER;
  237. wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWAP,
  238. &data, NULL);
  239. }
  240. /*
  241. * wireless extension handlers and helpers
  242. */
  243. /* SIOGIWNAME */
  244. static int gelic_wl_get_name(struct net_device *dev,
  245. struct iw_request_info *info,
  246. union iwreq_data *iwreq, char *extra)
  247. {
  248. strcpy(iwreq->name, "IEEE 802.11bg");
  249. return 0;
  250. }
  251. static void gelic_wl_get_ch_info(struct gelic_wl_info *wl)
  252. {
  253. struct gelic_card *card = port_to_card(wl_port(wl));
  254. u64 ch_info_raw, tmp;
  255. int status;
  256. if (!test_and_set_bit(GELIC_WL_STAT_CH_INFO, &wl->stat)) {
  257. status = lv1_net_control(bus_id(card), dev_id(card),
  258. GELIC_LV1_GET_CHANNEL, 0, 0, 0,
  259. &ch_info_raw,
  260. &tmp);
  261. /* some fw versions may return error */
  262. if (status) {
  263. if (status != LV1_NO_ENTRY)
  264. pr_info("%s: available ch unknown\n", __func__);
  265. wl->ch_info = 0x07ff;/* 11 ch */
  266. } else
  267. /* 16 bits of MSB has available channels */
  268. wl->ch_info = ch_info_raw >> 48;
  269. }
  270. }
  271. /* SIOGIWRANGE */
  272. static int gelic_wl_get_range(struct net_device *netdev,
  273. struct iw_request_info *info,
  274. union iwreq_data *iwreq, char *extra)
  275. {
  276. struct iw_point *point = &iwreq->data;
  277. struct iw_range *range = (struct iw_range *)extra;
  278. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  279. unsigned int i, chs;
  280. pr_debug("%s: <-\n", __func__);
  281. point->length = sizeof(struct iw_range);
  282. memset(range, 0, sizeof(struct iw_range));
  283. range->we_version_compiled = WIRELESS_EXT;
  284. range->we_version_source = 22;
  285. /* available channels and frequencies */
  286. gelic_wl_get_ch_info(wl);
  287. for (i = 0, chs = 0;
  288. i < NUM_CHANNELS && chs < IW_MAX_FREQUENCIES; i++)
  289. if (wl->ch_info & (1 << i)) {
  290. range->freq[chs].i = i + 1;
  291. range->freq[chs].m = channel_freq[i];
  292. range->freq[chs].e = 6;
  293. chs++;
  294. }
  295. range->num_frequency = chs;
  296. range->old_num_frequency = chs;
  297. range->num_channels = chs;
  298. range->old_num_channels = chs;
  299. /* bitrates */
  300. for (i = 0; i < NUM_BITRATES; i++)
  301. range->bitrate[i] = bitrate_list[i];
  302. range->num_bitrates = i;
  303. /* signal levels */
  304. range->max_qual.qual = 100; /* relative value */
  305. range->max_qual.level = 100;
  306. range->avg_qual.qual = 50;
  307. range->avg_qual.level = 50;
  308. range->sensitivity = 0;
  309. /* Event capability */
  310. IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
  311. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
  312. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
  313. /* encryption capability */
  314. range->enc_capa = IW_ENC_CAPA_WPA |
  315. IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP |
  316. IW_ENC_CAPA_4WAY_HANDSHAKE;
  317. if (wpa2_capable())
  318. range->enc_capa |= IW_ENC_CAPA_WPA2;
  319. range->encoding_size[0] = 5; /* 40bit WEP */
  320. range->encoding_size[1] = 13; /* 104bit WEP */
  321. range->encoding_size[2] = 32; /* WPA-PSK */
  322. range->num_encoding_sizes = 3;
  323. range->max_encoding_tokens = GELIC_WEP_KEYS;
  324. /* scan capability */
  325. range->scan_capa = IW_SCAN_CAPA_ESSID;
  326. pr_debug("%s: ->\n", __func__);
  327. return 0;
  328. }
  329. /* SIOC{G,S}IWSCAN */
  330. static int gelic_wl_set_scan(struct net_device *netdev,
  331. struct iw_request_info *info,
  332. union iwreq_data *wrqu, char *extra)
  333. {
  334. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  335. struct iw_scan_req *req;
  336. u8 *essid = NULL;
  337. size_t essid_len = 0;
  338. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  339. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  340. req = (struct iw_scan_req*)extra;
  341. essid = req->essid;
  342. essid_len = req->essid_len;
  343. pr_debug("%s: ESSID scan =%s\n", __func__, essid);
  344. }
  345. return gelic_wl_start_scan(wl, 1, essid, essid_len);
  346. }
  347. #define OUI_LEN 3
  348. static const u8 rsn_oui[OUI_LEN] = { 0x00, 0x0f, 0xac };
  349. static const u8 wpa_oui[OUI_LEN] = { 0x00, 0x50, 0xf2 };
  350. /*
  351. * synthesize WPA/RSN IE data
  352. * See WiFi WPA specification and IEEE 802.11-2007 7.3.2.25
  353. * for the format
  354. */
  355. static size_t gelic_wl_synthesize_ie(u8 *buf,
  356. struct gelic_eurus_scan_info *scan)
  357. {
  358. const u8 *oui_header;
  359. u8 *start = buf;
  360. int rsn;
  361. int ccmp;
  362. pr_debug("%s: <- sec=%16x\n", __func__, scan->security);
  363. switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_MASK) {
  364. case GELIC_EURUS_SCAN_SEC_WPA:
  365. rsn = 0;
  366. break;
  367. case GELIC_EURUS_SCAN_SEC_WPA2:
  368. rsn = 1;
  369. break;
  370. default:
  371. /* WEP or none. No IE returned */
  372. return 0;
  373. }
  374. switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_WPA_MASK) {
  375. case GELIC_EURUS_SCAN_SEC_WPA_TKIP:
  376. ccmp = 0;
  377. break;
  378. case GELIC_EURUS_SCAN_SEC_WPA_AES:
  379. ccmp = 1;
  380. break;
  381. default:
  382. if (rsn) {
  383. ccmp = 1;
  384. pr_info("%s: no cipher info. defaulted to CCMP\n",
  385. __func__);
  386. } else {
  387. ccmp = 0;
  388. pr_info("%s: no cipher info. defaulted to TKIP\n",
  389. __func__);
  390. }
  391. }
  392. if (rsn)
  393. oui_header = rsn_oui;
  394. else
  395. oui_header = wpa_oui;
  396. /* element id */
  397. if (rsn)
  398. *buf++ = WLAN_EID_RSN;
  399. else
  400. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  401. /* length filed; set later */
  402. buf++;
  403. /* wpa special header */
  404. if (!rsn) {
  405. memcpy(buf, wpa_oui, OUI_LEN);
  406. buf += OUI_LEN;
  407. *buf++ = 0x01;
  408. }
  409. /* version */
  410. *buf++ = 0x01; /* version 1.0 */
  411. *buf++ = 0x00;
  412. /* group cipher */
  413. memcpy(buf, oui_header, OUI_LEN);
  414. buf += OUI_LEN;
  415. if (ccmp)
  416. *buf++ = 0x04; /* CCMP */
  417. else
  418. *buf++ = 0x02; /* TKIP */
  419. /* pairwise key count always 1 */
  420. *buf++ = 0x01;
  421. *buf++ = 0x00;
  422. /* pairwise key suit */
  423. memcpy(buf, oui_header, OUI_LEN);
  424. buf += OUI_LEN;
  425. if (ccmp)
  426. *buf++ = 0x04; /* CCMP */
  427. else
  428. *buf++ = 0x02; /* TKIP */
  429. /* AKM count is 1 */
  430. *buf++ = 0x01;
  431. *buf++ = 0x00;
  432. /* AKM suite is assumed as PSK*/
  433. memcpy(buf, oui_header, OUI_LEN);
  434. buf += OUI_LEN;
  435. *buf++ = 0x02; /* PSK */
  436. /* RSN capabilities is 0 */
  437. *buf++ = 0x00;
  438. *buf++ = 0x00;
  439. /* set length field */
  440. start[1] = (buf - start - 2);
  441. pr_debug("%s: ->\n", __func__);
  442. return buf - start;
  443. }
  444. struct ie_item {
  445. u8 *data;
  446. u8 len;
  447. };
  448. struct ie_info {
  449. struct ie_item wpa;
  450. struct ie_item rsn;
  451. };
  452. static void gelic_wl_parse_ie(u8 *data, size_t len,
  453. struct ie_info *ie_info)
  454. {
  455. size_t data_left = len;
  456. u8 *pos = data;
  457. u8 item_len;
  458. u8 item_id;
  459. pr_debug("%s: data=%p len=%ld\n", __func__,
  460. data, len);
  461. memset(ie_info, 0, sizeof(struct ie_info));
  462. while (2 <= data_left) {
  463. item_id = *pos++;
  464. item_len = *pos++;
  465. data_left -= 2;
  466. if (data_left < item_len)
  467. break;
  468. switch (item_id) {
  469. case WLAN_EID_VENDOR_SPECIFIC:
  470. if ((OUI_LEN + 1 <= item_len) &&
  471. !memcmp(pos, wpa_oui, OUI_LEN) &&
  472. pos[OUI_LEN] == 0x01) {
  473. ie_info->wpa.data = pos - 2;
  474. ie_info->wpa.len = item_len + 2;
  475. }
  476. break;
  477. case WLAN_EID_RSN:
  478. ie_info->rsn.data = pos - 2;
  479. /* length includes the header */
  480. ie_info->rsn.len = item_len + 2;
  481. break;
  482. default:
  483. pr_debug("%s: ignore %#x,%d\n", __func__,
  484. item_id, item_len);
  485. break;
  486. }
  487. pos += item_len;
  488. data_left -= item_len;
  489. }
  490. pr_debug("%s: wpa=%p,%d wpa2=%p,%d\n", __func__,
  491. ie_info->wpa.data, ie_info->wpa.len,
  492. ie_info->rsn.data, ie_info->rsn.len);
  493. }
  494. /*
  495. * translate the scan informations from hypervisor to a
  496. * independent format
  497. */
  498. static char *gelic_wl_translate_scan(struct net_device *netdev,
  499. struct iw_request_info *info,
  500. char *ev,
  501. char *stop,
  502. struct gelic_wl_scan_info *network)
  503. {
  504. struct iw_event iwe;
  505. struct gelic_eurus_scan_info *scan = network->hwinfo;
  506. char *tmp;
  507. u8 rate;
  508. unsigned int i, j, len;
  509. u8 buf[64]; /* arbitrary size large enough */
  510. pr_debug("%s: <-\n", __func__);
  511. /* first entry should be AP's mac address */
  512. iwe.cmd = SIOCGIWAP;
  513. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  514. memcpy(iwe.u.ap_addr.sa_data, &scan->bssid[2], ETH_ALEN);
  515. ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_ADDR_LEN);
  516. /* ESSID */
  517. iwe.cmd = SIOCGIWESSID;
  518. iwe.u.data.flags = 1;
  519. iwe.u.data.length = strnlen(scan->essid, 32);
  520. ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
  521. /* FREQUENCY */
  522. iwe.cmd = SIOCGIWFREQ;
  523. iwe.u.freq.m = be16_to_cpu(scan->channel);
  524. iwe.u.freq.e = 0; /* table value in MHz */
  525. iwe.u.freq.i = 0;
  526. ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_FREQ_LEN);
  527. /* RATES */
  528. iwe.cmd = SIOCGIWRATE;
  529. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  530. /* to stuff multiple values in one event */
  531. tmp = ev + iwe_stream_lcp_len(info);
  532. /* put them in ascendant order (older is first) */
  533. i = 0;
  534. j = 0;
  535. pr_debug("%s: rates=%d rate=%d\n", __func__,
  536. network->rate_len, network->rate_ext_len);
  537. while (i < network->rate_len) {
  538. if (j < network->rate_ext_len &&
  539. ((scan->ext_rate[j] & 0x7f) < (scan->rate[i] & 0x7f)))
  540. rate = scan->ext_rate[j++] & 0x7f;
  541. else
  542. rate = scan->rate[i++] & 0x7f;
  543. iwe.u.bitrate.value = rate * 500000; /* 500kbps unit */
  544. tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
  545. IW_EV_PARAM_LEN);
  546. }
  547. while (j < network->rate_ext_len) {
  548. iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
  549. tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
  550. IW_EV_PARAM_LEN);
  551. }
  552. /* Check if we added any rate */
  553. if (iwe_stream_lcp_len(info) < (tmp - ev))
  554. ev = tmp;
  555. /* ENCODE */
  556. iwe.cmd = SIOCGIWENCODE;
  557. if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_PRIVACY)
  558. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  559. else
  560. iwe.u.data.flags = IW_ENCODE_DISABLED;
  561. iwe.u.data.length = 0;
  562. ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
  563. /* MODE */
  564. iwe.cmd = SIOCGIWMODE;
  565. if (be16_to_cpu(scan->capability) &
  566. (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
  567. if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_ESS)
  568. iwe.u.mode = IW_MODE_MASTER;
  569. else
  570. iwe.u.mode = IW_MODE_ADHOC;
  571. ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_UINT_LEN);
  572. }
  573. /* QUAL */
  574. iwe.cmd = IWEVQUAL;
  575. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED |
  576. IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
  577. iwe.u.qual.level = be16_to_cpu(scan->rssi);
  578. iwe.u.qual.qual = be16_to_cpu(scan->rssi);
  579. iwe.u.qual.noise = 0;
  580. ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_QUAL_LEN);
  581. /* RSN */
  582. memset(&iwe, 0, sizeof(iwe));
  583. if (be16_to_cpu(scan->size) <= sizeof(*scan)) {
  584. /* If wpa[2] capable station, synthesize IE and put it */
  585. len = gelic_wl_synthesize_ie(buf, scan);
  586. if (len) {
  587. iwe.cmd = IWEVGENIE;
  588. iwe.u.data.length = len;
  589. ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
  590. }
  591. } else {
  592. /* this scan info has IE data */
  593. struct ie_info ie_info;
  594. size_t data_len;
  595. data_len = be16_to_cpu(scan->size) - sizeof(*scan);
  596. gelic_wl_parse_ie(scan->elements, data_len, &ie_info);
  597. if (ie_info.wpa.len && (ie_info.wpa.len <= sizeof(buf))) {
  598. memcpy(buf, ie_info.wpa.data, ie_info.wpa.len);
  599. iwe.cmd = IWEVGENIE;
  600. iwe.u.data.length = ie_info.wpa.len;
  601. ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
  602. }
  603. if (ie_info.rsn.len && (ie_info.rsn.len <= sizeof(buf))) {
  604. memset(&iwe, 0, sizeof(iwe));
  605. memcpy(buf, ie_info.rsn.data, ie_info.rsn.len);
  606. iwe.cmd = IWEVGENIE;
  607. iwe.u.data.length = ie_info.rsn.len;
  608. ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
  609. }
  610. }
  611. pr_debug("%s: ->\n", __func__);
  612. return ev;
  613. }
  614. static int gelic_wl_get_scan(struct net_device *netdev,
  615. struct iw_request_info *info,
  616. union iwreq_data *wrqu, char *extra)
  617. {
  618. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  619. struct gelic_wl_scan_info *scan_info;
  620. char *ev = extra;
  621. char *stop = ev + wrqu->data.length;
  622. int ret = 0;
  623. unsigned long this_time = jiffies;
  624. pr_debug("%s: <-\n", __func__);
  625. if (mutex_lock_interruptible(&wl->scan_lock))
  626. return -EAGAIN;
  627. switch (wl->scan_stat) {
  628. case GELIC_WL_SCAN_STAT_SCANNING:
  629. /* If a scan in progress, caller should call me again */
  630. ret = -EAGAIN;
  631. goto out;
  632. case GELIC_WL_SCAN_STAT_INIT:
  633. /* last scan request failed or never issued */
  634. ret = -ENODEV;
  635. goto out;
  636. case GELIC_WL_SCAN_STAT_GOT_LIST:
  637. /* ok, use current list */
  638. break;
  639. }
  640. list_for_each_entry(scan_info, &wl->network_list, list) {
  641. if (wl->scan_age == 0 ||
  642. time_after(scan_info->last_scanned + wl->scan_age,
  643. this_time))
  644. ev = gelic_wl_translate_scan(netdev, info,
  645. ev, stop,
  646. scan_info);
  647. else
  648. pr_debug("%s:entry too old\n", __func__);
  649. if (stop - ev <= IW_EV_ADDR_LEN) {
  650. ret = -E2BIG;
  651. goto out;
  652. }
  653. }
  654. wrqu->data.length = ev - extra;
  655. wrqu->data.flags = 0;
  656. out:
  657. mutex_unlock(&wl->scan_lock);
  658. pr_debug("%s: -> %d %d\n", __func__, ret, wrqu->data.length);
  659. return ret;
  660. }
  661. #ifdef DEBUG
  662. static void scan_list_dump(struct gelic_wl_info *wl)
  663. {
  664. struct gelic_wl_scan_info *scan_info;
  665. int i;
  666. i = 0;
  667. list_for_each_entry(scan_info, &wl->network_list, list) {
  668. pr_debug("%s: item %d\n", __func__, i++);
  669. pr_debug("valid=%d eurusindex=%d last=%lx\n",
  670. scan_info->valid, scan_info->eurus_index,
  671. scan_info->last_scanned);
  672. pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
  673. scan_info->rate_len, scan_info->rate_ext_len,
  674. scan_info->essid_len);
  675. /* -- */
  676. pr_debug("bssid=%pM\n", &scan_info->hwinfo->bssid[2]);
  677. pr_debug("essid=%s\n", scan_info->hwinfo->essid);
  678. }
  679. }
  680. #endif
  681. static int gelic_wl_set_auth(struct net_device *netdev,
  682. struct iw_request_info *info,
  683. union iwreq_data *data, char *extra)
  684. {
  685. struct iw_param *param = &data->param;
  686. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  687. unsigned long irqflag;
  688. int ret = 0;
  689. pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
  690. spin_lock_irqsave(&wl->lock, irqflag);
  691. switch (param->flags & IW_AUTH_INDEX) {
  692. case IW_AUTH_WPA_VERSION:
  693. if (param->value & IW_AUTH_WPA_VERSION_DISABLED) {
  694. pr_debug("%s: NO WPA selected\n", __func__);
  695. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  696. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  697. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  698. }
  699. if (param->value & IW_AUTH_WPA_VERSION_WPA) {
  700. pr_debug("%s: WPA version 1 selected\n", __func__);
  701. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
  702. wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
  703. wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
  704. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  705. }
  706. if (param->value & IW_AUTH_WPA_VERSION_WPA2) {
  707. /*
  708. * As the hypervisor may not tell the cipher
  709. * information of the AP if it is WPA2,
  710. * you will not decide suitable cipher from
  711. * its beacon.
  712. * You should have knowledge about the AP's
  713. * cipher information in other method prior to
  714. * the association.
  715. */
  716. if (!precise_ie())
  717. pr_info("%s: WPA2 may not work\n", __func__);
  718. if (wpa2_capable()) {
  719. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
  720. wl->group_cipher_method = GELIC_WL_CIPHER_AES;
  721. wl->pairwise_cipher_method =
  722. GELIC_WL_CIPHER_AES;
  723. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  724. } else
  725. ret = -EINVAL;
  726. }
  727. break;
  728. case IW_AUTH_CIPHER_PAIRWISE:
  729. if (param->value &
  730. (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
  731. pr_debug("%s: WEP selected\n", __func__);
  732. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  733. }
  734. if (param->value & IW_AUTH_CIPHER_TKIP) {
  735. pr_debug("%s: TKIP selected\n", __func__);
  736. wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
  737. }
  738. if (param->value & IW_AUTH_CIPHER_CCMP) {
  739. pr_debug("%s: CCMP selected\n", __func__);
  740. wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
  741. }
  742. if (param->value & IW_AUTH_CIPHER_NONE) {
  743. pr_debug("%s: no auth selected\n", __func__);
  744. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  745. }
  746. break;
  747. case IW_AUTH_CIPHER_GROUP:
  748. if (param->value &
  749. (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
  750. pr_debug("%s: WEP selected\n", __func__);
  751. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  752. }
  753. if (param->value & IW_AUTH_CIPHER_TKIP) {
  754. pr_debug("%s: TKIP selected\n", __func__);
  755. wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
  756. }
  757. if (param->value & IW_AUTH_CIPHER_CCMP) {
  758. pr_debug("%s: CCMP selected\n", __func__);
  759. wl->group_cipher_method = GELIC_WL_CIPHER_AES;
  760. }
  761. if (param->value & IW_AUTH_CIPHER_NONE) {
  762. pr_debug("%s: no auth selected\n", __func__);
  763. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  764. }
  765. break;
  766. case IW_AUTH_80211_AUTH_ALG:
  767. if (param->value & IW_AUTH_ALG_SHARED_KEY) {
  768. pr_debug("%s: shared key specified\n", __func__);
  769. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  770. } else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
  771. pr_debug("%s: open system specified\n", __func__);
  772. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  773. } else
  774. ret = -EINVAL;
  775. break;
  776. case IW_AUTH_WPA_ENABLED:
  777. if (param->value) {
  778. pr_debug("%s: WPA enabled\n", __func__);
  779. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
  780. } else {
  781. pr_debug("%s: WPA disabled\n", __func__);
  782. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  783. }
  784. break;
  785. case IW_AUTH_KEY_MGMT:
  786. if (param->value & IW_AUTH_KEY_MGMT_PSK)
  787. break;
  788. /* intentionally fall through */
  789. default:
  790. ret = -EOPNOTSUPP;
  791. break;
  792. }
  793. if (!ret)
  794. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  795. spin_unlock_irqrestore(&wl->lock, irqflag);
  796. pr_debug("%s: -> %d\n", __func__, ret);
  797. return ret;
  798. }
  799. static int gelic_wl_get_auth(struct net_device *netdev,
  800. struct iw_request_info *info,
  801. union iwreq_data *iwreq, char *extra)
  802. {
  803. struct iw_param *param = &iwreq->param;
  804. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  805. unsigned long irqflag;
  806. int ret = 0;
  807. pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
  808. spin_lock_irqsave(&wl->lock, irqflag);
  809. switch (param->flags & IW_AUTH_INDEX) {
  810. case IW_AUTH_WPA_VERSION:
  811. switch (wl->wpa_level) {
  812. case GELIC_WL_WPA_LEVEL_WPA:
  813. param->value |= IW_AUTH_WPA_VERSION_WPA;
  814. break;
  815. case GELIC_WL_WPA_LEVEL_WPA2:
  816. param->value |= IW_AUTH_WPA_VERSION_WPA2;
  817. break;
  818. default:
  819. param->value |= IW_AUTH_WPA_VERSION_DISABLED;
  820. }
  821. break;
  822. case IW_AUTH_80211_AUTH_ALG:
  823. if (wl->auth_method == GELIC_EURUS_AUTH_SHARED)
  824. param->value = IW_AUTH_ALG_SHARED_KEY;
  825. else if (wl->auth_method == GELIC_EURUS_AUTH_OPEN)
  826. param->value = IW_AUTH_ALG_OPEN_SYSTEM;
  827. break;
  828. case IW_AUTH_WPA_ENABLED:
  829. switch (wl->wpa_level) {
  830. case GELIC_WL_WPA_LEVEL_WPA:
  831. case GELIC_WL_WPA_LEVEL_WPA2:
  832. param->value = 1;
  833. break;
  834. default:
  835. param->value = 0;
  836. break;
  837. }
  838. break;
  839. default:
  840. ret = -EOPNOTSUPP;
  841. }
  842. spin_unlock_irqrestore(&wl->lock, irqflag);
  843. pr_debug("%s: -> %d\n", __func__, ret);
  844. return ret;
  845. }
  846. /* SIOC{S,G}IWESSID */
  847. static int gelic_wl_set_essid(struct net_device *netdev,
  848. struct iw_request_info *info,
  849. union iwreq_data *data, char *extra)
  850. {
  851. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  852. unsigned long irqflag;
  853. pr_debug("%s: <- l=%d f=%d\n", __func__,
  854. data->essid.length, data->essid.flags);
  855. if (IW_ESSID_MAX_SIZE < data->essid.length)
  856. return -EINVAL;
  857. spin_lock_irqsave(&wl->lock, irqflag);
  858. if (data->essid.flags) {
  859. wl->essid_len = data->essid.length;
  860. memcpy(wl->essid, extra, wl->essid_len);
  861. pr_debug("%s: essid = '%s'\n", __func__, extra);
  862. set_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
  863. } else {
  864. pr_debug("%s: ESSID any\n", __func__);
  865. clear_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
  866. }
  867. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  868. spin_unlock_irqrestore(&wl->lock, irqflag);
  869. gelic_wl_try_associate(netdev); /* FIXME */
  870. pr_debug("%s: ->\n", __func__);
  871. return 0;
  872. }
  873. static int gelic_wl_get_essid(struct net_device *netdev,
  874. struct iw_request_info *info,
  875. union iwreq_data *data, char *extra)
  876. {
  877. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  878. unsigned long irqflag;
  879. pr_debug("%s: <-\n", __func__);
  880. mutex_lock(&wl->assoc_stat_lock);
  881. spin_lock_irqsave(&wl->lock, irqflag);
  882. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat) ||
  883. wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
  884. memcpy(extra, wl->essid, wl->essid_len);
  885. data->essid.length = wl->essid_len;
  886. data->essid.flags = 1;
  887. } else
  888. data->essid.flags = 0;
  889. mutex_unlock(&wl->assoc_stat_lock);
  890. spin_unlock_irqrestore(&wl->lock, irqflag);
  891. pr_debug("%s: -> len=%d\n", __func__, data->essid.length);
  892. return 0;
  893. }
  894. /* SIO{S,G}IWENCODE */
  895. static int gelic_wl_set_encode(struct net_device *netdev,
  896. struct iw_request_info *info,
  897. union iwreq_data *data, char *extra)
  898. {
  899. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  900. struct iw_point *enc = &data->encoding;
  901. __u16 flags;
  902. unsigned long irqflag;
  903. int key_index, index_specified;
  904. int ret = 0;
  905. pr_debug("%s: <-\n", __func__);
  906. flags = enc->flags & IW_ENCODE_FLAGS;
  907. key_index = enc->flags & IW_ENCODE_INDEX;
  908. pr_debug("%s: key_index = %d\n", __func__, key_index);
  909. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  910. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  911. if (GELIC_WEP_KEYS < key_index)
  912. return -EINVAL;
  913. spin_lock_irqsave(&wl->lock, irqflag);
  914. if (key_index) {
  915. index_specified = 1;
  916. key_index--;
  917. } else {
  918. index_specified = 0;
  919. key_index = wl->current_key;
  920. }
  921. if (flags & IW_ENCODE_NOKEY) {
  922. /* if just IW_ENCODE_NOKEY, change current key index */
  923. if (!flags && index_specified) {
  924. wl->current_key = key_index;
  925. goto done;
  926. }
  927. if (flags & IW_ENCODE_DISABLED) {
  928. if (!index_specified) {
  929. /* disable encryption */
  930. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  931. wl->pairwise_cipher_method =
  932. GELIC_WL_CIPHER_NONE;
  933. /* invalidate all key */
  934. wl->key_enabled = 0;
  935. } else
  936. clear_bit(key_index, &wl->key_enabled);
  937. }
  938. if (flags & IW_ENCODE_OPEN)
  939. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  940. if (flags & IW_ENCODE_RESTRICTED) {
  941. pr_info("%s: shared key mode enabled\n", __func__);
  942. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  943. }
  944. } else {
  945. if (IW_ENCODING_TOKEN_MAX < enc->length) {
  946. ret = -EINVAL;
  947. goto done;
  948. }
  949. wl->key_len[key_index] = enc->length;
  950. memcpy(wl->key[key_index], extra, enc->length);
  951. set_bit(key_index, &wl->key_enabled);
  952. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  953. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  954. }
  955. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  956. done:
  957. spin_unlock_irqrestore(&wl->lock, irqflag);
  958. pr_debug("%s: ->\n", __func__);
  959. return ret;
  960. }
  961. static int gelic_wl_get_encode(struct net_device *netdev,
  962. struct iw_request_info *info,
  963. union iwreq_data *data, char *extra)
  964. {
  965. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  966. struct iw_point *enc = &data->encoding;
  967. unsigned long irqflag;
  968. unsigned int key_index, index_specified;
  969. int ret = 0;
  970. pr_debug("%s: <-\n", __func__);
  971. key_index = enc->flags & IW_ENCODE_INDEX;
  972. pr_debug("%s: flag=%#x point=%p len=%d extra=%p\n", __func__,
  973. enc->flags, enc->pointer, enc->length, extra);
  974. if (GELIC_WEP_KEYS < key_index)
  975. return -EINVAL;
  976. spin_lock_irqsave(&wl->lock, irqflag);
  977. if (key_index) {
  978. index_specified = 1;
  979. key_index--;
  980. } else {
  981. index_specified = 0;
  982. key_index = wl->current_key;
  983. }
  984. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  985. switch (wl->auth_method) {
  986. case GELIC_EURUS_AUTH_OPEN:
  987. enc->flags = IW_ENCODE_OPEN;
  988. break;
  989. case GELIC_EURUS_AUTH_SHARED:
  990. enc->flags = IW_ENCODE_RESTRICTED;
  991. break;
  992. }
  993. } else
  994. enc->flags = IW_ENCODE_DISABLED;
  995. if (test_bit(key_index, &wl->key_enabled)) {
  996. if (enc->length < wl->key_len[key_index]) {
  997. ret = -EINVAL;
  998. goto done;
  999. }
  1000. enc->length = wl->key_len[key_index];
  1001. memcpy(extra, wl->key[key_index], wl->key_len[key_index]);
  1002. } else {
  1003. enc->length = 0;
  1004. enc->flags |= IW_ENCODE_NOKEY;
  1005. }
  1006. enc->flags |= key_index + 1;
  1007. pr_debug("%s: -> flag=%x len=%d\n", __func__,
  1008. enc->flags, enc->length);
  1009. done:
  1010. spin_unlock_irqrestore(&wl->lock, irqflag);
  1011. return ret;
  1012. }
  1013. /* SIOC{S,G}IWAP */
  1014. static int gelic_wl_set_ap(struct net_device *netdev,
  1015. struct iw_request_info *info,
  1016. union iwreq_data *data, char *extra)
  1017. {
  1018. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1019. unsigned long irqflag;
  1020. pr_debug("%s: <-\n", __func__);
  1021. if (data->ap_addr.sa_family != ARPHRD_ETHER)
  1022. return -EINVAL;
  1023. spin_lock_irqsave(&wl->lock, irqflag);
  1024. if (is_valid_ether_addr(data->ap_addr.sa_data)) {
  1025. memcpy(wl->bssid, data->ap_addr.sa_data,
  1026. ETH_ALEN);
  1027. set_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
  1028. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  1029. pr_debug("%s: bss=%pM\n", __func__, wl->bssid);
  1030. } else {
  1031. pr_debug("%s: clear bssid\n", __func__);
  1032. clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
  1033. eth_zero_addr(wl->bssid);
  1034. }
  1035. spin_unlock_irqrestore(&wl->lock, irqflag);
  1036. pr_debug("%s: ->\n", __func__);
  1037. return 0;
  1038. }
  1039. static int gelic_wl_get_ap(struct net_device *netdev,
  1040. struct iw_request_info *info,
  1041. union iwreq_data *data, char *extra)
  1042. {
  1043. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1044. unsigned long irqflag;
  1045. pr_debug("%s: <-\n", __func__);
  1046. mutex_lock(&wl->assoc_stat_lock);
  1047. spin_lock_irqsave(&wl->lock, irqflag);
  1048. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
  1049. data->ap_addr.sa_family = ARPHRD_ETHER;
  1050. memcpy(data->ap_addr.sa_data, wl->active_bssid,
  1051. ETH_ALEN);
  1052. } else
  1053. eth_zero_addr(data->ap_addr.sa_data);
  1054. spin_unlock_irqrestore(&wl->lock, irqflag);
  1055. mutex_unlock(&wl->assoc_stat_lock);
  1056. pr_debug("%s: ->\n", __func__);
  1057. return 0;
  1058. }
  1059. /* SIOC{S,G}IWENCODEEXT */
  1060. static int gelic_wl_set_encodeext(struct net_device *netdev,
  1061. struct iw_request_info *info,
  1062. union iwreq_data *data, char *extra)
  1063. {
  1064. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1065. struct iw_point *enc = &data->encoding;
  1066. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1067. __u16 alg;
  1068. __u16 flags;
  1069. unsigned long irqflag;
  1070. int key_index;
  1071. int ret = 0;
  1072. pr_debug("%s: <-\n", __func__);
  1073. flags = enc->flags & IW_ENCODE_FLAGS;
  1074. alg = ext->alg;
  1075. key_index = enc->flags & IW_ENCODE_INDEX;
  1076. pr_debug("%s: key_index = %d\n", __func__, key_index);
  1077. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  1078. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  1079. pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
  1080. pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);
  1081. if (GELIC_WEP_KEYS < key_index)
  1082. return -EINVAL;
  1083. spin_lock_irqsave(&wl->lock, irqflag);
  1084. if (key_index)
  1085. key_index--;
  1086. else
  1087. key_index = wl->current_key;
  1088. if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
  1089. /* reques to change default key index */
  1090. pr_debug("%s: request to change default key to %d\n",
  1091. __func__, key_index);
  1092. wl->current_key = key_index;
  1093. goto done;
  1094. }
  1095. if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
  1096. pr_debug("%s: alg disabled\n", __func__);
  1097. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  1098. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  1099. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  1100. wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
  1101. } else if (alg == IW_ENCODE_ALG_WEP) {
  1102. pr_debug("%s: WEP requested\n", __func__);
  1103. if (flags & IW_ENCODE_OPEN) {
  1104. pr_debug("%s: open key mode\n", __func__);
  1105. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  1106. }
  1107. if (flags & IW_ENCODE_RESTRICTED) {
  1108. pr_debug("%s: shared key mode\n", __func__);
  1109. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  1110. }
  1111. if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
  1112. pr_info("%s: key is too long %d\n", __func__,
  1113. ext->key_len);
  1114. ret = -EINVAL;
  1115. goto done;
  1116. }
  1117. /* OK, update the key */
  1118. wl->key_len[key_index] = ext->key_len;
  1119. memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
  1120. memcpy(wl->key[key_index], ext->key, ext->key_len);
  1121. set_bit(key_index, &wl->key_enabled);
  1122. /* remember wep info changed */
  1123. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  1124. } else if (alg == IW_ENCODE_ALG_PMK) {
  1125. if (ext->key_len != WPA_PSK_LEN) {
  1126. pr_err("%s: PSK length wrong %d\n", __func__,
  1127. ext->key_len);
  1128. ret = -EINVAL;
  1129. goto done;
  1130. }
  1131. memset(wl->psk, 0, sizeof(wl->psk));
  1132. memcpy(wl->psk, ext->key, ext->key_len);
  1133. wl->psk_len = ext->key_len;
  1134. wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
  1135. /* remember PSK configured */
  1136. set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
  1137. }
  1138. done:
  1139. spin_unlock_irqrestore(&wl->lock, irqflag);
  1140. pr_debug("%s: ->\n", __func__);
  1141. return ret;
  1142. }
  1143. static int gelic_wl_get_encodeext(struct net_device *netdev,
  1144. struct iw_request_info *info,
  1145. union iwreq_data *data, char *extra)
  1146. {
  1147. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1148. struct iw_point *enc = &data->encoding;
  1149. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1150. unsigned long irqflag;
  1151. int key_index;
  1152. int ret = 0;
  1153. int max_key_len;
  1154. pr_debug("%s: <-\n", __func__);
  1155. max_key_len = enc->length - sizeof(struct iw_encode_ext);
  1156. if (max_key_len < 0)
  1157. return -EINVAL;
  1158. key_index = enc->flags & IW_ENCODE_INDEX;
  1159. pr_debug("%s: key_index = %d\n", __func__, key_index);
  1160. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  1161. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  1162. if (GELIC_WEP_KEYS < key_index)
  1163. return -EINVAL;
  1164. spin_lock_irqsave(&wl->lock, irqflag);
  1165. if (key_index)
  1166. key_index--;
  1167. else
  1168. key_index = wl->current_key;
  1169. memset(ext, 0, sizeof(struct iw_encode_ext));
  1170. switch (wl->group_cipher_method) {
  1171. case GELIC_WL_CIPHER_WEP:
  1172. ext->alg = IW_ENCODE_ALG_WEP;
  1173. enc->flags |= IW_ENCODE_ENABLED;
  1174. break;
  1175. case GELIC_WL_CIPHER_TKIP:
  1176. ext->alg = IW_ENCODE_ALG_TKIP;
  1177. enc->flags |= IW_ENCODE_ENABLED;
  1178. break;
  1179. case GELIC_WL_CIPHER_AES:
  1180. ext->alg = IW_ENCODE_ALG_CCMP;
  1181. enc->flags |= IW_ENCODE_ENABLED;
  1182. break;
  1183. case GELIC_WL_CIPHER_NONE:
  1184. default:
  1185. ext->alg = IW_ENCODE_ALG_NONE;
  1186. enc->flags |= IW_ENCODE_NOKEY;
  1187. break;
  1188. }
  1189. if (!(enc->flags & IW_ENCODE_NOKEY)) {
  1190. if (max_key_len < wl->key_len[key_index]) {
  1191. ret = -E2BIG;
  1192. goto out;
  1193. }
  1194. if (test_bit(key_index, &wl->key_enabled))
  1195. memcpy(ext->key, wl->key[key_index],
  1196. wl->key_len[key_index]);
  1197. else
  1198. pr_debug("%s: disabled key requested ix=%d\n",
  1199. __func__, key_index);
  1200. }
  1201. out:
  1202. spin_unlock_irqrestore(&wl->lock, irqflag);
  1203. pr_debug("%s: ->\n", __func__);
  1204. return ret;
  1205. }
  1206. /* SIOC{S,G}IWMODE */
  1207. static int gelic_wl_set_mode(struct net_device *netdev,
  1208. struct iw_request_info *info,
  1209. union iwreq_data *data, char *extra)
  1210. {
  1211. __u32 mode = data->mode;
  1212. int ret;
  1213. pr_debug("%s: <-\n", __func__);
  1214. if (mode == IW_MODE_INFRA)
  1215. ret = 0;
  1216. else
  1217. ret = -EOPNOTSUPP;
  1218. pr_debug("%s: -> %d\n", __func__, ret);
  1219. return ret;
  1220. }
  1221. static int gelic_wl_get_mode(struct net_device *netdev,
  1222. struct iw_request_info *info,
  1223. union iwreq_data *data, char *extra)
  1224. {
  1225. __u32 *mode = &data->mode;
  1226. pr_debug("%s: <-\n", __func__);
  1227. *mode = IW_MODE_INFRA;
  1228. pr_debug("%s: ->\n", __func__);
  1229. return 0;
  1230. }
  1231. /* SIOCGIWNICKN */
  1232. static int gelic_wl_get_nick(struct net_device *net_dev,
  1233. struct iw_request_info *info,
  1234. union iwreq_data *data, char *extra)
  1235. {
  1236. strcpy(extra, "gelic_wl");
  1237. data->data.length = strlen(extra);
  1238. data->data.flags = 1;
  1239. return 0;
  1240. }
  1241. /* --- */
  1242. static struct iw_statistics *gelic_wl_get_wireless_stats(
  1243. struct net_device *netdev)
  1244. {
  1245. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1246. struct gelic_eurus_cmd *cmd;
  1247. struct iw_statistics *is;
  1248. struct gelic_eurus_rssi_info *rssi;
  1249. void *buf;
  1250. pr_debug("%s: <-\n", __func__);
  1251. buf = (void *)__get_free_page(GFP_KERNEL);
  1252. if (!buf)
  1253. return NULL;
  1254. is = &wl->iwstat;
  1255. memset(is, 0, sizeof(*is));
  1256. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
  1257. buf, sizeof(*rssi));
  1258. if (cmd && !cmd->status && !cmd->cmd_status) {
  1259. rssi = buf;
  1260. is->qual.level = be16_to_cpu(rssi->rssi);
  1261. is->qual.updated = IW_QUAL_LEVEL_UPDATED |
  1262. IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
  1263. } else
  1264. /* not associated */
  1265. is->qual.updated = IW_QUAL_ALL_INVALID;
  1266. kfree(cmd);
  1267. free_page((unsigned long)buf);
  1268. pr_debug("%s: ->\n", __func__);
  1269. return is;
  1270. }
  1271. /*
  1272. * scanning helpers
  1273. */
  1274. static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
  1275. u8 *essid, size_t essid_len)
  1276. {
  1277. struct gelic_eurus_cmd *cmd;
  1278. int ret = 0;
  1279. void *buf = NULL;
  1280. size_t len;
  1281. pr_debug("%s: <- always=%d\n", __func__, always_scan);
  1282. if (mutex_lock_interruptible(&wl->scan_lock))
  1283. return -ERESTARTSYS;
  1284. /*
  1285. * If already a scan in progress, do not trigger more
  1286. */
  1287. if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
  1288. pr_debug("%s: scanning now\n", __func__);
  1289. goto out;
  1290. }
  1291. init_completion(&wl->scan_done);
  1292. /*
  1293. * If we have already a bss list, don't try to get new
  1294. * unless we are doing an ESSID scan
  1295. */
  1296. if ((!essid_len && !always_scan)
  1297. && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
  1298. pr_debug("%s: already has the list\n", __func__);
  1299. complete(&wl->scan_done);
  1300. goto out;
  1301. }
  1302. /* ESSID scan ? */
  1303. if (essid_len && essid) {
  1304. buf = (void *)__get_free_page(GFP_KERNEL);
  1305. if (!buf) {
  1306. ret = -ENOMEM;
  1307. goto out;
  1308. }
  1309. len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
  1310. memset(buf, 0, len);
  1311. memcpy(buf, essid, essid_len);
  1312. pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
  1313. } else
  1314. len = 0;
  1315. /*
  1316. * issue start scan request
  1317. */
  1318. wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
  1319. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
  1320. buf, len);
  1321. if (!cmd || cmd->status || cmd->cmd_status) {
  1322. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  1323. complete(&wl->scan_done);
  1324. ret = -ENOMEM;
  1325. goto out;
  1326. }
  1327. kfree(cmd);
  1328. out:
  1329. free_page((unsigned long)buf);
  1330. mutex_unlock(&wl->scan_lock);
  1331. pr_debug("%s: ->\n", __func__);
  1332. return ret;
  1333. }
  1334. /*
  1335. * retrieve scan result from the chip (hypervisor)
  1336. * this function is invoked by schedule work.
  1337. */
  1338. static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
  1339. {
  1340. struct gelic_eurus_cmd *cmd = NULL;
  1341. struct gelic_wl_scan_info *target, *tmp;
  1342. struct gelic_wl_scan_info *oldest = NULL;
  1343. struct gelic_eurus_scan_info *scan_info;
  1344. unsigned int scan_info_size;
  1345. union iwreq_data data;
  1346. unsigned long this_time = jiffies;
  1347. unsigned int data_len, i, found, r;
  1348. void *buf;
  1349. pr_debug("%s:start\n", __func__);
  1350. mutex_lock(&wl->scan_lock);
  1351. buf = (void *)__get_free_page(GFP_KERNEL);
  1352. if (!buf) {
  1353. pr_info("%s: scan buffer alloc failed\n", __func__);
  1354. goto out;
  1355. }
  1356. if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
  1357. /*
  1358. * stop() may be called while scanning, ignore result
  1359. */
  1360. pr_debug("%s: scan complete when stat != scanning(%d)\n",
  1361. __func__, wl->scan_stat);
  1362. goto out;
  1363. }
  1364. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
  1365. buf, PAGE_SIZE);
  1366. if (!cmd || cmd->status || cmd->cmd_status) {
  1367. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  1368. pr_info("%s:cmd failed\n", __func__);
  1369. kfree(cmd);
  1370. goto out;
  1371. }
  1372. data_len = cmd->size;
  1373. pr_debug("%s: data_len = %d\n", __func__, data_len);
  1374. kfree(cmd);
  1375. /* OK, bss list retrieved */
  1376. wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
  1377. /* mark all entries are old */
  1378. list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
  1379. target->valid = 0;
  1380. /* expire too old entries */
  1381. if (time_before(target->last_scanned + wl->scan_age,
  1382. this_time)) {
  1383. kfree(target->hwinfo);
  1384. target->hwinfo = NULL;
  1385. list_move_tail(&target->list, &wl->network_free_list);
  1386. }
  1387. }
  1388. /* put them in the network_list */
  1389. for (i = 0, scan_info_size = 0, scan_info = buf;
  1390. scan_info_size < data_len;
  1391. i++, scan_info_size += be16_to_cpu(scan_info->size),
  1392. scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
  1393. pr_debug("%s:size=%d bssid=%pM scan_info=%p\n", __func__,
  1394. be16_to_cpu(scan_info->size),
  1395. &scan_info->bssid[2], scan_info);
  1396. /*
  1397. * The wireless firmware may return invalid channel 0 and/or
  1398. * invalid rate if the AP emits zero length SSID ie. As this
  1399. * scan information is useless, ignore it
  1400. */
  1401. if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
  1402. pr_debug("%s: invalid scan info\n", __func__);
  1403. continue;
  1404. }
  1405. found = 0;
  1406. oldest = NULL;
  1407. list_for_each_entry(target, &wl->network_list, list) {
  1408. if (ether_addr_equal(&target->hwinfo->bssid[2],
  1409. &scan_info->bssid[2])) {
  1410. found = 1;
  1411. pr_debug("%s: same BBS found scanned list\n",
  1412. __func__);
  1413. break;
  1414. }
  1415. if (!oldest ||
  1416. (target->last_scanned < oldest->last_scanned))
  1417. oldest = target;
  1418. }
  1419. if (!found) {
  1420. /* not found in the list */
  1421. if (list_empty(&wl->network_free_list)) {
  1422. /* expire oldest */
  1423. target = oldest;
  1424. } else {
  1425. target = list_entry(wl->network_free_list.next,
  1426. struct gelic_wl_scan_info,
  1427. list);
  1428. }
  1429. }
  1430. /* update the item */
  1431. target->last_scanned = this_time;
  1432. target->valid = 1;
  1433. target->eurus_index = i;
  1434. kfree(target->hwinfo);
  1435. target->hwinfo = kmemdup(scan_info,
  1436. be16_to_cpu(scan_info->size),
  1437. GFP_KERNEL);
  1438. if (!target->hwinfo)
  1439. continue;
  1440. /* copy hw scan info */
  1441. target->essid_len = strnlen(scan_info->essid,
  1442. sizeof(scan_info->essid));
  1443. target->rate_len = 0;
  1444. for (r = 0; r < 12; r++)
  1445. if (scan_info->rate[r])
  1446. target->rate_len++;
  1447. if (8 < target->rate_len)
  1448. pr_info("%s: AP returns %d rates\n", __func__,
  1449. target->rate_len);
  1450. target->rate_ext_len = 0;
  1451. for (r = 0; r < 16; r++)
  1452. if (scan_info->ext_rate[r])
  1453. target->rate_ext_len++;
  1454. list_move_tail(&target->list, &wl->network_list);
  1455. }
  1456. memset(&data, 0, sizeof(data));
  1457. wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
  1458. NULL);
  1459. out:
  1460. free_page((unsigned long)buf);
  1461. complete(&wl->scan_done);
  1462. mutex_unlock(&wl->scan_lock);
  1463. pr_debug("%s:end\n", __func__);
  1464. }
  1465. /*
  1466. * Select an appropriate bss from current scan list regarding
  1467. * current settings from userspace.
  1468. * The caller must hold wl->scan_lock,
  1469. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1470. */
  1471. static void update_best(struct gelic_wl_scan_info **best,
  1472. struct gelic_wl_scan_info *candid,
  1473. int *best_weight,
  1474. int *weight)
  1475. {
  1476. if (*best_weight < ++(*weight)) {
  1477. *best_weight = *weight;
  1478. *best = candid;
  1479. }
  1480. }
  1481. static
  1482. struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
  1483. {
  1484. struct gelic_wl_scan_info *scan_info;
  1485. struct gelic_wl_scan_info *best_bss;
  1486. int weight, best_weight;
  1487. u16 security;
  1488. pr_debug("%s: <-\n", __func__);
  1489. best_bss = NULL;
  1490. best_weight = 0;
  1491. list_for_each_entry(scan_info, &wl->network_list, list) {
  1492. pr_debug("%s: station %p\n", __func__, scan_info);
  1493. if (!scan_info->valid) {
  1494. pr_debug("%s: station invalid\n", __func__);
  1495. continue;
  1496. }
  1497. /* If bss specified, check it only */
  1498. if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
  1499. if (ether_addr_equal(&scan_info->hwinfo->bssid[2],
  1500. wl->bssid)) {
  1501. best_bss = scan_info;
  1502. pr_debug("%s: bssid matched\n", __func__);
  1503. break;
  1504. } else {
  1505. pr_debug("%s: bssid unmatched\n", __func__);
  1506. continue;
  1507. }
  1508. }
  1509. weight = 0;
  1510. /* security */
  1511. security = be16_to_cpu(scan_info->hwinfo->security) &
  1512. GELIC_EURUS_SCAN_SEC_MASK;
  1513. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1514. if (security == GELIC_EURUS_SCAN_SEC_WPA2)
  1515. update_best(&best_bss, scan_info,
  1516. &best_weight, &weight);
  1517. else
  1518. continue;
  1519. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
  1520. if (security == GELIC_EURUS_SCAN_SEC_WPA)
  1521. update_best(&best_bss, scan_info,
  1522. &best_weight, &weight);
  1523. else
  1524. continue;
  1525. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
  1526. wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1527. if (security == GELIC_EURUS_SCAN_SEC_WEP)
  1528. update_best(&best_bss, scan_info,
  1529. &best_weight, &weight);
  1530. else
  1531. continue;
  1532. }
  1533. /* If ESSID is set, check it */
  1534. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
  1535. if ((scan_info->essid_len == wl->essid_len) &&
  1536. !strncmp(wl->essid,
  1537. scan_info->hwinfo->essid,
  1538. scan_info->essid_len))
  1539. update_best(&best_bss, scan_info,
  1540. &best_weight, &weight);
  1541. else
  1542. continue;
  1543. }
  1544. }
  1545. #ifdef DEBUG
  1546. pr_debug("%s: -> bss=%p\n", __func__, best_bss);
  1547. if (best_bss) {
  1548. pr_debug("%s:addr=%pM\n", __func__,
  1549. &best_bss->hwinfo->bssid[2]);
  1550. }
  1551. #endif
  1552. return best_bss;
  1553. }
  1554. /*
  1555. * Setup WEP configuration to the chip
  1556. * The caller must hold wl->scan_lock,
  1557. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1558. */
  1559. static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
  1560. {
  1561. unsigned int i;
  1562. struct gelic_eurus_wep_cfg *wep;
  1563. struct gelic_eurus_cmd *cmd;
  1564. int wep104 = 0;
  1565. int have_key = 0;
  1566. int ret = 0;
  1567. pr_debug("%s: <-\n", __func__);
  1568. /* we can assume no one should uses the buffer */
  1569. wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
  1570. if (!wep)
  1571. return -ENOMEM;
  1572. memset(wep, 0, sizeof(*wep));
  1573. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1574. pr_debug("%s: WEP mode\n", __func__);
  1575. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  1576. if (!test_bit(i, &wl->key_enabled))
  1577. continue;
  1578. pr_debug("%s: key#%d enabled\n", __func__, i);
  1579. have_key = 1;
  1580. if (wl->key_len[i] == 13)
  1581. wep104 = 1;
  1582. else if (wl->key_len[i] != 5) {
  1583. pr_info("%s: wrong wep key[%d]=%d\n",
  1584. __func__, i, wl->key_len[i]);
  1585. ret = -EINVAL;
  1586. goto out;
  1587. }
  1588. memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
  1589. }
  1590. if (!have_key) {
  1591. pr_info("%s: all wep key disabled\n", __func__);
  1592. ret = -EINVAL;
  1593. goto out;
  1594. }
  1595. if (wep104) {
  1596. pr_debug("%s: 104bit key\n", __func__);
  1597. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
  1598. } else {
  1599. pr_debug("%s: 40bit key\n", __func__);
  1600. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
  1601. }
  1602. } else {
  1603. pr_debug("%s: NO encryption\n", __func__);
  1604. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
  1605. }
  1606. /* issue wep setup */
  1607. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
  1608. wep, sizeof(*wep));
  1609. if (!cmd)
  1610. ret = -ENOMEM;
  1611. else if (cmd->status || cmd->cmd_status)
  1612. ret = -ENXIO;
  1613. kfree(cmd);
  1614. out:
  1615. free_page((unsigned long)wep);
  1616. pr_debug("%s: ->\n", __func__);
  1617. return ret;
  1618. }
  1619. #ifdef DEBUG
  1620. static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
  1621. {
  1622. switch (sec) {
  1623. case GELIC_EURUS_WPA_SEC_NONE:
  1624. return "NONE";
  1625. case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
  1626. return "WPA_TKIP_TKIP";
  1627. case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
  1628. return "WPA_TKIP_AES";
  1629. case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
  1630. return "WPA_AES_AES";
  1631. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
  1632. return "WPA2_TKIP_TKIP";
  1633. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
  1634. return "WPA2_TKIP_AES";
  1635. case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
  1636. return "WPA2_AES_AES";
  1637. }
  1638. return "";
  1639. };
  1640. #endif
  1641. static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
  1642. {
  1643. struct gelic_eurus_wpa_cfg *wpa;
  1644. struct gelic_eurus_cmd *cmd;
  1645. u16 security;
  1646. int ret = 0;
  1647. pr_debug("%s: <-\n", __func__);
  1648. /* we can assume no one should uses the buffer */
  1649. wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
  1650. if (!wpa)
  1651. return -ENOMEM;
  1652. memset(wpa, 0, sizeof(*wpa));
  1653. if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
  1654. pr_info("%s: PSK not configured yet\n", __func__);
  1655. /* copy key */
  1656. memcpy(wpa->psk, wl->psk, wl->psk_len);
  1657. /* set security level */
  1658. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1659. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1660. security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
  1661. } else {
  1662. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1663. precise_ie())
  1664. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
  1665. else
  1666. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
  1667. }
  1668. } else {
  1669. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1670. security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
  1671. } else {
  1672. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1673. precise_ie())
  1674. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
  1675. else
  1676. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
  1677. }
  1678. }
  1679. wpa->security = cpu_to_be16(security);
  1680. /* PSK type */
  1681. wpa->psk_type = cpu_to_be16(wl->psk_type);
  1682. #ifdef DEBUG
  1683. pr_debug("%s: sec=%s psktype=%s\n", __func__,
  1684. wpasecstr(wpa->security),
  1685. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1686. "BIN" : "passphrase");
  1687. #if 0
  1688. /*
  1689. * don't enable here if you plan to submit
  1690. * the debug log because this dumps your precious
  1691. * passphrase/key.
  1692. */
  1693. pr_debug("%s: psk=%s\n", __func__,
  1694. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1695. "N/A" : wpa->psk);
  1696. #endif
  1697. #endif
  1698. /* issue wpa setup */
  1699. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
  1700. wpa, sizeof(*wpa));
  1701. if (!cmd)
  1702. ret = -ENOMEM;
  1703. else if (cmd->status || cmd->cmd_status)
  1704. ret = -ENXIO;
  1705. kfree(cmd);
  1706. free_page((unsigned long)wpa);
  1707. pr_debug("%s: --> %d\n", __func__, ret);
  1708. return ret;
  1709. }
  1710. /*
  1711. * Start association. caller must hold assoc_stat_lock
  1712. */
  1713. static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
  1714. struct gelic_wl_scan_info *bss)
  1715. {
  1716. struct gelic_eurus_cmd *cmd;
  1717. struct gelic_eurus_common_cfg *common;
  1718. int ret = 0;
  1719. unsigned long rc;
  1720. pr_debug("%s: <-\n", __func__);
  1721. /* do common config */
  1722. common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
  1723. if (!common)
  1724. return -ENOMEM;
  1725. memset(common, 0, sizeof(*common));
  1726. common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
  1727. common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
  1728. common->scan_index = cpu_to_be16(bss->eurus_index);
  1729. switch (wl->auth_method) {
  1730. case GELIC_EURUS_AUTH_OPEN:
  1731. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
  1732. break;
  1733. case GELIC_EURUS_AUTH_SHARED:
  1734. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
  1735. break;
  1736. }
  1737. #ifdef DEBUG
  1738. scan_list_dump(wl);
  1739. #endif
  1740. pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
  1741. be16_to_cpu(common->scan_index),
  1742. be16_to_cpu(common->bss_type),
  1743. be16_to_cpu(common->auth_method));
  1744. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
  1745. common, sizeof(*common));
  1746. if (!cmd || cmd->status || cmd->cmd_status) {
  1747. ret = -ENOMEM;
  1748. kfree(cmd);
  1749. goto out;
  1750. }
  1751. kfree(cmd);
  1752. /* WEP/WPA */
  1753. switch (wl->wpa_level) {
  1754. case GELIC_WL_WPA_LEVEL_NONE:
  1755. /* If WEP or no security, setup WEP config */
  1756. ret = gelic_wl_do_wep_setup(wl);
  1757. break;
  1758. case GELIC_WL_WPA_LEVEL_WPA:
  1759. case GELIC_WL_WPA_LEVEL_WPA2:
  1760. ret = gelic_wl_do_wpa_setup(wl);
  1761. break;
  1762. }
  1763. if (ret) {
  1764. pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
  1765. ret);
  1766. ret = -EPERM;
  1767. gelic_wl_send_iwap_event(wl, NULL);
  1768. goto out;
  1769. }
  1770. /* start association */
  1771. init_completion(&wl->assoc_done);
  1772. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
  1773. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
  1774. NULL, 0);
  1775. if (!cmd || cmd->status || cmd->cmd_status) {
  1776. pr_debug("%s: assoc request failed\n", __func__);
  1777. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1778. kfree(cmd);
  1779. ret = -ENOMEM;
  1780. gelic_wl_send_iwap_event(wl, NULL);
  1781. goto out;
  1782. }
  1783. kfree(cmd);
  1784. /* wait for connected event */
  1785. rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
  1786. if (!rc) {
  1787. /* timeouted. Maybe key or cyrpt mode is wrong */
  1788. pr_info("%s: connect timeout\n", __func__);
  1789. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
  1790. NULL, 0);
  1791. kfree(cmd);
  1792. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1793. gelic_wl_send_iwap_event(wl, NULL);
  1794. ret = -ENXIO;
  1795. } else {
  1796. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
  1797. /* copy bssid */
  1798. memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
  1799. /* send connect event */
  1800. gelic_wl_send_iwap_event(wl, wl->active_bssid);
  1801. pr_info("%s: connected\n", __func__);
  1802. }
  1803. out:
  1804. free_page((unsigned long)common);
  1805. pr_debug("%s: ->\n", __func__);
  1806. return ret;
  1807. }
  1808. /*
  1809. * connected event
  1810. */
  1811. static void gelic_wl_connected_event(struct gelic_wl_info *wl,
  1812. u64 event)
  1813. {
  1814. u64 desired_event = 0;
  1815. switch (wl->wpa_level) {
  1816. case GELIC_WL_WPA_LEVEL_NONE:
  1817. desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
  1818. break;
  1819. case GELIC_WL_WPA_LEVEL_WPA:
  1820. case GELIC_WL_WPA_LEVEL_WPA2:
  1821. desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
  1822. break;
  1823. }
  1824. if (desired_event == event) {
  1825. pr_debug("%s: completed\n", __func__);
  1826. complete(&wl->assoc_done);
  1827. netif_carrier_on(port_to_netdev(wl_port(wl)));
  1828. } else
  1829. pr_debug("%s: event %#llx under wpa\n",
  1830. __func__, event);
  1831. }
  1832. /*
  1833. * disconnect event
  1834. */
  1835. static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
  1836. u64 event)
  1837. {
  1838. struct gelic_eurus_cmd *cmd;
  1839. int lock;
  1840. /*
  1841. * If we fall here in the middle of association,
  1842. * associate_bss() should be waiting for complation of
  1843. * wl->assoc_done.
  1844. * As it waits with timeout, just leave assoc_done
  1845. * uncompleted, then it terminates with timeout
  1846. */
  1847. if (!mutex_trylock(&wl->assoc_stat_lock)) {
  1848. pr_debug("%s: already locked\n", __func__);
  1849. lock = 0;
  1850. } else {
  1851. pr_debug("%s: obtain lock\n", __func__);
  1852. lock = 1;
  1853. }
  1854. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  1855. kfree(cmd);
  1856. /* send disconnected event to the supplicant */
  1857. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  1858. gelic_wl_send_iwap_event(wl, NULL);
  1859. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1860. netif_carrier_off(port_to_netdev(wl_port(wl)));
  1861. if (lock)
  1862. mutex_unlock(&wl->assoc_stat_lock);
  1863. }
  1864. /*
  1865. * event worker
  1866. */
  1867. #ifdef DEBUG
  1868. static const char *eventstr(enum gelic_lv1_wl_event event)
  1869. {
  1870. static char buf[32];
  1871. char *ret;
  1872. if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
  1873. ret = "EURUS_READY";
  1874. else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
  1875. ret = "SCAN_COMPLETED";
  1876. else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
  1877. ret = "DEAUTH";
  1878. else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
  1879. ret = "BEACON_LOST";
  1880. else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
  1881. ret = "CONNECTED";
  1882. else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
  1883. ret = "WPA_CONNECTED";
  1884. else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
  1885. ret = "WPA_ERROR";
  1886. else {
  1887. sprintf(buf, "Unknown(%#x)", event);
  1888. ret = buf;
  1889. }
  1890. return ret;
  1891. }
  1892. #else
  1893. static const char *eventstr(enum gelic_lv1_wl_event event)
  1894. {
  1895. return NULL;
  1896. }
  1897. #endif
  1898. static void gelic_wl_event_worker(struct work_struct *work)
  1899. {
  1900. struct gelic_wl_info *wl;
  1901. struct gelic_port *port;
  1902. u64 event, tmp;
  1903. int status;
  1904. pr_debug("%s:start\n", __func__);
  1905. wl = container_of(work, struct gelic_wl_info, event_work.work);
  1906. port = wl_port(wl);
  1907. while (1) {
  1908. status = lv1_net_control(bus_id(port->card), dev_id(port->card),
  1909. GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
  1910. &event, &tmp);
  1911. if (status) {
  1912. if (status != LV1_NO_ENTRY)
  1913. pr_debug("%s:wlan event failed %d\n",
  1914. __func__, status);
  1915. /* got all events */
  1916. pr_debug("%s:end\n", __func__);
  1917. return;
  1918. }
  1919. pr_debug("%s: event=%s\n", __func__, eventstr(event));
  1920. switch (event) {
  1921. case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
  1922. gelic_wl_scan_complete_event(wl);
  1923. break;
  1924. case GELIC_LV1_WL_EVENT_BEACON_LOST:
  1925. case GELIC_LV1_WL_EVENT_DEAUTH:
  1926. gelic_wl_disconnect_event(wl, event);
  1927. break;
  1928. case GELIC_LV1_WL_EVENT_CONNECTED:
  1929. case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
  1930. gelic_wl_connected_event(wl, event);
  1931. break;
  1932. default:
  1933. break;
  1934. }
  1935. } /* while */
  1936. }
  1937. /*
  1938. * association worker
  1939. */
  1940. static void gelic_wl_assoc_worker(struct work_struct *work)
  1941. {
  1942. struct gelic_wl_info *wl;
  1943. struct gelic_wl_scan_info *best_bss;
  1944. int ret;
  1945. unsigned long irqflag;
  1946. u8 *essid;
  1947. size_t essid_len;
  1948. wl = container_of(work, struct gelic_wl_info, assoc_work.work);
  1949. mutex_lock(&wl->assoc_stat_lock);
  1950. if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
  1951. goto out;
  1952. spin_lock_irqsave(&wl->lock, irqflag);
  1953. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
  1954. pr_debug("%s: assoc ESSID configured %s\n", __func__,
  1955. wl->essid);
  1956. essid = wl->essid;
  1957. essid_len = wl->essid_len;
  1958. } else {
  1959. essid = NULL;
  1960. essid_len = 0;
  1961. }
  1962. spin_unlock_irqrestore(&wl->lock, irqflag);
  1963. ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
  1964. if (ret == -ERESTARTSYS) {
  1965. pr_debug("%s: scan start failed association\n", __func__);
  1966. schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
  1967. goto out;
  1968. } else if (ret) {
  1969. pr_info("%s: scan prerequisite failed\n", __func__);
  1970. goto out;
  1971. }
  1972. /*
  1973. * Wait for bss scan completion
  1974. * If we have scan list already, gelic_wl_start_scan()
  1975. * returns OK and raises the complete. Thus,
  1976. * it's ok to wait unconditionally here
  1977. */
  1978. wait_for_completion(&wl->scan_done);
  1979. pr_debug("%s: scan done\n", __func__);
  1980. mutex_lock(&wl->scan_lock);
  1981. if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
  1982. gelic_wl_send_iwap_event(wl, NULL);
  1983. pr_info("%s: no scan list. association failed\n", __func__);
  1984. goto scan_lock_out;
  1985. }
  1986. /* find best matching bss */
  1987. best_bss = gelic_wl_find_best_bss(wl);
  1988. if (!best_bss) {
  1989. gelic_wl_send_iwap_event(wl, NULL);
  1990. pr_info("%s: no bss matched. association failed\n", __func__);
  1991. goto scan_lock_out;
  1992. }
  1993. /* ok, do association */
  1994. ret = gelic_wl_associate_bss(wl, best_bss);
  1995. if (ret)
  1996. pr_info("%s: association failed %d\n", __func__, ret);
  1997. scan_lock_out:
  1998. mutex_unlock(&wl->scan_lock);
  1999. out:
  2000. mutex_unlock(&wl->assoc_stat_lock);
  2001. }
  2002. /*
  2003. * Interrupt handler
  2004. * Called from the ethernet interrupt handler
  2005. * Processes wireless specific virtual interrupts only
  2006. */
  2007. void gelic_wl_interrupt(struct net_device *netdev, u64 status)
  2008. {
  2009. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2010. if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
  2011. pr_debug("%s:cmd complete\n", __func__);
  2012. complete(&wl->cmd_done_intr);
  2013. }
  2014. if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
  2015. pr_debug("%s:event received\n", __func__);
  2016. queue_delayed_work(wl->event_queue, &wl->event_work, 0);
  2017. }
  2018. }
  2019. /*
  2020. * driver helpers
  2021. */
  2022. static const iw_handler gelic_wl_wext_handler[] =
  2023. {
  2024. IW_HANDLER(SIOCGIWNAME, gelic_wl_get_name),
  2025. IW_HANDLER(SIOCGIWRANGE, gelic_wl_get_range),
  2026. IW_HANDLER(SIOCSIWSCAN, gelic_wl_set_scan),
  2027. IW_HANDLER(SIOCGIWSCAN, gelic_wl_get_scan),
  2028. IW_HANDLER(SIOCSIWAUTH, gelic_wl_set_auth),
  2029. IW_HANDLER(SIOCGIWAUTH, gelic_wl_get_auth),
  2030. IW_HANDLER(SIOCSIWESSID, gelic_wl_set_essid),
  2031. IW_HANDLER(SIOCGIWESSID, gelic_wl_get_essid),
  2032. IW_HANDLER(SIOCSIWENCODE, gelic_wl_set_encode),
  2033. IW_HANDLER(SIOCGIWENCODE, gelic_wl_get_encode),
  2034. IW_HANDLER(SIOCSIWAP, gelic_wl_set_ap),
  2035. IW_HANDLER(SIOCGIWAP, gelic_wl_get_ap),
  2036. IW_HANDLER(SIOCSIWENCODEEXT, gelic_wl_set_encodeext),
  2037. IW_HANDLER(SIOCGIWENCODEEXT, gelic_wl_get_encodeext),
  2038. IW_HANDLER(SIOCSIWMODE, gelic_wl_set_mode),
  2039. IW_HANDLER(SIOCGIWMODE, gelic_wl_get_mode),
  2040. IW_HANDLER(SIOCGIWNICKN, gelic_wl_get_nick),
  2041. };
  2042. static const struct iw_handler_def gelic_wl_wext_handler_def = {
  2043. .num_standard = ARRAY_SIZE(gelic_wl_wext_handler),
  2044. .standard = gelic_wl_wext_handler,
  2045. .get_wireless_stats = gelic_wl_get_wireless_stats,
  2046. };
  2047. static struct net_device *gelic_wl_alloc(struct gelic_card *card)
  2048. {
  2049. struct net_device *netdev;
  2050. struct gelic_port *port;
  2051. struct gelic_wl_info *wl;
  2052. unsigned int i;
  2053. pr_debug("%s:start\n", __func__);
  2054. netdev = alloc_etherdev(sizeof(struct gelic_port) +
  2055. sizeof(struct gelic_wl_info));
  2056. pr_debug("%s: netdev =%p card=%p\n", __func__, netdev, card);
  2057. if (!netdev)
  2058. return NULL;
  2059. strcpy(netdev->name, "wlan%d");
  2060. port = netdev_priv(netdev);
  2061. port->netdev = netdev;
  2062. port->card = card;
  2063. port->type = GELIC_PORT_WIRELESS;
  2064. wl = port_wl(port);
  2065. pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
  2066. /* allocate scan list */
  2067. wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
  2068. GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
  2069. if (!wl->networks)
  2070. goto fail_bss;
  2071. wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
  2072. if (!wl->eurus_cmd_queue)
  2073. goto fail_cmd_workqueue;
  2074. wl->event_queue = create_singlethread_workqueue("gelic_event");
  2075. if (!wl->event_queue)
  2076. goto fail_event_workqueue;
  2077. INIT_LIST_HEAD(&wl->network_free_list);
  2078. INIT_LIST_HEAD(&wl->network_list);
  2079. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
  2080. list_add_tail(&wl->networks[i].list,
  2081. &wl->network_free_list);
  2082. init_completion(&wl->cmd_done_intr);
  2083. INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
  2084. INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
  2085. mutex_init(&wl->scan_lock);
  2086. mutex_init(&wl->assoc_stat_lock);
  2087. init_completion(&wl->scan_done);
  2088. /* for the case that no scan request is issued and stop() is called */
  2089. complete(&wl->scan_done);
  2090. spin_lock_init(&wl->lock);
  2091. wl->scan_age = 5*HZ; /* FIXME */
  2092. /* buffer for receiving scanned list etc */
  2093. BUILD_BUG_ON(PAGE_SIZE <
  2094. sizeof(struct gelic_eurus_scan_info) *
  2095. GELIC_EURUS_MAX_SCAN);
  2096. pr_debug("%s:end\n", __func__);
  2097. return netdev;
  2098. fail_event_workqueue:
  2099. destroy_workqueue(wl->eurus_cmd_queue);
  2100. fail_cmd_workqueue:
  2101. kfree(wl->networks);
  2102. fail_bss:
  2103. free_netdev(netdev);
  2104. pr_debug("%s:end error\n", __func__);
  2105. return NULL;
  2106. }
  2107. static void gelic_wl_free(struct gelic_wl_info *wl)
  2108. {
  2109. struct gelic_wl_scan_info *scan_info;
  2110. unsigned int i;
  2111. pr_debug("%s: <-\n", __func__);
  2112. pr_debug("%s: destroy queues\n", __func__);
  2113. destroy_workqueue(wl->eurus_cmd_queue);
  2114. destroy_workqueue(wl->event_queue);
  2115. scan_info = wl->networks;
  2116. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
  2117. kfree(scan_info->hwinfo);
  2118. kfree(wl->networks);
  2119. free_netdev(port_to_netdev(wl_port(wl)));
  2120. pr_debug("%s: ->\n", __func__);
  2121. }
  2122. static int gelic_wl_try_associate(struct net_device *netdev)
  2123. {
  2124. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2125. int ret = -1;
  2126. unsigned int i;
  2127. pr_debug("%s: <-\n", __func__);
  2128. /* check constraits for start association */
  2129. /* for no access restriction AP */
  2130. if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
  2131. if (test_bit(GELIC_WL_STAT_CONFIGURED,
  2132. &wl->stat))
  2133. goto do_associate;
  2134. else {
  2135. pr_debug("%s: no wep, not configured\n", __func__);
  2136. return ret;
  2137. }
  2138. }
  2139. /* for WEP, one of four keys should be set */
  2140. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  2141. /* one of keys set */
  2142. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  2143. if (test_bit(i, &wl->key_enabled))
  2144. goto do_associate;
  2145. }
  2146. pr_debug("%s: WEP, but no key specified\n", __func__);
  2147. return ret;
  2148. }
  2149. /* for WPA[2], psk should be set */
  2150. if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
  2151. (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
  2152. if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
  2153. &wl->stat))
  2154. goto do_associate;
  2155. else {
  2156. pr_debug("%s: AES/TKIP, but PSK not configured\n",
  2157. __func__);
  2158. return ret;
  2159. }
  2160. }
  2161. do_associate:
  2162. ret = schedule_delayed_work(&wl->assoc_work, 0);
  2163. pr_debug("%s: start association work %d\n", __func__, ret);
  2164. return ret;
  2165. }
  2166. /*
  2167. * netdev handlers
  2168. */
  2169. static int gelic_wl_open(struct net_device *netdev)
  2170. {
  2171. struct gelic_card *card = netdev_card(netdev);
  2172. pr_debug("%s:->%p\n", __func__, netdev);
  2173. gelic_card_up(card);
  2174. /* try to associate */
  2175. gelic_wl_try_associate(netdev);
  2176. netif_start_queue(netdev);
  2177. pr_debug("%s:<-\n", __func__);
  2178. return 0;
  2179. }
  2180. /*
  2181. * reset state machine
  2182. */
  2183. static int gelic_wl_reset_state(struct gelic_wl_info *wl)
  2184. {
  2185. struct gelic_wl_scan_info *target;
  2186. struct gelic_wl_scan_info *tmp;
  2187. /* empty scan list */
  2188. list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
  2189. list_move_tail(&target->list, &wl->network_free_list);
  2190. }
  2191. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  2192. /* clear configuration */
  2193. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  2194. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  2195. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  2196. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  2197. wl->key_enabled = 0;
  2198. wl->current_key = 0;
  2199. wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
  2200. wl->psk_len = 0;
  2201. wl->essid_len = 0;
  2202. memset(wl->essid, 0, sizeof(wl->essid));
  2203. memset(wl->bssid, 0, sizeof(wl->bssid));
  2204. memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
  2205. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  2206. memset(&wl->iwstat, 0, sizeof(wl->iwstat));
  2207. /* all status bit clear */
  2208. wl->stat = 0;
  2209. return 0;
  2210. }
  2211. /*
  2212. * Tell eurus to terminate association
  2213. */
  2214. static void gelic_wl_disconnect(struct net_device *netdev)
  2215. {
  2216. struct gelic_port *port = netdev_priv(netdev);
  2217. struct gelic_wl_info *wl = port_wl(port);
  2218. struct gelic_eurus_cmd *cmd;
  2219. /*
  2220. * If scann process is running on chip,
  2221. * further requests will be rejected
  2222. */
  2223. if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
  2224. wait_for_completion_timeout(&wl->scan_done, HZ);
  2225. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  2226. kfree(cmd);
  2227. gelic_wl_send_iwap_event(wl, NULL);
  2228. };
  2229. static int gelic_wl_stop(struct net_device *netdev)
  2230. {
  2231. struct gelic_port *port = netdev_priv(netdev);
  2232. struct gelic_wl_info *wl = port_wl(port);
  2233. struct gelic_card *card = netdev_card(netdev);
  2234. pr_debug("%s:<-\n", __func__);
  2235. /*
  2236. * Cancel pending association work.
  2237. * event work can run after netdev down
  2238. */
  2239. cancel_delayed_work(&wl->assoc_work);
  2240. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2241. gelic_wl_disconnect(netdev);
  2242. /* reset our state machine */
  2243. gelic_wl_reset_state(wl);
  2244. netif_stop_queue(netdev);
  2245. gelic_card_down(card);
  2246. pr_debug("%s:->\n", __func__);
  2247. return 0;
  2248. }
  2249. /* -- */
  2250. static const struct net_device_ops gelic_wl_netdevice_ops = {
  2251. .ndo_open = gelic_wl_open,
  2252. .ndo_stop = gelic_wl_stop,
  2253. .ndo_start_xmit = gelic_net_xmit,
  2254. .ndo_set_rx_mode = gelic_net_set_multi,
  2255. .ndo_change_mtu = gelic_net_change_mtu,
  2256. .ndo_tx_timeout = gelic_net_tx_timeout,
  2257. .ndo_set_mac_address = eth_mac_addr,
  2258. .ndo_validate_addr = eth_validate_addr,
  2259. #ifdef CONFIG_NET_POLL_CONTROLLER
  2260. .ndo_poll_controller = gelic_net_poll_controller,
  2261. #endif
  2262. };
  2263. static const struct ethtool_ops gelic_wl_ethtool_ops = {
  2264. .get_drvinfo = gelic_net_get_drvinfo,
  2265. .get_link = gelic_wl_get_link,
  2266. };
  2267. static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
  2268. {
  2269. struct gelic_wl_info *wl;
  2270. wl = port_wl(netdev_priv(netdev));
  2271. BUG_ON(!wl);
  2272. netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
  2273. netdev->ethtool_ops = &gelic_wl_ethtool_ops;
  2274. netdev->netdev_ops = &gelic_wl_netdevice_ops;
  2275. netdev->wireless_data = &wl->wireless_data;
  2276. netdev->wireless_handlers = &gelic_wl_wext_handler_def;
  2277. }
  2278. /*
  2279. * driver probe/remove
  2280. */
  2281. int gelic_wl_driver_probe(struct gelic_card *card)
  2282. {
  2283. int ret;
  2284. struct net_device *netdev;
  2285. pr_debug("%s:start\n", __func__);
  2286. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2287. return 0;
  2288. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2289. return 0;
  2290. /* alloc netdevice for wireless */
  2291. netdev = gelic_wl_alloc(card);
  2292. if (!netdev)
  2293. return -ENOMEM;
  2294. /* setup net_device structure */
  2295. SET_NETDEV_DEV(netdev, &card->dev->core);
  2296. gelic_wl_setup_netdev_ops(netdev);
  2297. /* setup some of net_device and register it */
  2298. ret = gelic_net_setup_netdev(netdev, card);
  2299. if (ret)
  2300. goto fail_setup;
  2301. card->netdev[GELIC_PORT_WIRELESS] = netdev;
  2302. /* add enable wireless interrupt */
  2303. card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
  2304. GELIC_CARD_WLAN_COMMAND_COMPLETED;
  2305. /* to allow wireless commands while both interfaces are down */
  2306. gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
  2307. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2308. pr_debug("%s:end\n", __func__);
  2309. return 0;
  2310. fail_setup:
  2311. gelic_wl_free(port_wl(netdev_port(netdev)));
  2312. return ret;
  2313. }
  2314. int gelic_wl_driver_remove(struct gelic_card *card)
  2315. {
  2316. struct gelic_wl_info *wl;
  2317. struct net_device *netdev;
  2318. pr_debug("%s:start\n", __func__);
  2319. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2320. return 0;
  2321. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2322. return 0;
  2323. netdev = card->netdev[GELIC_PORT_WIRELESS];
  2324. wl = port_wl(netdev_priv(netdev));
  2325. /* if the interface was not up, but associated */
  2326. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2327. gelic_wl_disconnect(netdev);
  2328. complete(&wl->cmd_done_intr);
  2329. /* cancel all work queue */
  2330. cancel_delayed_work(&wl->assoc_work);
  2331. cancel_delayed_work(&wl->event_work);
  2332. flush_workqueue(wl->eurus_cmd_queue);
  2333. flush_workqueue(wl->event_queue);
  2334. unregister_netdev(netdev);
  2335. /* disable wireless interrupt */
  2336. pr_debug("%s: disable intr\n", __func__);
  2337. card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
  2338. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2339. /* free bss list, netdev*/
  2340. gelic_wl_free(wl);
  2341. pr_debug("%s:end\n", __func__);
  2342. return 0;
  2343. }