main.c 63 KB

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