summaryrefslogtreecommitdiffstats
path: root/m168_firmware/zd1211.c
blob: b445ce535f548e6e2328ac8a1a9451adb5c94bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 *   Wi-Fi Detector opensource firmware
 *   ZD1211 through UART support
 *
 *   Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
 *
 *   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.
 */

#include "zd1211.h"
#include "util.h"
#include "lcd.h"
#include "main.h"
#include "errno.h"

#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>


/* Size of a scan data packet for one AP */
#define ZD1211_SCAN_PACK_SIZE		127

/* The packet data structure that we receive from the ZD1211 chip */
struct scan_packet {
	struct {
		uint8_t unknown;
		uint8_t channel;
		uint8_t rssi;
	} __attribute__((packed)) header;
	uint8_t __padding;
	uint8_t bssid[6];
	uint8_t seqctl[2];
	struct {
		uint8_t timestamp[8];
		uint8_t beaconint[2];
		uint8_t capab[2];
		uint8_t ie[0];
	} __attribute__((packed)) beacon;
} __attribute__((packed));

/* This is a hack to save RAM.
 * We don't need the LCD mem while receiving the data bits,
 * so we use it as temporary USART RX memory. */
struct usart_rx_data {
	/* The RX ring buffer */
#define USART_RX_RING_SIZE		90
	uint8_t rx_ring[USART_RX_RING_SIZE];
	int8_t rx_ring_first;
	int8_t rx_ring_last;
	uint8_t rx_ring_used;
	/* Error code from the RX IRQ handler */
	int8_t rx_irq_error;
	/* Buffer for the received struct scan_packet data */
#define USART_PACKET_BUFFER_SIZE	127
	uint8_t packet_buffer[USART_PACKET_BUFFER_SIZE];
};
#define get_rx_ring()		(((struct usart_rx_data *)lcd_buffer)->rx_ring)
#define get_rx_ring_first()	(&((struct usart_rx_data *)lcd_buffer)->rx_ring_first)
#define get_rx_ring_last()	(&((struct usart_rx_data *)lcd_buffer)->rx_ring_last)
#define get_rx_ring_used()	(&((struct usart_rx_data *)lcd_buffer)->rx_ring_used)
#define get_rx_irq_error()	(&((struct usart_rx_data *)lcd_buffer)->rx_irq_error)
#define get_packet_buffer()	(((struct usart_rx_data *)lcd_buffer)->packet_buffer)


/* Dynamic list for the scanned APs.
 * The data layout for this list is struct scanned_ap. */
#define AP_LIST_SIZE		300
uint8_t scanned_ap_list[AP_LIST_SIZE + 1];


static void usart_tx(uint8_t data)
{
	while (!(UCSR0A & (1 << UDRE0)))
		mb();
	UDR0 = data;
}

static int8_t usart_rx(uint8_t *data)
{
	uint8_t status;

	status = UCSR0A;
	if (!(status & (1 << RXC0)))
		return -ENODATA;
	if (unlikely(status & ((1 << FE0) | (1 << UPE0) | (1 << DOR0)))) {
		if (status & (1 << FE0))
			return -ERXFE;
		if (status & (1 << UPE0))
			return -ERXPE;
		if (status & (1 << DOR0))
			return -ERXOV;
	}
	*data = UDR0;

	return 0;
}

static void usart_init(void)
{
	uint8_t dummy;

	BUILD_BUG_ON(sizeof(struct usart_rx_data) > LCD_BUFFER_SIZE);
	lcd_clear_buffer();
	/* Set baud rate */
	UBRR0 = 0x000C;
	UCSR0A = (1 << U2X0);
	/* 8 Data bits, 1 Stop bit, No parity */
	UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);
	/* Enable transceiver and RX IRQs */
	UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
	/* Drain the RX buffer */
	while (usart_rx(&dummy) != -ENODATA)
		mb();
}

static void usart_exit(void)
{
	/* Disable transceiver */
	UCSR0B = 0;
	lcd_clear_buffer();
}

ISR(USART_RX_vect)
{
	uint8_t data = data;
	uint8_t *ringbuf;
	int8_t last;
	uint8_t used;
	int8_t err;

	ringbuf = get_rx_ring();
	while (1) {
		err = usart_rx(&data);
		if (err == -ENODATA)
			break;
		if (unlikely(err)) {
			*get_rx_irq_error() = err;
			break;
		}
		used = *get_rx_ring_used();
		if (unlikely(used == USART_RX_RING_SIZE)) {
			/* ring buffer overflow */
			*get_rx_irq_error() = -EOVERFLOW;
			break;
		}
		*get_rx_ring_used() = used + 1;
		last = *get_rx_ring_last();
		ringbuf[last] = data;
		last++;
		if (last == USART_RX_RING_SIZE - 1)
			last = 0;
		*get_rx_ring_last() = last;
	}
}

/* Returns the length of the IE payload (zero if not found)
 * and puts a pointer to the IE payload into ie_ptr */
