main.c 63 KB

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