main.c 62 KB

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