static uint8_t ieee80211_find_ie(const struct scan_packet *p, uint8_t ie_id,
				 const uint8_t **ie_ptr)
{
	uint8_t nrbytes;
	const uint8_t *d;
	uint8_t id, len;
	uint8_t found_ie_length = 0;

	nrbytes = ZD1211_SCAN_PACK_SIZE - offsetof(struct scan_packet, beacon.ie);
	d = p->beacon.ie;

	while (1) {
		if (nrbytes < 2)
			break;
		id = d[0];
		len = d[1];
		d += 2;
		nrbytes -= 2;
		if (id == ie_id) {
			*ie_ptr = d;
			found_ie_length = min(len, nrbytes);
			break;
		}
		/* Go to the next IE */
		if (len > nrbytes)
			break;
		d += len;
		nrbytes -= len;
	}

	return found_ie_length;
}

/* Read the next byte from the RX ringbuffer.
 * IRQs must be enabled.
 * timeout is in milliseconds. */
static int8_t rxring_read_next(uint8_t *data, uint8_t timeout)
{
	const uint8_t *ringbuf;
	int8_t first;
	uint8_t used, i;
	int8_t err;

	ringbuf = get_rx_ring();
	while (1) {
		cli();
		mb();
		err = *get_rx_irq_error();
		if (unlikely(err)) {
			/* There was some error in the lowlevel
			 * RX IRQ handler */
			sei();
			return err;
		}
		used = *get_rx_ring_used();
		if (!used) {
			sei();
			if (timeout == 0)
				return -ETIMEDOUT;
			for (i = 0; i < 200; i++) {
				cli();
				mb();
				used = *get_rx_ring_used();
				sei();
				if (used)
					break;
				if (key_is_pressed(KEY_SCAN)) {
					/* User interrupt */
					key_debounce(KEY_SCAN, 5);
					return -EINTR;
				}
				udelay(5);
			}
			if (timeout != 0xFF)
				timeout--;
			continue;
		}
		*get_rx_ring_used() = used - 1;
		first = *get_rx_ring_first();
		*data = ringbuf[first];
		first++;
		if (first == USART_RX_RING_SIZE - 1)
			first = 0;
		*get_rx_ring_first() = first;
		sei();
		break;
	}

	return 0;
}

void zd1211_init(void)
{
	ZD1211_POWER_DDR |= ZD1211_POWER_BIT;
	ZD1211_POWER_PORT &= ~ZD1211_POWER_BIT;
}

static void zd1211_start_scan(void)
{
	usart_init();
	ZD1211_POWER_PORT |= ZD1211_POWER_BIT;
	/* I'm not sure why we send 0xEE to the ZD here... */
	usart_tx(0xEE);
}

static void zd1211_disable(void)
{
	ZD1211_POWER_PORT &= ~ZD1211_POWER_BIT;
	mdelay(100);
	usart_exit();
}

static int8_t zd1211_wait_for_packet(uint8_t timeout)
{
	uint8_t data;
	int8_t err;

	while (1) {
		err = rxring_read_next(&data, timeout);
		if (err)
			return err;
		if (data == 0xAA)
			break;
	}

	return 0;
}

/* Check if we already have this AP in our list of scanned APs. */
static bool is_duplicate(const struct scan_packet *packet)
{
	const struct scanned_ap *ap;

	for_each_ap(ap) {
		if (compare_ether_addr(ap->bssid, packet->bssid) == 0)
			return 1;
	}

	return 0;
}

