main.c 62 KB

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