main.c 63 KB

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