main.c 62 KB

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