main.c 62 KB

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