Newer
Older
/*
* Driver for ZyDAS zd1201 based wireless USB devices.
*
* Copyright (c) 2004, 2005 Jeroen Vreeken (pe1rxq@amsat.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* Parts of this driver have been derived from a wlan-ng version
* modified by ZyDAS. They also made documentation available, thanks!
* Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
*/
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <linux/string.h>
#include <linux/if_arp.h>
#include <linux/firmware.h>
#include "zd1201.h"
static struct usb_device_id zd1201_table[] = {
{USB_DEVICE(0x0586, 0x3400)}, /* Peabird Wireless USB Adapter */
{USB_DEVICE(0x0ace, 0x1201)}, /* ZyDAS ZD1201 Wireless USB Adapter */
{USB_DEVICE(0x050d, 0x6051)}, /* Belkin F5D6051 usb adapter */
{USB_DEVICE(0x0db0, 0x6823)}, /* MSI UB11B usb adapter */
{USB_DEVICE(0x1044, 0x8005)}, /* GIGABYTE GN-WLBZ201 usb adapter */
static int ap; /* Are we an AP or a normal station? */
#define ZD1201_VERSION "0.15"
MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
MODULE_DESCRIPTION("Driver for ZyDAS ZD1201 based USB Wireless adapters");
MODULE_VERSION(ZD1201_VERSION);
MODULE_LICENSE("GPL");
module_param(ap, int, 0);
MODULE_PARM_DESC(ap, "If non-zero Access Point firmware will be loaded");
MODULE_DEVICE_TABLE(usb, zd1201_table);
static int zd1201_fw_upload(struct usb_device *dev, int apfw)
unsigned long len;
int err;
unsigned char ret;
char *buf;
char *fwfile;
if (apfw)
fwfile = "zd1201-ap.fw";
else
fwfile = "zd1201.fw";
err = request_firmware(&fw_entry, fwfile, &dev->dev);
if (err) {
dev_err(&dev->dev, "Failed to load %s firmware file!\n", fwfile);
dev_err(&dev->dev, "Make sure the hotplug firmware loader is installed.\n");
dev_err(&dev->dev, "Goto http://linux-lc100020.sourceforge.net for more info.\n");
return err;
}
data = fw_entry->data;
len = fw_entry->size;
buf = kmalloc(1024, GFP_ATOMIC);
if (!buf)
goto exit;
while (len > 0) {
int translen = (len > 1024) ? 1024 : len;
memcpy(buf, data, translen);
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0,
USB_DIR_OUT | 0x40, 0, 0, buf, translen,
ZD1201_FW_TIMEOUT);
if (err < 0)
goto exit;
len -= translen;
data += translen;
}
err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x2,
USB_DIR_OUT | 0x40, 0, 0, NULL, 0, ZD1201_FW_TIMEOUT);
if (err < 0)
goto exit;
err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x4,
USB_DIR_IN | 0x40, 0,0, &ret, sizeof(ret), ZD1201_FW_TIMEOUT);
if (err < 0)
goto exit;
if (ret & 0x80) {
err = -EIO;
goto exit;
}
err = 0;
exit:
kfree(buf);
MODULE_FIRMWARE("zd1201-ap.fw");
MODULE_FIRMWARE("zd1201.fw");
static void zd1201_usbfree(struct urb *urb)
{
struct zd1201 *zd = urb->context;
switch(urb->status) {
case -EILSEQ:
case -ENODEV:
case -ENOENT:
case -EPIPE:
case -EOVERFLOW:
case -ESHUTDOWN:
dev_warn(&zd->usb->dev, "%s: urb failed: %d\n",
zd->dev->name, urb->status);
}
kfree(urb->transfer_buffer);
usb_free_urb(urb);
}
/* cmdreq message:
u32 type
u16 cmd
u16 parm0
u16 parm1
u16 parm2
u8 pad[4]
total: 4 + 2 + 2 + 2 + 2 + 4 = 16
*/
static int zd1201_docmd(struct zd1201 *zd, int cmd, int parm0,
int parm1, int parm2)
{
unsigned char *command;
int ret;
struct urb *urb;
command = kmalloc(16, GFP_ATOMIC);
if (!command)
return -ENOMEM;
*((__le32*)command) = cpu_to_le32(ZD1201_USB_CMDREQ);
*((__le16*)&command[4]) = cpu_to_le16(cmd);
*((__le16*)&command[6]) = cpu_to_le16(parm0);
*((__le16*)&command[8]) = cpu_to_le16(parm1);
*((__le16*)&command[10])= cpu_to_le16(parm2);
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb) {
kfree(command);
return -ENOMEM;
}
usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret) {
kfree(command);
usb_free_urb(urb);
}
return ret;
}
/* Callback after sending out a packet */
static void zd1201_usbtx(struct urb *urb)
{
struct zd1201 *zd = urb->context;
netif_wake_queue(zd->dev);
}
static void zd1201_usbrx(struct urb *urb)
{
struct zd1201 *zd = urb->context;
int free = 0;
unsigned char *data = urb->transfer_buffer;
struct sk_buff *skb;
unsigned char type;
switch(urb->status) {
case -EILSEQ:
case -ENODEV:
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
case -ENOENT:
case -EPIPE:
case -EOVERFLOW:
case -ESHUTDOWN:
dev_warn(&zd->usb->dev, "%s: rx urb failed: %d\n",
zd->dev->name, urb->status);
free = 1;
goto exit;
}
if (urb->status != 0 || urb->actual_length == 0)
goto resubmit;
type = data[0];
if (type == ZD1201_PACKET_EVENTSTAT || type == ZD1201_PACKET_RESOURCE) {
memcpy(zd->rxdata, data, urb->actual_length);
zd->rxlen = urb->actual_length;
zd->rxdatas = 1;
wake_up(&zd->rxdataq);
}
/* Info frame */
if (type == ZD1201_PACKET_INQUIRE) {
int i = 0;
unsigned short infotype, framelen, copylen;
framelen = le16_to_cpu(*(__le16*)&data[4]);
infotype = le16_to_cpu(*(__le16*)&data[6]);
if (infotype == ZD1201_INF_LINKSTATUS) {
short linkstatus;
linkstatus = le16_to_cpu(*(__le16*)&data[8]);
switch(linkstatus) {
case 1:
netif_carrier_on(zd->dev);
break;
case 2:
netif_carrier_off(zd->dev);
break;
case 3:
netif_carrier_off(zd->dev);
break;
case 4:
netif_carrier_on(zd->dev);
break;
default:
netif_carrier_off(zd->dev);
}
goto resubmit;
}
if (infotype == ZD1201_INF_ASSOCSTATUS) {
short status = le16_to_cpu(*(__le16*)(data+8));
int event;
union iwreq_data wrqu;
switch (status) {
case ZD1201_ASSOCSTATUS_STAASSOC:
case ZD1201_ASSOCSTATUS_REASSOC:
event = IWEVREGISTERED;
break;
case ZD1201_ASSOCSTATUS_DISASSOC:
case ZD1201_ASSOCSTATUS_ASSOCFAIL:
case ZD1201_ASSOCSTATUS_AUTHFAIL:
default:
event = IWEVEXPIRED;
}
memcpy(wrqu.addr.sa_data, data+10, ETH_ALEN);
wrqu.addr.sa_family = ARPHRD_ETHER;
/* Send event to user space */
wireless_send_event(zd->dev, event, &wrqu, NULL);
goto resubmit;
}
if (infotype == ZD1201_INF_AUTHREQ) {
union iwreq_data wrqu;
memcpy(wrqu.addr.sa_data, data+8, ETH_ALEN);
wrqu.addr.sa_family = ARPHRD_ETHER;
/* There isn't a event that trully fits this request.
We assume that userspace will be smart enough to
see a new station being expired and sends back a
authstation ioctl to authorize it. */
wireless_send_event(zd->dev, IWEVEXPIRED, &wrqu, NULL);
goto resubmit;
}
/* Other infotypes are handled outside this handler */
zd->rxlen = 0;
while (i < urb->actual_length) {
copylen = le16_to_cpu(*(__le16*)&data[i+2]);
/* Sanity check, sometimes we get junk */
if (copylen+zd->rxlen > sizeof(zd->rxdata))
break;
memcpy(zd->rxdata+zd->rxlen, data+i+4, copylen);
zd->rxlen += copylen;
i += 64;
}
if (i >= urb->actual_length) {
zd->rxdatas = 1;
wake_up(&zd->rxdataq);
}
goto resubmit;
}
/* Actual data */
if (data[urb->actual_length-1] == ZD1201_PACKET_RXDATA) {
int datalen = urb->actual_length-1;
unsigned short len, fc, seq;
struct hlist_node *node;
len = ntohs(*(__be16 *)&data[datalen-2]);
if (len>datalen)
len=datalen;
fc = le16_to_cpu(*(__le16 *)&data[datalen-16]);
seq = le16_to_cpu(*(__le16 *)&data[datalen-24]);
if (datalen < 24)
goto resubmit;
if (!(skb = dev_alloc_skb(datalen+24)))
goto resubmit;
memcpy(skb_put(skb, 2), &data[datalen-16], 2);
memcpy(skb_put(skb, 2), &data[datalen-2], 2);
memcpy(skb_put(skb, 6), &data[datalen-14], 6);
memcpy(skb_put(skb, 6), &data[datalen-22], 6);
memcpy(skb_put(skb, 6), &data[datalen-8], 6);
memcpy(skb_put(skb, 2), &data[datalen-24], 2);
memcpy(skb_put(skb, len), data, len);
skb->protocol = eth_type_trans(skb, zd->dev);
zd->dev->stats.rx_packets++;
zd->dev->stats.rx_bytes += skb->len;
if ((seq & IEEE80211_SCTL_FRAG) ||
(fc & IEEE80211_FCTL_MOREFRAGS)) {
struct zd1201_frag *frag = NULL;
char *ptr;
if (datalen<14)
goto resubmit;
frag = kmalloc(sizeof(*frag), GFP_ATOMIC);
skb = dev_alloc_skb(IEEE80211_MAX_DATA_LEN +14+2);
if (!skb) {
kfree(frag);
goto resubmit;
}
frag->skb = skb;
skb_reserve(skb, 2);
memcpy(skb_put(skb, 12), &data[datalen-14], 12);
memcpy(skb_put(skb, 2), &data[6], 2);
memcpy(skb_put(skb, len), data+8, len);
hlist_add_head(&frag->fnode, &zd->fraglist);
goto resubmit;
}
hlist_for_each_entry(frag, node, &zd->fraglist, fnode)
if (frag->seq == (seq&IEEE80211_SCTL_SEQ))
break;
if (!frag)
goto resubmit;
skb = frag->skb;
ptr = skb_put(skb, len);
if (ptr)
memcpy(ptr, data+8, len);
goto resubmit;
hlist_del_init(&frag->fnode);
kfree(frag);
} else {
if (datalen<14)
goto resubmit;
skb = dev_alloc_skb(len + 14 + 2);
if (!skb)
goto resubmit;
skb_reserve(skb, 2);
memcpy(skb_put(skb, 12), &data[datalen-14], 12);
memcpy(skb_put(skb, 2), &data[6], 2);
memcpy(skb_put(skb, len), data+8, len);
}
skb->protocol = eth_type_trans(skb, zd->dev);
zd->dev->stats.rx_packets++;
zd->dev->stats.rx_bytes += skb->len;
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
netif_rx(skb);
}
resubmit:
memset(data, 0, ZD1201_RXSIZE);
urb->status = 0;
urb->dev = zd->usb;
if(usb_submit_urb(urb, GFP_ATOMIC))
free = 1;
exit:
if (free) {
zd->rxlen = 0;
zd->rxdatas = 1;
wake_up(&zd->rxdataq);
kfree(urb->transfer_buffer);
}
}
static int zd1201_getconfig(struct zd1201 *zd, int rid, void *riddata,
unsigned int riddatalen)
{
int err;
int i = 0;
int code;
int rid_fid;
int length;
unsigned char *pdata;
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
zd->rxdatas = 0;
err = zd1201_docmd(zd, ZD1201_CMDCODE_ACCESS, rid, 0, 0);
if (err)
return err;
wait_event_interruptible(zd->rxdataq, zd->rxdatas);
if (!zd->rxlen)
return -EIO;
code = le16_to_cpu(*(__le16*)(&zd->rxdata[4]));
rid_fid = le16_to_cpu(*(__le16*)(&zd->rxdata[6]));
length = le16_to_cpu(*(__le16*)(&zd->rxdata[8]));
if (length > zd->rxlen)
length = zd->rxlen-6;
/* If access bit is not on, then error */
if ((code & ZD1201_ACCESSBIT) != ZD1201_ACCESSBIT || rid_fid != rid )
return -EINVAL;
/* Not enough buffer for allocating data */
if (riddatalen != (length - 4)) {
dev_dbg(&zd->usb->dev, "riddatalen mismatches, expected=%u, (packet=%u) length=%u, rid=0x%04X, rid_fid=0x%04X\n",
riddatalen, zd->rxlen, length, rid, rid_fid);
return -ENODATA;
}
zd->rxdatas = 0;
/* Issue SetRxRid commnd */
err = zd1201_docmd(zd, ZD1201_CMDCODE_SETRXRID, rid, 0, length);
if (err)
return err;
/* Receive RID record from resource packets */
wait_event_interruptible(zd->rxdataq, zd->rxdatas);
if (!zd->rxlen)
return -EIO;
if (zd->rxdata[zd->rxlen - 1] != ZD1201_PACKET_RESOURCE) {
dev_dbg(&zd->usb->dev, "Packet type mismatch: 0x%x not 0x3\n",
zd->rxdata[zd->rxlen-1]);
return -EINVAL;
}
/* Set the data pointer and received data length */
pdata = zd->rxdata;
length = zd->rxlen;
do {
dev_dbg(&zd->usb->dev, "Rx Resource packet type error: %02X\n",
pdata[0]);
return -EINVAL;
}
if (actual_length != 64) {
/* Trim the last packet type byte */
actual_length--;
}
/* Skip the 4 bytes header (RID length and RID) */
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
pdata += 4;
actual_length -= 4;
}
memcpy(riddata, pdata, actual_length);
riddata += actual_length;
pdata += actual_length;
length -= 64;
i++;
} while (length > 0);
return 0;
}
/*
* resreq:
* byte type
* byte sequence
* u16 reserved
* byte data[12]
* total: 16
*/
static int zd1201_setconfig(struct zd1201 *zd, int rid, void *buf, int len, int wait)
{
int err;
unsigned char *request;
int reqlen;
char seq=0;
struct urb *urb;
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
len += 4; /* first 4 are for header */
zd->rxdatas = 0;
zd->rxlen = 0;
for (seq=0; len > 0; seq++) {
request = kmalloc(16, gfp_mask);
if (!request)
return -ENOMEM;
urb = usb_alloc_urb(0, gfp_mask);
if (!urb) {
kfree(request);
return -ENOMEM;
}
memset(request, 0, 16);
reqlen = len>12 ? 12 : len;
request[0] = ZD1201_USB_RESREQ;
request[1] = seq;
request[2] = 0;
request[3] = 0;
if (request[1] == 0) {
/* add header */
*(__le16*)&request[4] = cpu_to_le16((len-2+1)/2);
*(__le16*)&request[6] = cpu_to_le16(rid);
memcpy(request+8, buf, reqlen-4);
buf += reqlen-4;
} else {
memcpy(request+4, buf, reqlen);
buf += reqlen;
}
len -= reqlen;
usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb,
zd->endp_out2), request, 16, zd1201_usbfree, zd);
err = usb_submit_urb(urb, gfp_mask);
if (err)
goto err;
}
request = kmalloc(16, gfp_mask);
if (!request)
return -ENOMEM;
urb = usb_alloc_urb(0, gfp_mask);
if (!urb) {
kfree(request);
return -ENOMEM;
}
*((__le32*)request) = cpu_to_le32(ZD1201_USB_CMDREQ);
*((__le16*)&request[4]) =
cpu_to_le16(ZD1201_CMDCODE_ACCESS|ZD1201_ACCESSBIT);
*((__le16*)&request[6]) = cpu_to_le16(rid);
*((__le16*)&request[8]) = cpu_to_le16(0);
*((__le16*)&request[10]) = cpu_to_le16(0);
usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
request, 16, zd1201_usbfree, zd);
err = usb_submit_urb(urb, gfp_mask);
if (err)
goto err;
if (wait) {
wait_event_interruptible(zd->rxdataq, zd->rxdatas);
if (!zd->rxlen || le16_to_cpu(*(__le16*)&zd->rxdata[6]) != rid) {
dev_dbg(&zd->usb->dev, "wrong or no RID received\n");
}
}
return 0;
err:
kfree(request);
usb_free_urb(urb);
return err;
}
static inline int zd1201_getconfig16(struct zd1201 *zd, int rid, short *val)
{
int err;
__le16 zdval;
err = zd1201_getconfig(zd, rid, &zdval, sizeof(__le16));
if (err)
return err;
*val = le16_to_cpu(zdval);
return 0;
}
static inline int zd1201_setconfig16(struct zd1201 *zd, int rid, short val)
{
__le16 zdval = cpu_to_le16(val);
return (zd1201_setconfig(zd, rid, &zdval, sizeof(__le16), 1));
}
static int zd1201_drvr_start(struct zd1201 *zd)
{
int err, i;
short max;
__le16 zdmax;
unsigned char *buffer;
buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
usb_fill_bulk_urb(zd->rx_urb, zd->usb,
usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
zd1201_usbrx, zd);
err = usb_submit_urb(zd->rx_urb, GFP_KERNEL);
if (err)
goto err_buffer;
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
err = zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
if (err)
goto err_urb;
err = zd1201_getconfig(zd, ZD1201_RID_CNFMAXTXBUFFERNUMBER, &zdmax,
sizeof(__le16));
if (err)
goto err_urb;
max = le16_to_cpu(zdmax);
for (i=0; i<max; i++) {
err = zd1201_docmd(zd, ZD1201_CMDCODE_ALLOC, 1514, 0, 0);
if (err)
goto err_urb;
}
return 0;
err_urb:
usb_kill_urb(zd->rx_urb);
return err;
err_buffer:
kfree(buffer);
return err;
}
/* Magic alert: The firmware doesn't seem to like the MAC state being
* toggled in promisc (aka monitor) mode.
* (It works a number of times, but will halt eventually)
* So we turn it of before disabling and on after enabling if needed.
*/
static int zd1201_enable(struct zd1201 *zd)
{
int err;
if (zd->mac_enabled)
return 0;
err = zd1201_docmd(zd, ZD1201_CMDCODE_ENABLE, 0, 0, 0);
if (!err)
zd->mac_enabled = 1;
if (zd->monitor)
err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 1);
return err;
}
static int zd1201_disable(struct zd1201 *zd)
{
int err;
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
if (!zd->mac_enabled)
return 0;
if (zd->monitor) {
err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
if (err)
return err;
}
err = zd1201_docmd(zd, ZD1201_CMDCODE_DISABLE, 0, 0, 0);
if (!err)
zd->mac_enabled = 0;
return err;
}
static int zd1201_mac_reset(struct zd1201 *zd)
{
if (!zd->mac_enabled)
return 0;
zd1201_disable(zd);
return zd1201_enable(zd);
}
static int zd1201_join(struct zd1201 *zd, char *essid, int essidlen)
{
int err, val;
char buf[IW_ESSID_MAX_SIZE+2];
err = zd1201_disable(zd);
if (err)
return err;
val = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
val |= ZD1201_CNFAUTHENTICATION_SHAREDKEY;
err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, val);
if (err)
return err;
*(__le16 *)buf = cpu_to_le16(essidlen);
memcpy(buf+2, essid, essidlen);
if (!zd->ap) { /* Normal station */
err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
IW_ESSID_MAX_SIZE+2, 1);
if (err)
return err;
} else { /* AP */
err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNSSID, buf,
IW_ESSID_MAX_SIZE+2, 1);
if (err)
return err;
}
err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
zd->dev->dev_addr, zd->dev->addr_len, 1);
if (err)
return err;
err = zd1201_enable(zd);
if (err)
return err;
msleep(100);
return 0;
}
static int zd1201_net_open(struct net_device *dev)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
/* Start MAC with wildcard if no essid set */
if (!zd->mac_enabled)
zd1201_join(zd, zd->essid, zd->essidlen);
netif_start_queue(dev);
return 0;
}
static int zd1201_net_stop(struct net_device *dev)
{
netif_stop_queue(dev);
return 0;
}
/*
RFC 1042 encapsulates Ethernet frames in 802.11 frames
by prefixing them with 0xaa, 0xaa, 0x03) followed by a SNAP OID of 0
(0x00, 0x00, 0x00). Zd requires an additional padding, copy
of ethernet addresses, length of the standard RFC 1042 packet
and a command byte (which is nul for tx).
tx frame (from Wlan NG):
RFC 1042:
llc 0xAA 0xAA 0x03 (802.2 LLC)
snap 0x00 0x00 0x00 (Ethernet encapsulated)
type 2 bytes, Ethernet type field
payload (minus eth header)
Zydas specific:
padding 1B if (skb->len+8+1)%64==0
Eth MAC addr 12 bytes, Ethernet MAC addresses
length 2 bytes, RFC 1042 packet length
(llc+snap+type+payload)
zd 1 null byte, zd1201 packet type
*/
static netdev_tx_t zd1201_hard_start_xmit(struct sk_buff *skb,
struct net_device *dev)
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
unsigned char *txbuf = zd->txdata;
int txbuflen, pad = 0, err;
struct urb *urb = zd->tx_urb;
if (!zd->mac_enabled || zd->monitor) {
dev->stats.tx_dropped++;
return NETDEV_TX_OK;
}
netif_stop_queue(dev);
txbuflen = skb->len + 8 + 1;
if (txbuflen%64 == 0) {
pad = 1;
txbuflen++;
}
txbuf[0] = 0xAA;
txbuf[1] = 0xAA;
txbuf[2] = 0x03;
txbuf[3] = 0x00; /* rfc1042 */
txbuf[4] = 0x00;
txbuf[5] = 0x00;
skb_copy_from_linear_data_offset(skb, 12, txbuf + 6, skb->len - 12);
skb_copy_from_linear_data(skb, txbuf + skb->len - 12 + 6 + pad, 12);
*(__be16*)&txbuf[skb->len+6+pad] = htons(skb->len-12+6);
txbuf[txbuflen-1] = 0;
usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out),
txbuf, txbuflen, zd1201_usbtx, zd);
err = usb_submit_urb(zd->tx_urb, GFP_ATOMIC);
if (err) {
dev->stats.tx_errors++;
} else {
dev->stats.tx_packets++;
dev->stats.tx_bytes += skb->len;
return NETDEV_TX_OK;
}
static void zd1201_tx_timeout(struct net_device *dev)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
if (!zd)
return;
dev_warn(&zd->usb->dev, "%s: TX timeout, shooting down urb\n",
dev->name);
usb_unlink_urb(zd->tx_urb);
dev->stats.tx_errors++;
dev->trans_start = jiffies; /* prevent tx timeout */
}
static int zd1201_set_mac_address(struct net_device *dev, void *p)
{
struct sockaddr *addr = p;
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
int err;
if (!zd)
return -ENODEV;
err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
addr->sa_data, dev->addr_len, 1);
if (err)
return err;
memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
return zd1201_mac_reset(zd);
}
static struct iw_statistics *zd1201_get_wireless_stats(struct net_device *dev)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
return &zd->iwstats;
}
static void zd1201_set_multicast(struct net_device *dev)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
unsigned char reqbuf[ETH_ALEN*ZD1201_MAXMULTI];
int i;
if (netdev_mc_count(dev) > ZD1201_MAXMULTI)
netdev_for_each_mc_addr(ha, dev)
memcpy(reqbuf + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
zd1201_setconfig(zd, ZD1201_RID_CNFGROUPADDRESS, reqbuf,
netdev_mc_count(dev) * ETH_ALEN, 0);
}
static int zd1201_config_commit(struct net_device *dev,
struct iw_request_info *info, struct iw_point *data, char *essid)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
return zd1201_mac_reset(zd);
}
static int zd1201_get_name(struct net_device *dev,
struct iw_request_info *info, char *name, char *extra)
{
strcpy(name, "IEEE 802.11b");
return 0;
}
static int zd1201_set_freq(struct net_device *dev,
struct iw_request_info *info, struct iw_freq *freq, char *extra)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
short channel = 0;
int err;
if (freq->e == 0)
channel = freq->m;
else {
channel = ieee80211_freq_to_dsss_chan(freq->m);
if (channel < 0)
channel = 0;
}
err = zd1201_setconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, channel);
if (err)
return err;
zd1201_mac_reset(zd);
return 0;
}
static int zd1201_get_freq(struct net_device *dev,
struct iw_request_info *info, struct iw_freq *freq, char *extra)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
short channel;
int err;
err = zd1201_getconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, &channel);
if (err)
return err;
freq->e = 0;
freq->m = channel;
return 0;
}
static int zd1201_set_mode(struct net_device *dev,
struct iw_request_info *info, __u32 *mode, char *extra)
{
John W. Linville
committed
struct zd1201 *zd = netdev_priv(dev);
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
short porttype, monitor = 0;
unsigned char buffer[IW_ESSID_MAX_SIZE+2];
int err;
if (zd->ap) {
if (*mode != IW_MODE_MASTER)
return -EINVAL;
return 0;
}
err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
if (err)
return err;
zd->dev->type = ARPHRD_ETHER;
switch(*mode) {
case IW_MODE_MONITOR:
monitor = 1;
zd->dev->type = ARPHRD_IEEE80211;
/* Make sure we are no longer associated with by
setting an 'impossible' essid.
(otherwise we mess up firmware)
*/
zd1201_join(zd, "\0-*#\0", 5);
/* Put port in pIBSS */
case 8: /* No pseudo-IBSS in wireless extensions (yet) */
porttype = ZD1201_PORTTYPE_PSEUDOIBSS;
break;
case IW_MODE_ADHOC:
porttype = ZD1201_PORTTYPE_IBSS;
break;
case IW_MODE_INFRA:
porttype = ZD1201_PORTTYPE_BSS;
break;
default:
return -EINVAL;
}
err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
if (err)
return err;
if (zd->monitor && !monitor) {
zd1201_disable(zd);
*(__le16 *)buffer = cpu_to_le16(zd->essidlen);
memcpy(buffer+2, zd->essid, zd->essidlen);
err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID,
buffer, IW_ESSID_MAX_SIZE+2, 1);
if (err)
return err;
}
/* If monitor mode is set we don't actually turn it on here since it
* is done during mac reset anyway (see zd1201_mac_enable).
*/