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/slab.h>
  78. #include <linux/init.h>
  79. #include <linux/delay.h>
  80. #include <linux/device.h>
  81. #include <linux/netdevice.h>
  82. #include <linux/etherdevice.h>
  83. #include <linux/suspend.h>
  84. #include <linux/if_arp.h>
  85. #include <linux/wireless.h>
  86. #include <linux/ieee80211.h>
  87. #include <net/iw_handler.h>
  88. #include <net/cfg80211.h>
  89. #include "hermes_rid.h"
  90. #include "hermes_dld.h"
  91. #include "hw.h"
  92. #include "scan.h"
  93. #include "mic.h"
  94. #include "fw.h"
  95. #include "wext.h"
  96. #include "cfg.h"
  97. #include "main.h"
  98. #include "orinoco.h"
  99. /********************************************************************/
  100. /* Module information */
  101. /********************************************************************/
  102. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & "
  103. "David Gibson <hermes@gibson.dropbear.id.au>");
  104. MODULE_DESCRIPTION("Driver for Lucent Orinoco, Prism II based "
  105. "and similar wireless cards");
  106. MODULE_LICENSE("Dual MPL/GPL");
  107. /* Level of debugging. Used in the macros in orinoco.h */
  108. #ifdef ORINOCO_DEBUG
  109. int orinoco_debug = ORINOCO_DEBUG;
  110. EXPORT_SYMBOL(orinoco_debug);
  111. module_param(orinoco_debug, int, 0644);
  112. MODULE_PARM_DESC(orinoco_debug, "Debug level");
  113. #endif
  114. static int suppress_linkstatus; /* = 0 */
  115. module_param(suppress_linkstatus, bool, 0644);
  116. MODULE_PARM_DESC(suppress_linkstatus, "Don't log link status changes");
  117. static int ignore_disconnect; /* = 0 */
  118. module_param(ignore_disconnect, int, 0644);
  119. MODULE_PARM_DESC(ignore_disconnect,
  120. "Don't report lost link to the network layer");
  121. int force_monitor; /* = 0 */
  122. module_param(force_monitor, int, 0644);
  123. MODULE_PARM_DESC(force_monitor, "Allow monitor mode for all firmware versions");
  124. /********************************************************************/
  125. /* Internal constants */
  126. /********************************************************************/
  127. /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
  128. static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
  129. #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
  130. #define ORINOCO_MIN_MTU 256
  131. #define ORINOCO_MAX_MTU (IEEE80211_MAX_DATA_LEN - ENCAPS_OVERHEAD)
  132. #define MAX_IRQLOOPS_PER_IRQ 10
  133. #define MAX_IRQLOOPS_PER_JIFFY (20000/HZ) /* Based on a guestimate of
  134. * how many events the
  135. * device could
  136. * legitimately generate */
  137. #define DUMMY_FID 0xFFFF
  138. /*#define MAX_MULTICAST(priv) (priv->firmware_type == FIRMWARE_TYPE_AGERE ? \
  139. HERMES_MAX_MULTICAST : 0)*/
  140. #define MAX_MULTICAST(priv) (HERMES_MAX_MULTICAST)
  141. #define ORINOCO_INTEN (HERMES_EV_RX | HERMES_EV_ALLOC \
  142. | HERMES_EV_TX | HERMES_EV_TXEXC \
  143. | HERMES_EV_WTERR | HERMES_EV_INFO \
  144. | HERMES_EV_INFDROP)
  145. /********************************************************************/
  146. /* Data types */
  147. /********************************************************************/
  148. /* Beginning of the Tx descriptor, used in TxExc handling */
  149. struct hermes_txexc_data {
  150. struct hermes_tx_descriptor desc;
  151. __le16 frame_ctl;
  152. __le16 duration_id;
  153. u8 addr1[ETH_ALEN];
  154. } __attribute__ ((packed));
  155. /* Rx frame header except compatibility 802.3 header */
  156. struct hermes_rx_descriptor {
  157. /* Control */
  158. __le16 status;
  159. __le32 time;
  160. u8 silence;
  161. u8 signal;
  162. u8 rate;
  163. u8 rxflow;
  164. __le32 reserved;
  165. /* 802.11 header */
  166. __le16 frame_ctl;
  167. __le16 duration_id;
  168. u8 addr1[ETH_ALEN];
  169. u8 addr2[ETH_ALEN];
  170. u8 addr3[ETH_ALEN];
  171. __le16 seq_ctl;
  172. u8 addr4[ETH_ALEN];
  173. /* Data length */
  174. __le16 data_len;
  175. } __attribute__ ((packed));
  176. struct orinoco_rx_data {
  177. struct hermes_rx_descriptor *desc;
  178. struct sk_buff *skb;
  179. struct list_head list;
  180. };
  181. struct orinoco_scan_data {
  182. void *buf;
  183. size_t len;
  184. int type;
  185. struct list_head list;
  186. };
  187. /********************************************************************/
  188. /* Function prototypes */
  189. /********************************************************************/
  190. static int __orinoco_set_multicast_list(struct net_device *dev);
  191. static int __orinoco_up(struct orinoco_private *priv);
  192. static int __orinoco_down(struct orinoco_private *priv);
  193. static int __orinoco_commit(struct orinoco_private *priv);
  194. /********************************************************************/
  195. /* Internal helper functions */
  196. /********************************************************************/
  197. void set_port_type(struct orinoco_private *priv)
  198. {
  199. switch (priv->iw_mode) {
  200. case NL80211_IFTYPE_STATION:
  201. priv->port_type = 1;
  202. priv->createibss = 0;
  203. break;
  204. case NL80211_IFTYPE_ADHOC:
  205. if (priv->prefer_port3) {
  206. priv->port_type = 3;
  207. priv->createibss = 0;
  208. } else {
  209. priv->port_type = priv->ibss_port;
  210. priv->createibss = 1;
  211. }
  212. break;
  213. case NL80211_IFTYPE_MONITOR:
  214. priv->port_type = 3;
  215. priv->createibss = 0;
  216. break;
  217. default:
  218. printk(KERN_ERR "%s: Invalid priv->iw_mode in set_port_type()\n",
  219. priv->ndev->name);
  220. }
  221. }
  222. /********************************************************************/
  223. /* Device methods */
  224. /********************************************************************/
  225. static int orinoco_open(struct net_device *dev)
  226. {
  227. struct orinoco_private *priv = ndev_priv(dev);
  228. unsigned long flags;
  229. int err;
  230. if (orinoco_lock(priv, &flags) != 0)
  231. return -EBUSY;
  232. err = __orinoco_up(priv);
  233. if (!err)
  234. priv->open = 1;
  235. orinoco_unlock(priv, &flags);
  236. return err;
  237. }
  238. static int orinoco_stop(struct net_device *dev)
  239. {
  240. struct orinoco_private *priv = ndev_priv(dev);
  241. int err = 0;
  242. /* We mustn't use orinoco_lock() here, because we need to be
  243. able to close the interface even if hw_unavailable is set
  244. (e.g. as we're released after a PC Card removal) */
  245. spin_lock_irq(&priv->lock);
  246. priv->open = 0;
  247. err = __orinoco_down(priv);
  248. spin_unlock_irq(&priv->lock);
  249. return err;
  250. }
  251. static struct net_device_stats *orinoco_get_stats(struct net_device *dev)
  252. {
  253. struct orinoco_private *priv = ndev_priv(dev);
  254. return &priv->stats;
  255. }
  256. static void orinoco_set_multicast_list(struct net_device *dev)
  257. {
  258. struct orinoco_private *priv = ndev_priv(dev);
  259. unsigned long flags;
  260. if (orinoco_lock(priv, &flags) != 0) {
  261. printk(KERN_DEBUG "%s: orinoco_set_multicast_list() "
  262. "called when hw_unavailable\n", dev->name);
  263. return;
  264. }
  265. __orinoco_set_multicast_list(dev);
  266. orinoco_unlock(priv, &flags);
  267. }
  268. static int orinoco_change_mtu(struct net_device *dev, int new_mtu)
  269. {
  270. struct orinoco_private *priv = ndev_priv(dev);
  271. if ((new_mtu < ORINOCO_MIN_MTU) || (new_mtu > ORINOCO_MAX_MTU))
  272. return -EINVAL;
  273. /* MTU + encapsulation + header length */
  274. if ((new_mtu + ENCAPS_OVERHEAD + sizeof(struct ieee80211_hdr)) >
  275. (priv->nicbuf_size - ETH_HLEN))
  276. return -EINVAL;
  277. dev->mtu = new_mtu;
  278. return 0;
  279. }
  280. /********************************************************************/
  281. /* Tx path */
  282. /********************************************************************/
  283. static netdev_tx_t orinoco_xmit(struct sk_buff *skb, struct net_device *dev)
  284. {
  285. struct orinoco_private *priv = ndev_priv(dev);
  286. struct net_device_stats *stats = &priv->stats;
  287. struct orinoco_tkip_key *key;
  288. hermes_t *hw = &priv->hw;
  289. int err = 0;
  290. u16 txfid = priv->txfid;
  291. struct ethhdr *eh;
  292. int tx_control;
  293. unsigned long flags;
  294. int do_mic;
  295. if (!netif_running(dev)) {
  296. printk(KERN_ERR "%s: Tx on stopped device!\n",
  297. dev->name);
  298. return NETDEV_TX_BUSY;
  299. }
  300. if (netif_queue_stopped(dev)) {
  301. printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
  302. dev->name);
  303. return NETDEV_TX_BUSY;
  304. }
  305. if (orinoco_lock(priv, &flags) != 0) {
  306. printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n",
  307. dev->name);
  308. return NETDEV_TX_BUSY;
  309. }
  310. if (!netif_carrier_ok(dev) ||
  311. (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
  312. /* Oops, the firmware hasn't established a connection,
  313. silently drop the packet (this seems to be the
  314. safest approach). */
  315. goto drop;
  316. }
  317. /* Check packet length */
  318. if (skb->len < ETH_HLEN)
  319. goto drop;
  320. key = (struct orinoco_tkip_key *) priv->keys[priv->tx_key].key;
  321. do_mic = ((priv->encode_alg == ORINOCO_ALG_TKIP) &&
  322. (key != NULL));
  323. tx_control = HERMES_TXCTRL_TX_OK | HERMES_TXCTRL_TX_EX;
  324. if (do_mic)
  325. tx_control |= (priv->tx_key << HERMES_MIC_KEY_ID_SHIFT) |
  326. HERMES_TXCTRL_MIC;
  327. if (priv->has_alt_txcntl) {
  328. /* WPA enabled firmwares have tx_cntl at the end of
  329. * the 802.11 header. So write zeroed descriptor and
  330. * 802.11 header at the same time
  331. */
  332. char desc[HERMES_802_3_OFFSET];
  333. __le16 *txcntl = (__le16 *) &desc[HERMES_TXCNTL2_OFFSET];
  334. memset(&desc, 0, sizeof(desc));
  335. *txcntl = cpu_to_le16(tx_control);
  336. err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
  337. txfid, 0);
  338. if (err) {
  339. if (net_ratelimit())
  340. printk(KERN_ERR "%s: Error %d writing Tx "
  341. "descriptor to BAP\n", dev->name, err);
  342. goto busy;
  343. }
  344. } else {
  345. struct hermes_tx_descriptor desc;
  346. memset(&desc, 0, sizeof(desc));
  347. desc.tx_control = cpu_to_le16(tx_control);
  348. err = hermes_bap_pwrite(hw, USER_BAP, &desc, sizeof(desc),
  349. txfid, 0);
  350. if (err) {
  351. if (net_ratelimit())
  352. printk(KERN_ERR "%s: Error %d writing Tx "
  353. "descriptor to BAP\n", dev->name, err);
  354. goto busy;
  355. }
  356. /* Clear the 802.11 header and data length fields - some
  357. * firmwares (e.g. Lucent/Agere 8.xx) appear to get confused
  358. * if this isn't done. */
  359. hermes_clear_words(hw, HERMES_DATA0,
  360. HERMES_802_3_OFFSET - HERMES_802_11_OFFSET);
  361. }
  362. eh = (struct ethhdr *)skb->data;
  363. /* Encapsulate Ethernet-II frames */
  364. if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
  365. struct header_struct {
  366. struct ethhdr eth; /* 802.3 header */
  367. u8 encap[6]; /* 802.2 header */
  368. } __attribute__ ((packed)) hdr;
  369. /* Strip destination and source from the data */
  370. skb_pull(skb, 2 * ETH_ALEN);
  371. /* And move them to a separate header */
  372. memcpy(&hdr.eth, eh, 2 * ETH_ALEN);
  373. hdr.eth.h_proto = htons(sizeof(encaps_hdr) + skb->len);
  374. memcpy(hdr.encap, encaps_hdr, sizeof(encaps_hdr));
  375. /* Insert the SNAP header */
  376. if (skb_headroom(skb) < sizeof(hdr)) {
  377. printk(KERN_ERR
  378. "%s: Not enough headroom for 802.2 headers %d\n",
  379. dev->name, skb_headroom(skb));
  380. goto drop;
  381. }
  382. eh = (struct ethhdr *) skb_push(skb, sizeof(hdr));
  383. memcpy(eh, &hdr, sizeof(hdr));
  384. }
  385. err = hermes_bap_pwrite(hw, USER_BAP, skb->data, skb->len,
  386. txfid, HERMES_802_3_OFFSET);
  387. if (err) {
  388. printk(KERN_ERR "%s: Error %d writing packet to BAP\n",
  389. dev->name, err);
  390. goto busy;
  391. }
  392. /* Calculate Michael MIC */
  393. if (do_mic) {
  394. u8 mic_buf[MICHAEL_MIC_LEN + 1];
  395. u8 *mic;
  396. size_t offset;
  397. size_t len;
  398. if (skb->len % 2) {
  399. /* MIC start is on an odd boundary */
  400. mic_buf[0] = skb->data[skb->len - 1];
  401. mic = &mic_buf[1];
  402. offset = skb->len - 1;
  403. len = MICHAEL_MIC_LEN + 1;
  404. } else {
  405. mic = &mic_buf[0];
  406. offset = skb->len;
  407. len = MICHAEL_MIC_LEN;
  408. }
  409. orinoco_mic(priv->tx_tfm_mic, key->tx_mic,
  410. eh->h_dest, eh->h_source, 0 /* priority */,
  411. skb->data + ETH_HLEN, skb->len - ETH_HLEN, mic);
  412. /* Write the MIC */
  413. err = hermes_bap_pwrite(hw, USER_BAP, &mic_buf[0], len,
  414. txfid, HERMES_802_3_OFFSET + offset);
  415. if (err) {
  416. printk(KERN_ERR "%s: Error %d writing MIC to BAP\n",
  417. dev->name, err);
  418. goto busy;
  419. }
  420. }
  421. /* Finally, we actually initiate the send */
  422. netif_stop_queue(dev);
  423. err = hermes_docmd_wait(hw, HERMES_CMD_TX | HERMES_CMD_RECL,
  424. txfid, NULL);
  425. if (err) {
  426. netif_start_queue(dev);
  427. if (net_ratelimit())
  428. printk(KERN_ERR "%s: Error %d transmitting packet\n",
  429. dev->name, err);
  430. goto busy;
  431. }
  432. dev->trans_start = jiffies;
  433. stats->tx_bytes += HERMES_802_3_OFFSET + skb->len;
  434. goto ok;
  435. drop:
  436. stats->tx_errors++;
  437. stats->tx_dropped++;
  438. ok:
  439. orinoco_unlock(priv, &flags);
  440. dev_kfree_skb(skb);
  441. return NETDEV_TX_OK;
  442. busy:
  443. if (err == -EIO)
  444. schedule_work(&priv->reset_work);
  445. orinoco_unlock(priv, &flags);
  446. return NETDEV_TX_BUSY;
  447. }
  448. static void __orinoco_ev_alloc(struct net_device *dev, hermes_t *hw)
  449. {
  450. struct orinoco_private *priv = ndev_priv(dev);
  451. u16 fid = hermes_read_regn(hw, ALLOCFID);
  452. if (fid != priv->txfid) {
  453. if (fid != DUMMY_FID)
  454. printk(KERN_WARNING "%s: Allocate event on unexpected fid (%04X)\n",
  455. dev->name, fid);
  456. return;
  457. }
  458. hermes_write_regn(hw, ALLOCFID, DUMMY_FID);
  459. }
  460. static void __orinoco_ev_tx(struct net_device *dev, hermes_t *hw)
  461. {
  462. struct orinoco_private *priv = ndev_priv(dev);
  463. struct net_device_stats *stats = &priv->stats;
  464. stats->tx_packets++;
  465. netif_wake_queue(dev);
  466. hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
  467. }
  468. static void __orinoco_ev_txexc(struct net_device *dev, hermes_t *hw)
  469. {
  470. struct orinoco_private *priv = ndev_priv(dev);
  471. struct net_device_stats *stats = &priv->stats;
  472. u16 fid = hermes_read_regn(hw, TXCOMPLFID);
  473. u16 status;
  474. struct hermes_txexc_data hdr;
  475. int err = 0;
  476. if (fid == DUMMY_FID)
  477. return; /* Nothing's really happened */
  478. /* Read part of the frame header - we need status and addr1 */
  479. err = hermes_bap_pread(hw, IRQ_BAP, &hdr,
  480. sizeof(struct hermes_txexc_data),
  481. fid, 0);
  482. hermes_write_regn(hw, TXCOMPLFID, DUMMY_FID);
  483. stats->tx_errors++;
  484. if (err) {
  485. printk(KERN_WARNING "%s: Unable to read descriptor on Tx error "
  486. "(FID=%04X error %d)\n",
  487. dev->name, fid, err);
  488. return;
  489. }
  490. DEBUG(1, "%s: Tx error, err %d (FID=%04X)\n", dev->name,
  491. err, fid);
  492. /* We produce a TXDROP event only for retry or lifetime
  493. * exceeded, because that's the only status that really mean
  494. * that this particular node went away.
  495. * Other errors means that *we* screwed up. - Jean II */
  496. status = le16_to_cpu(hdr.desc.status);
  497. if (status & (HERMES_TXSTAT_RETRYERR | HERMES_TXSTAT_AGEDERR)) {
  498. union iwreq_data wrqu;
  499. /* Copy 802.11 dest address.
  500. * We use the 802.11 header because the frame may
  501. * not be 802.3 or may be mangled...
  502. * In Ad-Hoc mode, it will be the node address.
  503. * In managed mode, it will be most likely the AP addr
  504. * User space will figure out how to convert it to
  505. * whatever it needs (IP address or else).
  506. * - Jean II */
  507. memcpy(wrqu.addr.sa_data, hdr.addr1, ETH_ALEN);
  508. wrqu.addr.sa_family = ARPHRD_ETHER;
  509. /* Send event to user space */
  510. wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
  511. }
  512. netif_wake_queue(dev);
  513. }
  514. static void orinoco_tx_timeout(struct net_device *dev)
  515. {
  516. struct orinoco_private *priv = ndev_priv(dev);
  517. struct net_device_stats *stats = &priv->stats;
  518. struct hermes *hw = &priv->hw;
  519. printk(KERN_WARNING "%s: Tx timeout! "
  520. "ALLOCFID=%04x, TXCOMPLFID=%04x, EVSTAT=%04x\n",
  521. dev->name, hermes_read_regn(hw, ALLOCFID),
  522. hermes_read_regn(hw, TXCOMPLFID), hermes_read_regn(hw, EVSTAT));
  523. stats->tx_errors++;
  524. schedule_work(&priv->reset_work);
  525. }
  526. /********************************************************************/
  527. /* Rx path (data frames) */
  528. /********************************************************************/
  529. /* Does the frame have a SNAP header indicating it should be
  530. * de-encapsulated to Ethernet-II? */
  531. static inline int is_ethersnap(void *_hdr)
  532. {
  533. u8 *hdr = _hdr;
  534. /* We de-encapsulate all packets which, a) have SNAP headers
  535. * (i.e. SSAP=DSAP=0xaa and CTRL=0x3 in the 802.2 LLC header
  536. * and where b) the OUI of the SNAP header is 00:00:00 or
  537. * 00:00:f8 - we need both because different APs appear to use
  538. * different OUIs for some reason */
  539. return (memcmp(hdr, &encaps_hdr, 5) == 0)
  540. && ((hdr[5] == 0x00) || (hdr[5] == 0xf8));
  541. }
  542. static inline void orinoco_spy_gather(struct net_device *dev, u_char *mac,
  543. int level, int noise)
  544. {
  545. struct iw_quality wstats;
  546. wstats.level = level - 0x95;
  547. wstats.noise = noise - 0x95;
  548. wstats.qual = (level > noise) ? (level - noise) : 0;
  549. wstats.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  550. /* Update spy records */
  551. wireless_spy_update(dev, mac, &wstats);
  552. }
  553. static void orinoco_stat_gather(struct net_device *dev,
  554. struct sk_buff *skb,
  555. struct hermes_rx_descriptor *desc)
  556. {
  557. struct orinoco_private *priv = ndev_priv(dev);
  558. /* Using spy support with lots of Rx packets, like in an
  559. * infrastructure (AP), will really slow down everything, because
  560. * the MAC address must be compared to each entry of the spy list.
  561. * If the user really asks for it (set some address in the
  562. * spy list), we do it, but he will pay the price.
  563. * Note that to get here, you need both WIRELESS_SPY
  564. * compiled in AND some addresses in the list !!!
  565. */
  566. /* Note : gcc will optimise the whole section away if
  567. * WIRELESS_SPY is not defined... - Jean II */
  568. if (SPY_NUMBER(priv)) {
  569. orinoco_spy_gather(dev, skb_mac_header(skb) + ETH_ALEN,
  570. desc->signal, desc->silence);
  571. }
  572. }
  573. /*
  574. * orinoco_rx_monitor - handle received monitor frames.
  575. *
  576. * Arguments:
  577. * dev network device
  578. * rxfid received FID
  579. * desc rx descriptor of the frame
  580. *
  581. * Call context: interrupt
  582. */
  583. static void orinoco_rx_monitor(struct net_device *dev, u16 rxfid,
  584. struct hermes_rx_descriptor *desc)
  585. {
  586. u32 hdrlen = 30; /* return full header by default */
  587. u32 datalen = 0;
  588. u16 fc;
  589. int err;
  590. int len;
  591. struct sk_buff *skb;
  592. struct orinoco_private *priv = ndev_priv(dev);
  593. struct net_device_stats *stats = &priv->stats;
  594. hermes_t *hw = &priv->hw;
  595. len = le16_to_cpu(desc->data_len);
  596. /* Determine the size of the header and the data */
  597. fc = le16_to_cpu(desc->frame_ctl);
  598. switch (fc & IEEE80211_FCTL_FTYPE) {
  599. case IEEE80211_FTYPE_DATA:
  600. if ((fc & IEEE80211_FCTL_TODS)
  601. && (fc & IEEE80211_FCTL_FROMDS))
  602. hdrlen = 30;
  603. else
  604. hdrlen = 24;
  605. datalen = len;
  606. break;
  607. case IEEE80211_FTYPE_MGMT:
  608. hdrlen = 24;
  609. datalen = len;
  610. break;
  611. case IEEE80211_FTYPE_CTL:
  612. switch (fc & IEEE80211_FCTL_STYPE) {
  613. case IEEE80211_STYPE_PSPOLL:
  614. case IEEE80211_STYPE_RTS:
  615. case IEEE80211_STYPE_CFEND:
  616. case IEEE80211_STYPE_CFENDACK:
  617. hdrlen = 16;
  618. break;
  619. case IEEE80211_STYPE_CTS:
  620. case IEEE80211_STYPE_ACK:
  621. hdrlen = 10;
  622. break;
  623. }
  624. break;
  625. default:
  626. /* Unknown frame type */
  627. break;
  628. }
  629. /* sanity check the length */
  630. if (datalen > IEEE80211_MAX_DATA_LEN + 12) {
  631. printk(KERN_DEBUG "%s: oversized monitor frame, "
  632. "data length = %d\n", dev->name, datalen);
  633. stats->rx_length_errors++;
  634. goto update_stats;
  635. }
  636. skb = dev_alloc_skb(hdrlen + datalen);
  637. if (!skb) {
  638. printk(KERN_WARNING "%s: Cannot allocate skb for monitor frame\n",
  639. dev->name);
  640. goto update_stats;
  641. }
  642. /* Copy the 802.11 header to the skb */
  643. memcpy(skb_put(skb, hdrlen), &(desc->frame_ctl), hdrlen);
  644. skb_reset_mac_header(skb);
  645. /* If any, copy the data from the card to the skb */
  646. if (datalen > 0) {
  647. err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, datalen),
  648. ALIGN(datalen, 2), rxfid,
  649. HERMES_802_2_OFFSET);
  650. if (err) {
  651. printk(KERN_ERR "%s: error %d reading monitor frame\n",
  652. dev->name, err);
  653. goto drop;
  654. }
  655. }
  656. skb->dev = dev;
  657. skb->ip_summed = CHECKSUM_NONE;
  658. skb->pkt_type = PACKET_OTHERHOST;
  659. skb->protocol = cpu_to_be16(ETH_P_802_2);
  660. stats->rx_packets++;
  661. stats->rx_bytes += skb->len;
  662. netif_rx(skb);
  663. return;
  664. drop:
  665. dev_kfree_skb_irq(skb);
  666. update_stats:
  667. stats->rx_errors++;
  668. stats->rx_dropped++;
  669. }
  670. static void __orinoco_ev_rx(struct net_device *dev, hermes_t *hw)
  671. {
  672. struct orinoco_private *priv = ndev_priv(dev);
  673. struct net_device_stats *stats = &priv->stats;
  674. struct iw_statistics *wstats = &priv->wstats;
  675. struct sk_buff *skb = NULL;
  676. u16 rxfid, status;
  677. int length;
  678. struct hermes_rx_descriptor *desc;
  679. struct orinoco_rx_data *rx_data;
  680. int err;
  681. desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
  682. if (!desc) {
  683. printk(KERN_WARNING
  684. "%s: Can't allocate space for RX descriptor\n",
  685. dev->name);
  686. goto update_stats;
  687. }
  688. rxfid = hermes_read_regn(hw, RXFID);
  689. err = hermes_bap_pread(hw, IRQ_BAP, desc, sizeof(*desc),
  690. rxfid, 0);
  691. if (err) {
  692. printk(KERN_ERR "%s: error %d reading Rx descriptor. "
  693. "Frame dropped.\n", dev->name, err);
  694. goto update_stats;
  695. }
  696. status = le16_to_cpu(desc->status);
  697. if (status & HERMES_RXSTAT_BADCRC) {
  698. DEBUG(1, "%s: Bad CRC on Rx. Frame dropped.\n",
  699. dev->name);
  700. stats->rx_crc_errors++;
  701. goto update_stats;
  702. }
  703. /* Handle frames in monitor mode */
  704. if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
  705. orinoco_rx_monitor(dev, rxfid, desc);
  706. goto out;
  707. }
  708. if (status & HERMES_RXSTAT_UNDECRYPTABLE) {
  709. DEBUG(1, "%s: Undecryptable frame on Rx. Frame dropped.\n",
  710. dev->name);
  711. wstats->discard.code++;
  712. goto update_stats;
  713. }
  714. length = le16_to_cpu(desc->data_len);
  715. /* Sanity checks */
  716. if (length < 3) { /* No for even an 802.2 LLC header */
  717. /* At least on Symbol firmware with PCF we get quite a
  718. lot of these legitimately - Poll frames with no
  719. data. */
  720. goto out;
  721. }
  722. if (length > IEEE80211_MAX_DATA_LEN) {
  723. printk(KERN_WARNING "%s: Oversized frame received (%d bytes)\n",
  724. dev->name, length);
  725. stats->rx_length_errors++;
  726. goto update_stats;
  727. }
  728. /* Payload size does not include Michael MIC. Increase payload
  729. * size to read it together with the data. */
  730. if (status & HERMES_RXSTAT_MIC)
  731. length += MICHAEL_MIC_LEN;
  732. /* We need space for the packet data itself, plus an ethernet
  733. header, plus 2 bytes so we can align the IP header on a
  734. 32bit boundary, plus 1 byte so we can read in odd length
  735. packets from the card, which has an IO granularity of 16
  736. bits */
  737. skb = dev_alloc_skb(length+ETH_HLEN+2+1);
  738. if (!skb) {
  739. printk(KERN_WARNING "%s: Can't allocate skb for Rx\n",
  740. dev->name);
  741. goto update_stats;
  742. }
  743. /* We'll prepend the header, so reserve space for it. The worst
  744. case is no decapsulation, when 802.3 header is prepended and
  745. nothing is removed. 2 is for aligning the IP header. */
  746. skb_reserve(skb, ETH_HLEN + 2);
  747. err = hermes_bap_pread(hw, IRQ_BAP, skb_put(skb, length),
  748. ALIGN(length, 2), rxfid,
  749. HERMES_802_2_OFFSET);
  750. if (err) {
  751. printk(KERN_ERR "%s: error %d reading frame. "
  752. "Frame dropped.\n", dev->name, err);
  753. goto drop;
  754. }
  755. /* Add desc and skb to rx queue */
  756. rx_data = kzalloc(sizeof(*rx_data), GFP_ATOMIC);
  757. if (!rx_data) {
  758. printk(KERN_WARNING "%s: Can't allocate RX packet\n",
  759. dev->name);
  760. goto drop;
  761. }
  762. rx_data->desc = desc;
  763. rx_data->skb = skb;
  764. list_add_tail(&rx_data->list, &priv->rx_list);
  765. tasklet_schedule(&priv->rx_tasklet);
  766. return;
  767. drop:
  768. dev_kfree_skb_irq(skb);
  769. update_stats:
  770. stats->rx_errors++;
  771. stats->rx_dropped++;
  772. out:
  773. kfree(desc);
  774. }
  775. static void orinoco_rx(struct net_device *dev,
  776. struct hermes_rx_descriptor *desc,
  777. struct sk_buff *skb)
  778. {
  779. struct orinoco_private *priv = ndev_priv(dev);
  780. struct net_device_stats *stats = &priv->stats;
  781. u16 status, fc;
  782. int length;
  783. struct ethhdr *hdr;
  784. status = le16_to_cpu(desc->status);
  785. length = le16_to_cpu(desc->data_len);
  786. fc = le16_to_cpu(desc->frame_ctl);
  787. /* Calculate and check MIC */
  788. if (status & HERMES_RXSTAT_MIC) {
  789. struct orinoco_tkip_key *key;
  790. int key_id = ((status & HERMES_RXSTAT_MIC_KEY_ID) >>
  791. HERMES_MIC_KEY_ID_SHIFT);
  792. u8 mic[MICHAEL_MIC_LEN];
  793. u8 *rxmic;
  794. u8 *src = (fc & IEEE80211_FCTL_FROMDS) ?
  795. desc->addr3 : desc->addr2;
  796. /* Extract Michael MIC from payload */
  797. rxmic = skb->data + skb->len - MICHAEL_MIC_LEN;
  798. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  799. length -= MICHAEL_MIC_LEN;
  800. key = (struct orinoco_tkip_key *) priv->keys[key_id].key;
  801. if (!key) {
  802. printk(KERN_WARNING "%s: Received encrypted frame from "
  803. "%pM using key %i, but key is not installed\n",
  804. dev->name, src, key_id);
  805. goto drop;
  806. }
  807. orinoco_mic(priv->rx_tfm_mic, key->rx_mic, desc->addr1, src,
  808. 0, /* priority or QoS? */
  809. skb->data, skb->len, &mic[0]);
  810. if (memcmp(mic, rxmic,
  811. MICHAEL_MIC_LEN)) {
  812. union iwreq_data wrqu;
  813. struct iw_michaelmicfailure wxmic;
  814. printk(KERN_WARNING "%s: "
  815. "Invalid Michael MIC in data frame from %pM, "
  816. "using key %i\n",
  817. dev->name, src, key_id);
  818. /* TODO: update stats */
  819. /* Notify userspace */
  820. memset(&wxmic, 0, sizeof(wxmic));
  821. wxmic.flags = key_id & IW_MICFAILURE_KEY_ID;
  822. wxmic.flags |= (desc->addr1[0] & 1) ?
  823. IW_MICFAILURE_GROUP : IW_MICFAILURE_PAIRWISE;
  824. wxmic.src_addr.sa_family = ARPHRD_ETHER;
  825. memcpy(wxmic.src_addr.sa_data, src, ETH_ALEN);
  826. (void) orinoco_hw_get_tkip_iv(priv, key_id,
  827. &wxmic.tsc[0]);
  828. memset(&wrqu, 0, sizeof(wrqu));
  829. wrqu.data.length = sizeof(wxmic);
  830. wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu,
  831. (char *) &wxmic);
  832. goto drop;
  833. }
  834. }
  835. /* Handle decapsulation
  836. * In most cases, the firmware tell us about SNAP frames.
  837. * For some reason, the SNAP frames sent by LinkSys APs
  838. * are not properly recognised by most firmwares.
  839. * So, check ourselves */
  840. if (length >= ENCAPS_OVERHEAD &&
  841. (((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_1042) ||
  842. ((status & HERMES_RXSTAT_MSGTYPE) == HERMES_RXSTAT_TUNNEL) ||
  843. is_ethersnap(skb->data))) {
  844. /* These indicate a SNAP within 802.2 LLC within
  845. 802.11 frame which we'll need to de-encapsulate to
  846. the original EthernetII frame. */
  847. hdr = (struct ethhdr *)skb_push(skb,
  848. ETH_HLEN - ENCAPS_OVERHEAD);
  849. } else {
  850. /* 802.3 frame - prepend 802.3 header as is */
  851. hdr = (struct ethhdr *)skb_push(skb, ETH_HLEN);
  852. hdr->h_proto = htons(length);
  853. }
  854. memcpy(hdr->h_dest, desc->addr1, ETH_ALEN);
  855. if (fc & IEEE80211_FCTL_FROMDS)
  856. memcpy(hdr->h_source, desc->addr3, ETH_ALEN);
  857. else
  858. memcpy(hdr->h_source, desc->addr2, ETH_ALEN);
  859. skb->protocol = eth_type_trans(skb, dev);
  860. skb->ip_summed = CHECKSUM_NONE;
  861. if (fc & IEEE80211_FCTL_TODS)
  862. skb->pkt_type = PACKET_OTHERHOST;
  863. /* Process the wireless stats if needed */
  864. orinoco_stat_gather(dev, skb, desc);
  865. /* Pass the packet to the networking stack */
  866. netif_rx(skb);
  867. stats->rx_packets++;
  868. stats->rx_bytes += length;
  869. return;
  870. drop:
  871. dev_kfree_skb(skb);
  872. stats->rx_errors++;
  873. stats->rx_dropped++;
  874. }
  875. static void orinoco_rx_isr_tasklet(unsigned long data)
  876. {
  877. struct orinoco_private *priv = (struct orinoco_private *) data;
  878. struct net_device *dev = priv->ndev;
  879. struct orinoco_rx_data *rx_data, *temp;
  880. struct hermes_rx_descriptor *desc;
  881. struct sk_buff *skb;
  882. unsigned long flags;
  883. /* orinoco_rx requires the driver lock, and we also need to
  884. * protect priv->rx_list, so just hold the lock over the
  885. * lot.
  886. *
  887. * If orinoco_lock fails, we've unplugged the card. In this
  888. * case just abort. */
  889. if (orinoco_lock(priv, &flags) != 0)
  890. return;
  891. /* extract desc and skb from queue */
  892. list_for_each_entry_safe(rx_data, temp, &priv->rx_list, list) {
  893. desc = rx_data->desc;
  894. skb = rx_data->skb;
  895. list_del(&rx_data->list);
  896. kfree(rx_data);
  897. orinoco_rx(dev, desc, skb);
  898. kfree(desc);
  899. }
  900. orinoco_unlock(priv, &flags);
  901. }
  902. /********************************************************************/
  903. /* Rx path (info frames) */
  904. /********************************************************************/
  905. static void print_linkstatus(struct net_device *dev, u16 status)
  906. {
  907. char *s;
  908. if (suppress_linkstatus)
  909. return;
  910. switch (status) {
  911. case HERMES_LINKSTATUS_NOT_CONNECTED:
  912. s = "Not Connected";
  913. break;
  914. case HERMES_LINKSTATUS_CONNECTED:
  915. s = "Connected";
  916. break;
  917. case HERMES_LINKSTATUS_DISCONNECTED:
  918. s = "Disconnected";
  919. break;
  920. case HERMES_LINKSTATUS_AP_CHANGE:
  921. s = "AP Changed";
  922. break;
  923. case HERMES_LINKSTATUS_AP_OUT_OF_RANGE:
  924. s = "AP Out of Range";
  925. break;
  926. case HERMES_LINKSTATUS_AP_IN_RANGE:
  927. s = "AP In Range";
  928. break;
  929. case HERMES_LINKSTATUS_ASSOC_FAILED:
  930. s = "Association Failed";
  931. break;
  932. default:
  933. s = "UNKNOWN";
  934. }
  935. printk(KERN_DEBUG "%s: New link status: %s (%04x)\n",
  936. dev->name, s, status);
  937. }
  938. /* Search scan results for requested BSSID, join it if found */
  939. static void orinoco_join_ap(struct work_struct *work)
  940. {
  941. struct orinoco_private *priv =
  942. container_of(work, struct orinoco_private, join_work);
  943. struct net_device *dev = priv->ndev;
  944. struct hermes *hw = &priv->hw;
  945. int err;
  946. unsigned long flags;
  947. struct join_req {
  948. u8 bssid[ETH_ALEN];
  949. __le16 channel;
  950. } __attribute__ ((packed)) req;
  951. const int atom_len = offsetof(struct prism2_scan_apinfo, atim);
  952. struct prism2_scan_apinfo *atom = NULL;
  953. int offset = 4;
  954. int found = 0;
  955. u8 *buf;
  956. u16 len;
  957. /* Allocate buffer for scan results */
  958. buf = kmalloc(MAX_SCAN_LEN, GFP_KERNEL);
  959. if (!buf)
  960. return;
  961. if (orinoco_lock(priv, &flags) != 0)
  962. goto fail_lock;
  963. /* Sanity checks in case user changed something in the meantime */
  964. if (!priv->bssid_fixed)
  965. goto out;
  966. if (strlen(priv->desired_essid) == 0)
  967. goto out;
  968. /* Read scan results from the firmware */
  969. err = hermes_read_ltv(hw, USER_BAP,
  970. HERMES_RID_SCANRESULTSTABLE,
  971. MAX_SCAN_LEN, &len, buf);
  972. if (err) {
  973. printk(KERN_ERR "%s: Cannot read scan results\n",
  974. dev->name);
  975. goto out;
  976. }
  977. len = HERMES_RECLEN_TO_BYTES(len);
  978. /* Go through the scan results looking for the channel of the AP
  979. * we were requested to join */
  980. for (; offset + atom_len <= len; offset += atom_len) {
  981. atom = (struct prism2_scan_apinfo *) (buf + offset);
  982. if (memcmp(&atom->bssid, priv->desired_bssid, ETH_ALEN) == 0) {
  983. found = 1;
  984. break;
  985. }
  986. }
  987. if (!found) {
  988. DEBUG(1, "%s: Requested AP not found in scan results\n",
  989. dev->name);
  990. goto out;
  991. }
  992. memcpy(req.bssid, priv->desired_bssid, ETH_ALEN);
  993. req.channel = atom->channel; /* both are little-endian */
  994. err = HERMES_WRITE_RECORD(hw, USER_BAP, HERMES_RID_CNFJOINREQUEST,
  995. &req);
  996. if (err)
  997. printk(KERN_ERR "%s: Error issuing join request\n", dev->name);
  998. out:
  999. orinoco_unlock(priv, &flags);
  1000. fail_lock:
  1001. kfree(buf);
  1002. }
  1003. /* Send new BSSID to userspace */
  1004. static void orinoco_send_bssid_wevent(struct orinoco_private *priv)
  1005. {
  1006. struct net_device *dev = priv->ndev;
  1007. struct hermes *hw = &priv->hw;
  1008. union iwreq_data wrqu;
  1009. int err;
  1010. err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTBSSID,
  1011. ETH_ALEN, NULL, wrqu.ap_addr.sa_data);
  1012. if (err != 0)
  1013. return;
  1014. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1015. /* Send event to user space */
  1016. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  1017. }
  1018. static void orinoco_send_assocreqie_wevent(struct orinoco_private *priv)
  1019. {
  1020. struct net_device *dev = priv->ndev;
  1021. struct hermes *hw = &priv->hw;
  1022. union iwreq_data wrqu;
  1023. int err;
  1024. u8 buf[88];
  1025. u8 *ie;
  1026. if (!priv->has_wpa)
  1027. return;
  1028. err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_REQ_INFO,
  1029. sizeof(buf), NULL, &buf);
  1030. if (err != 0)
  1031. return;
  1032. ie = orinoco_get_wpa_ie(buf, sizeof(buf));
  1033. if (ie) {
  1034. int rem = sizeof(buf) - (ie - &buf[0]);
  1035. wrqu.data.length = ie[1] + 2;
  1036. if (wrqu.data.length > rem)
  1037. wrqu.data.length = rem;
  1038. if (wrqu.data.length)
  1039. /* Send event to user space */
  1040. wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, ie);
  1041. }
  1042. }
  1043. static void orinoco_send_assocrespie_wevent(struct orinoco_private *priv)
  1044. {
  1045. struct net_device *dev = priv->ndev;
  1046. struct hermes *hw = &priv->hw;
  1047. union iwreq_data wrqu;
  1048. int err;
  1049. u8 buf[88]; /* TODO: verify max size or IW_GENERIC_IE_MAX */
  1050. u8 *ie;
  1051. if (!priv->has_wpa)
  1052. return;
  1053. err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENT_ASSOC_RESP_INFO,
  1054. sizeof(buf), NULL, &buf);
  1055. if (err != 0)
  1056. return;
  1057. ie = orinoco_get_wpa_ie(buf, sizeof(buf));
  1058. if (ie) {
  1059. int rem = sizeof(buf) - (ie - &buf[0]);
  1060. wrqu.data.length = ie[1] + 2;
  1061. if (wrqu.data.length > rem)
  1062. wrqu.data.length = rem;
  1063. if (wrqu.data.length)
  1064. /* Send event to user space */
  1065. wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, ie);
  1066. }
  1067. }
  1068. static void orinoco_send_wevents(struct work_struct *work)
  1069. {
  1070. struct orinoco_private *priv =
  1071. container_of(work, struct orinoco_private, wevent_work);
  1072. unsigned long flags;
  1073. if (orinoco_lock(priv, &flags) != 0)
  1074. return;
  1075. orinoco_send_assocreqie_wevent(priv);
  1076. orinoco_send_assocrespie_wevent(priv);
  1077. orinoco_send_bssid_wevent(priv);
  1078. orinoco_unlock(priv, &flags);
  1079. }
  1080. static void qbuf_scan(struct orinoco_private *priv, void *buf,
  1081. int len, int type)
  1082. {
  1083. struct orinoco_scan_data *sd;
  1084. unsigned long flags;
  1085. sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
  1086. sd->buf = buf;
  1087. sd->len = len;
  1088. sd->type = type;
  1089. spin_lock_irqsave(&priv->scan_lock, flags);
  1090. list_add_tail(&sd->list, &priv->scan_list);
  1091. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1092. schedule_work(&priv->process_scan);
  1093. }
  1094. static void qabort_scan(struct orinoco_private *priv)
  1095. {
  1096. struct orinoco_scan_data *sd;
  1097. unsigned long flags;
  1098. sd = kmalloc(sizeof(*sd), GFP_ATOMIC);
  1099. sd->len = -1; /* Abort */
  1100. spin_lock_irqsave(&priv->scan_lock, flags);
  1101. list_add_tail(&sd->list, &priv->scan_list);
  1102. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1103. schedule_work(&priv->process_scan);
  1104. }
  1105. static void orinoco_process_scan_results(struct work_struct *work)
  1106. {
  1107. struct orinoco_private *priv =
  1108. container_of(work, struct orinoco_private, process_scan);
  1109. struct orinoco_scan_data *sd, *temp;
  1110. unsigned long flags;
  1111. void *buf;
  1112. int len;
  1113. int type;
  1114. spin_lock_irqsave(&priv->scan_lock, flags);
  1115. list_for_each_entry_safe(sd, temp, &priv->scan_list, list) {
  1116. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1117. buf = sd->buf;
  1118. len = sd->len;
  1119. type = sd->type;
  1120. list_del(&sd->list);
  1121. kfree(sd);
  1122. if (len > 0) {
  1123. if (type == HERMES_INQ_CHANNELINFO)
  1124. orinoco_add_extscan_result(priv, buf, len);
  1125. else
  1126. orinoco_add_hostscan_results(priv, buf, len);
  1127. kfree(buf);
  1128. } else if (priv->scan_request) {
  1129. /* Either abort or complete the scan */
  1130. cfg80211_scan_done(priv->scan_request, (len < 0));
  1131. priv->scan_request = NULL;
  1132. }
  1133. spin_lock_irqsave(&priv->scan_lock, flags);
  1134. }
  1135. spin_unlock_irqrestore(&priv->scan_lock, flags);
  1136. }
  1137. static void __orinoco_ev_info(struct net_device *dev, hermes_t *hw)
  1138. {
  1139. struct orinoco_private *priv = ndev_priv(dev);
  1140. u16 infofid;
  1141. struct {
  1142. __le16 len;
  1143. __le16 type;
  1144. } __attribute__ ((packed)) info;
  1145. int len, type;
  1146. int err;
  1147. /* This is an answer to an INQUIRE command that we did earlier,
  1148. * or an information "event" generated by the card
  1149. * The controller return to us a pseudo frame containing
  1150. * the information in question - Jean II */
  1151. infofid = hermes_read_regn(hw, INFOFID);
  1152. /* Read the info frame header - don't try too hard */
  1153. err = hermes_bap_pread(hw, IRQ_BAP, &info, sizeof(info),
  1154. infofid, 0);
  1155. if (err) {
  1156. printk(KERN_ERR "%s: error %d reading info frame. "
  1157. "Frame dropped.\n", dev->name, err);
  1158. return;
  1159. }
  1160. len = HERMES_RECLEN_TO_BYTES(le16_to_cpu(info.len));
  1161. type = le16_to_cpu(info.type);
  1162. switch (type) {
  1163. case HERMES_INQ_TALLIES: {
  1164. struct hermes_tallies_frame tallies;
  1165. struct iw_statistics *wstats = &priv->wstats;
  1166. if (len > sizeof(tallies)) {
  1167. printk(KERN_WARNING "%s: Tallies frame too long (%d bytes)\n",
  1168. dev->name, len);
  1169. len = sizeof(tallies);
  1170. }
  1171. err = hermes_bap_pread(hw, IRQ_BAP, &tallies, len,
  1172. infofid, sizeof(info));
  1173. if (err)
  1174. break;
  1175. /* Increment our various counters */
  1176. /* wstats->discard.nwid - no wrong BSSID stuff */
  1177. wstats->discard.code +=
  1178. le16_to_cpu(tallies.RxWEPUndecryptable);
  1179. if (len == sizeof(tallies))
  1180. wstats->discard.code +=
  1181. le16_to_cpu(tallies.RxDiscards_WEPICVError) +
  1182. le16_to_cpu(tallies.RxDiscards_WEPExcluded);
  1183. wstats->discard.misc +=
  1184. le16_to_cpu(tallies.TxDiscardsWrongSA);
  1185. wstats->discard.fragment +=
  1186. le16_to_cpu(tallies.RxMsgInBadMsgFragments);
  1187. wstats->discard.retries +=
  1188. le16_to_cpu(tallies.TxRetryLimitExceeded);
  1189. /* wstats->miss.beacon - no match */
  1190. }
  1191. break;
  1192. case HERMES_INQ_LINKSTATUS: {
  1193. struct hermes_linkstatus linkstatus;
  1194. u16 newstatus;
  1195. int connected;
  1196. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  1197. break;
  1198. if (len != sizeof(linkstatus)) {
  1199. printk(KERN_WARNING "%s: Unexpected size for linkstatus frame (%d bytes)\n",
  1200. dev->name, len);
  1201. break;
  1202. }
  1203. err = hermes_bap_pread(hw, IRQ_BAP, &linkstatus, len,
  1204. infofid, sizeof(info));
  1205. if (err)
  1206. break;
  1207. newstatus = le16_to_cpu(linkstatus.linkstatus);
  1208. /* Symbol firmware uses "out of range" to signal that
  1209. * the hostscan frame can be requested. */
  1210. if (newstatus == HERMES_LINKSTATUS_AP_OUT_OF_RANGE &&
  1211. priv->firmware_type == FIRMWARE_TYPE_SYMBOL &&
  1212. priv->has_hostscan && priv->scan_request) {
  1213. hermes_inquire(hw, HERMES_INQ_HOSTSCAN_SYMBOL);
  1214. break;
  1215. }
  1216. connected = (newstatus == HERMES_LINKSTATUS_CONNECTED)
  1217. || (newstatus == HERMES_LINKSTATUS_AP_CHANGE)
  1218. || (newstatus == HERMES_LINKSTATUS_AP_IN_RANGE);
  1219. if (connected)
  1220. netif_carrier_on(dev);
  1221. else if (!ignore_disconnect)
  1222. netif_carrier_off(dev);
  1223. if (newstatus != priv->last_linkstatus) {
  1224. priv->last_linkstatus = newstatus;
  1225. print_linkstatus(dev, newstatus);
  1226. /* The info frame contains only one word which is the
  1227. * status (see hermes.h). The status is pretty boring
  1228. * in itself, that's why we export the new BSSID...
  1229. * Jean II */
  1230. schedule_work(&priv->wevent_work);
  1231. }
  1232. }
  1233. break;
  1234. case HERMES_INQ_SCAN:
  1235. if (!priv->scan_request && priv->bssid_fixed &&
  1236. priv->firmware_type == FIRMWARE_TYPE_INTERSIL) {
  1237. schedule_work(&priv->join_work);
  1238. break;
  1239. }
  1240. /* fall through */
  1241. case HERMES_INQ_HOSTSCAN:
  1242. case HERMES_INQ_HOSTSCAN_SYMBOL: {
  1243. /* Result of a scanning. Contains information about
  1244. * cells in the vicinity - Jean II */
  1245. unsigned char *buf;
  1246. /* Sanity check */
  1247. if (len > 4096) {
  1248. printk(KERN_WARNING "%s: Scan results too large (%d bytes)\n",
  1249. dev->name, len);
  1250. qabort_scan(priv);
  1251. break;
  1252. }
  1253. /* Allocate buffer for results */
  1254. buf = kmalloc(len, GFP_ATOMIC);
  1255. if (buf == NULL) {
  1256. /* No memory, so can't printk()... */
  1257. qabort_scan(priv);
  1258. break;
  1259. }
  1260. /* Read scan data */
  1261. err = hermes_bap_pread(hw, IRQ_BAP, (void *) buf, len,
  1262. infofid, sizeof(info));
  1263. if (err) {
  1264. kfree(buf);
  1265. qabort_scan(priv);
  1266. break;
  1267. }
  1268. #ifdef ORINOCO_DEBUG
  1269. {
  1270. int i;
  1271. printk(KERN_DEBUG "Scan result [%02X", buf[0]);
  1272. for (i = 1; i < (len * 2); i++)
  1273. printk(":%02X", buf[i]);
  1274. printk("]\n");
  1275. }
  1276. #endif /* ORINOCO_DEBUG */
  1277. qbuf_scan(priv, buf, len, type);
  1278. }
  1279. break;
  1280. case HERMES_INQ_CHANNELINFO:
  1281. {
  1282. struct agere_ext_scan_info *bss;
  1283. if (!priv->scan_request) {
  1284. printk(KERN_DEBUG "%s: Got chaninfo without scan, "
  1285. "len=%d\n", dev->name, len);
  1286. break;
  1287. }
  1288. /* An empty result indicates that the scan is complete */
  1289. if (len == 0) {
  1290. qbuf_scan(priv, NULL, len, type);
  1291. break;
  1292. }
  1293. /* Sanity check */
  1294. else if (len < (offsetof(struct agere_ext_scan_info,
  1295. data) + 2)) {
  1296. /* Drop this result now so we don't have to
  1297. * keep checking later */
  1298. printk(KERN_WARNING
  1299. "%s: Ext scan results too short (%d bytes)\n",
  1300. dev->name, len);
  1301. break;
  1302. }
  1303. bss = kmalloc(len, GFP_ATOMIC);
  1304. if (bss == NULL)
  1305. break;
  1306. /* Read scan data */
  1307. err = hermes_bap_pread(hw, IRQ_BAP, (void *) bss, len,
  1308. infofid, sizeof(info));
  1309. if (err)
  1310. kfree(bss);
  1311. else
  1312. qbuf_scan(priv, bss, len, type);
  1313. break;
  1314. }
  1315. case HERMES_INQ_SEC_STAT_AGERE:
  1316. /* Security status (Agere specific) */
  1317. /* Ignore this frame for now */
  1318. if (priv->firmware_type == FIRMWARE_TYPE_AGERE)
  1319. break;
  1320. /* fall through */
  1321. default:
  1322. printk(KERN_DEBUG "%s: Unknown information frame received: "
  1323. "type 0x%04x, length %d\n", dev->name, type, len);
  1324. /* We don't actually do anything about it */
  1325. break;
  1326. }
  1327. return;
  1328. }
  1329. static void __orinoco_ev_infdrop(struct net_device *dev, hermes_t *hw)
  1330. {
  1331. if (net_ratelimit())
  1332. printk(KERN_DEBUG "%s: Information frame lost.\n", dev->name);
  1333. }
  1334. /********************************************************************/
  1335. /* Internal hardware control routines */
  1336. /********************************************************************/
  1337. static int __orinoco_up(struct orinoco_private *priv)
  1338. {
  1339. struct net_device *dev = priv->ndev;
  1340. struct hermes *hw = &priv->hw;
  1341. int err;
  1342. netif_carrier_off(dev); /* just to make sure */
  1343. err = __orinoco_commit(priv);
  1344. if (err) {
  1345. printk(KERN_ERR "%s: Error %d configuring card\n",
  1346. dev->name, err);
  1347. return err;
  1348. }
  1349. /* Fire things up again */
  1350. hermes_set_irqmask(hw, ORINOCO_INTEN);
  1351. err = hermes_enable_port(hw, 0);
  1352. if (err) {
  1353. printk(KERN_ERR "%s: Error %d enabling MAC port\n",
  1354. dev->name, err);
  1355. return err;
  1356. }
  1357. netif_start_queue(dev);
  1358. return 0;
  1359. }
  1360. static int __orinoco_down(struct orinoco_private *priv)
  1361. {
  1362. struct net_device *dev = priv->ndev;
  1363. struct hermes *hw = &priv->hw;
  1364. int err;
  1365. netif_stop_queue(dev);
  1366. if (!priv->hw_unavailable) {
  1367. if (!priv->broken_disableport) {
  1368. err = hermes_disable_port(hw, 0);
  1369. if (err) {
  1370. /* Some firmwares (e.g. Intersil 1.3.x) seem
  1371. * to have problems disabling the port, oh
  1372. * well, too bad. */
  1373. printk(KERN_WARNING "%s: Error %d disabling MAC port\n",
  1374. dev->name, err);
  1375. priv->broken_disableport = 1;
  1376. }
  1377. }
  1378. hermes_set_irqmask(hw, 0);
  1379. hermes_write_regn(hw, EVACK, 0xffff);
  1380. }
  1381. /* firmware will have to reassociate */
  1382. netif_carrier_off(dev);
  1383. priv->last_linkstatus = 0xffff;
  1384. return 0;
  1385. }
  1386. static int orinoco_reinit_firmware(struct orinoco_private *priv)
  1387. {
  1388. struct hermes *hw = &priv->hw;
  1389. int err;
  1390. err = hermes_init(hw);
  1391. if (priv->do_fw_download && !err) {
  1392. err = orinoco_download(priv);
  1393. if (err)
  1394. priv->do_fw_download = 0;
  1395. }
  1396. if (!err)
  1397. err = orinoco_hw_allocate_fid(priv);
  1398. return err;
  1399. }
  1400. static int
  1401. __orinoco_set_multicast_list(struct net_device *dev)
  1402. {
  1403. struct orinoco_private *priv = ndev_priv(dev);
  1404. int err = 0;
  1405. int promisc, mc_count;
  1406. /* The Hermes doesn't seem to have an allmulti mode, so we go
  1407. * into promiscuous mode and let the upper levels deal. */
  1408. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  1409. (netdev_mc_count(dev) > MAX_MULTICAST(priv))) {
  1410. promisc = 1;
  1411. mc_count = 0;
  1412. } else {
  1413. promisc = 0;
  1414. mc_count = netdev_mc_count(dev);
  1415. }
  1416. err = __orinoco_hw_set_multicast_list(priv, dev, mc_count, 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);