main.c 62 KB

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