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