main.c 62 KB

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