1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for ZyDAS zd1201 based USB wireless devices. 4 * 5 * Copyright (c) 2004, 2005 Jeroen Vreeken (pe1rxq@amsat.org) 6 * 7 * Parts of this driver have been derived from a wlan-ng version 8 * modified by ZyDAS. They also made documentation available, thanks! 9 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/usb.h> 14 #include <linux/slab.h> 15 #include <linux/netdevice.h> 16 #include <linux/etherdevice.h> 17 #include <linux/wireless.h> 18 #include <net/cfg80211.h> 19 #include <net/iw_handler.h> 20 #include <linux/string.h> 21 #include <linux/if_arp.h> 22 #include <linux/firmware.h> 23 #include "zd1201.h" 24 25 static const struct usb_device_id zd1201_table[] = { 26 {USB_DEVICE(0x0586, 0x3400)}, /* Peabird USB Wireless Adapter */ 27 {USB_DEVICE(0x0ace, 0x1201)}, /* ZyDAS ZD1201 USB Wireless Adapter */ 28 {USB_DEVICE(0x050d, 0x6051)}, /* Belkin F5D6051 usb adapter */ 29 {USB_DEVICE(0x0db0, 0x6823)}, /* MSI UB11B usb adapter */ 30 {USB_DEVICE(0x1044, 0x8004)}, /* Gigabyte GN-WLBZ101 */ 31 {USB_DEVICE(0x1044, 0x8005)}, /* GIGABYTE GN-WLBZ201 usb adapter */ 32 {} 33 }; 34 35 static int ap; /* Are we an AP or a normal station? */ 36 37 #define ZD1201_VERSION "0.15" 38 39 MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>"); 40 MODULE_DESCRIPTION("Driver for ZyDAS ZD1201 based USB Wireless adapters"); 41 MODULE_VERSION(ZD1201_VERSION); 42 MODULE_LICENSE("GPL"); 43 module_param(ap, int, 0); 44 MODULE_PARM_DESC(ap, "If non-zero Access Point firmware will be loaded"); 45 MODULE_DEVICE_TABLE(usb, zd1201_table); 46 47 48 static int zd1201_fw_upload(struct usb_device *dev, int apfw) 49 { 50 const struct firmware *fw_entry; 51 const char *data; 52 unsigned long len; 53 int err; 54 unsigned char ret; 55 char *buf; 56 char *fwfile; 57 58 if (apfw) 59 fwfile = "zd1201-ap.fw"; 60 else 61 fwfile = "zd1201.fw"; 62 63 err = request_firmware(&fw_entry, fwfile, &dev->dev); 64 if (err) { 65 dev_err(&dev->dev, "Failed to load %s firmware file!\n", fwfile); 66 dev_err(&dev->dev, "Make sure the hotplug firmware loader is installed.\n"); 67 dev_err(&dev->dev, "Goto http://linux-lc100020.sourceforge.net for more info.\n"); 68 return err; 69 } 70 71 data = fw_entry->data; 72 len = fw_entry->size; 73 74 buf = kmalloc(1024, GFP_ATOMIC); 75 if (!buf) { 76 err = -ENOMEM; 77 goto exit; 78 } 79 80 while (len > 0) { 81 int translen = (len > 1024) ? 1024 : len; 82 memcpy(buf, data, translen); 83 84 err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0, 85 USB_DIR_OUT | 0x40, 0, 0, buf, translen, 86 ZD1201_FW_TIMEOUT); 87 if (err < 0) 88 goto exit; 89 90 len -= translen; 91 data += translen; 92 } 93 94 err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x2, 95 USB_DIR_OUT | 0x40, 0, 0, NULL, 0, ZD1201_FW_TIMEOUT); 96 if (err < 0) 97 goto exit; 98 99 err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x4, 100 USB_DIR_IN | 0x40, 0, 0, buf, sizeof(ret), ZD1201_FW_TIMEOUT); 101 if (err < 0) 102 goto exit; 103 104 memcpy(&ret, buf, sizeof(ret)); 105 106 if (ret & 0x80) { 107 err = -EIO; 108 goto exit; 109 } 110 111 err = 0; 112 exit: 113 kfree(buf); 114 release_firmware(fw_entry); 115 return err; 116 } 117 118 MODULE_FIRMWARE("zd1201-ap.fw"); 119 MODULE_FIRMWARE("zd1201.fw"); 120 121 static void zd1201_usbfree(struct urb *urb) 122 { 123 struct zd1201 *zd = urb->context; 124 125 switch(urb->status) { 126 case -EILSEQ: 127 case -ENODEV: 128 case -ETIME: 129 case -ENOENT: 130 case -EPIPE: 131 case -EOVERFLOW: 132 case -ESHUTDOWN: 133 dev_warn(&zd->usb->dev, "%s: urb failed: %d\n", 134 zd->dev->name, urb->status); 135 } 136 137 kfree(urb->transfer_buffer); 138 usb_free_urb(urb); 139 } 140 141 /* cmdreq message: 142 u32 type 143 u16 cmd 144 u16 parm0 145 u16 parm1 146 u16 parm2 147 u8 pad[4] 148 149 total: 4 + 2 + 2 + 2 + 2 + 4 = 16 150 */ 151 static int zd1201_docmd(struct zd1201 *zd, int cmd, int parm0, 152 int parm1, int parm2) 153 { 154 unsigned char *command; 155 int ret; 156 struct urb *urb; 157 158 command = kmalloc(16, GFP_ATOMIC); 159 if (!command) 160 return -ENOMEM; 161 162 *((__le32*)command) = cpu_to_le32(ZD1201_USB_CMDREQ); 163 *((__le16*)&command[4]) = cpu_to_le16(cmd); 164 *((__le16*)&command[6]) = cpu_to_le16(parm0); 165 *((__le16*)&command[8]) = cpu_to_le16(parm1); 166 *((__le16*)&command[10])= cpu_to_le16(parm2); 167 168 urb = usb_alloc_urb(0, GFP_ATOMIC); 169 if (!urb) { 170 kfree(command); 171 return -ENOMEM; 172 } 173 usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2), 174 command, 16, zd1201_usbfree, zd); 175 ret = usb_submit_urb(urb, GFP_ATOMIC); 176 if (ret) { 177 kfree(command); 178 usb_free_urb(urb); 179 } 180 181 return ret; 182 } 183 184 /* Callback after sending out a packet */ 185 static void zd1201_usbtx(struct urb *urb) 186 { 187 struct zd1201 *zd = urb->context; 188 netif_wake_queue(zd->dev); 189 } 190 191 /* Incoming data */ 192 static void zd1201_usbrx(struct urb *urb) 193 { 194 struct zd1201 *zd = urb->context; 195 int free = 0; 196 unsigned char *data = urb->transfer_buffer; 197 struct sk_buff *skb; 198 unsigned char type; 199 200 if (!zd) 201 return; 202 203 switch(urb->status) { 204 case -EILSEQ: 205 case -ENODEV: 206 case -ETIME: 207 case -ENOENT: 208 case -EPIPE: 209 case -EOVERFLOW: 210 case -ESHUTDOWN: 211 dev_warn(&zd->usb->dev, "%s: rx urb failed: %d\n", 212 zd->dev->name, urb->status); 213 free = 1; 214 goto exit; 215 } 216 217 if (urb->status != 0 || urb->actual_length == 0) 218 goto resubmit; 219 220 type = data[0]; 221 if (type == ZD1201_PACKET_EVENTSTAT || type == ZD1201_PACKET_RESOURCE) { 222 memcpy(zd->rxdata, data, urb->actual_length); 223 zd->rxlen = urb->actual_length; 224 zd->rxdatas = 1; 225 wake_up(&zd->rxdataq); 226 } 227 /* Info frame */ 228 if (type == ZD1201_PACKET_INQUIRE) { 229 int i = 0; 230 unsigned short infotype, copylen; 231 infotype = le16_to_cpu(*(__le16*)&data[6]); 232 233 if (infotype == ZD1201_INF_LINKSTATUS) { 234 short linkstatus; 235 236 linkstatus = le16_to_cpu(*(__le16*)&data[8]); 237 switch(linkstatus) { 238 case 1: 239 netif_carrier_on(zd->dev); 240 break; 241 case 2: 242 netif_carrier_off(zd->dev); 243 break; 244 case 3: 245 netif_carrier_off(zd->dev); 246 break; 247 case 4: 248 netif_carrier_on(zd->dev); 249 break; 250 default: 251 netif_carrier_off(zd->dev); 252 } 253 goto resubmit; 254 } 255 if (infotype == ZD1201_INF_ASSOCSTATUS) { 256 short status = le16_to_cpu(*(__le16*)(data+8)); 257 int event; 258 union iwreq_data wrqu; 259 260 switch (status) { 261 case ZD1201_ASSOCSTATUS_STAASSOC: 262 case ZD1201_ASSOCSTATUS_REASSOC: 263 event = IWEVREGISTERED; 264 break; 265 case ZD1201_ASSOCSTATUS_DISASSOC: 266 case ZD1201_ASSOCSTATUS_ASSOCFAIL: 267 case ZD1201_ASSOCSTATUS_AUTHFAIL: 268 default: 269 event = IWEVEXPIRED; 270 } 271 memcpy(wrqu.addr.sa_data, data+10, ETH_ALEN); 272 wrqu.addr.sa_family = ARPHRD_ETHER; 273 274 /* Send event to user space */ 275 wireless_send_event(zd->dev, event, &wrqu, NULL); 276 277 goto resubmit; 278 } 279 if (infotype == ZD1201_INF_AUTHREQ) { 280 union iwreq_data wrqu; 281 282 memcpy(wrqu.addr.sa_data, data+8, ETH_ALEN); 283 wrqu.addr.sa_family = ARPHRD_ETHER; 284 /* There isn't a event that trully fits this request. 285 We assume that userspace will be smart enough to 286 see a new station being expired and sends back a 287 authstation ioctl to authorize it. */ 288 wireless_send_event(zd->dev, IWEVEXPIRED, &wrqu, NULL); 289 goto resubmit; 290 } 291 /* Other infotypes are handled outside this handler */ 292 zd->rxlen = 0; 293 while (i < urb->actual_length) { 294 copylen = le16_to_cpu(*(__le16*)&data[i+2]); 295 /* Sanity check, sometimes we get junk */ 296 if (copylen+zd->rxlen > sizeof(zd->rxdata)) 297 break; 298 memcpy(zd->rxdata+zd->rxlen, data+i+4, copylen); 299 zd->rxlen += copylen; 300 i += 64; 301 } 302 if (i >= urb->actual_length) { 303 zd->rxdatas = 1; 304 wake_up(&zd->rxdataq); 305 } 306 goto resubmit; 307 } 308 /* Actual data */ 309 if (data[urb->actual_length-1] == ZD1201_PACKET_RXDATA) { 310 int datalen = urb->actual_length-1; 311 unsigned short len, fc, seq; 312 313 len = ntohs(*(__be16 *)&data[datalen-2]); 314 if (len>datalen) 315 len=datalen; 316 fc = le16_to_cpu(*(__le16 *)&data[datalen-16]); 317 seq = le16_to_cpu(*(__le16 *)&data[datalen-24]); 318 319 if (zd->monitor) { 320 if (datalen < 24) 321 goto resubmit; 322 if (!(skb = dev_alloc_skb(datalen+24))) 323 goto resubmit; 324 325 skb_put_data(skb, &data[datalen - 16], 2); 326 skb_put_data(skb, &data[datalen - 2], 2); 327 skb_put_data(skb, &data[datalen - 14], 6); 328 skb_put_data(skb, &data[datalen - 22], 6); 329 skb_put_data(skb, &data[datalen - 8], 6); 330 skb_put_data(skb, &data[datalen - 24], 2); 331 skb_put_data(skb, data, len); 332 skb->protocol = eth_type_trans(skb, zd->dev); 333 zd->dev->stats.rx_packets++; 334 zd->dev->stats.rx_bytes += skb->len; 335 netif_rx(skb); 336 goto resubmit; 337 } 338 339 if ((seq & IEEE80211_SCTL_FRAG) || 340 (fc & IEEE80211_FCTL_MOREFRAGS)) { 341 struct zd1201_frag *frag = NULL; 342 char *ptr; 343 344 if (datalen<14) 345 goto resubmit; 346 if ((seq & IEEE80211_SCTL_FRAG) == 0) { 347 frag = kmalloc(sizeof(*frag), GFP_ATOMIC); 348 if (!frag) 349 goto resubmit; 350 skb = dev_alloc_skb(IEEE80211_MAX_DATA_LEN +14+2); 351 if (!skb) { 352 kfree(frag); 353 goto resubmit; 354 } 355 frag->skb = skb; 356 frag->seq = seq & IEEE80211_SCTL_SEQ; 357 skb_reserve(skb, 2); 358 skb_put_data(skb, &data[datalen - 14], 12); 359 skb_put_data(skb, &data[6], 2); 360 skb_put_data(skb, data + 8, len); 361 hlist_add_head(&frag->fnode, &zd->fraglist); 362 goto resubmit; 363 } 364 hlist_for_each_entry(frag, &zd->fraglist, fnode) 365 if (frag->seq == (seq&IEEE80211_SCTL_SEQ)) 366 break; 367 if (!frag) 368 goto resubmit; 369 skb = frag->skb; 370 ptr = skb_put(skb, len); 371 if (ptr) 372 memcpy(ptr, data+8, len); 373 if (fc & IEEE80211_FCTL_MOREFRAGS) 374 goto resubmit; 375 hlist_del_init(&frag->fnode); 376 kfree(frag); 377 } else { 378 if (datalen<14) 379 goto resubmit; 380 skb = dev_alloc_skb(len + 14 + 2); 381 if (!skb) 382 goto resubmit; 383 skb_reserve(skb, 2); 384 skb_put_data(skb, &data[datalen - 14], 12); 385 skb_put_data(skb, &data[6], 2); 386 skb_put_data(skb, data + 8, len); 387 } 388 skb->protocol = eth_type_trans(skb, zd->dev); 389 zd->dev->stats.rx_packets++; 390 zd->dev->stats.rx_bytes += skb->len; 391 netif_rx(skb); 392 } 393 resubmit: 394 memset(data, 0, ZD1201_RXSIZE); 395 396 urb->status = 0; 397 urb->dev = zd->usb; 398 if(usb_submit_urb(urb, GFP_ATOMIC)) 399 free = 1; 400 401 exit: 402 if (free) { 403 zd->rxlen = 0; 404 zd->rxdatas = 1; 405 wake_up(&zd->rxdataq); 406 kfree(urb->transfer_buffer); 407 } 408 } 409 410 static int zd1201_getconfig(struct zd1201 *zd, int rid, void *riddata, 411 unsigned int riddatalen) 412 { 413 int err; 414 int i = 0; 415 int code; 416 int rid_fid; 417 int length; 418 unsigned char *pdata; 419 420 zd->rxdatas = 0; 421 err = zd1201_docmd(zd, ZD1201_CMDCODE_ACCESS, rid, 0, 0); 422 if (err) 423 return err; 424 425 wait_event_interruptible(zd->rxdataq, zd->rxdatas); 426 if (!zd->rxlen) 427 return -EIO; 428 429 code = le16_to_cpu(*(__le16*)(&zd->rxdata[4])); 430 rid_fid = le16_to_cpu(*(__le16*)(&zd->rxdata[6])); 431 length = le16_to_cpu(*(__le16*)(&zd->rxdata[8])); 432 if (length > zd->rxlen) 433 length = zd->rxlen-6; 434 435 /* If access bit is not on, then error */ 436 if ((code & ZD1201_ACCESSBIT) != ZD1201_ACCESSBIT || rid_fid != rid ) 437 return -EINVAL; 438 439 /* Not enough buffer for allocating data */ 440 if (riddatalen != (length - 4)) { 441 dev_dbg(&zd->usb->dev, "riddatalen mismatches, expected=%u, (packet=%u) length=%u, rid=0x%04X, rid_fid=0x%04X\n", 442 riddatalen, zd->rxlen, length, rid, rid_fid); 443 return -ENODATA; 444 } 445 446 zd->rxdatas = 0; 447 /* Issue SetRxRid commnd */ 448 err = zd1201_docmd(zd, ZD1201_CMDCODE_SETRXRID, rid, 0, length); 449 if (err) 450 return err; 451 452 /* Receive RID record from resource packets */ 453 wait_event_interruptible(zd->rxdataq, zd->rxdatas); 454 if (!zd->rxlen) 455 return -EIO; 456 457 if (zd->rxdata[zd->rxlen - 1] != ZD1201_PACKET_RESOURCE) { 458 dev_dbg(&zd->usb->dev, "Packet type mismatch: 0x%x not 0x3\n", 459 zd->rxdata[zd->rxlen-1]); 460 return -EINVAL; 461 } 462 463 /* Set the data pointer and received data length */ 464 pdata = zd->rxdata; 465 length = zd->rxlen; 466 467 do { 468 int actual_length; 469 470 actual_length = (length > 64) ? 64 : length; 471 472 if (pdata[0] != 0x3) { 473 dev_dbg(&zd->usb->dev, "Rx Resource packet type error: %02X\n", 474 pdata[0]); 475 return -EINVAL; 476 } 477 478 if (actual_length != 64) { 479 /* Trim the last packet type byte */ 480 actual_length--; 481 } 482 483 /* Skip the 4 bytes header (RID length and RID) */ 484 if (i == 0) { 485 pdata += 8; 486 actual_length -= 8; 487 } else { 488 pdata += 4; 489 actual_length -= 4; 490 } 491 492 memcpy(riddata, pdata, actual_length); 493 riddata += actual_length; 494 pdata += actual_length; 495 length -= 64; 496 i++; 497 } while (length > 0); 498 499 return 0; 500 } 501 502 /* 503 * resreq: 504 * byte type 505 * byte sequence 506 * u16 reserved 507 * byte data[12] 508 * total: 16 509 */ 510 static int zd1201_setconfig(struct zd1201 *zd, int rid, const void *buf, int len, int wait) 511 { 512 int err; 513 unsigned char *request; 514 int reqlen; 515 char seq=0; 516 struct urb *urb; 517 gfp_t gfp_mask = wait ? GFP_NOIO : GFP_ATOMIC; 518 519 len += 4; /* first 4 are for header */ 520 521 zd->rxdatas = 0; 522 zd->rxlen = 0; 523 for (seq=0; len > 0; seq++) { 524 request = kzalloc(16, gfp_mask); 525 if (!request) 526 return -ENOMEM; 527 urb = usb_alloc_urb(0, gfp_mask); 528 if (!urb) { 529 kfree(request); 530 return -ENOMEM; 531 } 532 reqlen = len>12 ? 12 : len; 533 request[0] = ZD1201_USB_RESREQ; 534 request[1] = seq; 535 request[2] = 0; 536 request[3] = 0; 537 if (request[1] == 0) { 538 /* add header */ 539 *(__le16*)&request[4] = cpu_to_le16((len-2+1)/2); 540 *(__le16*)&request[6] = cpu_to_le16(rid); 541 memcpy(request+8, buf, reqlen-4); 542 buf += reqlen-4; 543 } else { 544 memcpy(request+4, buf, reqlen); 545 buf += reqlen; 546 } 547 548 len -= reqlen; 549 550 usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, 551 zd->endp_out2), request, 16, zd1201_usbfree, zd); 552 err = usb_submit_urb(urb, gfp_mask); 553 if (err) 554 goto err; 555 } 556 557 request = kmalloc(16, gfp_mask); 558 if (!request) 559 return -ENOMEM; 560 urb = usb_alloc_urb(0, gfp_mask); 561 if (!urb) { 562 kfree(request); 563 return -ENOMEM; 564 } 565 *((__le32*)request) = cpu_to_le32(ZD1201_USB_CMDREQ); 566 *((__le16*)&request[4]) = 567 cpu_to_le16(ZD1201_CMDCODE_ACCESS|ZD1201_ACCESSBIT); 568 *((__le16*)&request[6]) = cpu_to_le16(rid); 569 *((__le16*)&request[8]) = cpu_to_le16(0); 570 *((__le16*)&request[10]) = cpu_to_le16(0); 571 usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2), 572 request, 16, zd1201_usbfree, zd); 573 err = usb_submit_urb(urb, gfp_mask); 574 if (err) 575 goto err; 576 577 if (wait) { 578 wait_event_interruptible(zd->rxdataq, zd->rxdatas); 579 if (!zd->rxlen || le16_to_cpu(*(__le16*)&zd->rxdata[6]) != rid) { 580 dev_dbg(&zd->usb->dev, "wrong or no RID received\n"); 581 } 582 } 583 584 return 0; 585 err: 586 kfree(request); 587 usb_free_urb(urb); 588 return err; 589 } 590 591 static inline int zd1201_getconfig16(struct zd1201 *zd, int rid, short *val) 592 { 593 int err; 594 __le16 zdval; 595 596 err = zd1201_getconfig(zd, rid, &zdval, sizeof(__le16)); 597 if (err) 598 return err; 599 *val = le16_to_cpu(zdval); 600 return 0; 601 } 602 603 static inline int zd1201_setconfig16(struct zd1201 *zd, int rid, short val) 604 { 605 __le16 zdval = cpu_to_le16(val); 606 return (zd1201_setconfig(zd, rid, &zdval, sizeof(__le16), 1)); 607 } 608 609 static int zd1201_drvr_start(struct zd1201 *zd) 610 { 611 int err, i; 612 short max; 613 __le16 zdmax; 614 unsigned char *buffer; 615 616 buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL); 617 if (!buffer) 618 return -ENOMEM; 619 620 usb_fill_bulk_urb(zd->rx_urb, zd->usb, 621 usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE, 622 zd1201_usbrx, zd); 623 624 err = usb_submit_urb(zd->rx_urb, GFP_KERNEL); 625 if (err) 626 goto err_buffer; 627 628 err = zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0); 629 if (err) 630 goto err_urb; 631 632 err = zd1201_getconfig(zd, ZD1201_RID_CNFMAXTXBUFFERNUMBER, &zdmax, 633 sizeof(__le16)); 634 if (err) 635 goto err_urb; 636 637 max = le16_to_cpu(zdmax); 638 for (i=0; i<max; i++) { 639 err = zd1201_docmd(zd, ZD1201_CMDCODE_ALLOC, 1514, 0, 0); 640 if (err) 641 goto err_urb; 642 } 643 644 return 0; 645 646 err_urb: 647 usb_kill_urb(zd->rx_urb); 648 return err; 649 err_buffer: 650 kfree(buffer); 651 return err; 652 } 653 654 /* Magic alert: The firmware doesn't seem to like the MAC state being 655 * toggled in promisc (aka monitor) mode. 656 * (It works a number of times, but will halt eventually) 657 * So we turn it of before disabling and on after enabling if needed. 658 */ 659 static int zd1201_enable(struct zd1201 *zd) 660 { 661 int err; 662 663 if (zd->mac_enabled) 664 return 0; 665 666 err = zd1201_docmd(zd, ZD1201_CMDCODE_ENABLE, 0, 0, 0); 667 if (!err) 668 zd->mac_enabled = 1; 669 670 if (zd->monitor) 671 err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 1); 672 673 return err; 674 } 675 676 static int zd1201_disable(struct zd1201 *zd) 677 { 678 int err; 679 680 if (!zd->mac_enabled) 681 return 0; 682 if (zd->monitor) { 683 err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0); 684 if (err) 685 return err; 686 } 687 688 err = zd1201_docmd(zd, ZD1201_CMDCODE_DISABLE, 0, 0, 0); 689 if (!err) 690 zd->mac_enabled = 0; 691 return err; 692 } 693 694 static int zd1201_mac_reset(struct zd1201 *zd) 695 { 696 if (!zd->mac_enabled) 697 return 0; 698 zd1201_disable(zd); 699 return zd1201_enable(zd); 700 } 701 702 static int zd1201_join(struct zd1201 *zd, char *essid, int essidlen) 703 { 704 int err, val; 705 char buf[IW_ESSID_MAX_SIZE+2]; 706 707 err = zd1201_disable(zd); 708 if (err) 709 return err; 710 711 val = ZD1201_CNFAUTHENTICATION_OPENSYSTEM; 712 val |= ZD1201_CNFAUTHENTICATION_SHAREDKEY; 713 err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, val); 714 if (err) 715 return err; 716 717 *(__le16 *)buf = cpu_to_le16(essidlen); 718 memcpy(buf+2, essid, essidlen); 719 if (!zd->ap) { /* Normal station */ 720 err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf, 721 IW_ESSID_MAX_SIZE+2, 1); 722 if (err) 723 return err; 724 } else { /* AP */ 725 err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNSSID, buf, 726 IW_ESSID_MAX_SIZE+2, 1); 727 if (err) 728 return err; 729 } 730 731 err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR, 732 zd->dev->dev_addr, zd->dev->addr_len, 1); 733 if (err) 734 return err; 735 736 err = zd1201_enable(zd); 737 if (err) 738 return err; 739 740 msleep(100); 741 return 0; 742 } 743 744 static int zd1201_net_open(struct net_device *dev) 745 { 746 struct zd1201 *zd = netdev_priv(dev); 747 748 /* Start MAC with wildcard if no essid set */ 749 if (!zd->mac_enabled) 750 zd1201_join(zd, zd->essid, zd->essidlen); 751 netif_start_queue(dev); 752 753 return 0; 754 } 755 756 static int zd1201_net_stop(struct net_device *dev) 757 { 758 netif_stop_queue(dev); 759 return 0; 760 } 761 762 /* 763 RFC 1042 encapsulates Ethernet frames in 802.11 frames 764 by prefixing them with 0xaa, 0xaa, 0x03) followed by a SNAP OID of 0 765 (0x00, 0x00, 0x00). Zd requires an additional padding, copy 766 of ethernet addresses, length of the standard RFC 1042 packet 767 and a command byte (which is nul for tx). 768 769 tx frame (from Wlan NG): 770 RFC 1042: 771 llc 0xAA 0xAA 0x03 (802.2 LLC) 772 snap 0x00 0x00 0x00 (Ethernet encapsulated) 773 type 2 bytes, Ethernet type field 774 payload (minus eth header) 775 Zydas specific: 776 padding 1B if (skb->len+8+1)%64==0 777 Eth MAC addr 12 bytes, Ethernet MAC addresses 778 length 2 bytes, RFC 1042 packet length 779 (llc+snap+type+payload) 780 zd 1 null byte, zd1201 packet type 781 */ 782 static netdev_tx_t zd1201_hard_start_xmit(struct sk_buff *skb, 783 struct net_device *dev) 784 { 785 struct zd1201 *zd = netdev_priv(dev); 786 unsigned char *txbuf = zd->txdata; 787 int txbuflen, pad = 0, err; 788 struct urb *urb = zd->tx_urb; 789 790 if (!zd->mac_enabled || zd->monitor) { 791 dev->stats.tx_dropped++; 792 kfree_skb(skb); 793 return NETDEV_TX_OK; 794 } 795 netif_stop_queue(dev); 796 797 txbuflen = skb->len + 8 + 1; 798 if (txbuflen%64 == 0) { 799 pad = 1; 800 txbuflen++; 801 } 802 txbuf[0] = 0xAA; 803 txbuf[1] = 0xAA; 804 txbuf[2] = 0x03; 805 txbuf[3] = 0x00; /* rfc1042 */ 806 txbuf[4] = 0x00; 807 txbuf[5] = 0x00; 808 809 skb_copy_from_linear_data_offset(skb, 12, txbuf + 6, skb->len - 12); 810 if (pad) 811 txbuf[skb->len-12+6]=0; 812 skb_copy_from_linear_data(skb, txbuf + skb->len - 12 + 6 + pad, 12); 813 *(__be16*)&txbuf[skb->len+6+pad] = htons(skb->len-12+6); 814 txbuf[txbuflen-1] = 0; 815 816 usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out), 817 txbuf, txbuflen, zd1201_usbtx, zd); 818 819 err = usb_submit_urb(zd->tx_urb, GFP_ATOMIC); 820 if (err) { 821 dev->stats.tx_errors++; 822 netif_start_queue(dev); 823 } else { 824 dev->stats.tx_packets++; 825 dev->stats.tx_bytes += skb->len; 826 } 827 kfree_skb(skb); 828 829 return NETDEV_TX_OK; 830 } 831 832 static void zd1201_tx_timeout(struct net_device *dev, unsigned int txqueue) 833 { 834 struct zd1201 *zd = netdev_priv(dev); 835 836 if (!zd) 837 return; 838 dev_warn(&zd->usb->dev, "%s: TX timeout, shooting down urb\n", 839 dev->name); 840 usb_unlink_urb(zd->tx_urb); 841 dev->stats.tx_errors++; 842 /* Restart the timeout to quiet the watchdog: */ 843 netif_trans_update(dev); /* prevent tx timeout */ 844 } 845 846 static int zd1201_set_mac_address(struct net_device *dev, void *p) 847 { 848 struct sockaddr *addr = p; 849 struct zd1201 *zd = netdev_priv(dev); 850 int err; 851 852 if (!zd) 853 return -ENODEV; 854 855 err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR, 856 addr->sa_data, dev->addr_len, 1); 857 if (err) 858 return err; 859 eth_hw_addr_set(dev, addr->sa_data); 860 861 return zd1201_mac_reset(zd); 862 } 863 864 static struct iw_statistics *zd1201_get_wireless_stats(struct net_device *dev) 865 { 866 struct zd1201 *zd = netdev_priv(dev); 867 868 return &zd->iwstats; 869 } 870 871 static void zd1201_set_multicast(struct net_device *dev) 872 { 873 struct zd1201 *zd = netdev_priv(dev); 874 struct netdev_hw_addr *ha; 875 unsigned char reqbuf[ETH_ALEN*ZD1201_MAXMULTI]; 876 int i; 877 878 if (netdev_mc_count(dev) > ZD1201_MAXMULTI) 879 return; 880 881 i = 0; 882 netdev_for_each_mc_addr(ha, dev) 883 memcpy(reqbuf + i++ * ETH_ALEN, ha->addr, ETH_ALEN); 884 zd1201_setconfig(zd, ZD1201_RID_CNFGROUPADDRESS, reqbuf, 885 netdev_mc_count(dev) * ETH_ALEN, 0); 886 } 887 888 static int zd1201_config_commit(struct net_device *dev, 889 struct iw_request_info *info, union iwreq_data *wrqu, char *essid) 890 { 891 struct zd1201 *zd = netdev_priv(dev); 892 893 return zd1201_mac_reset(zd); 894 } 895 896 static int zd1201_get_name(struct net_device *dev, 897 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 898 { 899 strcpy(wrqu->name, "IEEE 802.11b"); 900 return 0; 901 } 902 903 static int zd1201_set_freq(struct net_device *dev, 904 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 905 { 906 struct iw_freq *freq = &wrqu->freq; 907 struct zd1201 *zd = netdev_priv(dev); 908 short channel = 0; 909 int err; 910 911 if (freq->e == 0) 912 channel = freq->m; 913 else 914 channel = ieee80211_frequency_to_channel(freq->m); 915 916 err = zd1201_setconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, channel); 917 if (err) 918 return err; 919 920 zd1201_mac_reset(zd); 921 922 return 0; 923 } 924 925 static int zd1201_get_freq(struct net_device *dev, 926 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 927 { 928 struct iw_freq *freq = &wrqu->freq; 929 struct zd1201 *zd = netdev_priv(dev); 930 short channel; 931 int err; 932 933 err = zd1201_getconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, &channel); 934 if (err) 935 return err; 936 freq->e = 0; 937 freq->m = channel; 938 939 return 0; 940 } 941 942 static int zd1201_set_mode(struct net_device *dev, 943 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 944 { 945 __u32 *mode = &wrqu->mode; 946 struct zd1201 *zd = netdev_priv(dev); 947 short porttype, monitor = 0; 948 unsigned char buffer[IW_ESSID_MAX_SIZE+2]; 949 int err; 950 951 if (zd->ap) { 952 if (*mode != IW_MODE_MASTER) 953 return -EINVAL; 954 return 0; 955 } 956 957 err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0); 958 if (err) 959 return err; 960 zd->dev->type = ARPHRD_ETHER; 961 switch(*mode) { 962 case IW_MODE_MONITOR: 963 monitor = 1; 964 zd->dev->type = ARPHRD_IEEE80211; 965 /* Make sure we are no longer associated with by 966 setting an 'impossible' essid. 967 (otherwise we mess up firmware) 968 */ 969 zd1201_join(zd, "\0-*#\0", 5); 970 /* Put port in pIBSS */ 971 fallthrough; 972 case 8: /* No pseudo-IBSS in wireless extensions (yet) */ 973 porttype = ZD1201_PORTTYPE_PSEUDOIBSS; 974 break; 975 case IW_MODE_ADHOC: 976 porttype = ZD1201_PORTTYPE_IBSS; 977 break; 978 case IW_MODE_INFRA: 979 porttype = ZD1201_PORTTYPE_BSS; 980 break; 981 default: 982 return -EINVAL; 983 } 984 985 err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype); 986 if (err) 987 return err; 988 if (zd->monitor && !monitor) { 989 zd1201_disable(zd); 990 *(__le16 *)buffer = cpu_to_le16(zd->essidlen); 991 memcpy(buffer+2, zd->essid, zd->essidlen); 992 err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, 993 buffer, IW_ESSID_MAX_SIZE+2, 1); 994 if (err) 995 return err; 996 } 997 zd->monitor = monitor; 998 /* If monitor mode is set we don't actually turn it on here since it 999 * is done during mac reset anyway (see zd1201_mac_enable). 1000 */ 1001 zd1201_mac_reset(zd); 1002 1003 return 0; 1004 } 1005 1006 static int zd1201_get_mode(struct net_device *dev, 1007 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1008 { 1009 __u32 *mode = &wrqu->mode; 1010 struct zd1201 *zd = netdev_priv(dev); 1011 short porttype; 1012 int err; 1013 1014 err = zd1201_getconfig16(zd, ZD1201_RID_CNFPORTTYPE, &porttype); 1015 if (err) 1016 return err; 1017 switch(porttype) { 1018 case ZD1201_PORTTYPE_IBSS: 1019 *mode = IW_MODE_ADHOC; 1020 break; 1021 case ZD1201_PORTTYPE_BSS: 1022 *mode = IW_MODE_INFRA; 1023 break; 1024 case ZD1201_PORTTYPE_WDS: 1025 *mode = IW_MODE_REPEAT; 1026 break; 1027 case ZD1201_PORTTYPE_PSEUDOIBSS: 1028 *mode = 8;/* No Pseudo-IBSS... */ 1029 break; 1030 case ZD1201_PORTTYPE_AP: 1031 *mode = IW_MODE_MASTER; 1032 break; 1033 default: 1034 dev_dbg(&zd->usb->dev, "Unknown porttype: %d\n", 1035 porttype); 1036 *mode = IW_MODE_AUTO; 1037 } 1038 if (zd->monitor) 1039 *mode = IW_MODE_MONITOR; 1040 1041 return 0; 1042 } 1043 1044 static int zd1201_get_range(struct net_device *dev, 1045 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1046 { 1047 struct iw_point *wrq = &wrqu->data; 1048 struct iw_range *range = (struct iw_range *)extra; 1049 1050 wrq->length = sizeof(struct iw_range); 1051 memset(range, 0, sizeof(struct iw_range)); 1052 range->we_version_compiled = WIRELESS_EXT; 1053 range->we_version_source = WIRELESS_EXT; 1054 1055 range->max_qual.qual = 128; 1056 range->max_qual.level = 128; 1057 range->max_qual.noise = 128; 1058 range->max_qual.updated = 7; 1059 1060 range->encoding_size[0] = 5; 1061 range->encoding_size[1] = 13; 1062 range->num_encoding_sizes = 2; 1063 range->max_encoding_tokens = ZD1201_NUMKEYS; 1064 1065 range->num_bitrates = 4; 1066 range->bitrate[0] = 1000000; 1067 range->bitrate[1] = 2000000; 1068 range->bitrate[2] = 5500000; 1069 range->bitrate[3] = 11000000; 1070 1071 range->min_rts = 0; 1072 range->min_frag = ZD1201_FRAGMIN; 1073 range->max_rts = ZD1201_RTSMAX; 1074 range->min_frag = ZD1201_FRAGMAX; 1075 1076 return 0; 1077 } 1078 1079 /* Little bit of magic here: we only get the quality if we poll 1080 * for it, and we never get an actual request to trigger such 1081 * a poll. Therefore we 'assume' that the user will soon ask for 1082 * the stats after asking the bssid. 1083 */ 1084 static int zd1201_get_wap(struct net_device *dev, 1085 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1086 { 1087 struct sockaddr *ap_addr = &wrqu->ap_addr; 1088 struct zd1201 *zd = netdev_priv(dev); 1089 unsigned char buffer[6]; 1090 1091 if (!zd1201_getconfig(zd, ZD1201_RID_COMMSQUALITY, buffer, 6)) { 1092 /* Unfortunately the quality and noise reported is useless. 1093 they seem to be accumulators that increase until you 1094 read them, unless we poll on a fixed interval we can't 1095 use them 1096 */ 1097 /*zd->iwstats.qual.qual = le16_to_cpu(((__le16 *)buffer)[0]);*/ 1098 zd->iwstats.qual.level = le16_to_cpu(((__le16 *)buffer)[1]); 1099 /*zd->iwstats.qual.noise = le16_to_cpu(((__le16 *)buffer)[2]);*/ 1100 zd->iwstats.qual.updated = 2; 1101 } 1102 1103 return zd1201_getconfig(zd, ZD1201_RID_CURRENTBSSID, ap_addr->sa_data, 6); 1104 } 1105 1106 static int zd1201_set_scan(struct net_device *dev, 1107 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1108 { 1109 /* We do everything in get_scan */ 1110 return 0; 1111 } 1112 1113 static int zd1201_get_scan(struct net_device *dev, 1114 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1115 { 1116 struct iw_point *srq = &wrqu->data; 1117 struct zd1201 *zd = netdev_priv(dev); 1118 int err, i, j, enabled_save; 1119 struct iw_event iwe; 1120 char *cev = extra; 1121 char *end_buf = extra + IW_SCAN_MAX_DATA; 1122 1123 /* No scanning in AP mode */ 1124 if (zd->ap) 1125 return -EOPNOTSUPP; 1126 1127 /* Scan doesn't seem to work if disabled */ 1128 enabled_save = zd->mac_enabled; 1129 zd1201_enable(zd); 1130 1131 zd->rxdatas = 0; 1132 err = zd1201_docmd(zd, ZD1201_CMDCODE_INQUIRE, 1133 ZD1201_INQ_SCANRESULTS, 0, 0); 1134 if (err) 1135 return err; 1136 1137 wait_event_interruptible(zd->rxdataq, zd->rxdatas); 1138 if (!zd->rxlen) 1139 return -EIO; 1140 1141 if (le16_to_cpu(*(__le16*)&zd->rxdata[2]) != ZD1201_INQ_SCANRESULTS) 1142 return -EIO; 1143 1144 for(i=8; i<zd->rxlen; i+=62) { 1145 iwe.cmd = SIOCGIWAP; 1146 iwe.u.ap_addr.sa_family = ARPHRD_ETHER; 1147 memcpy(iwe.u.ap_addr.sa_data, zd->rxdata+i+6, 6); 1148 cev = iwe_stream_add_event(info, cev, end_buf, 1149 &iwe, IW_EV_ADDR_LEN); 1150 1151 iwe.cmd = SIOCGIWESSID; 1152 iwe.u.data.length = zd->rxdata[i+16]; 1153 iwe.u.data.flags = 1; 1154 cev = iwe_stream_add_point(info, cev, end_buf, 1155 &iwe, zd->rxdata+i+18); 1156 1157 iwe.cmd = SIOCGIWMODE; 1158 if (zd->rxdata[i+14]&0x01) 1159 iwe.u.mode = IW_MODE_MASTER; 1160 else 1161 iwe.u.mode = IW_MODE_ADHOC; 1162 cev = iwe_stream_add_event(info, cev, end_buf, 1163 &iwe, IW_EV_UINT_LEN); 1164 1165 iwe.cmd = SIOCGIWFREQ; 1166 iwe.u.freq.m = zd->rxdata[i+0]; 1167 iwe.u.freq.e = 0; 1168 cev = iwe_stream_add_event(info, cev, end_buf, 1169 &iwe, IW_EV_FREQ_LEN); 1170 1171 iwe.cmd = SIOCGIWRATE; 1172 iwe.u.bitrate.fixed = 0; 1173 iwe.u.bitrate.disabled = 0; 1174 for (j=0; j<10; j++) if (zd->rxdata[i+50+j]) { 1175 iwe.u.bitrate.value = (zd->rxdata[i+50+j]&0x7f)*500000; 1176 cev = iwe_stream_add_event(info, cev, end_buf, 1177 &iwe, IW_EV_PARAM_LEN); 1178 } 1179 1180 iwe.cmd = SIOCGIWENCODE; 1181 iwe.u.data.length = 0; 1182 if (zd->rxdata[i+14]&0x10) 1183 iwe.u.data.flags = IW_ENCODE_ENABLED; 1184 else 1185 iwe.u.data.flags = IW_ENCODE_DISABLED; 1186 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, NULL); 1187 1188 iwe.cmd = IWEVQUAL; 1189 iwe.u.qual.qual = zd->rxdata[i+4]; 1190 iwe.u.qual.noise= zd->rxdata[i+2]/10-100; 1191 iwe.u.qual.level = (256+zd->rxdata[i+4]*100)/255-100; 1192 iwe.u.qual.updated = 7; 1193 cev = iwe_stream_add_event(info, cev, end_buf, 1194 &iwe, IW_EV_QUAL_LEN); 1195 } 1196 1197 if (!enabled_save) 1198 zd1201_disable(zd); 1199 1200 srq->length = cev - extra; 1201 srq->flags = 0; 1202 1203 return 0; 1204 } 1205 1206 static int zd1201_set_essid(struct net_device *dev, 1207 struct iw_request_info *info, union iwreq_data *wrqu, char *essid) 1208 { 1209 struct iw_point *data = &wrqu->data; 1210 struct zd1201 *zd = netdev_priv(dev); 1211 1212 if (data->length > IW_ESSID_MAX_SIZE) 1213 return -EINVAL; 1214 if (data->length < 1) 1215 data->length = 1; 1216 zd->essidlen = data->length; 1217 memset(zd->essid, 0, IW_ESSID_MAX_SIZE+1); 1218 memcpy(zd->essid, essid, data->length); 1219 return zd1201_join(zd, zd->essid, zd->essidlen); 1220 } 1221 1222 static int zd1201_get_essid(struct net_device *dev, 1223 struct iw_request_info *info, union iwreq_data *wrqu, char *essid) 1224 { 1225 struct iw_point *data = &wrqu->data; 1226 struct zd1201 *zd = netdev_priv(dev); 1227 1228 memcpy(essid, zd->essid, zd->essidlen); 1229 data->flags = 1; 1230 data->length = zd->essidlen; 1231 1232 return 0; 1233 } 1234 1235 static int zd1201_get_nick(struct net_device *dev, struct iw_request_info *info, 1236 union iwreq_data *wrqu, char *nick) 1237 { 1238 struct iw_point *data = &wrqu->data; 1239 strcpy(nick, "zd1201"); 1240 data->flags = 1; 1241 data->length = strlen(nick); 1242 return 0; 1243 } 1244 1245 static int zd1201_set_rate(struct net_device *dev, 1246 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1247 { 1248 struct iw_param *rrq = &wrqu->bitrate; 1249 struct zd1201 *zd = netdev_priv(dev); 1250 short rate; 1251 int err; 1252 1253 switch (rrq->value) { 1254 case 1000000: 1255 rate = ZD1201_RATEB1; 1256 break; 1257 case 2000000: 1258 rate = ZD1201_RATEB2; 1259 break; 1260 case 5500000: 1261 rate = ZD1201_RATEB5; 1262 break; 1263 case 11000000: 1264 default: 1265 rate = ZD1201_RATEB11; 1266 break; 1267 } 1268 if (!rrq->fixed) { /* Also enable all lower bitrates */ 1269 rate |= rate-1; 1270 } 1271 1272 err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL, rate); 1273 if (err) 1274 return err; 1275 1276 return zd1201_mac_reset(zd); 1277 } 1278 1279 static int zd1201_get_rate(struct net_device *dev, 1280 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1281 { 1282 struct iw_param *rrq = &wrqu->bitrate; 1283 struct zd1201 *zd = netdev_priv(dev); 1284 short rate; 1285 int err; 1286 1287 err = zd1201_getconfig16(zd, ZD1201_RID_CURRENTTXRATE, &rate); 1288 if (err) 1289 return err; 1290 1291 switch(rate) { 1292 case 1: 1293 rrq->value = 1000000; 1294 break; 1295 case 2: 1296 rrq->value = 2000000; 1297 break; 1298 case 5: 1299 rrq->value = 5500000; 1300 break; 1301 case 11: 1302 rrq->value = 11000000; 1303 break; 1304 default: 1305 rrq->value = 0; 1306 } 1307 rrq->fixed = 0; 1308 rrq->disabled = 0; 1309 1310 return 0; 1311 } 1312 1313 static int zd1201_set_rts(struct net_device *dev, struct iw_request_info *info, 1314 union iwreq_data *wrqu, char *extra) 1315 { 1316 struct iw_param *rts = &wrqu->rts; 1317 struct zd1201 *zd = netdev_priv(dev); 1318 int err; 1319 short val = rts->value; 1320 1321 if (rts->disabled || !rts->fixed) 1322 val = ZD1201_RTSMAX; 1323 if (val > ZD1201_RTSMAX) 1324 return -EINVAL; 1325 if (val < 0) 1326 return -EINVAL; 1327 1328 err = zd1201_setconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, val); 1329 if (err) 1330 return err; 1331 return zd1201_mac_reset(zd); 1332 } 1333 1334 static int zd1201_get_rts(struct net_device *dev, struct iw_request_info *info, 1335 union iwreq_data *wrqu, char *extra) 1336 { 1337 struct iw_param *rts = &wrqu->rts; 1338 struct zd1201 *zd = netdev_priv(dev); 1339 short rtst; 1340 int err; 1341 1342 err = zd1201_getconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, &rtst); 1343 if (err) 1344 return err; 1345 rts->value = rtst; 1346 rts->disabled = (rts->value == ZD1201_RTSMAX); 1347 rts->fixed = 1; 1348 1349 return 0; 1350 } 1351 1352 static int zd1201_set_frag(struct net_device *dev, struct iw_request_info *info, 1353 union iwreq_data *wrqu, char *extra) 1354 { 1355 struct iw_param *frag = &wrqu->frag; 1356 struct zd1201 *zd = netdev_priv(dev); 1357 int err; 1358 short val = frag->value; 1359 1360 if (frag->disabled || !frag->fixed) 1361 val = ZD1201_FRAGMAX; 1362 if (val > ZD1201_FRAGMAX) 1363 return -EINVAL; 1364 if (val < ZD1201_FRAGMIN) 1365 return -EINVAL; 1366 if (val & 1) 1367 return -EINVAL; 1368 err = zd1201_setconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, val); 1369 if (err) 1370 return err; 1371 return zd1201_mac_reset(zd); 1372 } 1373 1374 static int zd1201_get_frag(struct net_device *dev, struct iw_request_info *info, 1375 union iwreq_data *wrqu, char *extra) 1376 { 1377 struct iw_param *frag = &wrqu->frag; 1378 struct zd1201 *zd = netdev_priv(dev); 1379 short fragt; 1380 int err; 1381 1382 err = zd1201_getconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, &fragt); 1383 if (err) 1384 return err; 1385 frag->value = fragt; 1386 frag->disabled = (frag->value == ZD1201_FRAGMAX); 1387 frag->fixed = 1; 1388 1389 return 0; 1390 } 1391 1392 static int zd1201_set_retry(struct net_device *dev, 1393 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1394 { 1395 return 0; 1396 } 1397 1398 static int zd1201_get_retry(struct net_device *dev, 1399 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1400 { 1401 return 0; 1402 } 1403 1404 static int zd1201_set_encode(struct net_device *dev, 1405 struct iw_request_info *info, union iwreq_data *wrqu, char *key) 1406 { 1407 struct iw_point *erq = &wrqu->encoding; 1408 struct zd1201 *zd = netdev_priv(dev); 1409 short i; 1410 int err, rid; 1411 1412 if (erq->length > ZD1201_MAXKEYLEN) 1413 return -EINVAL; 1414 1415 i = (erq->flags & IW_ENCODE_INDEX)-1; 1416 if (i == -1) { 1417 err = zd1201_getconfig16(zd,ZD1201_RID_CNFDEFAULTKEYID,&i); 1418 if (err) 1419 return err; 1420 } else { 1421 err = zd1201_setconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, i); 1422 if (err) 1423 return err; 1424 } 1425 1426 if (i < 0 || i >= ZD1201_NUMKEYS) 1427 return -EINVAL; 1428 1429 rid = ZD1201_RID_CNFDEFAULTKEY0 + i; 1430 err = zd1201_setconfig(zd, rid, key, erq->length, 1); 1431 if (err) 1432 return err; 1433 zd->encode_keylen[i] = erq->length; 1434 memcpy(zd->encode_keys[i], key, erq->length); 1435 1436 i=0; 1437 if (!(erq->flags & IW_ENCODE_DISABLED & IW_ENCODE_MODE)) { 1438 i |= 0x01; 1439 zd->encode_enabled = 1; 1440 } else 1441 zd->encode_enabled = 0; 1442 if (erq->flags & IW_ENCODE_RESTRICTED & IW_ENCODE_MODE) { 1443 i |= 0x02; 1444 zd->encode_restricted = 1; 1445 } else 1446 zd->encode_restricted = 0; 1447 err = zd1201_setconfig16(zd, ZD1201_RID_CNFWEBFLAGS, i); 1448 if (err) 1449 return err; 1450 1451 if (zd->encode_enabled) 1452 i = ZD1201_CNFAUTHENTICATION_SHAREDKEY; 1453 else 1454 i = ZD1201_CNFAUTHENTICATION_OPENSYSTEM; 1455 err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, i); 1456 if (err) 1457 return err; 1458 1459 return zd1201_mac_reset(zd); 1460 } 1461 1462 static int zd1201_get_encode(struct net_device *dev, 1463 struct iw_request_info *info, union iwreq_data *wrqu, char *key) 1464 { 1465 struct iw_point *erq = &wrqu->encoding; 1466 struct zd1201 *zd = netdev_priv(dev); 1467 short i; 1468 int err; 1469 1470 if (zd->encode_enabled) 1471 erq->flags = IW_ENCODE_ENABLED; 1472 else 1473 erq->flags = IW_ENCODE_DISABLED; 1474 if (zd->encode_restricted) 1475 erq->flags |= IW_ENCODE_RESTRICTED; 1476 else 1477 erq->flags |= IW_ENCODE_OPEN; 1478 1479 i = (erq->flags & IW_ENCODE_INDEX) -1; 1480 if (i == -1) { 1481 err = zd1201_getconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, &i); 1482 if (err) 1483 return err; 1484 } 1485 if (i<0 || i>= ZD1201_NUMKEYS) 1486 return -EINVAL; 1487 1488 erq->flags |= i+1; 1489 1490 erq->length = zd->encode_keylen[i]; 1491 memcpy(key, zd->encode_keys[i], erq->length); 1492 1493 return 0; 1494 } 1495 1496 static int zd1201_set_power(struct net_device *dev, 1497 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1498 { 1499 struct iw_param *vwrq = &wrqu->power; 1500 struct zd1201 *zd = netdev_priv(dev); 1501 short enabled, duration, level; 1502 int err; 1503 1504 enabled = vwrq->disabled ? 0 : 1; 1505 if (enabled) { 1506 if (vwrq->flags & IW_POWER_PERIOD) { 1507 duration = vwrq->value; 1508 err = zd1201_setconfig16(zd, 1509 ZD1201_RID_CNFMAXSLEEPDURATION, duration); 1510 if (err) 1511 return err; 1512 goto out; 1513 } 1514 if (vwrq->flags & IW_POWER_TIMEOUT) { 1515 err = zd1201_getconfig16(zd, 1516 ZD1201_RID_CNFMAXSLEEPDURATION, &duration); 1517 if (err) 1518 return err; 1519 level = vwrq->value * 4 / duration; 1520 if (level > 4) 1521 level = 4; 1522 if (level < 0) 1523 level = 0; 1524 err = zd1201_setconfig16(zd, ZD1201_RID_CNFPMEPS, 1525 level); 1526 if (err) 1527 return err; 1528 goto out; 1529 } 1530 return -EINVAL; 1531 } 1532 out: 1533 return zd1201_setconfig16(zd, ZD1201_RID_CNFPMENABLED, enabled); 1534 } 1535 1536 static int zd1201_get_power(struct net_device *dev, 1537 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1538 { 1539 struct iw_param *vwrq = &wrqu->power; 1540 struct zd1201 *zd = netdev_priv(dev); 1541 short enabled, level, duration; 1542 int err; 1543 1544 err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMENABLED, &enabled); 1545 if (err) 1546 return err; 1547 err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMEPS, &level); 1548 if (err) 1549 return err; 1550 err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXSLEEPDURATION, &duration); 1551 if (err) 1552 return err; 1553 vwrq->disabled = enabled ? 0 : 1; 1554 if (vwrq->flags & IW_POWER_TYPE) { 1555 if (vwrq->flags & IW_POWER_PERIOD) { 1556 vwrq->value = duration; 1557 vwrq->flags = IW_POWER_PERIOD; 1558 } else { 1559 vwrq->value = duration * level / 4; 1560 vwrq->flags = IW_POWER_TIMEOUT; 1561 } 1562 } 1563 if (vwrq->flags & IW_POWER_MODE) { 1564 if (enabled && level) 1565 vwrq->flags = IW_POWER_UNICAST_R; 1566 else 1567 vwrq->flags = IW_POWER_ALL_R; 1568 } 1569 1570 return 0; 1571 } 1572 1573 1574 static const iw_handler zd1201_iw_handler[] = 1575 { 1576 IW_HANDLER(SIOCSIWCOMMIT, zd1201_config_commit), 1577 IW_HANDLER(SIOCGIWNAME, zd1201_get_name), 1578 IW_HANDLER(SIOCSIWFREQ, zd1201_set_freq), 1579 IW_HANDLER(SIOCGIWFREQ, zd1201_get_freq), 1580 IW_HANDLER(SIOCSIWMODE, zd1201_set_mode), 1581 IW_HANDLER(SIOCGIWMODE, zd1201_get_mode), 1582 IW_HANDLER(SIOCGIWRANGE, zd1201_get_range), 1583 IW_HANDLER(SIOCGIWAP, zd1201_get_wap), 1584 IW_HANDLER(SIOCSIWSCAN, zd1201_set_scan), 1585 IW_HANDLER(SIOCGIWSCAN, zd1201_get_scan), 1586 IW_HANDLER(SIOCSIWESSID, zd1201_set_essid), 1587 IW_HANDLER(SIOCGIWESSID, zd1201_get_essid), 1588 IW_HANDLER(SIOCGIWNICKN, zd1201_get_nick), 1589 IW_HANDLER(SIOCSIWRATE, zd1201_set_rate), 1590 IW_HANDLER(SIOCGIWRATE, zd1201_get_rate), 1591 IW_HANDLER(SIOCSIWRTS, zd1201_set_rts), 1592 IW_HANDLER(SIOCGIWRTS, zd1201_get_rts), 1593 IW_HANDLER(SIOCSIWFRAG, zd1201_set_frag), 1594 IW_HANDLER(SIOCGIWFRAG, zd1201_get_frag), 1595 IW_HANDLER(SIOCSIWRETRY, zd1201_set_retry), 1596 IW_HANDLER(SIOCGIWRETRY, zd1201_get_retry), 1597 IW_HANDLER(SIOCSIWENCODE, zd1201_set_encode), 1598 IW_HANDLER(SIOCGIWENCODE, zd1201_get_encode), 1599 IW_HANDLER(SIOCSIWPOWER, zd1201_set_power), 1600 IW_HANDLER(SIOCGIWPOWER, zd1201_get_power), 1601 }; 1602 1603 static int zd1201_set_hostauth(struct net_device *dev, 1604 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1605 { 1606 struct iw_param *rrq = &wrqu->param; 1607 struct zd1201 *zd = netdev_priv(dev); 1608 1609 if (!zd->ap) 1610 return -EOPNOTSUPP; 1611 1612 return zd1201_setconfig16(zd, ZD1201_RID_CNFHOSTAUTH, rrq->value); 1613 } 1614 1615 static int zd1201_get_hostauth(struct net_device *dev, 1616 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1617 { 1618 struct iw_param *rrq = &wrqu->param; 1619 struct zd1201 *zd = netdev_priv(dev); 1620 short hostauth; 1621 int err; 1622 1623 if (!zd->ap) 1624 return -EOPNOTSUPP; 1625 1626 err = zd1201_getconfig16(zd, ZD1201_RID_CNFHOSTAUTH, &hostauth); 1627 if (err) 1628 return err; 1629 rrq->value = hostauth; 1630 rrq->fixed = 1; 1631 1632 return 0; 1633 } 1634 1635 static int zd1201_auth_sta(struct net_device *dev, 1636 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1637 { 1638 struct sockaddr *sta = &wrqu->ap_addr; 1639 struct zd1201 *zd = netdev_priv(dev); 1640 unsigned char buffer[10]; 1641 1642 if (!zd->ap) 1643 return -EOPNOTSUPP; 1644 1645 memcpy(buffer, sta->sa_data, ETH_ALEN); 1646 *(short*)(buffer+6) = 0; /* 0==success, 1==failure */ 1647 *(short*)(buffer+8) = 0; 1648 1649 return zd1201_setconfig(zd, ZD1201_RID_AUTHENTICATESTA, buffer, 10, 1); 1650 } 1651 1652 static int zd1201_set_maxassoc(struct net_device *dev, 1653 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1654 { 1655 struct iw_param *rrq = &wrqu->param; 1656 struct zd1201 *zd = netdev_priv(dev); 1657 1658 if (!zd->ap) 1659 return -EOPNOTSUPP; 1660 1661 return zd1201_setconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, rrq->value); 1662 } 1663 1664 static int zd1201_get_maxassoc(struct net_device *dev, 1665 struct iw_request_info *info, union iwreq_data *wrqu, char *extra) 1666 { 1667 struct iw_param *rrq = &wrqu->param; 1668 struct zd1201 *zd = netdev_priv(dev); 1669 short maxassoc; 1670 int err; 1671 1672 if (!zd->ap) 1673 return -EOPNOTSUPP; 1674 1675 err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, &maxassoc); 1676 if (err) 1677 return err; 1678 rrq->value = maxassoc; 1679 rrq->fixed = 1; 1680 1681 return 0; 1682 } 1683 1684 static const iw_handler zd1201_private_handler[] = { 1685 zd1201_set_hostauth, /* ZD1201SIWHOSTAUTH */ 1686 zd1201_get_hostauth, /* ZD1201GIWHOSTAUTH */ 1687 zd1201_auth_sta, /* ZD1201SIWAUTHSTA */ 1688 NULL, /* nothing to get */ 1689 zd1201_set_maxassoc, /* ZD1201SIMAXASSOC */ 1690 zd1201_get_maxassoc, /* ZD1201GIMAXASSOC */ 1691 }; 1692 1693 static const struct iw_priv_args zd1201_private_args[] = { 1694 { ZD1201SIWHOSTAUTH, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 1695 IW_PRIV_TYPE_NONE, "sethostauth" }, 1696 { ZD1201GIWHOSTAUTH, IW_PRIV_TYPE_NONE, 1697 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostauth" }, 1698 { ZD1201SIWAUTHSTA, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1, 1699 IW_PRIV_TYPE_NONE, "authstation" }, 1700 { ZD1201SIWMAXASSOC, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 1701 IW_PRIV_TYPE_NONE, "setmaxassoc" }, 1702 { ZD1201GIWMAXASSOC, IW_PRIV_TYPE_NONE, 1703 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmaxassoc" }, 1704 }; 1705 1706 static const struct iw_handler_def zd1201_iw_handlers = { 1707 .num_standard = ARRAY_SIZE(zd1201_iw_handler), 1708 .num_private = ARRAY_SIZE(zd1201_private_handler), 1709 .num_private_args = ARRAY_SIZE(zd1201_private_args), 1710 .standard = zd1201_iw_handler, 1711 .private = zd1201_private_handler, 1712 .private_args = (struct iw_priv_args *) zd1201_private_args, 1713 .get_wireless_stats = zd1201_get_wireless_stats, 1714 }; 1715 1716 static const struct net_device_ops zd1201_netdev_ops = { 1717 .ndo_open = zd1201_net_open, 1718 .ndo_stop = zd1201_net_stop, 1719 .ndo_start_xmit = zd1201_hard_start_xmit, 1720 .ndo_tx_timeout = zd1201_tx_timeout, 1721 .ndo_set_rx_mode = zd1201_set_multicast, 1722 .ndo_set_mac_address = zd1201_set_mac_address, 1723 .ndo_validate_addr = eth_validate_addr, 1724 }; 1725 1726 static int zd1201_probe(struct usb_interface *interface, 1727 const struct usb_device_id *id) 1728 { 1729 struct zd1201 *zd; 1730 struct net_device *dev; 1731 struct usb_device *usb; 1732 int err; 1733 short porttype; 1734 char buf[IW_ESSID_MAX_SIZE+2]; 1735 u8 addr[ETH_ALEN]; 1736 1737 usb = interface_to_usbdev(interface); 1738 1739 dev = alloc_etherdev(sizeof(*zd)); 1740 if (!dev) 1741 return -ENOMEM; 1742 zd = netdev_priv(dev); 1743 zd->dev = dev; 1744 1745 zd->ap = ap; 1746 zd->usb = usb; 1747 zd->removed = 0; 1748 init_waitqueue_head(&zd->rxdataq); 1749 INIT_HLIST_HEAD(&zd->fraglist); 1750 1751 err = zd1201_fw_upload(usb, zd->ap); 1752 if (err) { 1753 dev_err(&usb->dev, "zd1201 firmware upload failed: %d\n", err); 1754 goto err_zd; 1755 } 1756 1757 zd->endp_in = 1; 1758 zd->endp_out = 1; 1759 zd->endp_out2 = 2; 1760 zd->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 1761 zd->tx_urb = usb_alloc_urb(0, GFP_KERNEL); 1762 if (!zd->rx_urb || !zd->tx_urb) { 1763 err = -ENOMEM; 1764 goto err_zd; 1765 } 1766 1767 mdelay(100); 1768 err = zd1201_drvr_start(zd); 1769 if (err) 1770 goto err_zd; 1771 1772 err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXDATALEN, 2312); 1773 if (err) 1774 goto err_start; 1775 1776 err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL, 1777 ZD1201_RATEB1 | ZD1201_RATEB2 | ZD1201_RATEB5 | ZD1201_RATEB11); 1778 if (err) 1779 goto err_start; 1780 1781 dev->netdev_ops = &zd1201_netdev_ops; 1782 dev->wireless_handlers = &zd1201_iw_handlers; 1783 dev->watchdog_timeo = ZD1201_TX_TIMEOUT; 1784 strcpy(dev->name, "wlan%d"); 1785 1786 err = zd1201_getconfig(zd, ZD1201_RID_CNFOWNMACADDR, addr, ETH_ALEN); 1787 if (err) 1788 goto err_start; 1789 eth_hw_addr_set(dev, addr); 1790 1791 /* Set wildcard essid to match zd->essid */ 1792 *(__le16 *)buf = cpu_to_le16(0); 1793 err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf, 1794 IW_ESSID_MAX_SIZE+2, 1); 1795 if (err) 1796 goto err_start; 1797 1798 if (zd->ap) 1799 porttype = ZD1201_PORTTYPE_AP; 1800 else 1801 porttype = ZD1201_PORTTYPE_BSS; 1802 err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype); 1803 if (err) 1804 goto err_start; 1805 1806 SET_NETDEV_DEV(dev, &usb->dev); 1807 1808 err = register_netdev(dev); 1809 if (err) 1810 goto err_start; 1811 dev_info(&usb->dev, "%s: ZD1201 USB Wireless interface\n", 1812 dev->name); 1813 1814 usb_set_intfdata(interface, zd); 1815 zd1201_enable(zd); /* zd1201 likes to startup enabled, */ 1816 zd1201_disable(zd); /* interfering with all the wifis in range */ 1817 return 0; 1818 1819 err_start: 1820 /* Leave the device in reset state */ 1821 zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0); 1822 err_zd: 1823 usb_free_urb(zd->tx_urb); 1824 usb_free_urb(zd->rx_urb); 1825 free_netdev(dev); 1826 return err; 1827 } 1828 1829 static void zd1201_disconnect(struct usb_interface *interface) 1830 { 1831 struct zd1201 *zd = usb_get_intfdata(interface); 1832 struct hlist_node *node2; 1833 struct zd1201_frag *frag; 1834 1835 if (!zd) 1836 return; 1837 usb_set_intfdata(interface, NULL); 1838 1839 hlist_for_each_entry_safe(frag, node2, &zd->fraglist, fnode) { 1840 hlist_del_init(&frag->fnode); 1841 kfree_skb(frag->skb); 1842 kfree(frag); 1843 } 1844 1845 if (zd->tx_urb) { 1846 usb_kill_urb(zd->tx_urb); 1847 usb_free_urb(zd->tx_urb); 1848 } 1849 if (zd->rx_urb) { 1850 usb_kill_urb(zd->rx_urb); 1851 usb_free_urb(zd->rx_urb); 1852 } 1853 1854 if (zd->dev) { 1855 unregister_netdev(zd->dev); 1856 free_netdev(zd->dev); 1857 } 1858 } 1859 1860 #ifdef CONFIG_PM 1861 1862 static int zd1201_suspend(struct usb_interface *interface, 1863 pm_message_t message) 1864 { 1865 struct zd1201 *zd = usb_get_intfdata(interface); 1866 1867 netif_device_detach(zd->dev); 1868 1869 zd->was_enabled = zd->mac_enabled; 1870 1871 if (zd->was_enabled) 1872 return zd1201_disable(zd); 1873 else 1874 return 0; 1875 } 1876 1877 static int zd1201_resume(struct usb_interface *interface) 1878 { 1879 struct zd1201 *zd = usb_get_intfdata(interface); 1880 1881 if (!zd || !zd->dev) 1882 return -ENODEV; 1883 1884 netif_device_attach(zd->dev); 1885 1886 if (zd->was_enabled) 1887 return zd1201_enable(zd); 1888 else 1889 return 0; 1890 } 1891 1892 #else 1893 1894 #define zd1201_suspend NULL 1895 #define zd1201_resume NULL 1896 1897 #endif 1898 1899 static struct usb_driver zd1201_usb = { 1900 .name = "zd1201", 1901 .probe = zd1201_probe, 1902 .disconnect = zd1201_disconnect, 1903 .id_table = zd1201_table, 1904 .suspend = zd1201_suspend, 1905 .resume = zd1201_resume, 1906 .disable_hub_initiated_lpm = 1, 1907 }; 1908 1909 module_usb_driver(zd1201_usb); 1910