main.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. /* main.c - (formerly known as dldwd_cs.c, orinoco_cs.c and orinoco.c)
  2. *
  3. * A driver for Hermes or Prism 2 chipset based PCMCIA wireless
  4. * adaptors, with Lucent/Agere, Intersil or Symbol firmware.
  5. *
  6. * Current maintainers (as of 29 September 2003) are:
  7. * Pavel Roskin <proski AT gnu.org>
  8. * and David Gibson <hermes AT gibson.dropbear.id.au>
  9. *
  10. * (C) Copyright David Gibson, IBM Corporation 2001-2003.
  11. * Copyright (C) 2000 David Gibson, Linuxcare Australia.
  12. * With some help from :
  13. * Copyright (C) 2001 Jean Tourrilhes, HP Labs
  14. * Copyright (C) 2001 Benjamin Herrenschmidt
  15. *
  16. * Based on dummy_cs.c 1.27 2000/06/12 21:27:25
  17. *
  18. * Portions based on wvlan_cs.c 1.0.6, Copyright Andreas Neuhaus <andy
  19. * AT fasta.fh-dortmund.de>
  20. * http://www.stud.fh-dortmund.de/~andy/wvlan/
  21. *
  22. * The contents of this file are subject to the Mozilla Public License
  23. * Version 1.1 (the "License"); you may not use this file except in
  24. * compliance with the License. You may obtain a copy of the License
  25. * at http://www.mozilla.org/MPL/
  26. *
  27. * Software distributed under the License is distributed on an "AS IS"
  28. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  29. * the License for the specific language governing rights and
  30. * limitations under the License.
  31. *
  32. * The initial developer of the original code is David A. Hinds
  33. * <dahinds AT users.sourceforge.net>. Portions created by David
  34. * A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights
  35. * Reserved.
  36. *
  37. * Alternatively, the contents of this file may be used under the
  38. * terms of the GNU General Public License version 2 (the "GPL"), in
  39. * which case the provisions of the GPL are applicable instead of the
  40. * above. If you wish to allow the use of your version of this file
  41. * only under the terms of the GPL and not to allow others to use your
  42. * version of this file under the MPL, indicate your decision by
  43. * deleting the provisions above and replace them with the notice and
  44. * other provisions required by the GPL. If you do not delete the
  45. * provisions above, a recipient may use your version of this file
  46. * under either the MPL or the GPL. */
  47. /*
  48. * TODO
  49. * o Handle de-encapsulation within network layer, provide 802.11
  50. * headers (patch from Thomas 'Dent' Mirlacher)
  51. * o Fix possible races in SPY handling.
  52. * o Disconnect wireless extensions from fundamental configuration.
  53. * o (maybe) Software WEP support (patch from Stano Meduna).
  54. * o (maybe) Use multiple Tx buffers - driver handling queue
  55. * rather than firmware.
  56. */
  57. /* Locking and synchronization:
  58. *
  59. * The basic principle is that everything is serialized through a
  60. * single spinlock, priv->lock. The lock is used in user, bh and irq
  61. * context, so when taken outside hardirq context it should always be
  62. * taken with interrupts disabled. The lock protects both the
  63. * hardware and the struct orinoco_private.
  64. *
  65. * Another flag, priv->hw_unavailable indicates that the hardware is
  66. * unavailable for an extended period of time (e.g. suspended, or in
  67. * the middle of a hard reset). This flag is protected by the
  68. * spinlock. All code which touches the hardware should check the
  69. * flag after taking the lock, and if it is set, give up on whatever
  70. * they are doing and drop the lock again. The orinoco_lock()
  71. * function handles this (it unlocks and returns -EBUSY if
  72. * hw_unavailable is non-zero).
  73. */
  74. #define DRIVER_NAME "orinoco"
  75. #include <linux/module.h>
  76. #include <linux/kernel.h>
  77. #include <linux/slab.h>
  78. #include <linux/init.h>
  79. #include <linux/delay.h>
  80. #include <linux/device.h>
  81. #include <linux/netdevice.h>
  82. #include <linux/etherdevice.h>
  83. #include <linux/suspend.h>
  84. #include <linux/if_arp.h>
  85. #include <linux/wireless.h>
  86. #include <linux/ieee80211.h>
  87. #include <net/iw_handler.h>
  88. #include <net/cfg80211.h>
  89. #include "hermes_rid.h"
  90. #include "hermes_dld.h"
  91. #include "hw.h"
  92. #include "scan.h"
  93. #include "mic.h"
  94. #include "fw.h"
  95. #include "wext.h"
  96. #include "cfg.h"
  97. #include "main.h"
  98. #include "orinoco.h"
  99. /********************************************************************/
  100. /* Module information */
  101. /********************************************************************/
  102. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
  103. "David Gibson <hermes@gibson.dropbear.id.au>");
  104. MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
  105. "and similar wireless cards");
  106. MODULE_LICENSE("Dual MPL/GPL");
  107. /* Level of debugging. Used in the macros in orinoco.h */
  108. #ifdef ORINOCO_DEBUG
  109. int orinoco_debug = ORINOCO_DEBUG;
  110. EXPORT_SYMBOL(orinoco_debug);
  111. module_param(orinoco_debug, int, 0644);
  112. MODULE_PARM_DESC(orinoco_debug, "Debug level");
  113. #endif
  114. static bool suppress_linkstatus; /* = 0 */
  115. module_param(suppress_linkstatus, bool, 0644);
  116. MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
  117. static int ignore_disconnect; /* = 0 */
  118. module_param(ignore_disconnect, int, 0644);
  119. MODULE_PARM_DESC(ignore_disconnect,
  120. "Don't report lost link to the network layer");
  121. int force_monitor; /* = 0 */
  122. module_param(force_monitor, int, 0644);
  123. MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
  124. /********************************************************************/
  125. /* Internal constants */
  126. /********************************************************************/
  127. /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
  128. static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
  129. #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
  130. #define ORINOCO_MIN_MTU 256
  131. #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
  132. #define MAX_IRQLOOPS_PER_IRQ 10
  133. #define MAX_IRQLOOPS_PER_JIFFY (20000 / HZ) /* Based on a guestimate of
  134. * how many events the
  135. * device could
  136. * legitimately generate */
  137. #define DUMMY_FID 0xFFFF
  138. /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
  139. HERMES_MAX_MULTICAST : 0)*/
  140. #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
  141. #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
  142. | HERMES_EV_TX | HERMES_EV_TXEXC \
  143. | HERMES_EV_WTERR | HERMES_EV_INFO \
  144. | HERMES_EV_INFDROP)
  145. /********************************************************************/
  146. /* Data types */
  147. /********************************************************************/
  148. /* Beginning of the Tx descriptor, used in TxExc handling */
  149. struct hermes_txexc_data {
  150. struct hermes_tx_descriptor desc;
  151. __le16 frame_ctl;
  152. __le16 duration_id;
  153. u8 addr1[ETH_ALEN];
  154. } __packed;
  155. /* Rx frame header except compatibility 802.3 header */
  156. struct hermes_rx_descriptor {
  157. /* Control */
  158. __le16 status;
  159. __le32 time;
  160. u8 silence;
  161. u8 signal;
  162. u8 rate;
  163. u8 rxflow;
  164. __le32 reserved;
  165. /* 802.11 header */
  166. __le16 frame_ctl;
  167. __le16 duration_id;
  168. u8 addr1[ETH_ALEN];
  169. u8 addr2[ETH_ALEN];
  170. u8 addr3[ETH_ALEN];
  171. __le16 seq_ctl;
  172. u8 addr4[ETH_ALEN];
  173. /* Data length */
  174. __le16 data_len;
  175. } __packed;
  176. struct orinoco_rx_data {
  177. struct hermes_rx_descriptor *desc;
  178. struct sk_buff *skb;
  179. struct list_head list;
  180. };
  181. struct orinoco_scan_data {
  182. void *buf;
  183. size_t len;
  184. int type;
  185. struct list_head list;
  186. };
  187. /********************************************************************/
  188. /* Function prototypes */
  189. /********************************************************************/
  190. static int __orinoco_set_multicast_list(struct net_device *dev);
  191. static int __orinoco_up(struct orinoco_private *priv);
  192. static int __orinoco_down(struct orinoco_private *priv);
  193. static int __orinoco_commit(struct orinoco_private *priv);
  194. /********************************************************************/
  195. /* Internal helper functions */
  196. /********************************************************************/
  197. void set_port_type(struct orinoco_private *priv)
  198. {
  199. switch (priv->iw_mode) {
  200. case NL80211_IFTYPE_STATION:
  201. priv->port_type = 1;
  202. priv->createibss = 0;
  203. break;
  204. case NL80211_IFTYPE_ADHOC:
  205. if (priv->prefer_port3) {
  206. priv->port_type = 3;
  207. priv->createibss = 0;
  208. } else {
  209. priv->port_type = priv->ibss_port;
  210. priv->createibss = 1;
  211. }
  212. break;
  213. case NL80211_IFTYPE_MONITOR:
  214. priv->port_type = 3;
  215. priv->createibss = 0;
  216. break;
  217. default:
  218. printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
  219. priv->ndev->name);
  220. }
  221. }
  222. /********************************************************************/
  223. /* Device methods */
  224. /********************************************************************/
  225. int orinoco_open(struct net_device *dev)
  226. {
  227. struct orinoco_private *priv = ndev_priv(dev);
  228. unsigned long flags;
  229. int err;
  230. if (orinoco_lock(priv, &flags) != 0)
  231. return -EBUSY;
  232. err = __orinoco_up(priv);
  233. if (!err)
  234. priv->open = 1;
  235. orinoco_unlock(priv, &flags);
  236. return err;
  237. }
  238. EXPORT_SYMBOL(orinoco_open);
  239. int orinoco_stop(struct net_device *dev)
  240. {
  241. struct orinoco_private *priv = ndev_priv(dev);
  242. int err = 0;
  243. /* We mustn't use orinoco_lock() here, because we need to be
  244. able to close the interface even if hw_unavailable is set
  245. (e.g. as we're released after a PC Card removal) */
  246. orinoco_lock_irq(priv);
  247. priv->open = 0;
  248. err = __orinoco_down(priv);
  249. orinoco_unlock_irq(priv);
  250. return err;
  251. }
  252. EXPORT_SYMBOL(orinoco_stop);
  253. struct net_device_stats *orinoco_get_stats(struct net_device *dev)
  254. {
  255. struct orinoco_private *priv = ndev_priv(dev);
  256. return &priv->stats;
  257. }
  258. EXPORT_SYMBOL(orinoco_get_stats);
  259. void orinoco_set_multicast_list(struct net_device *dev)
  260. {
  261. struct orinoco_private *priv = ndev_priv(dev);
  262. unsigned long flags;
  263. if (orinoco_lock(priv, &flags) != 0) {
  264. printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
  265. "called when hw_unavailable\n", dev->name);
  266. return;
  267. }
  268. __orinoco_set_multicast_list(dev);
  269. orinoco_unlock(priv, &flags);
  270. }
  271. EXPORT_SYMBOL(orinoco_set_multicast_list);
  272. int orinoco_change_mtu(struct net_device *dev, int new_mtu)
  273. {
  274. struct orinoco_private *priv = ndev_priv(dev);
  275. /* MTU + encapsulation + header length */
  276. if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
  277. (priv->nicbuf_size - ETH_HLEN))
  278. return -EINVAL;
  279. dev->mtu = new_mtu;
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(orinoco_change_mtu);
  283. /********************************************************************/
  284. /* Tx path */
  285. /********************************************************************/
  286. /* Add encapsulation and MIC to the existing SKB.
  287. * The main xmit routine will then send the whole lot to the card.
  288. * Need 8 bytes headroom
  289. * Need 8 bytes tailroom
  290. *
  291. * With encapsulated ethernet II frame
  292. * --------
  293. * 803.3 header (14 bytes)
  294. * dst[6]
  295. * -------- src[6]
  296. * 803.3 header (14 bytes) len[2]
  297. * dst[6] 803.2 header (8 bytes)
  298. * src[6] encaps[6]
  299. * len[2] <- leave alone -> len[2]
  300. * -------- -------- <-- 0
  301. * Payload Payload
  302. * ... ...
  303. *
  304. * -------- --------
  305. * MIC (8 bytes)
  306. * --------
  307. *
  308. * returns 0 on success, -ENOMEM on error.
  309. */
  310. int orinoco_process_xmit_skb(struct sk_buff *skb,
  311. struct net_device *dev,
  312. struct orinoco_private *priv,
  313. int *tx_control,
  314. u8 *mic_buf)
  315. {
  316. struct orinoco_tkip_key *key;
  317. struct ethhdr *eh;
  318. int do_mic;
  319. key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
  320. do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
  321. (key != NULL));
  322. if (do_mic)
  323. *tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
  324. HERMES_TXCTRL_MIC;
  325. eh = (struct ethhdr *)skb->data;
  326. /* Encapsulate Ethernet-II frames */
  327. if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
  328. struct header_struct {
  329. struct ethhdr eth; /* 802.3 header */
  330. u8 encap[6]; /* 802.2 header */
  331. } __packed hdr;
  332. int len = skb->len + sizeof(encaps_hdr) - (2 * ETH_ALEN);
  333. if (skb_headroom(skb) < ENCAPS_OVERHEAD) {
  334. if (net_ratelimit())
  335. printk(KERN_ERR
  336. "%s: Not enough headroom for 802.2 headers %d\n",
  337. dev->name, skb_headroom(skb));
  338. return -ENOMEM;
  339. }
  340. /* Fill in new header */
  341. memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
  342. hdr.eth.h_proto = htons(len);
  343. memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
  344. /* Make room for the new header, and copy it in */
  345. eh = (struct ethhdr *) skb_push(skb, ENCAPS_OVERHEAD);
  346. memcpy(eh, &hdr, sizeof(hdr));
  347. }
  348. /* Calculate Michael MIC */
  349. if (do_mic) {
  350. size_t len = skb->len - ETH_HLEN;
  351. u8 *mic = &mic_buf[0];
  352. /* Have to write to an even address, so copy the spare
  353. * byte across */
  354. if (skb->len % 2) {
  355. *mic = skb->data[skb->len - 1];
  356. mic++;
  357. }
  358. orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
  359. eh->h_dest, eh->h_source, 0 /* priority */,
  360. skb->data + ETH_HLEN,
  361. len, mic);
  362. }
  363. return 0;
  364. }
  365. EXPORT_SYMBOL(orinoco_process_xmit_skb);
  366. static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
  367. {
  368. struct orinoco_private *priv = ndev_priv(dev);
  369. struct net_device_stats *stats = &priv->stats;
  370. struct hermes *hw = &priv->hw;
  371. int err = 0;
  372. u16 txfid = priv->txfid;
  373. int tx_control;
  374. unsigned long flags;
  375. u8 mic_buf[MICHAEL_MIC_LEN + 1];
  376. if (!netif_running(dev)) {
  377. printk(KERN_ERR "%s: Tx on stopped device!\n",
  378. dev->name);
  379. return NETDEV_TX_BUSY;
  380. }
  381. if (netif_queue_stopped(dev)) {
  382. printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
  383. dev->name);
  384. return NETDEV_TX_BUSY;
  385. }
  386. if (orinoco_lock(priv, &flags) != 0) {
  387. printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
  388. dev->name);
  389. return NETDEV_TX_BUSY;
  390. }
  391. if (!netif_carrier_ok(dev) ||
  392. (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
  393. /* Oops, the firmware hasn't established a connection,
  394. silently drop the packet (this seems to be the
  395. safest approach). */
  396. goto drop;
  397. }
  398. /* Check packet length */
  399. if (skb->len < ETH_HLEN)
  400. goto drop;
  401. tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
  402. err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
  403. &mic_buf[0]);
  404. if (err)
  405. goto drop;
  406. if (priv->has_alt_txcntl) {
  407. /* WPA enabled firmwares have tx_cntl at the end of
  408. * the 802.11 header. So write zeroed descriptor and
  409. * 802.11 header at the same time
  410. */
  411. char desc[HERMES_802_3_OFFSET];
  412. __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
  413. memset(&desc, 0, sizeof(desc));
  414. *txcntl = cpu_to_le16(tx_control);
  415. err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
  416. txfid, 0);
  417. if (err) {
  418. if (net_ratelimit())
  419. printk(KERN_ERR "%s: Error %d writing Tx "
  420. "descriptor to BAP\n", dev->name, err);
  421. goto busy;
  422. }
  423. } else {
  424. struct hermes_tx_descriptor desc;
  425. memset(&desc, 0, sizeof(desc));
  426. desc.tx_control = cpu_to_le16(tx_control);
  427. err = hw->ops->bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
  428. txfid, 0);
  429. if (err) {
  430. if (net_ratelimit())
  431. printk(KERN_ERR "%s: Error %d writing Tx "
  432. "descriptor to BAP\n", dev->name, err);
  433. goto busy;
  434. }
  435. /* Clear the 802.11 header and data length fields - some
  436. * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
  437. * if this isn't done. */
  438. hermes_clear_words(hw, HERMES_DATA0,
  439. HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
  440. }
  441. err = hw->ops->bap_pwrite(hw, USER_BAP, skb->data, skb->len,
  442. txfid, HERMES_802_3_OFFSET);
  443. if (err) {
  444. printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
  445. dev->name, err);
  446. goto busy;
  447. }
  448. if (tx_control & HERMES_TXCTRL_MIC) {
  449. size_t offset = HERMES_802_3_OFFSET + skb->len;
  450. size_t len = MICHAEL_MIC_LEN;
  451. if (offset % 2) {
  452. offset--;
  453. len++;
  454. }
  455. err = hw->ops->bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
  456. txfid, offset);
  457. if (err) {
  458. printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
  459. dev->name, err);
  460. goto busy;
  461. }
  462. }
  463. /* Finally, we actually initiate the send */
  464. netif_stop_queue(dev);
  465. err = hw->ops->cmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
  466. txfid, NULL);
  467. if (err) {
  468. netif_start_queue(dev);
  469. if (net_ratelimit())
  470. printk(KERN_ERR "%s: Error %d transmitting packet\n",
  471. dev->name, err);
  472. goto busy;
  473. }
  474. stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
  475. goto ok;
  476. drop:
  477. stats->tx_errors++;
  478. stats->tx_dropped++;
  479. ok:
  480. orinoco_unlock(priv, &flags);
  481. dev_kfree_skb(skb);
  482. return NETDEV_TX_OK;
  483. busy:
  484. if (err == -EIO)
  485. schedule_work(&priv->reset_work);
  486. orinoco_unlock(priv, &flags);
  487. return NETDEV_TX_BUSY;
  488. }
  489. static void __orinoco_ev_alloc(struct net_device *dev, struct hermes *hw)
  490. {
  491. struct orinoco_private *priv = ndev_priv(dev);
  492. u16 fid = hermes_read_regn(hw, ALLOCFID);
  493. if (fid != priv->txfid) {
  494. if (fid != DUMMY_FID)
  495. printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
  496. dev->name, fid);
  497. return;
  498. }
  499. hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
  500. }
  501. static void __orinoco_ev_tx(struct net_device *dev, struct hermes *hw)
  502. {
  503. struct orinoco_private *priv = ndev_priv(dev);
  504. struct net_device_stats *stats = &priv->stats;
  505. stats->tx_packets++;
  506. netif_wake_queue(dev);
  507. hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
  508. }
  509. static void __orinoco_ev_txexc(struct net_device *dev, struct hermes *hw)
  510. {
  511. struct orinoco_private *priv = ndev_priv(dev);
  512. struct net_device_stats *stats = &priv->stats;
  513. u16 fid = hermes_read_regn(hw, TXCOMPLFID);
  514. u16 status;
  515. struct hermes_txexc_data hdr;
  516. int err = 0;
  517. if (fid == DUMMY_FID)
  518. return; /* Nothing's really happened */
  519. /* Read part of the frame header - we need status and addr1 */
  520. err = hw->ops->bap_pread(hw, IRQ_BAP, &hdr,
  521. sizeof(struct hermes_txexc_data),
  522. fid, 0);
  523. hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
  524. stats->tx_errors++;
  525. if (err) {
  526. printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
  527. "(FID=%04X error %d)\n",
  528. dev->name, fid, err);
  529. return;
  530. }
  531. DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
  532. err, fid);
  533. /* We produce a TXDROP event only for retry or lifetime
  534. * exceeded, because that's the only status that really mean
  535. * that this particular node went away.
  536. * Other errors means that *we* screwed up. - Jean II */
  537. status = le16_to_cpu(hdr.desc.status);
  538. if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
  539. union iwreq_data wrqu;
  540. /* Copy 802.11 dest address.
  541. * We use the 802.11 header because the frame may
  542. * not be 802.3 or may be mangled...
  543. * In Ad-Hoc mode, it will be the node address.
  544. * In managed mode, it will be most likely the AP addr
  545. * User space will figure out how to convert it to
  546. * whatever it needs (IP address or else).
  547. * - Jean II */
  548. memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
  549. wrqu.addr.sa_family = ARPHRD_ETHER;
  550. /* Send event to user space */
  551. wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
  552. }
  553. netif_wake_queue(dev);
  554. }
  555. void orinoco_tx_timeout(struct net_device *dev)
  556. {
  557. struct orinoco_private *priv = ndev_priv(dev);
  558. struct net_device_stats *stats = &priv->stats;
  559. struct hermes *hw = &priv->hw;
  560. printk(KERN_WARNING "%s: Tx timeout! "
  561. "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
  562. dev->name, hermes_read_regn(hw, ALLOCFID),
  563. hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
  564. stats->tx_errors++;
  565. schedule_work(&priv->reset_work);
  566. }
  567. EXPORT_SYMBOL(orinoco_tx_timeout);
  568. /********************************************************************/
  569. /* Rx path (data frames) */
  570. /********************************************************************/
  571. /* Does the frame have a SNAP header indicating it should be
  572. * de-encapsulated to Ethernet-II? */
  573. static inline int is_ethersnap(void *_hdr)
  574. {
  575. u8 *hdr = _hdr;
  576. /* We de-encapsulate all packets which, a) have SNAP headers
  577. * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
  578. * and where b) the OUI of the SNAP header is 00:00:00 or
  579. * 00:00:f8 - we need both because different APs appear to use
  580. * different OUIs for some reason */
  581. return (memcmp(hdr, &encaps_hdr, 5) == 0)
  582. && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
  583. }
  584. static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
  585. int level, int noise)
  586. {
  587. struct iw_quality wstats;
  588. wstats.level = level - 0x95;
  589. wstats.noise = noise - 0x95;
  590. wstats.qual = (level > noise) ? (level - noise) : 0;
  591. wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  592. /* Update spy records */
  593. wireless_spy_update(dev, mac, &wstats);
  594. }
  595. static void orinoco_stat_gather(struct net_device *dev,
  596. struct sk_buff *skb,
  597. struct hermes_rx_descriptor *desc)
  598. {
  599. struct orinoco_private *priv = ndev_priv(dev);
  600. /* Using spy support with lots of Rx packets, like in an
  601. * infrastructure (AP), will really slow down everything, because
  602. * the MAC address must be compared to each entry of the spy list.
  603. * If the user really asks for it (set some address in the
  604. * spy list), we do it, but he will pay the price.
  605. * Note that to get here, you need both WIRELESS_SPY
  606. * compiled in AND some addresses in the list !!!
  607. */
  608. /* Note : gcc will optimise the whole section away if
  609. * WIRELESS_SPY is not defined... - Jean II */
  610. if (SPY_NUMBER(priv)) {
  611. orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
  612. desc->signal, desc->silence);
  613. }
  614. }
  615. /*
  616. * orinoco_rx_monitor - handle received monitor frames.
  617. *
  618. * Arguments:
  619. * dev network device
  620. * rxfid received FID
  621. * desc rx descriptor of the frame
  622. *
  623. * Call context: interrupt
  624. */
  625. static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
  626. struct hermes_rx_descriptor *desc)
  627. {
  628. u32 hdrlen = 30; /* return full header by default */
  629. u32 datalen = 0;
  630. u16 fc;
  631. int err;
  632. int len;
  633. struct sk_buff *skb;
  634. struct orinoco_private *priv = ndev_priv(dev);
  635. struct net_device_stats *stats = &priv->stats;
  636. struct hermes *hw = &priv->hw;
  637. len = le16_to_cpu(desc->data_len);
  638. /* Determine the size of the header and the data */
  639. fc = le16_to_cpu(desc->frame_ctl);
  640. switch (fc & IEEE80211_FCTL_FTYPE) {
  641. case IEEE80211_FTYPE_DATA:
  642. if ((fc & IEEE80211_FCTL_TODS)
  643. && (fc & IEEE80211_FCTL_FROMDS))
  644. hdrlen = 30;
  645. else
  646. hdrlen = 24;
  647. datalen = len;
  648. break;
  649. case IEEE80211_FTYPE_MGMT:
  650. hdrlen = 24;
  651. datalen = len;
  652. break;
  653. case IEEE80211_FTYPE_CTL:
  654. switch (fc & IEEE80211_FCTL_STYPE) {
  655. case IEEE80211_STYPE_PSPOLL:
  656. case IEEE80211_STYPE_RTS:
  657. case IEEE80211_STYPE_CFEND:
  658. case IEEE80211_STYPE_CFENDACK:
  659. hdrlen = 16;
  660. break;
  661. case IEEE80211_STYPE_CTS:
  662. case IEEE80211_STYPE_ACK:
  663. hdrlen = 10;
  664. break;
  665. }
  666. break;
  667. default:
  668. /* Unknown frame type */
  669. break;
  670. }
  671. /* sanity check the length */
  672. if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
  673. printk(KERN_DEBUG "%s: oversized monitor frame, "
  674. "data length = %d\n", dev->name, datalen);
  675. stats->rx_length_errors++;
  676. goto update_stats;
  677. }
  678. skb = dev_alloc_skb(hdrlen + datalen);
  679. if (!skb) {
  680. printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
  681. dev->name);
  682. goto update_stats;
  683. }
  684. /* Copy the 802.11 header to the skb */
  685. memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
  686. skb_reset_mac_header(skb);
  687. /* If any, copy the data from the card to the skb */
  688. if (datalen > 0) {
  689. err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
  690. ALIGN(datalen, 2), rxfid,
  691. HERMES_802_2_OFFSET);
  692. if (err) {
  693. printk(KERN_ERR "%s: error %d reading monitor frame\n",
  694. dev->name, err);
  695. goto drop;
  696. }
  697. }
  698. skb->dev = dev;
  699. skb->ip_summed = CHECKSUM_NONE;
  700. skb->pkt_type = PACKET_OTHERHOST;
  701. skb->protocol = cpu_to_be16(ETH_P_802_2);
  702. stats->rx_packets++;
  703. stats->rx_bytes += skb->len;
  704. netif_rx(skb);
  705. return;
  706. drop:
  707. dev_kfree_skb_irq(skb);
  708. update_stats:
  709. stats->rx_errors++;
  710. stats->rx_dropped++;
  711. }
  712. void __orinoco_ev_rx(struct net_device *dev, struct hermes *hw)
  713. {
  714. struct orinoco_private *priv = ndev_priv(dev);
  715. struct net_device_stats *stats = &priv->stats;
  716. struct iw_statistics *wstats = &priv->wstats;
  717. struct sk_buff *skb = NULL;
  718. u16 rxfid, status;
  719. int length;
  720. struct hermes_rx_descriptor *desc;
  721. struct orinoco_rx_data *rx_data;
  722. int err;
  723. desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
  724. if (!desc)
  725. goto update_stats;
  726. rxfid = hermes_read_regn(hw, RXFID);
  727. err = hw->ops->bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
  728. rxfid, 0);
  729. if (err) {
  730. printk(KERN_ERR "%s: error %d reading Rx descriptor. "
  731. "Frame dropped.\n", dev->name, err);
  732. goto update_stats;
  733. }
  734. status = le16_to_cpu(desc->status);
  735. if (status & HERMES_RXSTAT_BADCRC) {
  736. DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
  737. dev->name);
  738. stats->rx_crc_errors++;
  739. goto update_stats;
  740. }
  741. /* Handle frames in monitor mode */
  742. if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
  743. orinoco_rx_monitor(dev, rxfid, desc);
  744. goto out;
  745. }
  746. if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
  747. DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
  748. dev->name);
  749. wstats->discard.code++;
  750. goto update_stats;
  751. }
  752. length = le16_to_cpu(desc->data_len);
  753. /* Sanity checks */
  754. if (length < 3) { /* No for even an 802.2 LLC header */
  755. /* At least on Symbol firmware with PCF we get quite a
  756. lot of these legitimately - Poll frames with no
  757. data. */
  758. goto out;
  759. }
  760. if (length > IEEE80211_MAX_DATA_LEN) {
  761. printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
  762. dev->name, length);
  763. stats->rx_length_errors++;
  764. goto update_stats;
  765. }
  766. /* Payload size does not include Michael MIC. Increase payload
  767. * size to read it together with the data. */
  768. if (status & HERMES_RXSTAT_MIC)
  769. length += MICHAEL_MIC_LEN;
  770. /* We need space for the packet data itself, plus an ethernet
  771. header, plus 2 bytes so we can align the IP header on a
  772. 32bit boundary, plus 1 byte so we can read in odd length
  773. packets from the card, which has an IO granularity of 16
  774. bits */
  775. skb = dev_alloc_skb(length + ETH_HLEN + 2 + 1);
  776. if (!skb) {
  777. printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
  778. dev->name);
  779. goto update_stats;
  780. }
  781. /* We'll prepend the header, so reserve space for it. The worst
  782. case is no decapsulation, when 802.3 header is prepended and
  783. nothing is removed. 2 is for aligning the IP header. */
  784. skb_reserve(skb, ETH_HLEN + 2);
  785. err = hw->ops->bap_pread(hw, IRQ_BAP, skb_put(skb, length),
  786. ALIGN(length, 2), rxfid,
  787. HERMES_802_2_OFFSET);
  788. if (err) {
  789. printk(KERN_ERR "%s: error %d reading frame. "
  790. "Frame dropped.\n", dev->name, err);
  791. goto drop;
  792. }
  793. /* Add desc and skb to rx queue */
  794. rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
  795. if (!rx_data)
  796. goto drop;
  797. rx_data->desc = desc;
  798. rx_data->skb = skb;
  799. list_add_tail(&rx_data->list, &priv->rx_list);
  800. tasklet_schedule(&priv->rx_tasklet);
  801. return;
  802. drop:
  803. dev_kfree_skb_irq(skb);
  804. update_stats:
  805. stats->rx_errors++;
  806. stats->rx_dropped++;
  807. out:
  808. kfree(desc);
  809. }
  810. EXPORT_SYMBOL(__orinoco_ev_rx);
  811. static void orinoco_rx(struct net_device *dev,
  812. struct hermes_rx_descriptor *desc,
  813. struct sk_buff *skb)
  814. {
  815. struct orinoco_private *priv = ndev_priv(dev);
  816. struct net_device_stats *stats = &priv->stats;
  817. u16 status, fc;
  818. int length;
  819. struct ethhdr *hdr;
  820. status = le16_to_cpu(desc->status);
  821. length = le16_to_cpu(desc->data_len);
  822. fc = le16_to_cpu(desc->frame_ctl);
  823. /* Calculate and check MIC */
  824. if (status & HERMES_RXSTAT_MIC) {
  825. struct orinoco_tkip_key *key;
  826. int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
  827. HERMES_MIC_KEY_ID_SHIFT);
  828. u8 mic[MICHAEL_MIC_LEN];
  829. u8 *rxmic;
  830. u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
  831. desc->addr3 : desc->addr2;
  832. /* Extract Michael MIC from payload */
  833. rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
  834. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  835. length -= MICHAEL_MIC_LEN;
  836. key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
  837. if (!key) {
  838. printk(KERN_WARNING "%s: Received encrypted frame from "
  839. "%pM using key %i, but key is not installed\n",
  840. dev->name, src, key_id);
  841. goto drop;
  842. }
  843. orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
  844. 0, /* priority or QoS? */
  845. skb->data, skb->len, &mic[0]);
  846. if (memcmp(mic, rxmic,
  847. MICHAEL_MIC_LEN)) {
  848. union iwreq_data wrqu;
  849. struct iw_michaelmicfailure wxmic;
  850. printk(KERN_WARNING "%s: "
  851. "Invalid Michael MIC in data frame from %pM, "
  852. "using key %i\n",
  853. dev->name, src, key_id);
  854. /* TODO: update stats */
  855. /* Notify userspace */
  856. memset(&wxmic, 0, sizeof(wxmic));
  857. wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
  858. wxmic.flags |= (desc->addr1[0] & 1) ?
  859. IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
  860. wxmic.src_addr.sa_family = ARPHRD_ETHER;
  861. memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
  862. (void) orinoco_hw_get_tkip_iv(priv, key_id,
  863. &wxmic.tsc[0]);
  864. memset(&wrqu, 0, sizeof(wrqu));
  865. wrqu.data.length = sizeof(wxmic);
  866. wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
  867. (char *) &wxmic);
  868. goto drop;
  869. }
  870. }
  871. /* Handle decapsulation
  872. * In most cases, the firmware tell us about SNAP frames.
  873. * For some reason, the SNAP frames sent by LinkSys APs
  874. * are not properly recognised by most firmwares.
  875. * So, check ourselves */
  876. if (length >= ENCAPS_OVERHEAD &&
  877. (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
  878. ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
  879. is_ethersnap(skb->data))) {
  880. /* These indicate a SNAP within 802.2 LLC within
  881. 802.11 frame which we'll need to de-encapsulate to
  882. the original EthernetII frame. */
  883. hdr = (struct ethhdr *)skb_push(skb,
  884. ETH_HLEN - ENCAPS_OVERHEAD);
  885. } else {
  886. /* 802.3 frame - prepend 802.3 header as is */
  887. hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
  888. hdr->h_proto = htons(length);
  889. }
  890. memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
  891. if (fc & IEEE80211_FCTL_FROMDS)
  892. memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
  893. else
  894. memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
  895. skb->protocol = eth_type_trans(skb, dev);
  896. skb->ip_summed = CHECKSUM_NONE;
  897. if (fc & IEEE80211_FCTL_TODS)
  898. skb->pkt_type = PACKET_OTHERHOST;
  899. /* Process the wireless stats if needed */
  900. orinoco_stat_gather(dev, skb, desc);
  901. /* Pass the packet to the networking stack */
  902. netif_rx(skb);
  903. stats->rx_packets++;
  904. stats->rx_bytes += length;
  905. return;
  906. drop:
  907. dev_kfree_skb(skb);
  908. stats->rx_errors++;
  909. stats->rx_dropped++;
  910. }
  911. static void orinoco_rx_isr_tasklet(unsigned long data)
  912. {
  913. struct orinoco_private *priv = (struct orinoco_private *) data;
  914. struct net_device *dev = priv->ndev;
  915. struct orinoco_rx_data *rx_data, *temp;
  916. struct hermes_rx_descriptor *desc;
  917. struct sk_buff *skb;
  918. unsigned long flags;
  919. /* orinoco_rx requires the driver lock, and we also need to
  920. * protect priv->rx_list, so just hold the lock over the
  921. * lot.
  922. *
  923. * If orinoco_lock fails, we've unplugged the card. In this
  924. * case just abort. */
  925. if (orinoco_lock(priv, &flags) != 0)
  926. return;
  927. /* extract desc and skb from queue */
  928. list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
  929. desc = rx_data->desc;
  930. skb = rx_data->skb;
  931. list_del(&rx_data->list);
  932. kfree(rx_data);
  933. orinoco_rx(dev, desc, skb);
  934. kfree(desc);
  935. }
  936. orinoco_unlock(priv, &flags);
  937. }
  938. /********************************************************************/
  939. /* Rx path (info frames) */
  940. /********************************************************************/
  941. static void print_linkstatus(struct net_device *dev, u16 status)
  942. {
  943. char *s;
  944. if (suppress_linkstatus)
  945. return;
  946. switch (status) {
  947. case HERMES_LINKSTATUS_NOT_CONNECTED:
  948. s = "Not Connected";
  949. break;
  950. case HERMES_LINKSTATUS_CONNECTED:
  951. s = "Connected";
  952. break;
  953. case HERMES_LINKSTATUS_DISCONNECTED:
  954. s = "Disconnected";
  955. break;
  956. case HERMES_LINKSTATUS_AP_CHANGE:
  957. s = "AP Changed";
  958. break;
  959. case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
  960. s = "AP Out of Range";
  961. break;
  962. case HERMES_LINKSTATUS_AP_IN_RANGE:
  963. s = "AP In Range";
  964. break;
  965. case HERMES_LINKSTATUS_ASSOC_FAILED:
  966. s = "Association Failed";
  967. break;
  968. default:
  969. s = "UNKNOWN";
  970. }
  971. printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
  972. dev->name, s, status);
  973. }
  974. /* Search scan results for requested BSSID, join it if found */
  975. static void orinoco_join_ap(struct work_struct *work)
  976. {
  977. struct orinoco_private *priv =
  978. container_of(work, struct orinoco_private, join_work);
  979. struct net_device *dev = priv->ndev;
  980. struct hermes *hw = &priv->hw;
  981. int err;
  982. unsigned long flags;
  983. struct join_req {
  984. u8 bssid[ETH_ALEN];
  985. __le16 channel;
  986. } __packed req;
  987. const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
  988. struct prism2_scan_apinfo *atom = NULL;
  989. int offset = 4;
  990. int found = 0;
  991. u8 *buf;
  992. u16 len;
  993. /* Allocate buffer for scan results */
  994. buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
  995. if (!buf)
  996. return;
  997. if (orinoco_lock(priv, &flags) != 0)
  998. goto fail_lock;
  999. /* Sanity checks in case user changed something in the meantime */
  1000. if (!priv->bssid_fixed)
  1001. goto out;
  1002. if (strlen(priv->desired_essid) == 0)
  1003. goto out;
  1004. /* Read scan results from the firmware */
  1005. err = hw->ops->read_ltv(hw, USER_BAP,
  1006. HERMES_RID_SCANRESULTSTABLE,
  1007. MAX_SCAN_LEN, &len, buf);
  1008. if (err) {
  1009. printk(KERN_ERR "%s: Cannot read scan results\n",
  1010. dev->name);
  1011. goto out;
  1012. }
  1013. len = HERMES_RECLEN_TO_BYTES(len);
  1014. /* Go through the scan results looking for the channel of the AP
  1015. * we were requested to join */
  1016. for (; offset + atom_len <= len; offset += atom_len) {
  1017. atom = (struct prism2_scan_apinfo *) (buf + offset);
  1018. if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
  1019. found = 1;
  1020. break;
  1021. }
  1022. }
  1023. if (!found) {
  1024. DEBUG(1, "%s: Requested AP not found in scan results\n",
  1025. dev->name);
  1026. goto out;
  1027. }
  1028. memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
  1029. req.channel = atom->channel; /* both are little-endian */
  1030. err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
  1031. &req);
  1032. if (err)
  1033. printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
  1034. out:
  1035. orinoco_unlock(priv, &flags);
  1036. fail_lock:
  1037. kfree(buf);
  1038. }
  1039. /* Send new BSSID to userspace */
  1040. static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
  1041. {
  1042. struct net_device *dev = priv->ndev;
  1043. struct hermes *hw = &priv->hw;
  1044. union iwreq_data wrqu;
  1045. int err;
  1046. err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
  1047. ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
  1048. if (err != 0)
  1049. return;
  1050. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1051. /* Send event to user space */
  1052. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  1053. }
  1054. static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
  1055. {
  1056. struct net_device *dev = priv->ndev;
  1057. struct hermes *hw = &priv->hw;
  1058. union iwreq_data wrqu;
  1059. int err;
  1060. u8 buf[88];
  1061. u8 *ie;
  1062. if (!priv->has_wpa)
  1063. return;
  1064. err = hw->ops->read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
  1065. sizeof(buf), NULL, &buf);
  1066. if (err != 0)
  1067. return;
  1068. ie = orinoco_get_wpa_ie(buf, sizeof(buf));
  1069. if (ie) {
  1070. int rem = sizeof(buf) - (ie - &buf[0]);
  1071. wrqu.data.length = ie[1] + 2;
  1072. if (wrqu.data.length > rem)
  1073. wrqu.data.length = rem;
  1074. if (wrqu.data.length)
  1075. /* Send event to user space */
  1076. wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
  1077. }
  1078. }
  1079. static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
  1080. {
  1081. struct net_device *dev = priv->ndev;
  1082. struct hermes *hw = &priv->hw;
  1083. union iwreq_data wrqu;
  1084. int err;
  1085. u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
  1086. u8 *ie;
  1087. if (!priv->has_wpa)
  1088. return;
  1089. err = hw->ops->read_ltv(hw, USER_BAP,
  1090. HERMES_RID_CURRENT_ASSOC_RESP_INFO,
  1091. sizeof(buf), NULL, &buf);
  1092. if (err != 0)
  1093. return;
  1094. ie = orinoco_get_wpa_ie(buf, sizeof(buf));
  1095. if (ie) {
  1096. int rem = sizeof(buf) - (ie - &buf[0]);
  1097. wrqu.data.length = ie[1] + 2;
  1098. if (wrqu.data.length > rem)
  1099. wrqu.data.length = rem;
  1100. if (wrqu.data.length)
  1101. /* Send event to user space */
  1102. wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
  1103. }
  1104. }
  1105. static void orinoco_send_wevents(struct work_struct *work)
  1106. {
  1107. struct orinoco_private *priv =
  1108. container_of(work, struct orinoco_private, wevent_work);
  1109. unsigned long flags;
  1110. if (orinoco_lock(priv, &flags) != 0)
  1111. return;
  1112. orinoco_send_assocreqie_wevent(priv);
  1113. orinoco_send_assocrespie_wevent(priv);
  1114. orinoco_send_bssid_wevent(priv);
  1115. orinoco_unlock(priv, &flags);
  1116. }
  1117. static void qbuf_scan(struct orinoco_private *priv, void *buf,
  1118. int len, int type)
  1119. {
  1120. struct orinoco_scan_data *sd;
  1121. unsigned long flags;
  1122. sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
  1123. if (!sd)
  1124. return;
  1125. sd->buf = buf;
  1126. sd->len = len;
  1127. sd->type = type;
  1128. spin_lock_irqsave(&priv->scan_lock, flags);
  1129. list_add_tail(&sd->list, &priv->scan_list);
  1130. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1131. schedule_work(&priv->process_scan);
  1132. }
  1133. static void qabort_scan(struct orinoco_private *priv)
  1134. {
  1135. struct orinoco_scan_data *sd;
  1136. unsigned long flags;
  1137. sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
  1138. if (!sd)
  1139. return;
  1140. sd->len = -1; /* Abort */
  1141. spin_lock_irqsave(&priv->scan_lock, flags);
  1142. list_add_tail(&sd->list, &priv->scan_list);
  1143. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1144. schedule_work(&priv->process_scan);
  1145. }
  1146. static void orinoco_process_scan_results(struct work_struct *work)
  1147. {
  1148. struct orinoco_private *priv =
  1149. container_of(work, struct orinoco_private, process_scan);
  1150. struct orinoco_scan_data *sd, *temp;
  1151. unsigned long flags;
  1152. void *buf;
  1153. int len;
  1154. int type;
  1155. spin_lock_irqsave(&priv->scan_lock, flags);
  1156. list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
  1157. buf = sd->buf;
  1158. len = sd->len;
  1159. type = sd->type;
  1160. list_del(&sd->list);
  1161. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1162. kfree(sd);
  1163. if (len > 0) {
  1164. if (type == HERMES_INQ_CHANNELINFO)
  1165. orinoco_add_extscan_result(priv, buf, len);
  1166. else
  1167. orinoco_add_hostscan_results(priv, buf, len);
  1168. kfree(buf);
  1169. } else {
  1170. /* Either abort or complete the scan */
  1171. orinoco_scan_done(priv, (len < 0));
  1172. }
  1173. spin_lock_irqsave(&priv->scan_lock, flags);
  1174. }
  1175. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1176. }
  1177. void __orinoco_ev_info(struct net_device *dev, struct hermes *hw)
  1178. {
  1179. struct orinoco_private *priv = ndev_priv(dev);
  1180. u16 infofid;
  1181. struct {
  1182. __le16 len;
  1183. __le16 type;
  1184. } __packed info;
  1185. int len, type;
  1186. int err;
  1187. /* This is an answer to an INQUIRE command that we did earlier,
  1188. * or an information "event" generated by the card
  1189. * The controller return to us a pseudo frame containing
  1190. * the information in question - Jean II */
  1191. infofid = hermes_read_regn(hw, INFOFID);
  1192. /* Read the info frame header - don't try too hard */
  1193. err = hw->ops->bap_pread(hw, IRQ_BAP, &info, sizeof(info),
  1194. infofid, 0);
  1195. if (err) {
  1196. printk(KERN_ERR "%s: error %d reading info frame. "
  1197. "Frame dropped.\n", dev->name, err);
  1198. return;
  1199. }
  1200. len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
  1201. type = le16_to_cpu(info.type);
  1202. switch (type) {
  1203. case HERMES_INQ_TALLIES: {
  1204. struct hermes_tallies_frame tallies;
  1205. struct iw_statistics *wstats = &priv->wstats;
  1206. if (len > sizeof(tallies)) {
  1207. printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
  1208. dev->name, len);
  1209. len = sizeof(tallies);
  1210. }
  1211. err = hw->ops->bap_pread(hw, IRQ_BAP, &tallies, len,
  1212. infofid, sizeof(info));
  1213. if (err)
  1214. break;
  1215. /* Increment our various counters */
  1216. /* wstats->discard.nwid - no wrong BSSID stuff */
  1217. wstats->discard.code +=
  1218. le16_to_cpu(tallies.RxWEPUndecryptable);
  1219. if (len == sizeof(tallies))
  1220. wstats->discard.code +=
  1221. le16_to_cpu(tallies.RxDiscards_WEPICVError) +
  1222. le16_to_cpu(tallies.RxDiscards_WEPExcluded);
  1223. wstats->discard.misc +=
  1224. le16_to_cpu(tallies.TxDiscardsWrongSA);
  1225. wstats->discard.fragment +=
  1226. le16_to_cpu(tallies.RxMsgInBadMsgFragments);
  1227. wstats->discard.retries +=
  1228. le16_to_cpu(tallies.TxRetryLimitExceeded);
  1229. /* wstats->miss.beacon - no match */
  1230. }
  1231. break;
  1232. case HERMES_INQ_LINKSTATUS: {
  1233. struct hermes_linkstatus linkstatus;
  1234. u16 newstatus;
  1235. int connected;
  1236. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  1237. break;
  1238. if (len != sizeof(linkstatus)) {
  1239. printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
  1240. dev->name, len);
  1241. break;
  1242. }
  1243. err = hw->ops->bap_pread(hw, IRQ_BAP, &linkstatus, len,
  1244. infofid, sizeof(info));
  1245. if (err)
  1246. break;
  1247. newstatus = le16_to_cpu(linkstatus.linkstatus);
  1248. /* Symbol firmware uses "out of range" to signal that
  1249. * the hostscan frame can be requested. */
  1250. if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
  1251. priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
  1252. priv->has_hostscan && priv->scan_request) {
  1253. hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
  1254. break;
  1255. }
  1256. connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
  1257. || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
  1258. || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
  1259. if (connected)
  1260. netif_carrier_on(dev);
  1261. else if (!ignore_disconnect)
  1262. netif_carrier_off(dev);
  1263. if (newstatus != priv->last_linkstatus) {
  1264. priv->last_linkstatus = newstatus;
  1265. print_linkstatus(dev, newstatus);
  1266. /* The info frame contains only one word which is the
  1267. * status (see hermes.h). The status is pretty boring
  1268. * in itself, that's why we export the new BSSID...
  1269. * Jean II */
  1270. schedule_work(&priv->wevent_work);
  1271. }
  1272. }
  1273. break;
  1274. case HERMES_INQ_SCAN:
  1275. if (!priv->scan_request && priv->bssid_fixed &&
  1276. priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
  1277. schedule_work(&priv->join_work);
  1278. break;
  1279. }
  1280. /* fall through */
  1281. case HERMES_INQ_HOSTSCAN:
  1282. case HERMES_INQ_HOSTSCAN_SYMBOL: {
  1283. /* Result of a scanning. Contains information about
  1284. * cells in the vicinity - Jean II */
  1285. unsigned char *buf;
  1286. /* Sanity check */
  1287. if (len > 4096) {
  1288. printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
  1289. dev->name, len);
  1290. qabort_scan(priv);
  1291. break;
  1292. }
  1293. /* Allocate buffer for results */
  1294. buf = kmalloc(len, GFP_ATOMIC);
  1295. if (buf == NULL) {
  1296. /* No memory, so can't printk()... */
  1297. qabort_scan(priv);
  1298. break;
  1299. }
  1300. /* Read scan data */
  1301. err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) buf, len,
  1302. infofid, sizeof(info));
  1303. if (err) {
  1304. kfree(buf);
  1305. qabort_scan(priv);
  1306. break;
  1307. }
  1308. #ifdef ORINOCO_DEBUG
  1309. {
  1310. int i;
  1311. printk(KERN_DEBUG "Scan result [%02X", buf[0]);
  1312. for (i = 1; i < (len * 2); i++)
  1313. printk(":%02X", buf[i]);
  1314. printk("]\n");
  1315. }
  1316. #endif /* ORINOCO_DEBUG */
  1317. qbuf_scan(priv, buf, len, type);
  1318. }
  1319. break;
  1320. case HERMES_INQ_CHANNELINFO:
  1321. {
  1322. struct agere_ext_scan_info *bss;
  1323. if (!priv->scan_request) {
  1324. printk(KERN_DEBUG "%s: Got chaninfo without scan, "
  1325. "len=%d\n", dev->name, len);
  1326. break;
  1327. }
  1328. /* An empty result indicates that the scan is complete */
  1329. if (len == 0) {
  1330. qbuf_scan(priv, NULL, len, type);
  1331. break;
  1332. }
  1333. /* Sanity check */
  1334. else if (len < (offsetof(struct agere_ext_scan_info,
  1335. data) + 2)) {
  1336. /* Drop this result now so we don't have to
  1337. * keep checking later */
  1338. printk(KERN_WARNING
  1339. "%s: Ext scan results too short (%d bytes)\n",
  1340. dev->name, len);
  1341. break;
  1342. }
  1343. bss = kmalloc(len, GFP_ATOMIC);
  1344. if (bss == NULL)
  1345. break;
  1346. /* Read scan data */
  1347. err = hw->ops->bap_pread(hw, IRQ_BAP, (void *) bss, len,
  1348. infofid, sizeof(info));
  1349. if (err)
  1350. kfree(bss);
  1351. else
  1352. qbuf_scan(priv, bss, len, type);
  1353. break;
  1354. }
  1355. case HERMES_INQ_SEC_STAT_AGERE:
  1356. /* Security status (Agere specific) */
  1357. /* Ignore this frame for now */
  1358. if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
  1359. break;
  1360. /* fall through */
  1361. default:
  1362. printk(KERN_DEBUG "%s: Unknown information frame received: "
  1363. "type 0x%04x, length %d\n", dev->name, type, len);
  1364. /* We don't actually do anything about it */
  1365. break;
  1366. }
  1367. }
  1368. EXPORT_SYMBOL(__orinoco_ev_info);
  1369. static void __orinoco_ev_infdrop(struct net_device *dev, struct hermes *hw)
  1370. {
  1371. if (net_ratelimit())
  1372. printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
  1373. }
  1374. /********************************************************************/
  1375. /* Internal hardware control routines */
  1376. /********************************************************************/
  1377. static int __orinoco_up(struct orinoco_private *priv)
  1378. {
  1379. struct net_device *dev = priv->ndev;
  1380. struct hermes *hw = &priv->hw;
  1381. int err;
  1382. netif_carrier_off(dev); /* just to make sure */
  1383. err = __orinoco_commit(priv);
  1384. if (err) {
  1385. printk(KERN_ERR "%s: Error %d configuring card\n",
  1386. dev->name, err);
  1387. return err;
  1388. }
  1389. /* Fire things up again */
  1390. hermes_set_irqmask(hw, ORINOCO_INTEN);
  1391. err = hermes_enable_port(hw, 0);
  1392. if (err) {
  1393. printk(KERN_ERR "%s: Error %d enabling MAC port\n",
  1394. dev->name, err);
  1395. return err;
  1396. }
  1397. netif_start_queue(dev);
  1398. return 0;
  1399. }
  1400. static int __orinoco_down(struct orinoco_private *priv)
  1401. {
  1402. struct net_device *dev = priv->ndev;
  1403. struct hermes *hw = &priv->hw;
  1404. int err;
  1405. netif_stop_queue(dev);
  1406. if (!priv->hw_unavailable) {
  1407. if (!priv->broken_disableport) {
  1408. err = hermes_disable_port(hw, 0);
  1409. if (err) {
  1410. /* Some firmwares (e.g. Intersil 1.3.x) seem
  1411. * to have problems disabling the port, oh
  1412. * well, too bad. */
  1413. printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
  1414. dev->name, err);
  1415. priv->broken_disableport = 1;
  1416. }
  1417. }
  1418. hermes_set_irqmask(hw, 0);
  1419. hermes_write_regn(hw, EVACK, 0xffff);
  1420. }
  1421. orinoco_scan_done(priv, true);
  1422. /* firmware will have to reassociate */
  1423. netif_carrier_off(dev);
  1424. priv->last_linkstatus = 0xffff;
  1425. return 0;
  1426. }
  1427. static int orinoco_reinit_firmware(struct orinoco_private *priv)
  1428. {
  1429. struct hermes *hw = &priv->hw;
  1430. int err;
  1431. err = hw->ops->init(hw);
  1432. if (priv->do_fw_download && !err) {
  1433. err = orinoco_download(priv);
  1434. if (err)
  1435. priv->do_fw_download = 0;
  1436. }
  1437. if (!err)
  1438. err = orinoco_hw_allocate_fid(priv);
  1439. return err;
  1440. }
  1441. static int
  1442. __orinoco_set_multicast_list(struct net_device *dev)
  1443. {
  1444. struct orinoco_private *priv = ndev_priv(dev);
  1445. int err = 0;
  1446. int promisc, mc_count;
  1447. /* The Hermes doesn't seem to have an allmulti mode, so we go
  1448. * into promiscuous mode and let the upper levels deal. */
  1449. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  1450. (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
  1451. promisc = 1;
  1452. mc_count = 0;
  1453. } else {
  1454. promisc = 0;
  1455. mc_count = netdev_mc_count(dev);
  1456. }
  1457. err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, promisc);
  1458. return err;
  1459. }
  1460. /* This must be called from user context, without locks held - use
  1461. * schedule_work() */
  1462. void orinoco_reset(struct work_struct *work)
  1463. {
  1464. struct orinoco_private *priv =
  1465. container_of(work, struct orinoco_private, reset_work);
  1466. struct net_device *dev = priv->ndev;
  1467. struct hermes *hw = &priv->hw;
  1468. int err;
  1469. unsigned long flags;
  1470. if (orinoco_lock(priv, &flags) != 0)
  1471. /* When the hardware becomes available again, whatever
  1472. * detects that is responsible for re-initializing
  1473. * it. So no need for anything further */
  1474. return;
  1475. netif_stop_queue(dev);
  1476. /* Shut off interrupts. Depending on what state the hardware
  1477. * is in, this might not work, but we'll try anyway */
  1478. hermes_set_irqmask(hw, 0);
  1479. hermes_write_regn(hw, EVACK, 0xffff);
  1480. priv->hw_unavailable++;
  1481. priv->last_linkstatus = 0xffff; /* firmware will have to reassociate */
  1482. netif_carrier_off(dev);
  1483. orinoco_unlock(priv, &flags);
  1484. /* Scanning support: Notify scan cancellation */
  1485. orinoco_scan_done(priv, true);
  1486. if (priv->hard_reset) {
  1487. err = (*priv->hard_reset)(priv);
  1488. if (err) {
  1489. printk(KERN_ERR "%s: orinoco_reset: Error %d "
  1490. "performing hard reset\n", dev->name, err);
  1491. goto disable;
  1492. }
  1493. }
  1494. err = orinoco_reinit_firmware(priv);
  1495. if (err) {
  1496. printk(KERN_ERR "%s: orinoco_reset: Error %d re-initializing firmware\n",
  1497. dev->name, err);
  1498. goto disable;
  1499. }
  1500. /* This has to be called from user context */
  1501. orinoco_lock_irq(priv);
  1502. priv->hw_unavailable--;
  1503. /* priv->open or priv->hw_unavailable might have changed while
  1504. * we dropped the lock */
  1505. if (priv->open && (!priv->hw_unavailable)) {
  1506. err = __orinoco_up(priv);
  1507. if (err) {
  1508. printk(KERN_ERR "%s: orinoco_reset: Error %d reenabling card\n",
  1509. dev->name, err);
  1510. } else
  1511. netif_trans_update(dev);
  1512. }
  1513. orinoco_unlock_irq(priv);
  1514. return;
  1515. disable:
  1516. hermes_set_irqmask(hw, 0);
  1517. netif_device_detach(dev);
  1518. printk(KERN_ERR "%s: Device has been disabled!\n", dev->name);
  1519. }
  1520. static int __orinoco_commit(struct orinoco_private *priv)
  1521. {
  1522. struct net_device *dev = priv->ndev;
  1523. int err = 0;
  1524. /* If we've called commit, we are reconfiguring or bringing the
  1525. * interface up. Maintaining countermeasures across this would
  1526. * be confusing, so note that we've disabled them. The port will
  1527. * be enabled later in orinoco_commit or __orinoco_up. */
  1528. priv->tkip_cm_active = 0;
  1529. err = orinoco_hw_program_rids(priv);
  1530. /* FIXME: what about netif_tx_lock */
  1531. (void) __orinoco_set_multicast_list(dev);
  1532. return err;
  1533. }
  1534. /* Ensures configuration changes are applied. May result in a reset.
  1535. * The caller should hold priv->lock
  1536. */
  1537. int orinoco_commit(struct orinoco_private *priv)
  1538. {
  1539. struct net_device *dev = priv->ndev;
  1540. struct hermes *hw = &priv->hw;
  1541. int err;
  1542. if (priv->broken_disableport) {
  1543. schedule_work(&priv->reset_work);
  1544. return 0;
  1545. }
  1546. err = hermes_disable_port(hw, 0);
  1547. if (err) {
  1548. printk(KERN_WARNING "%s: Unable to disable port "
  1549. "while reconfiguring card\n", dev->name);
  1550. priv->broken_disableport = 1;
  1551. goto out;
  1552. }
  1553. err = __orinoco_commit(priv);
  1554. if (err) {
  1555. printk(KERN_WARNING "%s: Unable to reconfigure card\n",
  1556. dev->name);
  1557. goto out;
  1558. }
  1559. err = hermes_enable_port(hw, 0);
  1560. if (err) {
  1561. printk(KERN_WARNING "%s: Unable to enable port while reconfiguring card\n",
  1562. dev->name);
  1563. goto out;
  1564. }
  1565. out:
  1566. if (err) {
  1567. printk(KERN_WARNING "%s: Resetting instead...\n", dev->name);
  1568. schedule_work(&priv->reset_work);
  1569. err = 0;
  1570. }
  1571. return err;
  1572. }
  1573. /********************************************************************/
  1574. /* Interrupt handler */
  1575. /********************************************************************/
  1576. static void __orinoco_ev_tick(struct net_device *dev, struct hermes *hw)
  1577. {
  1578. printk(KERN_DEBUG "%s: TICK\n", dev->name);
  1579. }
  1580. static void __orinoco_ev_wterr(struct net_device *dev, struct hermes *hw)
  1581. {
  1582. /* This seems to happen a fair bit under load, but ignoring it
  1583. seems to work fine...*/
  1584. printk(KERN_DEBUG "%s: MAC controller error (WTERR). Ignoring.\n",
  1585. dev->name);
  1586. }
  1587. irqreturn_t orinoco_interrupt(int irq, void *dev_id)
  1588. {
  1589. struct orinoco_private *priv = dev_id;
  1590. struct net_device *dev = priv->ndev;
  1591. struct hermes *hw = &priv->hw;
  1592. int count = MAX_IRQLOOPS_PER_IRQ;
  1593. u16 evstat, events;
  1594. /* These are used to detect a runaway interrupt situation.
  1595. *
  1596. * If we get more than MAX_IRQLOOPS_PER_JIFFY iterations in a jiffy,
  1597. * we panic and shut down the hardware
  1598. */
  1599. /* jiffies value the last time we were called */
  1600. static int last_irq_jiffy; /* = 0 */
  1601. static int loops_this_jiffy; /* = 0 */
  1602. unsigned long flags;
  1603. if (orinoco_lock(priv, &flags) != 0) {
  1604. /* If hw is unavailable - we don't know if the irq was
  1605. * for us or not */
  1606. return IRQ_HANDLED;
  1607. }
  1608. evstat = hermes_read_regn(hw, EVSTAT);
  1609. events = evstat & hw->inten;
  1610. if (!events) {
  1611. orinoco_unlock(priv, &flags);
  1612. return IRQ_NONE;
  1613. }
  1614. if (jiffies != last_irq_jiffy)
  1615. loops_this_jiffy = 0;
  1616. last_irq_jiffy = jiffies;
  1617. while (events && count--) {
  1618. if (++loops_this_jiffy > MAX_IRQLOOPS_PER_JIFFY) {
  1619. printk(KERN_WARNING "%s: IRQ handler is looping too "
  1620. "much! Resetting.\n", dev->name);
  1621. /* Disable interrupts for now */
  1622. hermes_set_irqmask(hw, 0);
  1623. schedule_work(&priv->reset_work);
  1624. break;
  1625. }
  1626. /* Check the card hasn't been removed */
  1627. if (!hermes_present(hw)) {
  1628. DEBUG(0, "orinoco_interrupt(): card removed\n");
  1629. break;
  1630. }
  1631. if (events & HERMES_EV_TICK)
  1632. __orinoco_ev_tick(dev, hw);
  1633. if (events & HERMES_EV_WTERR)
  1634. __orinoco_ev_wterr(dev, hw);
  1635. if (events & HERMES_EV_INFDROP)
  1636. __orinoco_ev_infdrop(dev, hw);
  1637. if (events & HERMES_EV_INFO)
  1638. __orinoco_ev_info(dev, hw);
  1639. if (events & HERMES_EV_RX)
  1640. __orinoco_ev_rx(dev, hw);
  1641. if (events & HERMES_EV_TXEXC)
  1642. __orinoco_ev_txexc(dev, hw);
  1643. if (events & HERMES_EV_TX)
  1644. __orinoco_ev_tx(dev, hw);
  1645. if (events & HERMES_EV_ALLOC)
  1646. __orinoco_ev_alloc(dev, hw);
  1647. hermes_write_regn(hw, EVACK, evstat);
  1648. evstat = hermes_read_regn(hw, EVSTAT);
  1649. events = evstat & hw->inten;
  1650. }
  1651. orinoco_unlock(priv, &flags);
  1652. return IRQ_HANDLED;
  1653. }
  1654. EXPORT_SYMBOL(orinoco_interrupt);
  1655. /********************************************************************/
  1656. /* Power management */
  1657. /********************************************************************/
  1658. #if defined(CONFIG_PM_SLEEP) && !defined(CONFIG_HERMES_CACHE_FW_ON_INIT)
  1659. static int orinoco_pm_notifier(struct notifier_block *notifier,
  1660. unsigned long pm_event,
  1661. void *unused)
  1662. {
  1663. struct orinoco_private *priv = container_of(notifier,
  1664. struct orinoco_private,
  1665. pm_notifier);
  1666. /* All we need to do is cache the firmware before suspend, and
  1667. * release it when we come out.
  1668. *
  1669. * Only need to do this if we're downloading firmware. */
  1670. if (!priv->do_fw_download)
  1671. return NOTIFY_DONE;
  1672. switch (pm_event) {
  1673. case PM_HIBERNATION_PREPARE:
  1674. case PM_SUSPEND_PREPARE:
  1675. orinoco_cache_fw(priv, 0);
  1676. break;
  1677. case PM_POST_RESTORE:
  1678. /* Restore from hibernation failed. We need to clean
  1679. * up in exactly the same way, so fall through. */
  1680. case PM_POST_HIBERNATION:
  1681. case PM_POST_SUSPEND:
  1682. orinoco_uncache_fw(priv);
  1683. break;
  1684. case PM_RESTORE_PREPARE:
  1685. default:
  1686. break;
  1687. }
  1688. return NOTIFY_DONE;
  1689. }
  1690. static void orinoco_register_pm_notifier(struct orinoco_private *priv)
  1691. {
  1692. priv->pm_notifier.notifier_call = orinoco_pm_notifier;
  1693. register_pm_notifier(&priv->pm_notifier);
  1694. }
  1695. static void orinoco_unregister_pm_notifier(struct orinoco_private *priv)
  1696. {
  1697. unregister_pm_notifier(&priv->pm_notifier);
  1698. }
  1699. #else /* !PM_SLEEP || HERMES_CACHE_FW_ON_INIT */
  1700. #define orinoco_register_pm_notifier(priv) do { } while (0)
  1701. #define orinoco_unregister_pm_notifier(priv) do { } while (0)
  1702. #endif
  1703. /********************************************************************/
  1704. /* Initialization */
  1705. /********************************************************************/
  1706. int orinoco_init(struct orinoco_private *priv)
  1707. {
  1708. struct device *dev = priv->dev;
  1709. struct wiphy *wiphy = priv_to_wiphy(priv);
  1710. struct hermes *hw = &priv->hw;
  1711. int err = 0;
  1712. /* No need to lock, the hw_unavailable flag is already set in
  1713. * alloc_orinocodev() */
  1714. priv->nicbuf_size = IEEE80211_MAX_FRAME_LEN + ETH_HLEN;
  1715. /* Initialize the firmware */
  1716. err = hw->ops->init(hw);
  1717. if (err != 0) {
  1718. dev_err(dev, "Failed to initialize firmware (err = %d)\n",
  1719. err);
  1720. goto out;
  1721. }
  1722. err = determine_fw_capabilities(priv, wiphy->fw_version,
  1723. sizeof(wiphy->fw_version),
  1724. &wiphy->hw_version);
  1725. if (err != 0) {
  1726. dev_err(dev, "Incompatible firmware, aborting\n");
  1727. goto out;
  1728. }
  1729. if (priv->do_fw_download) {
  1730. #ifdef CONFIG_HERMES_CACHE_FW_ON_INIT
  1731. orinoco_cache_fw(priv, 0);
  1732. #endif
  1733. err = orinoco_download(priv);
  1734. if (err)
  1735. priv->do_fw_download = 0;
  1736. /* Check firmware version again */
  1737. err = determine_fw_capabilities(priv, wiphy->fw_version,
  1738. sizeof(wiphy->fw_version),
  1739. &wiphy->hw_version);
  1740. if (err != 0) {
  1741. dev_err(dev, "Incompatible firmware, aborting\n");
  1742. goto out;
  1743. }
  1744. }
  1745. if (priv->has_port3)
  1746. dev_info(dev, "Ad-hoc demo mode supported\n");
  1747. if (priv->has_ibss)
  1748. dev_info(dev, "IEEE standard IBSS ad-hoc mode supported\n");
  1749. if (priv->has_wep)
  1750. dev_info(dev, "WEP supported, %s-bit key\n",
  1751. priv->has_big_wep ? "104" : "40");
  1752. if (priv->has_wpa) {
  1753. dev_info(dev, "WPA-PSK supported\n");
  1754. if (orinoco_mic_init(priv)) {
  1755. dev_err(dev, "Failed to setup MIC crypto algorithm. "
  1756. "Disabling WPA support\n");
  1757. priv->has_wpa = 0;
  1758. }
  1759. }
  1760. err = orinoco_hw_read_card_settings(priv, wiphy->perm_addr);
  1761. if (err)
  1762. goto out;
  1763. err = orinoco_hw_allocate_fid(priv);
  1764. if (err) {
  1765. dev_err(dev, "Failed to allocate NIC buffer!\n");
  1766. goto out;
  1767. }
  1768. /* Set up the default configuration */
  1769. priv->iw_mode = NL80211_IFTYPE_STATION;
  1770. /* By default use IEEE/IBSS ad-hoc mode if we have it */
  1771. priv->prefer_port3 = priv->has_port3 && (!priv->has_ibss);
  1772. set_port_type(priv);
  1773. priv->channel = 0; /* use firmware default */
  1774. priv->promiscuous = 0;
  1775. priv->encode_alg = ORINOCO_ALG_NONE;
  1776. priv->tx_key = 0;
  1777. priv->wpa_enabled = 0;
  1778. priv->tkip_cm_active = 0;
  1779. priv->key_mgmt = 0;
  1780. priv->wpa_ie_len = 0;
  1781. priv->wpa_ie = NULL;
  1782. if (orinoco_wiphy_register(wiphy)) {
  1783. err = -ENODEV;
  1784. goto out;
  1785. }
  1786. /* Make the hardware available, as long as it hasn't been
  1787. * removed elsewhere (e.g. by PCMCIA hot unplug) */
  1788. orinoco_lock_irq(priv);
  1789. priv->hw_unavailable--;
  1790. orinoco_unlock_irq(priv);
  1791. dev_dbg(dev, "Ready\n");
  1792. out:
  1793. return err;
  1794. }
  1795. EXPORT_SYMBOL(orinoco_init);
  1796. static const struct net_device_ops orinoco_netdev_ops = {
  1797. .ndo_open = orinoco_open,
  1798. .ndo_stop = orinoco_stop,
  1799. .ndo_start_xmit = orinoco_xmit,
  1800. .ndo_set_rx_mode = orinoco_set_multicast_list,
  1801. .ndo_change_mtu = orinoco_change_mtu,
  1802. .ndo_set_mac_address = eth_mac_addr,
  1803. .ndo_validate_addr = eth_validate_addr,
  1804. .ndo_tx_timeout = orinoco_tx_timeout,
  1805. .ndo_get_stats = orinoco_get_stats,
  1806. };
  1807. /* Allocate private data.
  1808. *
  1809. * This driver has a number of structures associated with it
  1810. * netdev - Net device structure for each network interface
  1811. * wiphy - structure associated with wireless phy
  1812. * wireless_dev (wdev) - structure for each wireless interface
  1813. * hw - structure for hermes chip info
  1814. * card - card specific structure for use by the card driver
  1815. * (airport, orinoco_cs)
  1816. * priv - orinoco private data
  1817. * device - generic linux device structure
  1818. *
  1819. * +---------+ +---------+
  1820. * | wiphy | | netdev |
  1821. * | +-------+ | +-------+
  1822. * | | priv | | | wdev |
  1823. * | | +-----+ +-+-------+
  1824. * | | | hw |
  1825. * | +-+-----+
  1826. * | | card |
  1827. * +-+-------+
  1828. *
  1829. * priv has a link to netdev and device
  1830. * wdev has a link to wiphy
  1831. */
  1832. struct orinoco_private
  1833. *alloc_orinocodev(int sizeof_card,
  1834. struct device *device,
  1835. int (*hard_reset)(struct orinoco_private *),
  1836. int (*stop_fw)(struct orinoco_private *, int))
  1837. {
  1838. struct orinoco_private *priv;
  1839. struct wiphy *wiphy;
  1840. /* allocate wiphy
  1841. * NOTE: We only support a single virtual interface
  1842. * but this may change when monitor mode is added
  1843. */
  1844. wiphy = wiphy_new(&orinoco_cfg_ops,
  1845. sizeof(struct orinoco_private) + sizeof_card);
  1846. if (!wiphy)
  1847. return NULL;
  1848. priv = wiphy_priv(wiphy);
  1849. priv->dev = device;
  1850. if (sizeof_card)
  1851. priv->card = (void *)((unsigned long)priv
  1852. + sizeof(struct orinoco_private));
  1853. else
  1854. priv->card = NULL;
  1855. orinoco_wiphy_init(wiphy);
  1856. #ifdef WIRELESS_SPY
  1857. priv->wireless_data.spy_data = &priv->spy_data;
  1858. #endif
  1859. /* Set up default callbacks */
  1860. priv->hard_reset = hard_reset;
  1861. priv->stop_fw = stop_fw;
  1862. spin_lock_init(&priv->lock);
  1863. priv->open = 0;
  1864. priv->hw_unavailable = 1; /* orinoco_init() must clear this
  1865. * before anything else touches the
  1866. * hardware */
  1867. INIT_WORK(&priv->reset_work, orinoco_reset);
  1868. INIT_WORK(&priv->join_work, orinoco_join_ap);
  1869. INIT_WORK(&priv->wevent_work, orinoco_send_wevents);
  1870. INIT_LIST_HEAD(&priv->rx_list);
  1871. tasklet_init(&priv->rx_tasklet, orinoco_rx_isr_tasklet,
  1872. (unsigned long) priv);
  1873. spin_lock_init(&priv->scan_lock);
  1874. INIT_LIST_HEAD(&priv->scan_list);
  1875. INIT_WORK(&priv->process_scan, orinoco_process_scan_results);
  1876. priv->last_linkstatus = 0xffff;
  1877. #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  1878. priv->cached_pri_fw = NULL;
  1879. priv->cached_fw = NULL;
  1880. #endif
  1881. /* Register PM notifiers */
  1882. orinoco_register_pm_notifier(priv);
  1883. return priv;
  1884. }
  1885. EXPORT_SYMBOL(alloc_orinocodev);
  1886. /* We can only support a single interface. We provide a separate
  1887. * function to set it up to distinguish between hardware
  1888. * initialisation and interface setup.
  1889. *
  1890. * The base_addr and irq parameters are passed on to netdev for use
  1891. * with SIOCGIFMAP.
  1892. */
  1893. int orinoco_if_add(struct orinoco_private *priv,
  1894. unsigned long base_addr,
  1895. unsigned int irq,
  1896. const struct net_device_ops *ops)
  1897. {
  1898. struct wiphy *wiphy = priv_to_wiphy(priv);
  1899. struct wireless_dev *wdev;
  1900. struct net_device *dev;
  1901. int ret;
  1902. dev = alloc_etherdev(sizeof(struct wireless_dev));
  1903. if (!dev)
  1904. return -ENOMEM;
  1905. /* Initialise wireless_dev */
  1906. wdev = netdev_priv(dev);
  1907. wdev->wiphy = wiphy;
  1908. wdev->iftype = NL80211_IFTYPE_STATION;
  1909. /* Setup / override net_device fields */
  1910. dev->ieee80211_ptr = wdev;
  1911. dev->watchdog_timeo = HZ; /* 1 second timeout */
  1912. dev->wireless_handlers = &orinoco_handler_def;
  1913. #ifdef WIRELESS_SPY
  1914. dev->wireless_data = &priv->wireless_data;
  1915. #endif
  1916. /* Default to standard ops if not set */
  1917. if (ops)
  1918. dev->netdev_ops = ops;
  1919. else
  1920. dev->netdev_ops = &orinoco_netdev_ops;
  1921. /* we use the default eth_mac_addr for setting the MAC addr */
  1922. /* Reserve space in skb for the SNAP header */
  1923. dev->needed_headroom = ENCAPS_OVERHEAD;
  1924. netif_carrier_off(dev);
  1925. memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
  1926. dev->base_addr = base_addr;
  1927. dev->irq = irq;
  1928. dev->min_mtu = ORINOCO_MIN_MTU;
  1929. dev->max_mtu = ORINOCO_MAX_MTU;
  1930. SET_NETDEV_DEV(dev, priv->dev);
  1931. ret = register_netdev(dev);
  1932. if (ret)
  1933. goto fail;
  1934. priv->ndev = dev;
  1935. /* Report what we've done */
  1936. dev_dbg(priv->dev, "Registerred interface %s.\n", dev->name);
  1937. return 0;
  1938. fail:
  1939. free_netdev(dev);
  1940. return ret;
  1941. }
  1942. EXPORT_SYMBOL(orinoco_if_add);
  1943. void orinoco_if_del(struct orinoco_private *priv)
  1944. {
  1945. struct net_device *dev = priv->ndev;
  1946. unregister_netdev(dev);
  1947. free_netdev(dev);
  1948. }
  1949. EXPORT_SYMBOL(orinoco_if_del);
  1950. void free_orinocodev(struct orinoco_private *priv)
  1951. {
  1952. struct wiphy *wiphy = priv_to_wiphy(priv);
  1953. struct orinoco_rx_data *rx_data, *temp;
  1954. struct orinoco_scan_data *sd, *sdtemp;
  1955. /* If the tasklet is scheduled when we call tasklet_kill it
  1956. * will run one final time. However the tasklet will only
  1957. * drain priv->rx_list if the hw is still available. */
  1958. tasklet_kill(&priv->rx_tasklet);
  1959. /* Explicitly drain priv->rx_list */
  1960. list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
  1961. list_del(&rx_data->list);
  1962. dev_kfree_skb(rx_data->skb);
  1963. kfree(rx_data->desc);
  1964. kfree(rx_data);
  1965. }
  1966. cancel_work_sync(&priv->process_scan);
  1967. /* Explicitly drain priv->scan_list */
  1968. list_for_each_entry_safe(sd, sdtemp, &priv->scan_list, list) {
  1969. list_del(&sd->list);
  1970. if (sd->len > 0)
  1971. kfree(sd->buf);
  1972. kfree(sd);
  1973. }
  1974. orinoco_unregister_pm_notifier(priv);
  1975. orinoco_uncache_fw(priv);
  1976. priv->wpa_ie_len = 0;
  1977. kfree(priv->wpa_ie);
  1978. orinoco_mic_free(priv);
  1979. wiphy_free(wiphy);
  1980. }
  1981. EXPORT_SYMBOL(free_orinocodev);
  1982. int orinoco_up(struct orinoco_private *priv)
  1983. {
  1984. struct net_device *dev = priv->ndev;
  1985. unsigned long flags;
  1986. int err;
  1987. priv->hw.ops->lock_irqsave(&priv->lock, &flags);
  1988. err = orinoco_reinit_firmware(priv);
  1989. if (err) {
  1990. printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
  1991. dev->name, err);
  1992. goto exit;
  1993. }
  1994. netif_device_attach(dev);
  1995. priv->hw_unavailable--;
  1996. if (priv->open && !priv->hw_unavailable) {
  1997. err = __orinoco_up(priv);
  1998. if (err)
  1999. printk(KERN_ERR "%s: Error %d restarting card\n",
  2000. dev->name, err);
  2001. }
  2002. exit:
  2003. priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
  2004. return 0;
  2005. }
  2006. EXPORT_SYMBOL(orinoco_up);
  2007. void orinoco_down(struct orinoco_private *priv)
  2008. {
  2009. struct net_device *dev = priv->ndev;
  2010. unsigned long flags;
  2011. int err;
  2012. priv->hw.ops->lock_irqsave(&priv->lock, &flags);
  2013. err = __orinoco_down(priv);
  2014. if (err)
  2015. printk(KERN_WARNING "%s: Error %d downing interface\n",
  2016. dev->name, err);
  2017. netif_device_detach(dev);
  2018. priv->hw_unavailable++;
  2019. priv->hw.ops->unlock_irqrestore(&priv->lock, &flags);
  2020. }
  2021. EXPORT_SYMBOL(orinoco_down);
  2022. /********************************************************************/
  2023. /* Module initialization */
  2024. /********************************************************************/
  2025. /* Can't be declared "const" or the whole __initdata section will
  2026. * become const */
  2027. static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
  2028. " (David Gibson <hermes@gibson.dropbear.id.au>, "
  2029. "Pavel Roskin <proski@gnu.org>, et al)";
  2030. static int __init init_orinoco(void)
  2031. {
  2032. printk(KERN_DEBUG "%s\n", version);
  2033. return 0;
  2034. }
  2035. static void __exit exit_orinoco(void)
  2036. {
  2037. }
  2038. module_init(init_orinoco);
  2039. module_exit(exit_orinoco);