static int8_t zd1211_parse_packet(const struct scan_packet *packet,
				  uint8_t **aplist)
{
	uint8_t ie_len;
	const uint8_t *ie_ptr;
	struct scanned_ap *ap;

	if (!is_valid_ap_ptr(*aplist))
		return -EOVERFLOW;
	ap = (struct scanned_ap *)(*aplist);
	ap->size = 0;

	if (is_duplicate(packet))
		return -EIGNORE;

	ie_len = ieee80211_find_ie(packet, 0 /* SSID */, &ie_ptr);
	if (ie_len) {
		/* Found an SSID IE.
		 * First truncate the SSID to a line in the LCD.
		 * (We don't need looong cryptic SSIDs, as we have the BSSID). */
		ie_len = min(ie_len, LCD_NR_CHARCOLUMNS);
		/* Check if we have enough buffer space. */
		if (is_valid_ap_ptr(*aplist + ie_len)) {
			/* Copy it into the APlist SSID field */
			memcpy(ap->ssid, ie_ptr, ie_len);
			ap->size += ie_len;
		}
	}
	ap->size += sizeof(struct scanned_ap);
	/* Copy the BSSID */
	memcpy(ap->bssid, packet->bssid, ETH_ALEN);
	/* Get the channel number */
	ie_len = ieee80211_find_ie(packet, 3 /* DS channel */, &ie_ptr);
	if (ie_len == 1)
		ap->flags_chan = *ie_ptr & AP_CHAN;
	else
		ap->flags_chan = packet->header.channel & AP_CHAN;
	/* The the RSN IE and extract crypto information from it. */
	ie_len = ieee80211_find_ie(packet, 48 /* RSN IE */, &ie_ptr);
	if (ie_len >= 12) {
		const uint8_t *pairwise, *group;
		uint8_t suite_type;

		/* We only support RSN IE version 1 */
		if ((ie_ptr[0] != 0x01) || (ie_ptr[1] != 0x00))
			goto rsn_unsupported;
		/* Get a pointer to the pairwise and group cipher suite. */
		group = ie_ptr + 2;
		pairwise = ie_ptr + 8;
		/* Check if this is a standard OUI. */
		if ((pairwise[0] != 0x00) ||
		    (pairwise[1] != 0x0F) ||
		    (pairwise[2] != 0xAC))
			goto rsn_unsupported;
		suite_type = pairwise[3];
		if (suite_type == 0) {
			/* Use group suite. */
			if ((group[0] != 0x00) ||
			    (group[1] != 0x0F) ||
			    (group[2] != 0xAC))
				goto rsn_unsupported;
			suite_type = group[3];
		}
		if ((suite_type != 1 /* WEP-40 */) &&
		    (suite_type != 5 /* WEP-104 */))
			ap->flags_chan |= AP_WPA;
    rsn_unsupported:;
	}
	/* Update flags */
	if (!(packet->beacon.capab[0] & 0x10))
		ap->flags_chan |= AP_OPEN_NET;
	if (packet->beacon.capab[0] & 0x02)
		ap->flags_chan |= AP_IS_IBSS;
	ap->rssi = packet->header.rssi;

	*aplist += ap->size;

	return 0;
}

static int16_t zd1211_read_scan_result(void)
{
	int8_t err;
	uint8_t i;
	uint8_t data;
	uint8_t *packet_buffer;
	uint8_t *aplist = scanned_ap_list;
	uint8_t nr_aps_found = 0;

	memset(aplist, 0, AP_LIST_SIZE);
	packet_buffer = get_packet_buffer();
	err = zd1211_wait_for_packet(0xFF);
	if (err)
		goto out;
	while (1) {
		/* We are receiving a scan result packet.
		 * Get and parse it. */
		for (i = 0; i < ZD1211_SCAN_PACK_SIZE; i++) {
			err = rxring_read_next(&data, 100);
			if (err)
				goto out;
			packet_buffer[i] = data;
		}
		err = zd1211_parse_packet((const struct scan_packet *)packet_buffer,
					  &aplist);
		if (err == -EOVERFLOW) {
			/* Scan list full. Ignore the rest (if any). */
			err = 0;
			goto out;
		}
		if (err && (err != -EIGNORE))
			goto out;
		if (err != -EIGNORE)
			nr_aps_found++;
		err = zd1211_wait_for_packet(200);
		if (err) {
			if (err == -ETIMEDOUT)
				err = 0;
			break;
		}
	}
out:
	BUG_ON(err > 0);

	return err ? err : nr_aps_found;
}

static uint8_t do_sort_aps(uint8_t cur_index, bool only_open)
{
	struct scanned_ap *ap, *best_ap;

	while (1) {
		best_ap = NULL;
		for_each_ap(ap) {
			if (only_open &&
			    !(ap->flags_chan & AP_OPEN_NET))
				continue;
			if (ap->flags_chan & __AP_SORTED)
				continue;
			if (!best_ap || (best_ap->rssi < ap->rssi))
				best_ap = ap;
		}
		if (!best_ap)
			break;
		best_ap->index = cur_index++;
		best_ap->flags_chan |= __AP_SORTED;
	}

	return cur_index;
}

/* Sort the scanned APs. First come open and best. */
static void zd1211_sort_aps(void)
{
	uint8_t cur_index;

	/* The best open networks will come first. */
	cur_index = do_sort_aps(0, 1);
	/* Closed networks will come last. */
	do_sort_aps(cur_index, 0);
}

int16_t zd1211_scan(void)
{
	int16_t nr_aps_found;

	zd1211_start_scan();
	nr_aps_found = zd1211_read_scan_result();
	zd1211_disable();
	zd1211_sort_aps();

	return nr_aps_found;
}

bool zd1211_usb_is_plugged_in(void)
{
	bool connected;

	/* Does not work while scanning. Assert this here. */
	BUG_ON(UCSR0B & (1 << TXEN0));
	/* If USART TXD pin is high, USB is connected. */
	DDRD &= ~(1 << 1);
	PORTD &= ~(1 << 1);
	connected = !!(PIND & (1 << 1));

	return connected;
}

const struct scanned_ap * zd1211_fetch_ap(uint8_t index)
{
	const struct scanned_ap *ap;

	for_each_ap(ap) {
		if (ap->index == index)
			return ap;
	}

	return NULL;
}
bues.ch cgit interface