[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #8:

CRC32 helper code. Sorry if there is already some global function that could
be used for this purpose. Simply took the code from Wireshark (license and
header documentation unchanged obviously).


diff -Naur qemu/hw/atheros_wlan_crc32.c qemu-altered/hw/atheros_wlan_crc32.c
--- qemu/hw/atheros_wlan_crc32.c1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan_crc32.c2008-03-01 12:33:11.0 
+0100
@@ -0,0 +1,238 @@
+/* crc32.c
+ * CRC-32 routine
+ *
+ * $Id: crc32.c 18197 2006-05-21 05:12:17Z sahlberg $
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <[EMAIL PROTECTED]>
+ * Copyright 1998 Gerald Combs
+ *
+ * Copied from README.developer
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Credits:
+ *
+ * Table from Solomon Peachy
+ * Routine from Chris Waters
+ */
+
+#include "atheros_wlan_crc32.h"
+
+/*
+ * Table for the AUTODIN/HDLC/802.x CRC.
+ *
+ * Polynomial is
+ *
+ *  x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^8 + x^7 +
+ *  x^5 + x^4 + x^2 + x + 1
+ */
+const guint32 crc32_ccitt_table[256] = {
+0x, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
+0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
+0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
+0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
+0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
+0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
+0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
+0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
+0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
+0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
+0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
+0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
+0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
+0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
+0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
+0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
+0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
+0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
+0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
+0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
+0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
+0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
+0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
+0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
+0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
+0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
+0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
+0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
+0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
+0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
+0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
+0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
+0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
+0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
+0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
+0x4969474d, 0x3e6e77db, 0xaed16a4a, 

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #6:

802.11 packet definitions. Code to create beacon frames, probe responses, 
authentication frames, association replies, etc.


diff -Naur qemu/hw/atheros_wlan_packet.c qemu-altered/hw/atheros_wlan_packet.c
--- qemu/hw/atheros_wlan_packet.c   1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan_packet.c   2008-03-01 12:33:11.0 
+0100
@@ -0,0 +1,481 @@
+/**
+ * QEMU WLAN access point emulation
+ * 
+ * Copyright (c) 2008 Clemens Kolbitsch
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Modifications:
+ *  2008-February-24  Clemens Kolbitsch :
+ *  New implementation based on ne2000.c
+ *
+ */
+
+
+#include "hw.h"
+#include "pci.h"
+#include "pc.h"
+#include "net.h"
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+
+#include "hw/atheros_wlan.h"
+#include "hw/atheros_wlan_crc32.h"
+#include "hw/atheros_wlan_packet.h"
+
+#define FRAME_INSERT(_8bit_data)   buf[i++] = _8bit_data
+
+
+static int insertCRC(struct mac80211_frame *frame, uint32_t frame_length)
+{
+   unsigned long crc;
+   unsigned char *fcs = (char*)frame;
+
+   crc = crc32_ccitt(fcs, frame_length);
+   memcpy(&fcs[frame_length], &crc, 4);
+
+   return frame_length + 4;
+}
+
+
+void Atheros_WLAN_init_frame(Atheros_WLANState *s, struct mac80211_frame 
*frame)
+{
+   if (!frame)
+   return;
+
+   frame->sequence_control.sequence_number = s->inject_sequence_number++;
+   memcpy(frame->source_address, s->ap_macaddr, 6);
+   memcpy(frame->bssid_address, s->ap_macaddr, 6);
+
+   frame->frame_length = insertCRC(frame, frame->frame_length);
+}
+
+
+struct mac80211_frame *Atheros_WLAN_create_beacon_frame()
+{
+   unsigned int i;
+   unsigned char *buf;
+   struct mac80211_frame *frame;
+
+   frame = (struct mac80211_frame *)malloc(sizeof(struct mac80211_frame));
+   if (!frame)
+   {
+   return NULL;
+   }
+
+   frame->next_frame = NULL;
+   frame->frame_control.protocol_version = 0;
+   frame->frame_control.type = IEEE80211_TYPE_MGT;
+   frame->frame_control.sub_type = IEEE80211_TYPE_MGT_SUBTYPE_BEACON;
+   frame->frame_control.flags = 0;
+   frame->duration_id = 0;
+   frame->sequence_control.fragment_number = 0;
+
+   for (i=0; i<6; frame->destination_address[i] = 0xff, i++);
+
+   i = 0;
+   buf = (unsigned char*)frame->data_and_fcs;
+
+   /*
+* Fixed params... typical AP params (12 byte)
+*
+* They include
+*  - Timestamp
+*  - Beacon Interval
+*  - Capability Information
+*/
+   FRAME_INSERT(0x8d);
+   FRAME_INSERT(0x61);
+   FRAME_INSERT(0xa5);
+   FRAME_INSERT(0x18);
+   FRAME_INSERT(0x00);
+   FRAME_INSERT(0x00);
+   FRAME_INSERT(0x00);
+   FRAME_INSERT(0x00);
+   FRAME_INSERT(0x64);
+   FRAME_INSERT(0x00);
+   FRAME_INSERT(0x01);
+   FRAME_INSERT(0x00);
+
+   FRAME_INSERT(IEEE80211_BEACON_PARAM_SSID);
+   FRAME_INSERT(4);// length
+   FRAME_INSERT('Q');  // SSID
+   FRAME_INSERT('L');  // SSID
+   FRAME_INSERT('a');  // SSID
+   FRAME_INSERT('n');  // SSID
+
+   FRAME_INSERT(IEEE80211_BEACON_PARAM_RATES);
+   FRAME_INSERT(8);// length
+   FRAME_INSERT(0x82);
+   FRAME_INSERT(0x84);
+   FRAME_INSERT(0x8b);
+   FRAME_INSERT(0x96);
+   FRAME_INSERT(0x24);
+   FRAME_INSERT(0x30);
+   FRAME_INSERT(0x48);
+   FRAME_INSERT(0x6c);
+
+   FRAME_INSERT(IEEE80211_BEACON_PARAM_CHANNEL);
+   FRAME_INSERT(1);// length
+   FRAME_INSERT(0x09);
+
+   frame->frame_length = IEEE80211_HEADER_SIZE + i;
+   return frame;
+}
+
+struct mac80211_frame *Atheros_WLAN_create_probe_response()
+{
+  

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #5:

The most important/difficult/ugly part: Hardware I/O. It is handled over a 
direclty
mapped memory regions that is written to/read from the device driver code. Based
on ath5k and plenty of reverse engineering.

NOTE: I did NOT reimplement the hardware!! I simply handled the events as they 
come
in and mess with the device memory to fake a real device. Thus the code is 
quite hard to
understand. Sorry *g*




diff -Naur qemu/hw/atheros_wlan_io.c qemu-altered/hw/atheros_wlan_io.c
--- qemu/hw/atheros_wlan_io.c   1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan_io.c   2008-03-01 12:33:11.0 +0100
@@ -0,0 +1,1188 @@
+/**
+ * QEMU WLAN device emulation
+ * 
+ * Copyright (c) 2008 Clemens Kolbitsch
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Modifications:
+ *  2008-February-24  Clemens Kolbitsch :
+ *  New implementation based on ne2000.c
+ *
+ */
+
+
+#include "hw.h"
+#include "pci.h"
+#include "pc.h"
+#include "net.h"
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+#include "hw/atheros_wlan.h"
+#include "hw/atheros_wlan_ap.h"
+
+
+/*
+ * MadWifi OPENHAL atheros constants
+ */
+#include "hw/ath5k.h"
+#include "hw/ath5k_hw.h"
+#include "hw/ath5kreg.h"
+
+
+static const struct Atheros_WLAN_frequency Atheros_WLAN_frequency_data[] =
+   {
+   { 20689, 3077, 2412 },  // channel 1
+   { 20715, 3078, 2417 },  // channel 2
+   { 20689, 3079, 2422 },  // channel 3
+   { 20715, 3079, 2427 },  // channel 4
+   { 20529, 3076, 2432 },  // channel 5
+   { 20507, 3078, 2437 },  // channel 6
+   { 20529, 3078, 2442 },  // channel 7
+   { 20507, 3079, 2447 },  // channel 8
+   { 20529, 3077, 2452 },  // channel 9
+   { 20635, 3078, 2457 },  // channel 10
+   { 20529, 3079, 2462 },  // channel 11
+   { 20635, 3079, 2467 },  // channel 12
+   { 20657, 3076, 2472 },  // channel 13
+   { 20529, 1029, 2484 }   // channel 14
+   };
+
+/*
+ * NOTE: By using this function instead
+ * of accessing the array directly through
+ * an index, we can leave out parts of the
+ * EEPROM data!!
+ */
+static int get_eeprom_data(Atheros_WLANState *s, uint32_t addr, uint32_t *val)
+{
+   if (val == NULL)
+   {
+   return 1;
+   }
+
+   // why?? but seems necessary...
+   addr--;
+
+   if ((addr < 0) || (addr > s->eeprom_size))
+   {
+   return 2;
+   }
+
+   *val = s->eeprom_data[addr];
+   return 0;
+}
+
+
+
+
+
+
+void updateFrequency(Atheros_WLANState *s)
+{
+   int i;
+   u_int32_t new_frequency = 0;
+   for (i=0; i < sizeof(Atheros_WLAN_frequency_data) / 
sizeof(Atheros_WLAN_frequency_data[0]); i++)
+   {
+   if (Atheros_WLAN_frequency_data[i].value1 != 
s->current_frequency_partial_data[0])
+   continue;
+   
+   if (Atheros_WLAN_frequency_data[i].value2 != 
s->current_frequency_partial_data[1])
+   continue;
+
+   new_frequency = Atheros_WLAN_frequency_data[i].frequency;
+   break;
+   }
+
+   if (new_frequency)
+   {
+   s->current_frequency = new_frequency;
+   }
+}
+
+
+
+static uint32_t mm_readl(Atheros_WLANState *s, target_phys_addr_t addr);
+static void mm_writel(Atheros_WLANState *s, target_phys_addr_t addr, uint32_t 
val);
+
+static void Atheros_WLAN_mmio_writeb(void *opaque, target_phys_addr_t addr, 
uint32_t val)
+{
+   DEBUG_PRINT(("!!! DEBUG INIMPLEMENTED !!!\n"));
+   DEBUG_PRINT(

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #4:

Virtual access point code. It is based on a message queue and a timer that 
repeatedly injects the ingoing messages and replies into the queue. Outgoing 
messages are parsed to extract the IP-datagram from the wireless packet and 
passed on to Qemu.

There are 3 important functions here:
 - Atheros_WLAN_handle_frame, is the AP state machine. Really simplified but 
sufficient ;-)

 - Atheros_WLAN_handleRxBuffer, inject a packet into the guest driver

 - Atheros_WLAN_handleTxBuffer, extract a packet from the guest driver



diff -Naur qemu/hw/atheros_wlan_ap.c qemu-altered/hw/atheros_wlan_ap.c
--- qemu/hw/atheros_wlan_ap.c   1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan_ap.c   2008-03-01 12:33:11.0 +0100
@@ -0,0 +1,771 @@
+/**
+ * QEMU WLAN access point emulation
+ * 
+ * Copyright (c) 2008 Clemens Kolbitsch
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a 
copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the 
rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Modifications:
+ *  2008-February-24  Clemens Kolbitsch :
+ *  New implementation based on ne2000.c
+ *
+ */
+
+
+#include "hw.h"
+#include "pci.h"
+#include "pc.h"
+#include "net.h"
+#include "qemu-timer.h"
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+/*
+ * PCI and EEPROM definitions
+ */
+#include "hw/atheros_wlan.h"
+#include "hw/atheros_wlan_ap.h"
+#include "hw/atheros_wlan_io.h"
+#include "hw/atheros_wlan_packet.h"
+
+/*
+ * MadWifi OPENHAL atheros constants
+ */
+#include "hw/ath5k_hw.h"
+#include "hw/ath5kreg.h"
+#include "hw/ath5k.h"
+
+static int semaphore_helper(int semaphore, int sem_op, int semaphore_nr, 
short flags)
+{
+   struct sembuf semp;
+   semp.sem_num = semaphore_nr;
+   semp.sem_op = sem_op;
+   semp.sem_flg = flags;
+
+   if (semaphore == -1)
+   {
+   /*
+* We don't have a semaphore... probably not
+* that bad, but having one is better :-)
+*/
+   return -1;
+   }
+
+   int ret;
+   while ((ret = semop(semaphore, &semp, 1)) < 0)
+   {
+   if (errno == EAGAIN && flags == IPC_NOWAIT)
+   {
+   return errno;
+   }
+   else if (errno != EINTR)
+   {
+   fprintf(stderr, "Semaphore error: 0x%x / %u\n", errno, 
errno);
+   return errno;
+   }
+   }
+
+   return ret;
+}
+
+
+static int signal_semaphore(int semaphore, int semaphore_nr)
+{
+   return semaphore_helper(semaphore, 1, semaphore_nr, 0);
+}
+static int wait_semaphore(int semaphore, int semaphore_nr)
+{
+   return semaphore_helper(semaphore, -1, semaphore_nr, 0);
+}
+
+void Atheros_WLAN_insert_frame(Atheros_WLANState *s, struct mac80211_frame 
*frame)
+{
+   struct mac80211_frame *i_frame;
+
+   wait_semaphore(s->access_semaphore, 0);
+
+   s->inject_queue_size++;
+   i_frame = s->inject_queue;
+   if (!i_frame)
+   {
+   s->inject_queue = frame;
+   }
+   else
+   {
+   while (i_frame->next_frame)
+   {
+   i_frame = i_frame->next_frame;
+   }
+
+   i_frame->next_frame = frame;
+   }
+
+   if (!s->inject_timer_running)
+   {
+   // if the injection timer is not
+   // running currently, let's schedule
+   // one run...
+   s->inject_timer_running = 1;
+   qemu_mod_timer(s->inject_timer, qemu_get_clock(rt_clock) + 5);
+   }
+
+   signal_semaphore(s->access_semaphore, 0);
+}
+
+static void Atheros_WLAN_beacon_timer(void *opaque)
+{
+   struct mac80211_frame *frame;
+   Atheros_WLANState *s = (Atheros_WLANState *)opaque;
+
+   frame = Atheros_WLAN_create_beacon_fra

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #3:

Setup code definitions. The 802.11 data structures, device memory, and 
debugging macros.



diff -Naur qemu/hw/atheros_wlan.h qemu-altered/hw/atheros_wlan.h
--- qemu/hw/atheros_wlan.h  1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan.h  2008-03-01 12:33:11.0 +0100
@@ -0,0 +1,345 @@
+/**
+ * QEMU WLAN device emulation
+ * 
+ * Copyright (c) 2008 Clemens Kolbitsch
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a 
copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the 
rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Modifications:
+ *  2008-February-24  Clemens Kolbitsch :
+ *  New implementation based on ne2000.c
+ *
+ */
+
+#ifndef atheros_wlan_h
+#define atheros_wlan_h 1
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+
+/*
+ * debug Atheros_WLAN card
+ *
+ * i.e. show all access traces
+ */
+// #define DEBUG_Atheros_WLAN 1
+// #define DEBUG_Atheros_AP_WLAN 1
+
+#define PCI_FREQUENCY 3300L
+
+#if defined (DEBUG_Atheros_WLAN)
+#  define DEBUG_PRINT(x) do { struct timeval __tt; gettimeofday(&__tt, NULL); 
printf("%u:%u  ", __tt.tv_sec, __tt.tv_usec); printf x ; } while (0)
+#else
+#  define DEBUG_PRINT(x)
+#endif
+
+#if defined (DEBUG_Atheros_AP_WLAN)
+#  define DEBUG_PRINT_AP(x) printf x ;
+#else
+#  define DEBUG_PRINT_AP(x)
+#endif
+
+
+
+/*
+ * The madwifi driver crashes if too
+ * many frames are in the receive
+ * queue linked list
+ *
+ * This can happen when interrupts are
+ * not picked up right away (what can
+ * happen due to qemu's lazy interrupt
+ * checking/handling)!!
+ *
+ * UPDATE: BinaryHAL suddenly seems to
+ * work with the WINDOWS_RX_FRAME as well
+ * which is even better (because more frames
+ * may be received concurrently...)
+ */
+#define MAX_CONCURRENT_RX_FRAMES_WINDOWS_OR_OPEN_HAL   999
+#define MAX_CONCURRENT_RX_FRAMES_BINARY_HAL10
+#define MAX_CONCURRENT_RX_FRAMES   
MAX_CONCURRENT_RX_FRAMES_WINDOWS_OR_OPEN_HAL
+
+/*
+ * In case we are connecting with a windows guest OS
+ * (or the ndiswrapper of the windows driver) we must
+ * define this macro... otherwise no packets will be
+ * received.
+ *
+ * If connecting with a linux guest/madwifi with the
+ * macro defined it won't work on the other hand!!!
+ */
+#define WINXP_DRIVER   1
+#define LINUX_DRIVER   2
+
+#define PCI_CONFIG_AR5212  1
+#define PCI_CONFIG_AR5424  2
+
+
+
+
+
+#defineIEEE80211_IDLE  0xff
+
+#defineIEEE80211_TYPE_MGT  0x00
+#defineIEEE80211_TYPE_CTL  0x01
+#defineIEEE80211_TYPE_DATA 0x02
+
+#defineIEEE80211_TYPE_MGT_SUBTYPE_BEACON   0x08
+#defineIEEE80211_TYPE_MGT_SUBTYPE_ACTION   0x0d
+#defineIEEE80211_TYPE_MGT_SUBTYPE_PROBE_REQ0x04
+#defineIEEE80211_TYPE_MGT_SUBTYPE_PROBE_RESP   0x05
+#defineIEEE80211_TYPE_MGT_SUBTYPE_AUTHENTICATION   0x0b
+#defineIEEE80211_TYPE_MGT_SUBTYPE_DEAUTHENTICATION 0x0c
+#defineIEEE80211_TYPE_MGT_SUBTYPE_ASSOCIATION_REQ  0x00
+#defineIEEE80211_TYPE_MGT_SUBTYPE_ASSOCIATION_RESP 0x01
+#defineIEEE80211_TYPE_MGT_SUBTYPE_DISASSOCIATION   0x09
+
+#defineIEEE80211_TYPE_CTL_SUBTYPE_ACK  0x0d
+
+#defineIEEE80211_TYPE_DATA_SUBTYPE_DATA0x00
+
+
+#defineIEEE80211_BEACON_PARAM_SSID 0x00
+#defineIEEE80211_BEACON_PARAM_SSID_STRING  "\x00"
+#defineIEEE80211_BEACON_PARAM_RATES0x01
+#defineIEEE80211_BEACON_PARAM_RATES_STRING "\x01"
+#defineIEEE80211_BEACON_PARAM_CHANNEL  0x03
+#defineIEEE80211_BEACON_PARAM_CHANNEL_STRING   "\x03"
+#defineIEEE80211_BEACON_PARAM_EXTENDED_RATE

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #2:

Setup code for the device




diff -Naur qemu/hw/atheros_wlan.c qemu-altered/hw/atheros_wlan.c
--- qemu/hw/atheros_wlan.c  1970-01-01 01:00:00.0 +0100
+++ qemu-altered/hw/atheros_wlan.c  2008-03-01 12:33:11.0 +0100
@@ -0,0 +1,348 @@
+/**
+ * QEMU WLAN device emulation
+ * 
+ * Copyright (c) 2008 Clemens Kolbitsch
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a 
copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the 
rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Modifications:
+ *  2008-February-24  Clemens Kolbitsch :
+ *  New implementation based on ne2000.c
+ *
+ */
+
+#include "hw.h"
+#include "pci.h"
+#include "pc.h"
+#include "net.h"
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+/*
+ * PCI and EEPROM definitions
+ */
+#include "hw/atheros_wlan.h"
+#include "hw/atheros_wlan_io.h"
+#include "hw/atheros_wlan_ap.h"
+#include "hw/atheros_wlan_eeprom.h"
+
+/*
+ * MadWifi OPENHAL atheros constants
+ */
+#include "hw/ath5k_hw.h"
+#include "hw/ath5kreg.h"
+#include "hw/ath5k.h"
+
+
+static void Atheros_WLAN_reset(NICInfo *nd, Atheros_WLANState *s)
+{
+   DEBUG_PRINT(("reset\n"));
+
+   /*
+* Restore mac address
+*/
+   memcpy(s->macaddr, nd->macaddr, 6);
+
+   /*
+* data from my local AR5212 device
+*/
+   SET_MEM_L(s->mem, 12, 0);
+   SET_MEM_L(s->mem, AR5K_SREV, 86);
+   SET_MEM_L(s->mem, AR5K_PCICFG, 0x00010014);
+   SET_MEM_L(s->mem, AR5K_PHY_CHIP_ID, 65);
+   SET_MEM_L(s->mem, AR5K_SLEEP_CTL, 0x0001);
+   SET_MEM_L(s->mem, 0x9820, 0x02020200);
+
+   Atheros_WLAN_update_irq(s);
+}
+
+void Atheros_WLAN_setup_type(NICInfo *nd, PCIAtheros_WLANState *d)
+{
+   // create buffer large enough to
+   // do all checks
+   char *device_name;
+   char nd_model[128];
+   uint8_t *pci_conf;
+   Atheros_WLANState *s;
+
+   device_name = nd_model;
+   pci_conf = d->dev.config;
+   s = &d->Atheros_WLAN;
+
+   snprintf(nd_model, sizeof(nd_model), "%s", nd->model);
+   
+
+   // skip "atheros_wlan"
+   // if it had not been part of nd->model, this
+   // module would not be loaded anyways!!
+   device_name += 12;
+   DEBUG_PRINT_AP(("Loading virtual wlan-pci device...\n"));
+   if (strncmp(device_name, "_winxp", 6) == 0)
+   {
+   s->device_driver_type = WINXP_DRIVER;
+   DEBUG_PRINT_AP((" * Make sure you are using a MS Windows 
driver!!\n"));
+
+   // skip "_winxp"
+   device_name += 6;
+   }
+   else if (strncmp(device_name, "_linux", 6) == 0)
+   {
+   s->device_driver_type = LINUX_DRIVER;
+   DEBUG_PRINT_AP((" * Make sure you are using a MadWifi 
driver!!\n"));
+
+   // skip "_linux"
+   device_name += 6;
+   }
+   else
+   {
+   s->device_driver_type = LINUX_DRIVER;
+   DEBUG_PRINT_AP((" * Unknown driver type '%s'... defaulting to 
Linux... Make 
sure you are using a MadWifi driver!!\n", nd->model));
+   }
+
+   if (strncmp(device_name, "_HPW400", 7) == 0)
+   {
+   s->eeprom_data = (u_int32_t*)Atheros_WLAN_eeprom_data_HPW400;
+   s->eeprom_size = sizeof(Atheros_WLAN_eeprom_data_HPW400);
+
+   memcpy(pci_conf, Atheros_WLAN_pci_config_HPW400, 256);
+
+   DEBUG_PRINT_AP((" * Using EEPROM and device configuration of HP 
W400!!
\n"));
+
+   // skip "_HPW400"
+   device_name += 7;
+   }
+   else if (strncmp(device_name, "_MacBook", 8) == 0)
+   {
+   s->eeprom_data = (u_int32_t*)Atheros_WLAN_eeprom_data_MacBook;
+   s->eeprom_size = sizeof(Atheros_WLAN_eeprom_data_MacBook);
+
+   memcpy(pci_conf, Atheros_WLAN_pci_config_MacBook, 256);
+   
+

[Qemu-devel] Re: Re: Re: Atheros Wireless Device Emulation

2008-03-18 Thread Clemens Kolbitsch
Patch #1:

Necessary changes to Qemu itself... Makefile and hw/pci.c to include the 
device:



diff -Naur qemu/hw/pci.c qemu-altered/hw/pci.c
--- qemu/hw/pci.c   2008-02-03 03:20:18.0 +0100
+++ qemu-altered/hw/pci.c   2008-03-01 12:59:50.0 +0100
@@ -636,6 +636,8 @@
 pci_i82559er_init(bus, nd, devfn);
 } else if (strcmp(nd->model, "rtl8139") == 0) {
 pci_rtl8139_init(bus, nd, devfn);
+} else if (strncmp(nd->model, "atheros_wlan", 12) == 0) {
+pci_Atheros_WLAN_init(bus, nd, devfn);
 } else if (strcmp(nd->model, "e1000") == 0) {
 pci_e1000_init(bus, nd, devfn);
 } else if (strcmp(nd->model, "pcnet") == 0) {
diff -Naur qemu/hw/pci.h qemu-altered/hw/pci.h
--- qemu/hw/pci.h   2008-02-03 03:20:18.0 +0100
+++ qemu-altered/hw/pci.h   2008-03-01 13:00:20.0 +0100
@@ -126,6 +126,10 @@
 
 void pci_rtl8139_init(PCIBus *bus, NICInfo *nd, int devfn);
 
+/* atheros_wlan.c */
+
+void pci_Atheros_WLAN_init(PCIBus *bus, NICInfo *nd, int devfn);
+
 /* e1000.c */
 void pci_e1000_init(PCIBus *bus, NICInfo *nd, int devfn);
 
diff -Naur qemu/Makefile.target qemu-altered/Makefile.target
--- qemu/Makefile.target2008-02-27 18:53:27.0 +0100
+++ qemu-altered/Makefile.target2008-03-01 12:58:54.0 +0100
@@ -528,6 +528,8 @@
 OBJS += pcnet.o
 OBJS += rtl8139.o
 OBJS += e1000.o
+OBJS += atheros_wlan.o atheros_wlan_io.o atheros_wlan_ap.o
+OBJS += atheros_wlan_packet.o atheros_wlan_crc32.o
 
 ifeq ($(TARGET_BASE_ARCH), i386)
 # Hardware support