main.c 63 KB

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