main.c 62 KB

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