1 /* 2 * USB Orinoco driver 3 * 4 * Copyright (c) 2003 Manuel Estrada Sainz 5 * 6 * The contents of this file are subject to the Mozilla Public License 7 * Version 1.1 (the "License"); you may not use this file except in 8 * compliance with the License. You may obtain a copy of the License 9 * at http://www.mozilla.org/MPL/ 10 * 11 * Software distributed under the License is distributed on an "AS IS" 12 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 13 * the License for the specific language governing rights and 14 * limitations under the License. 15 * 16 * Alternatively, the contents of this file may be used under the 17 * terms of the GNU General Public License version 2 (the "GPL"), in 18 * which case the provisions of the GPL are applicable instead of the 19 * above. If you wish to allow the use of your version of this file 20 * only under the terms of the GPL and not to allow others to use your 21 * version of this file under the MPL, indicate your decision by 22 * deleting the provisions above and replace them with the notice and 23 * other provisions required by the GPL. If you do not delete the 24 * provisions above, a recipient may use your version of this file 25 * under either the MPL or the GPL. 26 * 27 * Queueing code based on linux-wlan-ng 0.2.1-pre5 28 * 29 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved. 30 * 31 * The license is the same as above. 32 * 33 * Initialy based on USB Skeleton driver - 0.7 34 * 35 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) 36 * 37 * This program is free software; you can redistribute it and/or 38 * modify it under the terms of the GNU General Public License as 39 * published by the Free Software Foundation; either version 2 of 40 * the License, or (at your option) any later version. 41 * 42 * NOTE: The original USB Skeleton driver is GPL, but all that code is 43 * gone so MPL/GPL applies. 44 */ 45 46 #define DRIVER_NAME "orinoco_usb" 47 #define PFX DRIVER_NAME ": " 48 49 #include <linux/module.h> 50 #include <linux/kernel.h> 51 #include <linux/sched.h> 52 #include <linux/signal.h> 53 #include <linux/errno.h> 54 #include <linux/poll.h> 55 #include <linux/slab.h> 56 #include <linux/fcntl.h> 57 #include <linux/spinlock.h> 58 #include <linux/list.h> 59 #include <linux/usb.h> 60 #include <linux/timer.h> 61 62 #include <linux/netdevice.h> 63 #include <linux/if_arp.h> 64 #include <linux/etherdevice.h> 65 #include <linux/wireless.h> 66 #include <linux/firmware.h> 67 #include <linux/refcount.h> 68 69 #include "mic.h" 70 #include "orinoco.h" 71 72 #ifndef URB_ASYNC_UNLINK 73 #define URB_ASYNC_UNLINK 0 74 #endif 75 76 struct header_struct { 77 /* 802.3 */ 78 u8 dest[ETH_ALEN]; 79 u8 src[ETH_ALEN]; 80 __be16 len; 81 /* 802.2 */ 82 u8 dsap; 83 u8 ssap; 84 u8 ctrl; 85 /* SNAP */ 86 u8 oui[3]; 87 __be16 ethertype; 88 } __packed; 89 90 struct ez_usb_fw { 91 u16 size; 92 const u8 *code; 93 }; 94 95 static struct ez_usb_fw firmware = { 96 .size = 0, 97 .code = NULL, 98 }; 99 100 /* Debugging macros */ 101 #undef err 102 #define err(format, arg...) \ 103 do { printk(KERN_ERR PFX format "\n", ## arg); } while (0) 104 105 MODULE_FIRMWARE("orinoco_ezusb_fw"); 106 107 /* 108 * Under some conditions, the card gets stuck and stops paying attention 109 * to the world (i.e. data communication stalls) until we do something to 110 * it. Sending an INQ_TALLIES command seems to be enough and should be 111 * harmless otherwise. This behaviour has been observed when using the 112 * driver on a systemimager client during installation. In the past a 113 * timer was used to send INQ_TALLIES commands when there was no other 114 * activity, but it was troublesome and was removed. 115 */ 116 117 #define USB_COMPAQ_VENDOR_ID 0x049f /* Compaq Computer Corp. */ 118 #define USB_COMPAQ_WL215_ID 0x001f /* Compaq WL215 USB Adapter */ 119 #define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */ 120 #define USB_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */ 121 122 #define USB_MELCO_VENDOR_ID 0x0411 123 #define USB_BUFFALO_L11_ID 0x0006 /* BUFFALO WLI-USB-L11 */ 124 #define USB_BUFFALO_L11G_WR_ID 0x000B /* BUFFALO WLI-USB-L11G-WR */ 125 #define USB_BUFFALO_L11G_ID 0x000D /* BUFFALO WLI-USB-L11G */ 126 127 #define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */ 128 #define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB Client */ 129 130 #define USB_AVAYA8_VENDOR_ID 0x0D98 131 #define USB_AVAYAE_VENDOR_ID 0x0D9E 132 #define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */ 133 134 #define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */ 135 #define USB_AGERE_MODEL0801_ID 0x1000 /* Wireless USB Card Model 0801 */ 136 #define USB_AGERE_MODEL0802_ID 0x1001 /* Wireless USB Card Model 0802 */ 137 #define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */ 138 139 #define USB_ELSA_VENDOR_ID 0x05CC 140 #define USB_ELSA_AIRLANCER_ID 0x3100 /* ELSA AirLancer USB-11 */ 141 142 #define USB_LEGEND_VENDOR_ID 0x0E7C 143 #define USB_LEGEND_JOYNET_ID 0x0300 /* Joynet WLAN USB Card */ 144 145 #define USB_SAMSUNG_VENDOR_ID 0x04E8 146 #define USB_SAMSUNG_SEW2001U1_ID 0x5002 /* Samsung SEW-2001u Card */ 147 #define USB_SAMSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */ 148 #define USB_SAMSUNG_SEW2003U_ID 0x7011 /* Samsung SEW-2003U Card */ 149 150 #define USB_IGATE_VENDOR_ID 0x0681 151 #define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */ 152 153 #define USB_FUJITSU_VENDOR_ID 0x0BF8 154 #define USB_FUJITSU_E1100_ID 0x1002 /* connect2AIR WLAN E-1100 USB */ 155 156 #define USB_2WIRE_VENDOR_ID 0x1630 157 #define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */ 158 159 160 #define EZUSB_REQUEST_FW_TRANS 0xA0 161 #define EZUSB_REQUEST_TRIGER 0xAA 162 #define EZUSB_REQUEST_TRIG_AC 0xAC 163 #define EZUSB_CPUCS_REG 0x7F92 164 165 #define EZUSB_RID_TX 0x0700 166 #define EZUSB_RID_RX 0x0701 167 #define EZUSB_RID_INIT1 0x0702 168 #define EZUSB_RID_ACK 0x0710 169 #define EZUSB_RID_READ_PDA 0x0800 170 #define EZUSB_RID_PROG_INIT 0x0852 171 #define EZUSB_RID_PROG_SET_ADDR 0x0853 172 #define EZUSB_RID_PROG_BYTES 0x0854 173 #define EZUSB_RID_PROG_END 0x0855 174 #define EZUSB_RID_DOCMD 0x0860 175 176 /* Recognize info frames */ 177 #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF)) 178 179 #define EZUSB_MAGIC 0x0210 180 181 #define EZUSB_FRAME_DATA 1 182 #define EZUSB_FRAME_CONTROL 2 183 184 #define DEF_TIMEOUT (3 * HZ) 185 186 #define BULK_BUF_SIZE 2048 187 188 #define MAX_DL_SIZE (BULK_BUF_SIZE - sizeof(struct ezusb_packet)) 189 190 #define FW_BUF_SIZE 64 191 #define FW_VAR_OFFSET_PTR 0x359 192 #define FW_VAR_VALUE 0 193 #define FW_HOLE_START 0x100 194 #define FW_HOLE_END 0x300 195 196 struct ezusb_packet { 197 __le16 magic; /* 0x0210 */ 198 u8 req_reply_count; 199 u8 ans_reply_count; 200 __le16 frame_type; /* 0x01 for data frames, 0x02 otherwise */ 201 __le16 size; /* transport size */ 202 __le16 crc; /* CRC up to here */ 203 __le16 hermes_len; 204 __le16 hermes_rid; 205 u8 data[0]; 206 } __packed; 207 208 /* Table of devices that work or may work with this driver */ 209 static const struct usb_device_id ezusb_table[] = { 210 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID)}, 211 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID)}, 212 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID)}, 213 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11_ID)}, 214 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_WR_ID)}, 215 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_ID)}, 216 {USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID)}, 217 {USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID)}, 218 {USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID)}, 219 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0801_ID)}, 220 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0802_ID)}, 221 {USB_DEVICE(USB_ELSA_VENDOR_ID, USB_ELSA_AIRLANCER_ID)}, 222 {USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID)}, 223 {USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U1_ID, 224 0, 0)}, 225 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U2_ID)}, 226 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2003U_ID)}, 227 {USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID)}, 228 {USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID)}, 229 {USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID)}, 230 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID)}, 231 {} /* Terminating entry */ 232 }; 233 234 MODULE_DEVICE_TABLE(usb, ezusb_table); 235 236 /* Structure to hold all of our device specific stuff */ 237 struct ezusb_priv { 238 struct usb_device *udev; 239 struct net_device *dev; 240 struct mutex mtx; 241 spinlock_t req_lock; 242 struct list_head req_pending; 243 struct list_head req_active; 244 spinlock_t reply_count_lock; 245 u16 hermes_reg_fake[0x40]; 246 u8 *bap_buf; 247 struct urb *read_urb; 248 int read_pipe; 249 int write_pipe; 250 u8 reply_count; 251 }; 252 253 enum ezusb_state { 254 EZUSB_CTX_START, 255 EZUSB_CTX_QUEUED, 256 EZUSB_CTX_REQ_SUBMITTED, 257 EZUSB_CTX_REQ_COMPLETE, 258 EZUSB_CTX_RESP_RECEIVED, 259 EZUSB_CTX_REQ_TIMEOUT, 260 EZUSB_CTX_REQ_FAILED, 261 EZUSB_CTX_RESP_TIMEOUT, 262 EZUSB_CTX_REQSUBMIT_FAIL, 263 EZUSB_CTX_COMPLETE, 264 }; 265 266 struct request_context { 267 struct list_head list; 268 refcount_t refcount; 269 struct completion done; /* Signals that CTX is dead */ 270 int killed; 271 struct urb *outurb; /* OUT for req pkt */ 272 struct ezusb_priv *upriv; 273 struct ezusb_packet *buf; 274 int buf_length; 275 struct timer_list timer; /* Timeout handling */ 276 enum ezusb_state state; /* Current state */ 277 /* the RID that we will wait for */ 278 u16 out_rid; 279 u16 in_rid; 280 }; 281 282 283 /* Forward declarations */ 284 static void ezusb_ctx_complete(struct request_context *ctx); 285 static void ezusb_req_queue_run(struct ezusb_priv *upriv); 286 static void ezusb_bulk_in_callback(struct urb *urb); 287 288 static inline u8 ezusb_reply_inc(u8 count) 289 { 290 if (count < 0x7F) 291 return count + 1; 292 else 293 return 1; 294 } 295 296 static void ezusb_request_context_put(struct request_context *ctx) 297 { 298 if (!refcount_dec_and_test(&ctx->refcount)) 299 return; 300 301 WARN_ON(!ctx->done.done); 302 BUG_ON(ctx->outurb->status == -EINPROGRESS); 303 BUG_ON(timer_pending(&ctx->timer)); 304 usb_free_urb(ctx->outurb); 305 kfree(ctx->buf); 306 kfree(ctx); 307 } 308 309 static inline void ezusb_mod_timer(struct ezusb_priv *upriv, 310 struct timer_list *timer, 311 unsigned long expire) 312 { 313 if (!upriv->udev) 314 return; 315 mod_timer(timer, expire); 316 } 317 318 static void ezusb_request_timerfn(struct timer_list *t) 319 { 320 struct request_context *ctx = from_timer(ctx, t, timer); 321 322 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; 323 if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) { 324 ctx->state = EZUSB_CTX_REQ_TIMEOUT; 325 } else { 326 ctx->state = EZUSB_CTX_RESP_TIMEOUT; 327 dev_dbg(&ctx->outurb->dev->dev, "couldn't unlink\n"); 328 refcount_inc(&ctx->refcount); 329 ctx->killed = 1; 330 ezusb_ctx_complete(ctx); 331 ezusb_request_context_put(ctx); 332 } 333 }; 334 335 static struct request_context *ezusb_alloc_ctx(struct ezusb_priv *upriv, 336 u16 out_rid, u16 in_rid) 337 { 338 struct request_context *ctx; 339 340 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); 341 if (!ctx) 342 return NULL; 343 344 ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC); 345 if (!ctx->buf) { 346 kfree(ctx); 347 return NULL; 348 } 349 ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC); 350 if (!ctx->outurb) { 351 kfree(ctx->buf); 352 kfree(ctx); 353 return NULL; 354 } 355 356 ctx->upriv = upriv; 357 ctx->state = EZUSB_CTX_START; 358 ctx->out_rid = out_rid; 359 ctx->in_rid = in_rid; 360 361 refcount_set(&ctx->refcount, 1); 362 init_completion(&ctx->done); 363 364 timer_setup(&ctx->timer, ezusb_request_timerfn, 0); 365 return ctx; 366 } 367 368 369 /* Hopefully the real complete_all will soon be exported, in the mean 370 * while this should work. */ 371 static inline void ezusb_complete_all(struct completion *comp) 372 { 373 complete(comp); 374 complete(comp); 375 complete(comp); 376 complete(comp); 377 } 378 379 static void ezusb_ctx_complete(struct request_context *ctx) 380 { 381 struct ezusb_priv *upriv = ctx->upriv; 382 unsigned long flags; 383 384 spin_lock_irqsave(&upriv->req_lock, flags); 385 386 list_del_init(&ctx->list); 387 if (upriv->udev) { 388 spin_unlock_irqrestore(&upriv->req_lock, flags); 389 ezusb_req_queue_run(upriv); 390 spin_lock_irqsave(&upriv->req_lock, flags); 391 } 392 393 switch (ctx->state) { 394 case EZUSB_CTX_COMPLETE: 395 case EZUSB_CTX_REQSUBMIT_FAIL: 396 case EZUSB_CTX_REQ_FAILED: 397 case EZUSB_CTX_REQ_TIMEOUT: 398 case EZUSB_CTX_RESP_TIMEOUT: 399 spin_unlock_irqrestore(&upriv->req_lock, flags); 400 401 if ((ctx->out_rid == EZUSB_RID_TX) && upriv->dev) { 402 struct net_device *dev = upriv->dev; 403 struct net_device_stats *stats = &dev->stats; 404 405 if (ctx->state != EZUSB_CTX_COMPLETE) 406 stats->tx_errors++; 407 else 408 stats->tx_packets++; 409 410 netif_wake_queue(dev); 411 } 412 ezusb_complete_all(&ctx->done); 413 ezusb_request_context_put(ctx); 414 break; 415 416 default: 417 spin_unlock_irqrestore(&upriv->req_lock, flags); 418 if (!upriv->udev) { 419 /* This is normal, as all request contexts get flushed 420 * when the device is disconnected */ 421 err("Called, CTX not terminating, but device gone"); 422 ezusb_complete_all(&ctx->done); 423 ezusb_request_context_put(ctx); 424 break; 425 } 426 427 err("Called, CTX not in terminating state."); 428 /* Things are really bad if this happens. Just leak 429 * the CTX because it may still be linked to the 430 * queue or the OUT urb may still be active. 431 * Just leaking at least prevents an Oops or Panic. 432 */ 433 break; 434 } 435 } 436 437 /** 438 * ezusb_req_queue_run: 439 * Description: 440 * Note: Only one active CTX at any one time, because there's no 441 * other (reliable) way to match the response URB to the correct 442 * CTX. 443 **/ 444 static void ezusb_req_queue_run(struct ezusb_priv *upriv) 445 { 446 unsigned long flags; 447 struct request_context *ctx; 448 int result; 449 450 spin_lock_irqsave(&upriv->req_lock, flags); 451 452 if (!list_empty(&upriv->req_active)) 453 goto unlock; 454 455 if (list_empty(&upriv->req_pending)) 456 goto unlock; 457 458 ctx = 459 list_entry(upriv->req_pending.next, struct request_context, 460 list); 461 462 if (!ctx->upriv->udev) 463 goto unlock; 464 465 /* We need to split this off to avoid a race condition */ 466 list_move_tail(&ctx->list, &upriv->req_active); 467 468 if (ctx->state == EZUSB_CTX_QUEUED) { 469 refcount_inc(&ctx->refcount); 470 result = usb_submit_urb(ctx->outurb, GFP_ATOMIC); 471 if (result) { 472 ctx->state = EZUSB_CTX_REQSUBMIT_FAIL; 473 474 spin_unlock_irqrestore(&upriv->req_lock, flags); 475 476 err("Fatal, failed to submit command urb." 477 " error=%d\n", result); 478 479 ezusb_ctx_complete(ctx); 480 ezusb_request_context_put(ctx); 481 goto done; 482 } 483 484 ctx->state = EZUSB_CTX_REQ_SUBMITTED; 485 ezusb_mod_timer(ctx->upriv, &ctx->timer, 486 jiffies + DEF_TIMEOUT); 487 } 488 489 unlock: 490 spin_unlock_irqrestore(&upriv->req_lock, flags); 491 492 done: 493 return; 494 } 495 496 static void ezusb_req_enqueue_run(struct ezusb_priv *upriv, 497 struct request_context *ctx) 498 { 499 unsigned long flags; 500 501 spin_lock_irqsave(&upriv->req_lock, flags); 502 503 if (!ctx->upriv->udev) { 504 spin_unlock_irqrestore(&upriv->req_lock, flags); 505 goto done; 506 } 507 refcount_inc(&ctx->refcount); 508 list_add_tail(&ctx->list, &upriv->req_pending); 509 spin_unlock_irqrestore(&upriv->req_lock, flags); 510 511 ctx->state = EZUSB_CTX_QUEUED; 512 ezusb_req_queue_run(upriv); 513 514 done: 515 return; 516 } 517 518 static void ezusb_request_out_callback(struct urb *urb) 519 { 520 unsigned long flags; 521 enum ezusb_state state; 522 struct request_context *ctx = urb->context; 523 struct ezusb_priv *upriv = ctx->upriv; 524 525 spin_lock_irqsave(&upriv->req_lock, flags); 526 527 del_timer(&ctx->timer); 528 529 if (ctx->killed) { 530 spin_unlock_irqrestore(&upriv->req_lock, flags); 531 pr_warn("interrupt called with dead ctx\n"); 532 goto out; 533 } 534 535 state = ctx->state; 536 537 if (urb->status == 0) { 538 switch (state) { 539 case EZUSB_CTX_REQ_SUBMITTED: 540 if (ctx->in_rid) { 541 ctx->state = EZUSB_CTX_REQ_COMPLETE; 542 /* reply URB still pending */ 543 ezusb_mod_timer(upriv, &ctx->timer, 544 jiffies + DEF_TIMEOUT); 545 spin_unlock_irqrestore(&upriv->req_lock, 546 flags); 547 break; 548 } 549 /* fall through */ 550 case EZUSB_CTX_RESP_RECEIVED: 551 /* IN already received before this OUT-ACK */ 552 ctx->state = EZUSB_CTX_COMPLETE; 553 spin_unlock_irqrestore(&upriv->req_lock, flags); 554 ezusb_ctx_complete(ctx); 555 break; 556 557 default: 558 spin_unlock_irqrestore(&upriv->req_lock, flags); 559 err("Unexpected state(0x%x, %d) in OUT URB", 560 state, urb->status); 561 break; 562 } 563 } else { 564 /* If someone cancels the OUT URB then its status 565 * should be either -ECONNRESET or -ENOENT. 566 */ 567 switch (state) { 568 case EZUSB_CTX_REQ_SUBMITTED: 569 case EZUSB_CTX_RESP_RECEIVED: 570 ctx->state = EZUSB_CTX_REQ_FAILED; 571 /* fall through */ 572 573 case EZUSB_CTX_REQ_FAILED: 574 case EZUSB_CTX_REQ_TIMEOUT: 575 spin_unlock_irqrestore(&upriv->req_lock, flags); 576 577 ezusb_ctx_complete(ctx); 578 break; 579 580 default: 581 spin_unlock_irqrestore(&upriv->req_lock, flags); 582 583 err("Unexpected state(0x%x, %d) in OUT URB", 584 state, urb->status); 585 break; 586 } 587 } 588 out: 589 ezusb_request_context_put(ctx); 590 } 591 592 static void ezusb_request_in_callback(struct ezusb_priv *upriv, 593 struct urb *urb) 594 { 595 struct ezusb_packet *ans = urb->transfer_buffer; 596 struct request_context *ctx = NULL; 597 enum ezusb_state state; 598 unsigned long flags; 599 600 /* Find the CTX on the active queue that requested this URB */ 601 spin_lock_irqsave(&upriv->req_lock, flags); 602 if (upriv->udev) { 603 struct list_head *item; 604 605 list_for_each(item, &upriv->req_active) { 606 struct request_context *c; 607 int reply_count; 608 609 c = list_entry(item, struct request_context, list); 610 reply_count = 611 ezusb_reply_inc(c->buf->req_reply_count); 612 if ((ans->ans_reply_count == reply_count) 613 && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) { 614 ctx = c; 615 break; 616 } 617 netdev_dbg(upriv->dev, "Skipped (0x%x/0x%x) (%d/%d)\n", 618 le16_to_cpu(ans->hermes_rid), c->in_rid, 619 ans->ans_reply_count, reply_count); 620 } 621 } 622 623 if (ctx == NULL) { 624 spin_unlock_irqrestore(&upriv->req_lock, flags); 625 err("%s: got unexpected RID: 0x%04X", __func__, 626 le16_to_cpu(ans->hermes_rid)); 627 ezusb_req_queue_run(upriv); 628 return; 629 } 630 631 /* The data we want is in the in buffer, exchange */ 632 urb->transfer_buffer = ctx->buf; 633 ctx->buf = (void *) ans; 634 ctx->buf_length = urb->actual_length; 635 636 state = ctx->state; 637 switch (state) { 638 case EZUSB_CTX_REQ_SUBMITTED: 639 /* We have received our response URB before 640 * our request has been acknowledged. Do NOT 641 * destroy our CTX yet, because our OUT URB 642 * is still alive ... 643 */ 644 ctx->state = EZUSB_CTX_RESP_RECEIVED; 645 spin_unlock_irqrestore(&upriv->req_lock, flags); 646 647 /* Let the machine continue running. */ 648 break; 649 650 case EZUSB_CTX_REQ_COMPLETE: 651 /* This is the usual path: our request 652 * has already been acknowledged, and 653 * we have now received the reply. 654 */ 655 ctx->state = EZUSB_CTX_COMPLETE; 656 657 /* Stop the intimer */ 658 del_timer(&ctx->timer); 659 spin_unlock_irqrestore(&upriv->req_lock, flags); 660 661 /* Call the completion handler */ 662 ezusb_ctx_complete(ctx); 663 break; 664 665 default: 666 spin_unlock_irqrestore(&upriv->req_lock, flags); 667 668 pr_warn("Matched IN URB, unexpected context state(0x%x)\n", 669 state); 670 /* Throw this CTX away and try submitting another */ 671 del_timer(&ctx->timer); 672 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; 673 usb_unlink_urb(ctx->outurb); 674 ezusb_req_queue_run(upriv); 675 break; 676 } /* switch */ 677 } 678 679 680 static void ezusb_req_ctx_wait(struct ezusb_priv *upriv, 681 struct request_context *ctx) 682 { 683 switch (ctx->state) { 684 case EZUSB_CTX_QUEUED: 685 case EZUSB_CTX_REQ_SUBMITTED: 686 case EZUSB_CTX_REQ_COMPLETE: 687 case EZUSB_CTX_RESP_RECEIVED: 688 if (in_softirq()) { 689 /* If we get called from a timer, timeout timers don't 690 * get the chance to run themselves. So we make sure 691 * that we don't sleep for ever */ 692 int msecs = DEF_TIMEOUT * (1000 / HZ); 693 while (!ctx->done.done && msecs--) 694 udelay(1000); 695 } else { 696 wait_event_interruptible(ctx->done.wait, 697 ctx->done.done); 698 } 699 break; 700 default: 701 /* Done or failed - nothing to wait for */ 702 break; 703 } 704 } 705 706 static inline u16 build_crc(struct ezusb_packet *data) 707 { 708 u16 crc = 0; 709 u8 *bytes = (u8 *)data; 710 int i; 711 712 for (i = 0; i < 8; i++) 713 crc = (crc << 1) + bytes[i]; 714 715 return crc; 716 } 717 718 /** 719 * ezusb_fill_req: 720 * 721 * if data == NULL and length > 0 the data is assumed to be already in 722 * the target buffer and only the header is filled. 723 * 724 */ 725 static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid, 726 const void *data, u16 frame_type, u8 reply_count) 727 { 728 int total_size = sizeof(*req) + length; 729 730 BUG_ON(total_size > BULK_BUF_SIZE); 731 732 req->magic = cpu_to_le16(EZUSB_MAGIC); 733 req->req_reply_count = reply_count; 734 req->ans_reply_count = 0; 735 req->frame_type = cpu_to_le16(frame_type); 736 req->size = cpu_to_le16(length + 4); 737 req->crc = cpu_to_le16(build_crc(req)); 738 req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length)); 739 req->hermes_rid = cpu_to_le16(rid); 740 if (data) 741 memcpy(req->data, data, length); 742 return total_size; 743 } 744 745 static int ezusb_submit_in_urb(struct ezusb_priv *upriv) 746 { 747 int retval = 0; 748 void *cur_buf = upriv->read_urb->transfer_buffer; 749 750 if (upriv->read_urb->status == -EINPROGRESS) { 751 netdev_dbg(upriv->dev, "urb busy, not resubmiting\n"); 752 retval = -EBUSY; 753 goto exit; 754 } 755 usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe, 756 cur_buf, BULK_BUF_SIZE, 757 ezusb_bulk_in_callback, upriv); 758 upriv->read_urb->transfer_flags = 0; 759 retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC); 760 if (retval) 761 err("%s submit failed %d", __func__, retval); 762 763 exit: 764 return retval; 765 } 766 767 static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset) 768 { 769 int ret; 770 u8 *res_val = NULL; 771 772 if (!upriv->udev) { 773 err("%s: !upriv->udev", __func__); 774 return -EFAULT; 775 } 776 777 res_val = kmalloc(sizeof(*res_val), GFP_KERNEL); 778 779 if (!res_val) 780 return -ENOMEM; 781 782 *res_val = reset; /* avoid argument promotion */ 783 784 ret = usb_control_msg(upriv->udev, 785 usb_sndctrlpipe(upriv->udev, 0), 786 EZUSB_REQUEST_FW_TRANS, 787 USB_TYPE_VENDOR | USB_RECIP_DEVICE | 788 USB_DIR_OUT, EZUSB_CPUCS_REG, 0, res_val, 789 sizeof(*res_val), DEF_TIMEOUT); 790 791 kfree(res_val); 792 793 return ret; 794 } 795 796 static int ezusb_firmware_download(struct ezusb_priv *upriv, 797 struct ez_usb_fw *fw) 798 { 799 u8 *fw_buffer; 800 int retval, addr; 801 int variant_offset; 802 803 fw_buffer = kmalloc(FW_BUF_SIZE, GFP_KERNEL); 804 if (!fw_buffer) { 805 printk(KERN_ERR PFX "Out of memory for firmware buffer.\n"); 806 return -ENOMEM; 807 } 808 /* 809 * This byte is 1 and should be replaced with 0. The offset is 810 * 0x10AD in version 0.0.6. The byte in question should follow 811 * the end of the code pointed to by the jump in the beginning 812 * of the firmware. Also, it is read by code located at 0x358. 813 */ 814 variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]); 815 if (variant_offset >= fw->size) { 816 printk(KERN_ERR PFX "Invalid firmware variant offset: " 817 "0x%04x\n", variant_offset); 818 retval = -EINVAL; 819 goto fail; 820 } 821 822 retval = ezusb_8051_cpucs(upriv, 1); 823 if (retval < 0) 824 goto fail; 825 for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) { 826 /* 0x100-0x300 should be left alone, it contains card 827 * specific data, like USB enumeration information */ 828 if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END)) 829 continue; 830 831 memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE); 832 if (variant_offset >= addr && 833 variant_offset < addr + FW_BUF_SIZE) { 834 netdev_dbg(upriv->dev, 835 "Patching card_variant byte at 0x%04X\n", 836 variant_offset); 837 fw_buffer[variant_offset - addr] = FW_VAR_VALUE; 838 } 839 retval = usb_control_msg(upriv->udev, 840 usb_sndctrlpipe(upriv->udev, 0), 841 EZUSB_REQUEST_FW_TRANS, 842 USB_TYPE_VENDOR | USB_RECIP_DEVICE 843 | USB_DIR_OUT, 844 addr, 0x0, 845 fw_buffer, FW_BUF_SIZE, 846 DEF_TIMEOUT); 847 848 if (retval < 0) 849 goto fail; 850 } 851 retval = ezusb_8051_cpucs(upriv, 0); 852 if (retval < 0) 853 goto fail; 854 855 goto exit; 856 fail: 857 printk(KERN_ERR PFX "Firmware download failed, error %d\n", 858 retval); 859 exit: 860 kfree(fw_buffer); 861 return retval; 862 } 863 864 static int ezusb_access_ltv(struct ezusb_priv *upriv, 865 struct request_context *ctx, 866 u16 length, const void *data, u16 frame_type, 867 void *ans_buff, unsigned ans_size, u16 *ans_length) 868 { 869 int req_size; 870 int retval = 0; 871 enum ezusb_state state; 872 873 BUG_ON(in_irq()); 874 875 if (!upriv->udev) { 876 retval = -ENODEV; 877 goto exit; 878 } 879 880 if (upriv->read_urb->status != -EINPROGRESS) 881 err("%s: in urb not pending", __func__); 882 883 /* protect upriv->reply_count, guarantee sequential numbers */ 884 spin_lock_bh(&upriv->reply_count_lock); 885 req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data, 886 frame_type, upriv->reply_count); 887 usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe, 888 ctx->buf, req_size, 889 ezusb_request_out_callback, ctx); 890 891 if (ctx->in_rid) 892 upriv->reply_count = ezusb_reply_inc(upriv->reply_count); 893 894 ezusb_req_enqueue_run(upriv, ctx); 895 896 spin_unlock_bh(&upriv->reply_count_lock); 897 898 if (ctx->in_rid) 899 ezusb_req_ctx_wait(upriv, ctx); 900 901 state = ctx->state; 902 switch (state) { 903 case EZUSB_CTX_COMPLETE: 904 retval = ctx->outurb->status; 905 break; 906 907 case EZUSB_CTX_QUEUED: 908 case EZUSB_CTX_REQ_SUBMITTED: 909 if (!ctx->in_rid) 910 break; 911 /* fall through */ 912 default: 913 err("%s: Unexpected context state %d", __func__, 914 state); 915 /* fall through */ 916 case EZUSB_CTX_REQ_TIMEOUT: 917 case EZUSB_CTX_REQ_FAILED: 918 case EZUSB_CTX_RESP_TIMEOUT: 919 case EZUSB_CTX_REQSUBMIT_FAIL: 920 printk(KERN_ERR PFX "Access failed, resetting (state %d," 921 " reply_count %d)\n", state, upriv->reply_count); 922 upriv->reply_count = 0; 923 if (state == EZUSB_CTX_REQ_TIMEOUT 924 || state == EZUSB_CTX_RESP_TIMEOUT) { 925 printk(KERN_ERR PFX "ctx timed out\n"); 926 retval = -ETIMEDOUT; 927 } else { 928 printk(KERN_ERR PFX "ctx failed\n"); 929 retval = -EFAULT; 930 } 931 goto exit; 932 } 933 if (ctx->in_rid) { 934 struct ezusb_packet *ans = ctx->buf; 935 unsigned exp_len; 936 937 if (ans->hermes_len != 0) 938 exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12; 939 else 940 exp_len = 14; 941 942 if (exp_len != ctx->buf_length) { 943 err("%s: length mismatch for RID 0x%04x: " 944 "expected %d, got %d", __func__, 945 ctx->in_rid, exp_len, ctx->buf_length); 946 retval = -EIO; 947 goto exit; 948 } 949 950 if (ans_buff) 951 memcpy(ans_buff, ans->data, min(exp_len, ans_size)); 952 if (ans_length) 953 *ans_length = le16_to_cpu(ans->hermes_len); 954 } 955 exit: 956 ezusb_request_context_put(ctx); 957 return retval; 958 } 959 960 static int ezusb_write_ltv(struct hermes *hw, int bap, u16 rid, 961 u16 length, const void *data) 962 { 963 struct ezusb_priv *upriv = hw->priv; 964 u16 frame_type; 965 struct request_context *ctx; 966 967 if (length == 0) 968 return -EINVAL; 969 970 length = HERMES_RECLEN_TO_BYTES(length); 971 972 /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be 973 * set to be empty, but the USB bridge doesn't like it */ 974 if (length == 0) 975 return 0; 976 977 ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK); 978 if (!ctx) 979 return -ENOMEM; 980 981 if (rid == EZUSB_RID_TX) 982 frame_type = EZUSB_FRAME_DATA; 983 else 984 frame_type = EZUSB_FRAME_CONTROL; 985 986 return ezusb_access_ltv(upriv, ctx, length, data, frame_type, 987 NULL, 0, NULL); 988 } 989 990 static int ezusb_read_ltv(struct hermes *hw, int bap, u16 rid, 991 unsigned bufsize, u16 *length, void *buf) 992 { 993 struct ezusb_priv *upriv = hw->priv; 994 struct request_context *ctx; 995 996 if (bufsize % 2) 997 return -EINVAL; 998 999 ctx = ezusb_alloc_ctx(upriv, rid, rid); 1000 if (!ctx) 1001 return -ENOMEM; 1002 1003 return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL, 1004 buf, bufsize, length); 1005 } 1006 1007 static int ezusb_doicmd_wait(struct hermes *hw, u16 cmd, u16 parm0, u16 parm1, 1008 u16 parm2, struct hermes_response *resp) 1009 { 1010 struct ezusb_priv *upriv = hw->priv; 1011 struct request_context *ctx; 1012 1013 __le16 data[4] = { 1014 cpu_to_le16(cmd), 1015 cpu_to_le16(parm0), 1016 cpu_to_le16(parm1), 1017 cpu_to_le16(parm2), 1018 }; 1019 netdev_dbg(upriv->dev, 1020 "0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X\n", cmd, 1021 parm0, parm1, parm2); 1022 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK); 1023 if (!ctx) 1024 return -ENOMEM; 1025 1026 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data, 1027 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1028 } 1029 1030 static int ezusb_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0, 1031 struct hermes_response *resp) 1032 { 1033 struct ezusb_priv *upriv = hw->priv; 1034 struct request_context *ctx; 1035 1036 __le16 data[4] = { 1037 cpu_to_le16(cmd), 1038 cpu_to_le16(parm0), 1039 0, 1040 0, 1041 }; 1042 netdev_dbg(upriv->dev, "0x%04X, parm0 0x%04X\n", cmd, parm0); 1043 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK); 1044 if (!ctx) 1045 return -ENOMEM; 1046 1047 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data, 1048 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1049 } 1050 1051 static int ezusb_bap_pread(struct hermes *hw, int bap, 1052 void *buf, int len, u16 id, u16 offset) 1053 { 1054 struct ezusb_priv *upriv = hw->priv; 1055 struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer; 1056 int actual_length = upriv->read_urb->actual_length; 1057 1058 if (id == EZUSB_RID_RX) { 1059 if ((sizeof(*ans) + offset + len) > actual_length) { 1060 printk(KERN_ERR PFX "BAP read beyond buffer end " 1061 "in rx frame\n"); 1062 return -EINVAL; 1063 } 1064 memcpy(buf, ans->data + offset, len); 1065 return 0; 1066 } 1067 1068 if (EZUSB_IS_INFO(id)) { 1069 /* Include 4 bytes for length/type */ 1070 if ((sizeof(*ans) + offset + len - 4) > actual_length) { 1071 printk(KERN_ERR PFX "BAP read beyond buffer end " 1072 "in info frame\n"); 1073 return -EFAULT; 1074 } 1075 memcpy(buf, ans->data + offset - 4, len); 1076 } else { 1077 printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id); 1078 return -EINVAL; 1079 } 1080 1081 return 0; 1082 } 1083 1084 static int ezusb_read_pda(struct hermes *hw, __le16 *pda, 1085 u32 pda_addr, u16 pda_len) 1086 { 1087 struct ezusb_priv *upriv = hw->priv; 1088 struct request_context *ctx; 1089 __le16 data[] = { 1090 cpu_to_le16(pda_addr & 0xffff), 1091 cpu_to_le16(pda_len - 4) 1092 }; 1093 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_READ_PDA, EZUSB_RID_READ_PDA); 1094 if (!ctx) 1095 return -ENOMEM; 1096 1097 /* wl_lkm does not include PDA size in the PDA area. 1098 * We will pad the information into pda, so other routines 1099 * don't have to be modified */ 1100 pda[0] = cpu_to_le16(pda_len - 2); 1101 /* Includes CFG_PROD_DATA but not itself */ 1102 pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */ 1103 1104 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data, 1105 EZUSB_FRAME_CONTROL, &pda[2], pda_len - 4, 1106 NULL); 1107 } 1108 1109 static int ezusb_program_init(struct hermes *hw, u32 entry_point) 1110 { 1111 struct ezusb_priv *upriv = hw->priv; 1112 struct request_context *ctx; 1113 __le32 data = cpu_to_le32(entry_point); 1114 1115 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_INIT, EZUSB_RID_ACK); 1116 if (!ctx) 1117 return -ENOMEM; 1118 1119 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data, 1120 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1121 } 1122 1123 static int ezusb_program_end(struct hermes *hw) 1124 { 1125 struct ezusb_priv *upriv = hw->priv; 1126 struct request_context *ctx; 1127 1128 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_END, EZUSB_RID_ACK); 1129 if (!ctx) 1130 return -ENOMEM; 1131 1132 return ezusb_access_ltv(upriv, ctx, 0, NULL, 1133 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1134 } 1135 1136 static int ezusb_program_bytes(struct hermes *hw, const char *buf, 1137 u32 addr, u32 len) 1138 { 1139 struct ezusb_priv *upriv = hw->priv; 1140 struct request_context *ctx; 1141 __le32 data = cpu_to_le32(addr); 1142 int err; 1143 1144 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_SET_ADDR, EZUSB_RID_ACK); 1145 if (!ctx) 1146 return -ENOMEM; 1147 1148 err = ezusb_access_ltv(upriv, ctx, sizeof(data), &data, 1149 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1150 if (err) 1151 return err; 1152 1153 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_BYTES, EZUSB_RID_ACK); 1154 if (!ctx) 1155 return -ENOMEM; 1156 1157 return ezusb_access_ltv(upriv, ctx, len, buf, 1158 EZUSB_FRAME_CONTROL, NULL, 0, NULL); 1159 } 1160 1161 static int ezusb_program(struct hermes *hw, const char *buf, 1162 u32 addr, u32 len) 1163 { 1164 u32 ch_addr; 1165 u32 ch_len; 1166 int err = 0; 1167 1168 /* We can only send 2048 bytes out of the bulk xmit at a time, 1169 * so we have to split any programming into chunks of <2048 1170 * bytes. */ 1171 1172 ch_len = (len < MAX_DL_SIZE) ? len : MAX_DL_SIZE; 1173 ch_addr = addr; 1174 1175 while (ch_addr < (addr + len)) { 1176 pr_debug("Programming subblock of length %d " 1177 "to address 0x%08x. Data @ %p\n", 1178 ch_len, ch_addr, &buf[ch_addr - addr]); 1179 1180 err = ezusb_program_bytes(hw, &buf[ch_addr - addr], 1181 ch_addr, ch_len); 1182 if (err) 1183 break; 1184 1185 ch_addr += ch_len; 1186 ch_len = ((addr + len - ch_addr) < MAX_DL_SIZE) ? 1187 (addr + len - ch_addr) : MAX_DL_SIZE; 1188 } 1189 1190 return err; 1191 } 1192 1193 static netdev_tx_t ezusb_xmit(struct sk_buff *skb, struct net_device *dev) 1194 { 1195 struct orinoco_private *priv = ndev_priv(dev); 1196 struct net_device_stats *stats = &dev->stats; 1197 struct ezusb_priv *upriv = priv->card; 1198 u8 mic[MICHAEL_MIC_LEN + 1]; 1199 int err = 0; 1200 int tx_control; 1201 unsigned long flags; 1202 struct request_context *ctx; 1203 u8 *buf; 1204 int tx_size; 1205 1206 if (!netif_running(dev)) { 1207 printk(KERN_ERR "%s: Tx on stopped device!\n", 1208 dev->name); 1209 return NETDEV_TX_BUSY; 1210 } 1211 1212 if (netif_queue_stopped(dev)) { 1213 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n", 1214 dev->name); 1215 return NETDEV_TX_BUSY; 1216 } 1217 1218 if (orinoco_lock(priv, &flags) != 0) { 1219 printk(KERN_ERR 1220 "%s: ezusb_xmit() called while hw_unavailable\n", 1221 dev->name); 1222 return NETDEV_TX_BUSY; 1223 } 1224 1225 if (!netif_carrier_ok(dev) || 1226 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) { 1227 /* Oops, the firmware hasn't established a connection, 1228 silently drop the packet (this seems to be the 1229 safest approach). */ 1230 goto drop; 1231 } 1232 1233 /* Check packet length */ 1234 if (skb->len < ETH_HLEN) 1235 goto drop; 1236 1237 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0); 1238 if (!ctx) 1239 goto busy; 1240 1241 memset(ctx->buf, 0, BULK_BUF_SIZE); 1242 buf = ctx->buf->data; 1243 1244 tx_control = 0; 1245 1246 err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control, 1247 &mic[0]); 1248 if (err) 1249 goto drop; 1250 1251 { 1252 __le16 *tx_cntl = (__le16 *)buf; 1253 *tx_cntl = cpu_to_le16(tx_control); 1254 buf += sizeof(*tx_cntl); 1255 } 1256 1257 memcpy(buf, skb->data, skb->len); 1258 buf += skb->len; 1259 1260 if (tx_control & HERMES_TXCTRL_MIC) { 1261 u8 *m = mic; 1262 /* Mic has been offset so it can be copied to an even 1263 * address. We're copying eveything anyway, so we 1264 * don't need to copy that first byte. */ 1265 if (skb->len % 2) 1266 m++; 1267 memcpy(buf, m, MICHAEL_MIC_LEN); 1268 buf += MICHAEL_MIC_LEN; 1269 } 1270 1271 /* Finally, we actually initiate the send */ 1272 netif_stop_queue(dev); 1273 1274 /* The card may behave better if we send evenly sized usb transfers */ 1275 tx_size = ALIGN(buf - ctx->buf->data, 2); 1276 1277 err = ezusb_access_ltv(upriv, ctx, tx_size, NULL, 1278 EZUSB_FRAME_DATA, NULL, 0, NULL); 1279 1280 if (err) { 1281 netif_start_queue(dev); 1282 if (net_ratelimit()) 1283 printk(KERN_ERR "%s: Error %d transmitting packet\n", 1284 dev->name, err); 1285 goto busy; 1286 } 1287 1288 netif_trans_update(dev); 1289 stats->tx_bytes += skb->len; 1290 goto ok; 1291 1292 drop: 1293 stats->tx_errors++; 1294 stats->tx_dropped++; 1295 1296 ok: 1297 orinoco_unlock(priv, &flags); 1298 dev_kfree_skb(skb); 1299 return NETDEV_TX_OK; 1300 1301 busy: 1302 orinoco_unlock(priv, &flags); 1303 return NETDEV_TX_BUSY; 1304 } 1305 1306 static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid) 1307 { 1308 *fid = EZUSB_RID_TX; 1309 return 0; 1310 } 1311 1312 1313 static int ezusb_hard_reset(struct orinoco_private *priv) 1314 { 1315 struct ezusb_priv *upriv = priv->card; 1316 int retval = ezusb_8051_cpucs(upriv, 1); 1317 1318 if (retval < 0) { 1319 err("Failed to reset"); 1320 return retval; 1321 } 1322 1323 retval = ezusb_8051_cpucs(upriv, 0); 1324 if (retval < 0) { 1325 err("Failed to unreset"); 1326 return retval; 1327 } 1328 1329 netdev_dbg(upriv->dev, "sending control message\n"); 1330 retval = usb_control_msg(upriv->udev, 1331 usb_sndctrlpipe(upriv->udev, 0), 1332 EZUSB_REQUEST_TRIGER, 1333 USB_TYPE_VENDOR | USB_RECIP_DEVICE | 1334 USB_DIR_OUT, 0x0, 0x0, NULL, 0, 1335 DEF_TIMEOUT); 1336 if (retval < 0) { 1337 err("EZUSB_REQUEST_TRIGER failed retval %d", retval); 1338 return retval; 1339 } 1340 #if 0 1341 dbg("Sending EZUSB_REQUEST_TRIG_AC"); 1342 retval = usb_control_msg(upriv->udev, 1343 usb_sndctrlpipe(upriv->udev, 0), 1344 EZUSB_REQUEST_TRIG_AC, 1345 USB_TYPE_VENDOR | USB_RECIP_DEVICE | 1346 USB_DIR_OUT, 0x00FA, 0x0, NULL, 0, 1347 DEF_TIMEOUT); 1348 if (retval < 0) { 1349 err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval); 1350 return retval; 1351 } 1352 #endif 1353 1354 return 0; 1355 } 1356 1357 1358 static int ezusb_init(struct hermes *hw) 1359 { 1360 struct ezusb_priv *upriv = hw->priv; 1361 int retval; 1362 1363 BUG_ON(in_interrupt()); 1364 BUG_ON(!upriv); 1365 1366 upriv->reply_count = 0; 1367 /* Write the MAGIC number on the simulated registers to keep 1368 * orinoco.c happy */ 1369 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC); 1370 hermes_write_regn(hw, RXFID, EZUSB_RID_RX); 1371 1372 usb_kill_urb(upriv->read_urb); 1373 ezusb_submit_in_urb(upriv); 1374 1375 retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1, 1376 HERMES_BYTES_TO_RECLEN(2), "\x10\x00"); 1377 if (retval < 0) { 1378 printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval); 1379 return retval; 1380 } 1381 1382 retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL); 1383 if (retval < 0) { 1384 printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval); 1385 return retval; 1386 } 1387 1388 return 0; 1389 } 1390 1391 static void ezusb_bulk_in_callback(struct urb *urb) 1392 { 1393 struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context; 1394 struct ezusb_packet *ans = urb->transfer_buffer; 1395 u16 crc; 1396 u16 hermes_rid; 1397 1398 if (upriv->udev == NULL) 1399 return; 1400 1401 if (urb->status == -ETIMEDOUT) { 1402 /* When a device gets unplugged we get this every time 1403 * we resubmit, flooding the logs. Since we don't use 1404 * USB timeouts, it shouldn't happen any other time*/ 1405 pr_warn("%s: urb timed out, not resubmitting\n", __func__); 1406 return; 1407 } 1408 if (urb->status == -ECONNABORTED) { 1409 pr_warn("%s: connection abort, resubmitting urb\n", 1410 __func__); 1411 goto resubmit; 1412 } 1413 if ((urb->status == -EILSEQ) 1414 || (urb->status == -ENOENT) 1415 || (urb->status == -ECONNRESET)) { 1416 netdev_dbg(upriv->dev, "status %d, not resubmiting\n", 1417 urb->status); 1418 return; 1419 } 1420 if (urb->status) 1421 netdev_dbg(upriv->dev, "status: %d length: %d\n", 1422 urb->status, urb->actual_length); 1423 if (urb->actual_length < sizeof(*ans)) { 1424 err("%s: short read, ignoring", __func__); 1425 goto resubmit; 1426 } 1427 crc = build_crc(ans); 1428 if (le16_to_cpu(ans->crc) != crc) { 1429 err("CRC error, ignoring packet"); 1430 goto resubmit; 1431 } 1432 1433 hermes_rid = le16_to_cpu(ans->hermes_rid); 1434 if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) { 1435 ezusb_request_in_callback(upriv, urb); 1436 } else if (upriv->dev) { 1437 struct net_device *dev = upriv->dev; 1438 struct orinoco_private *priv = ndev_priv(dev); 1439 struct hermes *hw = &priv->hw; 1440 1441 if (hermes_rid == EZUSB_RID_RX) { 1442 __orinoco_ev_rx(dev, hw); 1443 } else { 1444 hermes_write_regn(hw, INFOFID, 1445 le16_to_cpu(ans->hermes_rid)); 1446 __orinoco_ev_info(dev, hw); 1447 } 1448 } 1449 1450 resubmit: 1451 if (upriv->udev) 1452 ezusb_submit_in_urb(upriv); 1453 } 1454 1455 static inline void ezusb_delete(struct ezusb_priv *upriv) 1456 { 1457 struct list_head *item; 1458 struct list_head *tmp_item; 1459 unsigned long flags; 1460 1461 BUG_ON(in_interrupt()); 1462 BUG_ON(!upriv); 1463 1464 mutex_lock(&upriv->mtx); 1465 1466 upriv->udev = NULL; /* No timer will be rearmed from here */ 1467 1468 usb_kill_urb(upriv->read_urb); 1469 1470 spin_lock_irqsave(&upriv->req_lock, flags); 1471 list_for_each_safe(item, tmp_item, &upriv->req_active) { 1472 struct request_context *ctx; 1473 int err; 1474 1475 ctx = list_entry(item, struct request_context, list); 1476 refcount_inc(&ctx->refcount); 1477 1478 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; 1479 err = usb_unlink_urb(ctx->outurb); 1480 1481 spin_unlock_irqrestore(&upriv->req_lock, flags); 1482 if (err == -EINPROGRESS) 1483 wait_for_completion(&ctx->done); 1484 1485 del_timer_sync(&ctx->timer); 1486 /* FIXME: there is an slight chance for the irq handler to 1487 * be running */ 1488 if (!list_empty(&ctx->list)) 1489 ezusb_ctx_complete(ctx); 1490 1491 ezusb_request_context_put(ctx); 1492 spin_lock_irqsave(&upriv->req_lock, flags); 1493 } 1494 spin_unlock_irqrestore(&upriv->req_lock, flags); 1495 1496 list_for_each_safe(item, tmp_item, &upriv->req_pending) 1497 ezusb_ctx_complete(list_entry(item, 1498 struct request_context, list)); 1499 1500 if (upriv->read_urb && upriv->read_urb->status == -EINPROGRESS) 1501 printk(KERN_ERR PFX "Some URB in progress\n"); 1502 1503 mutex_unlock(&upriv->mtx); 1504 1505 if (upriv->read_urb) { 1506 kfree(upriv->read_urb->transfer_buffer); 1507 usb_free_urb(upriv->read_urb); 1508 } 1509 kfree(upriv->bap_buf); 1510 if (upriv->dev) { 1511 struct orinoco_private *priv = ndev_priv(upriv->dev); 1512 orinoco_if_del(priv); 1513 wiphy_unregister(priv_to_wiphy(upriv)); 1514 free_orinocodev(priv); 1515 } 1516 } 1517 1518 static void ezusb_lock_irqsave(spinlock_t *lock, 1519 unsigned long *flags) __acquires(lock) 1520 { 1521 spin_lock_bh(lock); 1522 } 1523 1524 static void ezusb_unlock_irqrestore(spinlock_t *lock, 1525 unsigned long *flags) __releases(lock) 1526 { 1527 spin_unlock_bh(lock); 1528 } 1529 1530 static void ezusb_lock_irq(spinlock_t *lock) __acquires(lock) 1531 { 1532 spin_lock_bh(lock); 1533 } 1534 1535 static void ezusb_unlock_irq(spinlock_t *lock) __releases(lock) 1536 { 1537 spin_unlock_bh(lock); 1538 } 1539 1540 static const struct hermes_ops ezusb_ops = { 1541 .init = ezusb_init, 1542 .cmd_wait = ezusb_docmd_wait, 1543 .init_cmd_wait = ezusb_doicmd_wait, 1544 .allocate = ezusb_allocate, 1545 .read_ltv = ezusb_read_ltv, 1546 .write_ltv = ezusb_write_ltv, 1547 .bap_pread = ezusb_bap_pread, 1548 .read_pda = ezusb_read_pda, 1549 .program_init = ezusb_program_init, 1550 .program_end = ezusb_program_end, 1551 .program = ezusb_program, 1552 .lock_irqsave = ezusb_lock_irqsave, 1553 .unlock_irqrestore = ezusb_unlock_irqrestore, 1554 .lock_irq = ezusb_lock_irq, 1555 .unlock_irq = ezusb_unlock_irq, 1556 }; 1557 1558 static const struct net_device_ops ezusb_netdev_ops = { 1559 .ndo_open = orinoco_open, 1560 .ndo_stop = orinoco_stop, 1561 .ndo_start_xmit = ezusb_xmit, 1562 .ndo_set_rx_mode = orinoco_set_multicast_list, 1563 .ndo_change_mtu = orinoco_change_mtu, 1564 .ndo_set_mac_address = eth_mac_addr, 1565 .ndo_validate_addr = eth_validate_addr, 1566 .ndo_tx_timeout = orinoco_tx_timeout, 1567 }; 1568 1569 static int ezusb_probe(struct usb_interface *interface, 1570 const struct usb_device_id *id) 1571 { 1572 struct usb_device *udev = interface_to_usbdev(interface); 1573 struct orinoco_private *priv; 1574 struct hermes *hw; 1575 struct ezusb_priv *upriv = NULL; 1576 struct usb_interface_descriptor *iface_desc; 1577 struct usb_endpoint_descriptor *ep; 1578 const struct firmware *fw_entry = NULL; 1579 int retval = 0; 1580 int i; 1581 1582 priv = alloc_orinocodev(sizeof(*upriv), &udev->dev, 1583 ezusb_hard_reset, NULL); 1584 if (!priv) { 1585 err("Couldn't allocate orinocodev"); 1586 retval = -ENOMEM; 1587 goto exit; 1588 } 1589 1590 hw = &priv->hw; 1591 1592 upriv = priv->card; 1593 1594 mutex_init(&upriv->mtx); 1595 spin_lock_init(&upriv->reply_count_lock); 1596 1597 spin_lock_init(&upriv->req_lock); 1598 INIT_LIST_HEAD(&upriv->req_pending); 1599 INIT_LIST_HEAD(&upriv->req_active); 1600 1601 upriv->udev = udev; 1602 1603 hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake; 1604 hw->reg_spacing = HERMES_16BIT_REGSPACING; 1605 hw->priv = upriv; 1606 hw->ops = &ezusb_ops; 1607 1608 /* set up the endpoint information */ 1609 /* check out the endpoints */ 1610 1611 iface_desc = &interface->altsetting[0].desc; 1612 for (i = 0; i < iface_desc->bNumEndpoints; ++i) { 1613 ep = &interface->altsetting[0].endpoint[i].desc; 1614 1615 if (usb_endpoint_is_bulk_in(ep)) { 1616 /* we found a bulk in endpoint */ 1617 if (upriv->read_urb != NULL) { 1618 pr_warn("Found a second bulk in ep, ignored\n"); 1619 continue; 1620 } 1621 1622 upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL); 1623 if (!upriv->read_urb) 1624 goto error; 1625 if (le16_to_cpu(ep->wMaxPacketSize) != 64) 1626 pr_warn("bulk in: wMaxPacketSize!= 64\n"); 1627 if (ep->bEndpointAddress != (2 | USB_DIR_IN)) 1628 pr_warn("bulk in: bEndpointAddress: %d\n", 1629 ep->bEndpointAddress); 1630 upriv->read_pipe = usb_rcvbulkpipe(udev, 1631 ep-> 1632 bEndpointAddress); 1633 upriv->read_urb->transfer_buffer = 1634 kmalloc(BULK_BUF_SIZE, GFP_KERNEL); 1635 if (!upriv->read_urb->transfer_buffer) { 1636 err("Couldn't allocate IN buffer"); 1637 goto error; 1638 } 1639 } 1640 1641 if (usb_endpoint_is_bulk_out(ep)) { 1642 /* we found a bulk out endpoint */ 1643 if (upriv->bap_buf != NULL) { 1644 pr_warn("Found a second bulk out ep, ignored\n"); 1645 continue; 1646 } 1647 1648 if (le16_to_cpu(ep->wMaxPacketSize) != 64) 1649 pr_warn("bulk out: wMaxPacketSize != 64\n"); 1650 if (ep->bEndpointAddress != 2) 1651 pr_warn("bulk out: bEndpointAddress: %d\n", 1652 ep->bEndpointAddress); 1653 upriv->write_pipe = usb_sndbulkpipe(udev, 1654 ep-> 1655 bEndpointAddress); 1656 upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL); 1657 if (!upriv->bap_buf) { 1658 err("Couldn't allocate bulk_out_buffer"); 1659 goto error; 1660 } 1661 } 1662 } 1663 if (!upriv->bap_buf || !upriv->read_urb) { 1664 err("Didn't find the required bulk endpoints"); 1665 goto error; 1666 } 1667 1668 if (request_firmware(&fw_entry, "orinoco_ezusb_fw", 1669 &interface->dev) == 0) { 1670 firmware.size = fw_entry->size; 1671 firmware.code = fw_entry->data; 1672 } 1673 if (firmware.size && firmware.code) { 1674 if (ezusb_firmware_download(upriv, &firmware) < 0) 1675 goto error; 1676 } else { 1677 err("No firmware to download"); 1678 goto error; 1679 } 1680 1681 if (ezusb_hard_reset(priv) < 0) { 1682 err("Cannot reset the device"); 1683 goto error; 1684 } 1685 1686 /* If the firmware is already downloaded orinoco.c will call 1687 * ezusb_init but if the firmware is not already there, that will make 1688 * the kernel very unstable, so we try initializing here and quit in 1689 * case of error */ 1690 if (ezusb_init(hw) < 0) { 1691 err("Couldn't initialize the device"); 1692 err("Firmware may not be downloaded or may be wrong."); 1693 goto error; 1694 } 1695 1696 /* Initialise the main driver */ 1697 if (orinoco_init(priv) != 0) { 1698 err("orinoco_init() failed\n"); 1699 goto error; 1700 } 1701 1702 if (orinoco_if_add(priv, 0, 0, &ezusb_netdev_ops) != 0) { 1703 upriv->dev = NULL; 1704 err("%s: orinoco_if_add() failed", __func__); 1705 wiphy_unregister(priv_to_wiphy(priv)); 1706 goto error; 1707 } 1708 upriv->dev = priv->ndev; 1709 1710 goto exit; 1711 1712 error: 1713 ezusb_delete(upriv); 1714 if (upriv->dev) { 1715 /* upriv->dev was 0, so ezusb_delete() didn't free it */ 1716 free_orinocodev(priv); 1717 } 1718 upriv = NULL; 1719 retval = -EFAULT; 1720 exit: 1721 if (fw_entry) { 1722 firmware.code = NULL; 1723 firmware.size = 0; 1724 release_firmware(fw_entry); 1725 } 1726 usb_set_intfdata(interface, upriv); 1727 return retval; 1728 } 1729 1730 1731 static void ezusb_disconnect(struct usb_interface *intf) 1732 { 1733 struct ezusb_priv *upriv = usb_get_intfdata(intf); 1734 usb_set_intfdata(intf, NULL); 1735 ezusb_delete(upriv); 1736 printk(KERN_INFO PFX "Disconnected\n"); 1737 } 1738 1739 1740 /* usb specific object needed to register this driver with the usb subsystem */ 1741 static struct usb_driver orinoco_driver = { 1742 .name = DRIVER_NAME, 1743 .probe = ezusb_probe, 1744 .disconnect = ezusb_disconnect, 1745 .id_table = ezusb_table, 1746 .disable_hub_initiated_lpm = 1, 1747 }; 1748 1749 module_usb_driver(orinoco_driver); 1750 1751 MODULE_AUTHOR("Manuel Estrada Sainz"); 1752 MODULE_DESCRIPTION("Driver for Orinoco wireless LAN cards using EZUSB bridge"); 1753 MODULE_LICENSE("Dual MPL/GPL"); 1754