main.c 63 KB

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