Revert "svn-1276"
[orinoco_usb.git] / drivers / net / wireless / orinoco_usb.c
bloba96c0d63372f7a01a267e727a6cf425393fcb0f6
1 /*
2 * USB Orinoco driver
4 * Copyright (c) 2003 Manuel Estrada Sainz
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/
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.
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.
27 * Queueing code based on linux-wlan-ng 0.2.1-pre5
29 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
31 * The license is the same as above.
33 * Initialy based on USB Skeleton driver - 0.7
35 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
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.
42 * NOTE: The original USB Skeleton driver is GPL, but all that code is
43 * gone so MPL/GPL applies.
46 #define DRIVER_NAME "orinoco_usb"
47 #define PFX DRIVER_NAME ": "
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/init.h>
56 #include <linux/slab.h>
57 #include <linux/fcntl.h>
58 #include <linux/spinlock.h>
59 #include <linux/list.h>
60 #include <linux/smp_lock.h>
61 #include <linux/usb.h>
62 #include <linux/timer.h>
64 #include <linux/netdevice.h>
65 #include <linux/if_arp.h>
66 #include <linux/etherdevice.h>
67 #include <linux/wireless.h>
68 #include <linux/firmware.h>
70 #include "orinoco.h"
72 #ifndef URB_ASYNC_UNLINK
73 #define URB_ASYNC_UNLINK 0
74 #endif
76 /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
77 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
78 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
80 struct header_struct {
81 /* 802.3 */
82 u8 dest[ETH_ALEN];
83 u8 src[ETH_ALEN];
84 __be16 len;
85 /* 802.2 */
86 u8 dsap;
87 u8 ssap;
88 u8 ctrl;
89 /* SNAP */
90 u8 oui[3];
91 __be16 ethertype;
92 } __attribute__ ((packed));
94 struct ez_usb_fw {
95 u16 size;
96 u8 *code;
99 static struct ez_usb_fw firmware = {
100 .size = 0,
101 .code = NULL,
104 #ifdef CONFIG_USB_DEBUG
105 static int debug = 1;
106 #else
107 static int debug;
108 #endif
110 /* Debugging macros */
111 #undef dbg
112 #define dbg(format, arg...) \
113 do { if (debug) printk(KERN_DEBUG PFX "%s: " format "\n", \
114 __FUNCTION__ , ## arg); } while (0)
115 #undef err
116 #define err(format, arg...) \
117 do { printk(KERN_ERR PFX format "\n", ## arg); } while (0)
119 /* Module paramaters */
120 module_param(debug, int, 0644);
121 MODULE_PARM_DESC(debug, "Debug enabled or not");
124 * Under some conditions, the card gets stuck and stops paying attention
125 * to the world (i.e. data communication stalls) until we do something to
126 * it. Sending an INQ_TALLIES command seems to be enough and should be
127 * harmless otherwise. This behaviour has been observed when using the
128 * driver on a systemimager client during installation. In the past a
129 * timer was used to send INQ_TALLIES commands when there was no other
130 * activity, but it was troublesome and was removed.
133 #define USB_COMPAQ_VENDOR_ID 0x049f /* Compaq Computer Corp. */
134 #define USB_COMPAQ_WL215_ID 0x001f /* Compaq WL215 USB Adapter */
135 #define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */
136 #define USB_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */
138 #define USB_MELCO_VENDOR_ID 0x0411
139 #define USB_BUFFALO_L11_ID 0x0006 /* BUFFALO WLI-USB-L11 Wireless LAN Adapter */
140 #define USB_BUFFALO_L11G_WR_ID 0x000B /* BUFFALO WLI-USB-L11G-WR Wireless LAN Adapter */
141 #define USB_BUFFALO_L11G_ID 0x000D /* BUFFALO WLI-USB-L11G Wireless LAN Adapter */
143 #define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */
144 #define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB Client */
146 #define USB_AVAYA8_VENDOR_ID 0x0D98
147 #define USB_AVAYAE_VENDOR_ID 0x0D9E
148 #define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */
150 #define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */
151 #define USB_AGERE_MODEL0801_ID 0x1000 /* Wireless USB Card Model 0801 */
152 #define USB_AGERE_MODEL0802_ID 0x1001 /* Wireless USB Card Model 0802 */
153 #define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */
155 #define USB_ELSA_VENDOR_ID 0x05CC
156 #define USB_ELSA_AIRLANCER_ID 0x3100 /* ELSA AirLancer USB-11 */
158 #define USB_LEGEND_VENDOR_ID 0x0E7C
159 #define USB_LEGEND_JOYNET_ID 0x0300 /* Joynet WLAN USB Card */
161 #define USB_SAMSUNG_VENDOR_ID 0x04E8
162 #define USB_SAMSUNG_SEW2001U1_ID 0x5002 /* Samsung SEW-2001u Card */
163 #define USB_SAMSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */
164 #define USB_SAMSUNG_SEW2003U_ID 0x7011 /* Samsung SEW-2003U Card */
166 #define USB_IGATE_VENDOR_ID 0x0681
167 #define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */
169 #define USB_FUJITSU_VENDOR_ID 0x0BF8
170 #define USB_FUJITSU_E1100_ID 0x1002 /* connect2AIR WLAN E-1100 USB */
172 #define USB_2WIRE_VENDOR_ID 0x1630
173 #define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */
176 #define EZUSB_REQUEST_FW_TRANS 0xA0
177 #define EZUSB_REQUEST_TRIGER 0xAA
178 #define EZUSB_REQUEST_TRIG_AC 0xAC
179 #define EZUSB_CPUCS_REG 0x7F92
181 #define EZUSB_RID_TX 0x0700
182 #define EZUSB_RID_RX 0x0701
183 #define EZUSB_RID_INIT1 0x0702
184 #define EZUSB_RID_ACK 0x0710
185 #define EZUSB_RID_DOCMD 0x0860
187 /* Recognize info frames */
188 #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF))
190 #define EZUSB_MAGIC 0x0210
192 #define EZUSB_FRAME_DATA 1
193 #define EZUSB_FRAME_CONTROL 2
195 #define DEF_TIMEOUT (3*HZ)
197 #define BULK_BUF_SIZE 2048
199 #define FW_BUF_SIZE 64
200 #define FW_VAR_OFFSET_PTR 0x359
201 #define FW_VAR_VALUE 0
202 #define FW_HOLE_START 0x100
203 #define FW_HOLE_END 0x300
205 struct ezusb_packet {
206 __le16 magic; /* 0x0210 */
207 u8 req_reply_count;
208 u8 ans_reply_count;
209 __le16 frame_type; /* 0x01 for data frames, 0x02 otherwise */
210 __le16 size; /* transport size */
211 __le16 crc; /* CRC up to here */
212 __le16 hermes_len;
213 __le16 hermes_rid;
214 u8 data[0];
215 } __attribute__ ((packed));
217 /* Table of devices that work or may work with this driver */
218 static struct usb_device_id ezusb_table[] = {
219 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID)},
220 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID)},
221 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID)},
222 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11_ID)},
223 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_WR_ID)},
224 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_ID)},
225 {USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID)},
226 {USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
227 {USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
228 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0801_ID)},
229 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0802_ID)},
230 {USB_DEVICE(USB_ELSA_VENDOR_ID, USB_ELSA_AIRLANCER_ID)},
231 {USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID)},
232 {USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U1_ID,
233 0, 0)},
234 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U2_ID)},
235 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2003U_ID)},
236 {USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID)},
237 {USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID)},
238 {USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID)},
239 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID)},
240 {} /* Terminating entry */
243 MODULE_DEVICE_TABLE(usb, ezusb_table);
245 /* Structure to hold all of our device specific stuff */
246 struct ezusb_priv {
247 struct usb_device *udev;
248 struct net_device *dev;
249 struct semaphore sem;
250 spinlock_t req_lock;
251 struct list_head req_pending;
252 struct list_head req_active;
253 spinlock_t reply_count_lock;
254 u16 hermes_reg_fake[0x40];
255 u8 *bap_buf;
256 struct urb *read_urb;
257 int read_pipe;
258 int write_pipe;
259 u8 reply_count;
262 typedef enum {
263 EZUSB_CTX_START,
264 EZUSB_CTX_QUEUED,
265 EZUSB_CTX_REQ_SUBMITTED,
266 EZUSB_CTX_REQ_COMPLETE,
267 EZUSB_CTX_RESP_RECEIVED,
268 EZUSB_CTX_REQ_TIMEOUT,
269 EZUSB_CTX_REQ_FAILED,
270 EZUSB_CTX_RESP_TIMEOUT,
271 EZUSB_CTX_REQSUBMIT_FAIL,
272 EZUSB_CTX_COMPLETE,
273 } ezusb_state_t;
275 struct request_context {
276 struct list_head list;
277 atomic_t refcount;
278 struct completion done; /* Signals that CTLX is dead */
279 int killed;
280 struct urb *outurb; /* OUT for req pkt */
281 struct ezusb_priv *upriv;
282 struct ezusb_packet *buf;
283 int buf_length;
284 struct timer_list timer; /* Timeout handling */
285 ezusb_state_t state; /* Current state */
286 /* the RID that we will wait for */
287 u16 out_rid;
288 u16 in_rid;
292 /* Forward declarations */
293 static void ezusb_ctx_complete(struct request_context *ctx);
294 static void ezusb_req_queue_run(struct ezusb_priv *upriv);
295 static void ezusb_bulk_in_callback(struct urb *urb, struct pt_regs *);
298 /* Debug functions */
299 static inline void hex_dump(const void *data_p, int size)
301 int i;
302 const u8 *data = data_p;
304 printk(KERN_DEBUG "Dump at %p, length: %d\n", data, size);
306 for (i = 0; i < size; i++) {
307 if ((i & 0xf) == 0)
308 printk(KERN_DEBUG "0x%04X: ", i);
309 if ((i & 3) == 0)
310 printk(" ");
311 printk("%02X ", data[i]);
312 if ((i & 0xf) == 0xf)
313 printk("\n");
315 if ((i & 0xf) != 0)
316 printk("\n");
320 static inline u8 ezusb_reply_inc(u8 count)
322 if (count < 0x7F)
323 return count + 1;
324 else
325 return 1;
328 static void ezusb_request_context_put(struct request_context *ctx)
330 if (!atomic_dec_and_test(&ctx->refcount))
331 return;
333 WARN_ON(!ctx->done.done);
334 BUG_ON(ctx->outurb->status == -EINPROGRESS);
335 BUG_ON(timer_pending(&ctx->timer));
336 usb_free_urb(ctx->outurb);
337 kfree(ctx->buf);
338 kfree(ctx);
341 static inline void ezusb_mod_timer(struct ezusb_priv *upriv,
342 struct timer_list *timer,
343 unsigned long expire)
345 if (!upriv->udev)
346 return;
347 mod_timer(timer, expire);
350 static void ezusb_request_timerfn(u_long _ctx)
352 struct request_context *ctx = (void *) _ctx;
354 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
355 if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
356 ctx->state = EZUSB_CTX_REQ_TIMEOUT;
357 } else {
358 ctx->state = EZUSB_CTX_RESP_TIMEOUT;
359 dbg("couldn't unlink");
360 atomic_inc(&ctx->refcount);
361 ctx->killed = 1;
362 ezusb_ctx_complete(ctx);
363 ezusb_request_context_put(ctx);
367 static struct request_context *ezusb_alloc_ctx(struct ezusb_priv *upriv,
368 u16 out_rid, u16 in_rid)
370 struct request_context *ctx;
372 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
373 if (!ctx) {
374 return NULL;
376 memset(ctx, 0, sizeof(*ctx));
378 ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC);
379 if (!ctx->buf) {
380 kfree(ctx);
381 return NULL;
383 ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC);
384 if (!ctx->outurb) {
385 kfree(ctx->buf);
386 kfree(ctx);
387 return NULL;
390 ctx->upriv = upriv;
391 ctx->state = EZUSB_CTX_START;
392 ctx->out_rid = out_rid;
393 ctx->in_rid = in_rid;
395 atomic_set(&ctx->refcount, 1);
396 init_completion(&ctx->done);
398 init_timer(&ctx->timer);
399 ctx->timer.function = ezusb_request_timerfn;
400 ctx->timer.data = (u_long) ctx;
401 return ctx;
405 /* Hopefully the real complete_all will soon be exported, in the mean
406 * while this should work. */
407 static inline void ezusb_complete_all(struct completion *comp)
409 complete(comp);
410 complete(comp);
411 complete(comp);
412 complete(comp);
415 static void ezusb_ctx_complete(struct request_context *ctx)
417 struct ezusb_priv *upriv = ctx->upriv;
418 unsigned long flags;
420 spin_lock_irqsave(&upriv->req_lock, flags);
422 list_del_init(&ctx->list);
423 if (upriv->udev) {
424 spin_unlock_irqrestore(&upriv->req_lock, flags);
425 ezusb_req_queue_run(upriv);
426 spin_lock_irqsave(&upriv->req_lock, flags);
429 switch (ctx->state) {
430 case EZUSB_CTX_COMPLETE:
431 case EZUSB_CTX_REQSUBMIT_FAIL:
432 case EZUSB_CTX_REQ_FAILED:
433 case EZUSB_CTX_REQ_TIMEOUT:
434 case EZUSB_CTX_RESP_TIMEOUT:
435 spin_unlock_irqrestore(&upriv->req_lock, flags);
437 if ((ctx->out_rid == EZUSB_RID_TX) && upriv->dev) {
438 struct net_device *dev = upriv->dev;
439 struct orinoco_private *priv = netdev_priv(dev);
440 struct net_device_stats *stats = &priv->stats;
442 if (ctx->state != EZUSB_CTX_COMPLETE)
443 stats->tx_errors++;
444 else
445 stats->tx_packets++;
447 netif_wake_queue(dev);
449 ezusb_complete_all(&ctx->done);
450 ezusb_request_context_put(ctx);
451 break;
453 default:
454 spin_unlock_irqrestore(&upriv->req_lock, flags);
455 if (!upriv->udev) {
456 /* This is normal, as all request contexts get flushed
457 * when the device is disconnected */
458 err("Called, CTLX not terminating, but device gone");
459 ezusb_complete_all(&ctx->done);
460 ezusb_request_context_put(ctx);
461 break;
464 err("Called, CTLX not in terminating state.");
465 /* Things are really bad if this happens. Just leak
466 * the CTLX because it may still be linked to the
467 * queue or the OUT urb may still be active.
468 * Just leaking at least prevents an Oops or Panic.
470 break;
475 * ezusb_req_queue_run:
476 * Description:
477 * Note: Only one active CTX at any one time, because there's no
478 * other (reliable) way to match the response URB to the correct
479 * CTX.
481 static void ezusb_req_queue_run(struct ezusb_priv *upriv)
483 unsigned long flags;
484 struct request_context *ctx;
485 int result;
487 spin_lock_irqsave(&upriv->req_lock, flags);
489 if (!list_empty(&upriv->req_active))
490 goto unlock;
492 if (list_empty(&upriv->req_pending))
493 goto unlock;
495 ctx =
496 list_entry(upriv->req_pending.next, struct request_context,
497 list);
499 if (!ctx->upriv->udev)
500 goto unlock;
502 /* We need to split this off to avoid a race condition */
503 list_move_tail(&ctx->list, &upriv->req_active);
505 if (ctx->state == EZUSB_CTX_QUEUED) {
506 atomic_inc(&ctx->refcount);
507 result = usb_submit_urb(ctx->outurb, GFP_ATOMIC);
508 if (result) {
509 ctx->state = EZUSB_CTX_REQSUBMIT_FAIL;
511 spin_unlock_irqrestore(&upriv->req_lock, flags);
513 err("Fatal, failed to submit command urb."
514 " error=%d\n", result);
516 ezusb_ctx_complete(ctx);
517 ezusb_request_context_put(ctx);
518 goto done;
521 ctx->state = EZUSB_CTX_REQ_SUBMITTED;
522 ezusb_mod_timer(ctx->upriv, &ctx->timer,
523 jiffies + DEF_TIMEOUT);
526 unlock:
527 spin_unlock_irqrestore(&upriv->req_lock, flags);
529 done:
530 return;
533 static void ezusb_req_enqueue_run(struct ezusb_priv *upriv,
534 struct request_context *ctx)
536 unsigned long flags;
538 spin_lock_irqsave(&upriv->req_lock, flags);
540 if (!ctx->upriv->udev) {
541 spin_unlock_irqrestore(&upriv->req_lock, flags);
542 goto done;
544 atomic_inc(&ctx->refcount);
545 list_add_tail(&ctx->list, &upriv->req_pending);
546 spin_unlock_irqrestore(&upriv->req_lock, flags);
548 ctx->state = EZUSB_CTX_QUEUED;
549 ezusb_req_queue_run(upriv);
551 done:
552 return;
555 static void ezusb_request_out_callback(struct urb *urb,
556 struct pt_regs *pt_regs)
558 unsigned long flags;
559 ezusb_state_t state;
560 struct request_context *ctx = urb->context;
561 struct ezusb_priv *upriv = ctx->upriv;
563 spin_lock_irqsave(&upriv->req_lock, flags);
565 del_timer(&ctx->timer);
567 if (ctx->killed) {
568 spin_unlock_irqrestore(&upriv->req_lock, flags);
569 warn("interrupt called with dead ctx");
570 goto out;
573 state = ctx->state;
575 if (urb->status == 0) {
576 switch (state) {
577 case EZUSB_CTX_REQ_SUBMITTED:
578 if (ctx->in_rid) {
579 ctx->state = EZUSB_CTX_REQ_COMPLETE;
580 /* reply URB still pending */
581 ezusb_mod_timer(upriv, &ctx->timer,
582 jiffies + DEF_TIMEOUT);
583 spin_unlock_irqrestore(&upriv->req_lock,
584 flags);
585 break;
587 /* fall through */
588 case EZUSB_CTX_RESP_RECEIVED:
589 /* IN already received before this OUT-ACK */
590 ctx->state = EZUSB_CTX_COMPLETE;
591 spin_unlock_irqrestore(&upriv->req_lock, flags);
592 ezusb_ctx_complete(ctx);
593 break;
595 default:
596 spin_unlock_irqrestore(&upriv->req_lock, flags);
597 err("Unexpected state(0x%x, %d) in OUT URB",
598 state, urb->status);
599 break;
601 } else {
602 /* If someone cancels the OUT URB then its status
603 * should be either -ECONNRESET or -ENOENT.
605 switch (state) {
606 case EZUSB_CTX_REQ_SUBMITTED:
607 case EZUSB_CTX_RESP_RECEIVED:
608 ctx->state = EZUSB_CTX_REQ_FAILED;
609 /* fall through */
611 case EZUSB_CTX_REQ_FAILED:
612 case EZUSB_CTX_REQ_TIMEOUT:
613 spin_unlock_irqrestore(&upriv->req_lock, flags);
615 ezusb_ctx_complete(ctx);
616 break;
618 default:
619 spin_unlock_irqrestore(&upriv->req_lock, flags);
621 err("Unexpected state(0x%x, %d) in OUT URB",
622 state, urb->status);
623 break;
626 out:
627 ezusb_request_context_put(ctx);
630 static void ezusb_request_in_callback(struct ezusb_priv *upriv,
631 struct urb *urb)
633 struct ezusb_packet *ans = urb->transfer_buffer;
634 struct request_context *ctx = NULL;
635 ezusb_state_t state;
636 unsigned long flags;
638 /* Find the CTLX on the active queue that requested this URB */
639 spin_lock_irqsave(&upriv->req_lock, flags);
640 if (upriv->udev) {
641 struct list_head *item;
643 list_for_each(item, &upriv->req_active) {
644 struct request_context *c;
645 int reply_count;
647 c = list_entry(item, struct request_context, list);
648 reply_count =
649 ezusb_reply_inc(c->buf->req_reply_count);
650 if ((ans->ans_reply_count == reply_count)
651 && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) {
652 ctx = c;
653 break;
655 dbg("Skipped (0x%x/0x%x) (%d/%d)",
656 le16_to_cpu(ans->hermes_rid),
657 c->in_rid, ans->ans_reply_count, reply_count);
661 if (ctx == NULL) {
662 spin_unlock_irqrestore(&upriv->req_lock, flags);
663 err("%s: got unexpected RID: 0x%04X", __FUNCTION__,
664 le16_to_cpu(ans->hermes_rid));
665 ezusb_req_queue_run(upriv);
666 return;
669 /* The data we want is in the in buffer, exchange */
670 urb->transfer_buffer = ctx->buf;
671 ctx->buf = (void *) ans;
672 ctx->buf_length = urb->actual_length;
674 state = ctx->state;
675 switch (state) {
676 case EZUSB_CTX_REQ_SUBMITTED:
677 /* We have received our response URB before
678 * our request has been acknowledged. Do NOT
679 * destroy our CTX yet, because our OUT URB
680 * is still alive ...
682 ctx->state = EZUSB_CTX_RESP_RECEIVED;
683 spin_unlock_irqrestore(&upriv->req_lock, flags);
685 /* Let the machine continue running. */
686 break;
688 case EZUSB_CTX_REQ_COMPLETE:
689 /* This is the usual path: our request
690 * has already been acknowledged, and
691 * we have now received the reply.
693 ctx->state = EZUSB_CTX_COMPLETE;
695 /* Stop the intimer */
696 del_timer(&ctx->timer);
697 spin_unlock_irqrestore(&upriv->req_lock, flags);
699 /* Call the completion handler */
700 ezusb_ctx_complete(ctx);
701 break;
703 default:
704 spin_unlock_irqrestore(&upriv->req_lock, flags);
706 warn("Matched IN URB, unexpected context state(0x%x)",
707 state);
708 /* Throw this CTX away and try submitting another */
709 del_timer(&ctx->timer);
710 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
711 usb_unlink_urb(ctx->outurb);
712 ezusb_req_queue_run(upriv);
713 break;
714 } /* switch */
718 static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
719 struct request_context *ctx)
721 switch (ctx->state) {
722 case EZUSB_CTX_QUEUED:
723 case EZUSB_CTX_REQ_SUBMITTED:
724 case EZUSB_CTX_REQ_COMPLETE:
725 case EZUSB_CTX_RESP_RECEIVED:
726 if (in_atomic()) {
727 /* If we get called from a timer, timeout timers don't
728 * get the chance to run themselfs. So we make sure
729 * that we don't sleep for ever */
730 int msecs = DEF_TIMEOUT * (1000 / HZ);
731 while (!ctx->done.done && msecs--)
732 udelay(1000);
733 } else {
734 wait_event_interruptible(ctx->done.wait,
735 ctx->done.done);
737 break;
738 default:
739 /* Done or failed - nothing to wait for */
740 break;
744 static inline u16 build_crc(struct ezusb_packet *data)
746 u16 crc = 0;
747 u8 *bytes = (u8 *)data;
748 int i;
750 for (i = 0; i < 8; i++)
751 crc = (crc << 1) + bytes[i];
753 return crc;
757 * ezusb_fill_req:
759 * if data == NULL and length > 0 the data is assumed to be already in
760 * the target buffer and only the header is filled.
763 static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid,
764 const void *data, u16 frame_type, u8 reply_count)
766 int total_size = sizeof(*req) + length;
768 BUG_ON(total_size > BULK_BUF_SIZE);
770 req->magic = cpu_to_le16(EZUSB_MAGIC);
771 req->req_reply_count = reply_count;
772 req->ans_reply_count = 0;
773 req->frame_type = cpu_to_le16(frame_type);
774 req->size = cpu_to_le16(length + 4);
775 req->crc = cpu_to_le16(build_crc(req));
776 req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length));
777 req->hermes_rid = cpu_to_le16(rid);
778 if (data)
779 memcpy(req->data, data, length);
780 return total_size;
783 static int ezusb_submit_in_urb(struct ezusb_priv *upriv)
785 int retval = 0;
786 void *cur_buf = upriv->read_urb->transfer_buffer;
788 if (upriv->read_urb->status == -EINPROGRESS) {
789 dbg("urb busy, not resubmiting");
790 retval = -EBUSY;
791 goto exit;
793 usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe,
794 cur_buf, BULK_BUF_SIZE,
795 ezusb_bulk_in_callback, upriv);
796 upriv->read_urb->transfer_flags = 0;
797 retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC);
798 if (retval)
799 err("%s submit failed %d", __FUNCTION__, retval);
801 exit:
802 return retval;
805 static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset)
807 u8 res_val = reset; /* avoid argument promotion */
809 if (!upriv->udev) {
810 err("%s: !upriv->udev", __FUNCTION__);
811 return -EFAULT;
813 return usb_control_msg(upriv->udev,
814 usb_sndctrlpipe(upriv->udev, 0),
815 EZUSB_REQUEST_FW_TRANS,
816 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
817 USB_DIR_OUT, EZUSB_CPUCS_REG, 0, &res_val,
818 sizeof(res_val), DEF_TIMEOUT);
821 static int ezusb_firmware_download(struct ezusb_priv *upriv,
822 struct ez_usb_fw *fw)
824 u8 fw_buffer[FW_BUF_SIZE];
825 int retval, addr;
826 int variant_offset;
829 * This byte is 1 and should be replaced with 0. The offset is
830 * 0x10AD in version 0.0.6. The byte in question should follow
831 * the end of the code pointed to by the jump in the beginning
832 * of the firmware. Also, it is read by code located at 0x358.
834 variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
835 if (variant_offset >= fw->size) {
836 printk(KERN_ERR PFX "Invalid firmware variant offset: "
837 "0x%04x\n", variant_offset);
838 retval = -EINVAL;
839 goto fail;
842 retval = ezusb_8051_cpucs(upriv, 1);
843 if (retval < 0)
844 goto fail;
845 for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
846 /* 0x100-0x300 should be left alone, it contains card
847 * specific data, like USB enumeration information */
848 if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
849 continue;
851 memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
852 if (variant_offset >= addr &&
853 variant_offset < addr + FW_BUF_SIZE) {
854 dbg("Patching card_variant byte at 0x%04X",
855 variant_offset);
856 fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
858 retval = usb_control_msg(upriv->udev,
859 usb_sndctrlpipe(upriv->udev, 0),
860 EZUSB_REQUEST_FW_TRANS,
861 USB_TYPE_VENDOR | USB_RECIP_DEVICE
862 | USB_DIR_OUT,
863 addr, 0x0,
864 fw_buffer, FW_BUF_SIZE,
865 DEF_TIMEOUT);
867 if (retval < 0)
868 goto fail;
870 retval = ezusb_8051_cpucs(upriv, 0);
871 if (retval < 0)
872 goto fail;
874 goto exit;
875 fail:
876 printk(KERN_ERR PFX "Firmware download failed, error %d\n",
877 retval);
878 exit:
879 return retval;
882 static int ezusb_access_ltv(struct ezusb_priv *upriv,
883 struct request_context *ctx,
884 u16 length, const void *data, u16 frame_type,
885 void *ans_buff, int ans_size, u16 *ans_length)
887 int req_size;
888 int retval = 0;
889 ezusb_state_t state;
891 BUG_ON(in_irq());
893 if (!upriv->udev) {
894 dbg("Device disconnected");
895 return -ENODEV;
898 if (upriv->read_urb->status != -EINPROGRESS) {
899 err("%s: in urb not pending", __FUNCTION__);
902 /* protect upriv->reply_count, guarantee sequential numbers */
903 spin_lock_bh(&upriv->reply_count_lock);
904 req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
905 frame_type, upriv->reply_count);
906 usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
907 ctx->buf, req_size,
908 ezusb_request_out_callback, ctx);
910 if (ctx->in_rid)
911 upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
913 ezusb_req_enqueue_run(upriv, ctx);
915 spin_unlock_bh(&upriv->reply_count_lock);
917 if (ctx->in_rid)
918 ezusb_req_ctx_wait(upriv, ctx);
920 state = ctx->state;
921 switch (state) {
922 case EZUSB_CTX_COMPLETE:
923 retval = ctx->outurb->status;
924 break;
926 case EZUSB_CTX_QUEUED:
927 case EZUSB_CTX_REQ_SUBMITTED:
928 if (!ctx->in_rid)
929 break;
930 default:
931 err("%s: Unexpected context state %d", __FUNCTION__,
932 state);
933 /* fall though */
934 case EZUSB_CTX_REQ_TIMEOUT:
935 case EZUSB_CTX_REQ_FAILED:
936 case EZUSB_CTX_RESP_TIMEOUT:
937 case EZUSB_CTX_REQSUBMIT_FAIL:
938 printk(KERN_ERR PFX "Access failed, resetting (state %d,"
939 " reply_count %d)\n", state, upriv->reply_count);
940 upriv->reply_count = 0;
941 if (state == EZUSB_CTX_REQ_TIMEOUT
942 || state == EZUSB_CTX_RESP_TIMEOUT) {
943 printk(KERN_ERR PFX "ctx timed out\n");
944 retval = -ETIMEDOUT;
945 } else {
946 printk(KERN_ERR PFX "ctx failed\n");
947 retval = -EFAULT;
949 goto exit;
950 break;
952 if (ctx->in_rid) {
953 struct ezusb_packet *ans = ctx->buf;
954 int exp_len;
956 if (ans->hermes_len != 0)
957 exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
958 else
959 exp_len = 14;
961 if (exp_len != ctx->buf_length) {
962 err("%s: lenght mismatch for RID 0x%04x: "
963 "expected %d, got %d", __FUNCTION__,
964 ctx->in_rid, exp_len, ctx->buf_length);
965 retval = -EIO;
966 goto exit;
969 if (ans_buff)
970 memcpy(ans_buff, ans->data,
971 min_t(int, exp_len, ans_size));
972 if (ans_length)
973 *ans_length = le16_to_cpu(ans->hermes_len);
975 exit:
976 ezusb_request_context_put(ctx);
977 return retval;
980 static int ezusb_write_ltv(hermes_t *hw, int bap, u16 rid,
981 u16 length, const void *data)
983 struct ezusb_priv *upriv = hw->priv;
984 u16 frame_type;
985 struct request_context *ctx;
987 if (length == 0)
988 return -EINVAL;
990 length = HERMES_RECLEN_TO_BYTES(length);
992 /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
993 * set to be empty, but the USB bridge doesn't like it */
994 if (length == 0)
995 return 0;
997 ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
998 if (!ctx)
999 return -ENOMEM;
1001 if (rid == EZUSB_RID_TX)
1002 frame_type = EZUSB_FRAME_DATA;
1003 else
1004 frame_type = EZUSB_FRAME_CONTROL;
1006 return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
1007 NULL, 0, NULL);
1010 static int ezusb_read_ltv(hermes_t *hw, int bap, u16 rid,
1011 unsigned bufsize, u16 *length, void *buf)
1013 struct ezusb_priv *upriv = hw->priv;
1014 struct request_context *ctx;
1016 if ((bufsize < 0) || (bufsize % 2))
1017 return -EINVAL;
1019 ctx = ezusb_alloc_ctx(upriv, rid, rid);
1020 if (!ctx)
1021 return -ENOMEM;
1023 return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
1024 buf, bufsize, length);
1027 static int ezusb_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
1028 struct hermes_response *resp)
1030 struct ezusb_priv *upriv = hw->priv;
1031 struct request_context *ctx;
1033 __le16 data[4] = {
1034 cpu_to_le16(cmd),
1035 cpu_to_le16(parm0),
1039 dbg("0x%04X, parm0 0x%04X", cmd, parm0);
1040 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
1041 if (!ctx)
1042 return -ENOMEM;
1044 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1045 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1048 static int ezusb_bap_pread(struct hermes *hw, int bap,
1049 void *buf, int len, u16 id, u16 offset)
1051 struct ezusb_priv *upriv = hw->priv;
1052 struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
1053 int actual_length = upriv->read_urb->actual_length;
1055 if (id == EZUSB_RID_RX) {
1056 if ((sizeof(*ans) + offset + len) > actual_length) {
1057 printk(KERN_ERR PFX "BAP read beyond buffer end "
1058 "in rx frame\n");
1059 return -EINVAL;
1061 memcpy(buf, ans->data + offset, len);
1062 return 0;
1065 if (EZUSB_IS_INFO(id)) {
1066 /* Include 4 bytes for length/type */
1067 if ((sizeof(*ans) + offset + len - 4) > actual_length) {
1068 printk(KERN_ERR PFX "BAP read beyond buffer end "
1069 "in info frame\n");
1070 return -EFAULT;
1072 memcpy(buf, ans->data + offset - 4, len);
1073 } else {
1074 printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
1075 return -EINVAL;
1078 return 0;
1081 static int ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
1083 struct orinoco_private *priv =
1084 (struct orinoco_private *) netdev_priv(dev);
1085 struct net_device_stats *stats = &priv->stats;
1086 struct ezusb_priv *upriv = priv->card;
1087 int err = 0;
1088 char *p;
1089 struct ethhdr *eh;
1090 int len, data_len, data_off;
1091 __le16 tx_control;
1092 unsigned long flags;
1093 struct request_context *ctx;
1094 u8 *buf;
1095 int tx_size;
1097 if (!netif_running(dev)) {
1098 printk(KERN_ERR "%s: Tx on stopped device!\n",
1099 dev->name);
1100 return 1;
1103 if (netif_queue_stopped(dev)) {
1104 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
1105 dev->name);
1106 return 1;
1109 if (orinoco_lock(priv, &flags) != 0) {
1110 printk(KERN_ERR
1111 "%s: orinoco_xmit() called while hw_unavailable\n",
1112 dev->name);
1113 return 1;
1116 if (!netif_carrier_ok(dev) ||
1117 (priv->iw_mode == IW_MODE_MONITOR)) {
1118 /* Oops, the firmware hasn't established a connection,
1119 silently drop the packet (this seems to be the
1120 safest approach). */
1121 stats->tx_errors++;
1122 orinoco_unlock(priv, &flags);
1123 dev_kfree_skb(skb);
1124 return 0;
1127 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
1128 if (!ctx) {
1129 err = -ENOMEM;
1130 goto fail;
1132 memset(ctx->buf, 0, BULK_BUF_SIZE);
1133 buf = ctx->buf->data;
1135 /* Length of the packet body */
1136 /* FIXME: what if the skb is smaller than this? */
1137 len = max_t(int, skb->len - ETH_HLEN, ETH_ZLEN - ETH_HLEN);
1139 eh = (struct ethhdr *) skb->data;
1141 tx_control = cpu_to_le16(0);
1142 memcpy(buf, &tx_control, sizeof(tx_control));
1143 buf += sizeof(tx_control);
1144 /* Encapsulate Ethernet-II frames */
1145 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
1146 struct header_struct *hdr = (void *) buf;
1147 buf += sizeof(*hdr);
1148 data_len = len;
1149 data_off = sizeof(tx_control) + sizeof(*hdr);
1150 p = skb->data + ETH_HLEN;
1152 /* 802.3 header */
1153 memcpy(hdr->dest, eh->h_dest, ETH_ALEN);
1154 memcpy(hdr->src, eh->h_source, ETH_ALEN);
1155 hdr->len = htons(data_len + ENCAPS_OVERHEAD);
1157 /* 802.2 header */
1158 memcpy(&hdr->dsap, &encaps_hdr, sizeof(encaps_hdr));
1160 hdr->ethertype = eh->h_proto;
1161 } else { /* IEEE 802.3 frame */
1162 data_len = len + ETH_HLEN;
1163 data_off = sizeof(tx_control);
1164 p = skb->data;
1167 memcpy(buf, p, data_len);
1168 buf += data_len;
1170 /* Finally, we actually initiate the send */
1171 netif_stop_queue(dev);
1173 /* The card may behave better if we send evenly sized usb transfers */
1174 tx_size = ALIGN(buf - ctx->buf->data, 2);
1176 err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
1177 EZUSB_FRAME_DATA, NULL, 0, NULL);
1179 if (err) {
1180 netif_start_queue(dev);
1181 if (net_ratelimit())
1182 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1183 dev->name, err);
1184 stats->tx_errors++;
1185 goto fail;
1188 dev->trans_start = jiffies;
1189 stats->tx_bytes += data_off + data_len;
1191 orinoco_unlock(priv, &flags);
1193 dev_kfree_skb(skb);
1195 return 0;
1196 fail:
1197 orinoco_unlock(priv, &flags);
1198 return err;
1201 static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
1203 *fid = EZUSB_RID_TX;
1204 return 0;
1208 static int ezusb_hard_reset(struct orinoco_private *priv)
1210 struct ezusb_priv *upriv = priv->card;
1211 int retval = ezusb_8051_cpucs(upriv, 1);
1213 if (retval < 0) {
1214 err("Failed to reset");
1215 return retval;
1218 retval = ezusb_8051_cpucs(upriv, 0);
1219 if (retval < 0) {
1220 err("Failed to unreset");
1221 return retval;
1224 dbg("sending control message");
1225 retval = usb_control_msg(upriv->udev,
1226 usb_sndctrlpipe(upriv->udev, 0),
1227 EZUSB_REQUEST_TRIGER,
1228 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1229 USB_DIR_OUT, 0x0, 0x0, NULL, 0,
1230 DEF_TIMEOUT);
1231 if (retval < 0) {
1232 err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
1233 return retval;
1235 #if 0
1236 dbg("Sending EZUSB_REQUEST_TRIG_AC");
1237 retval = usb_control_msg(upriv->udev,
1238 usb_sndctrlpipe(upriv->udev, 0),
1239 EZUSB_REQUEST_TRIG_AC,
1240 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1241 USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
1242 DEF_TIMEOUT);
1243 if (retval < 0) {
1244 err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
1245 return retval;
1247 #endif
1249 return 0;
1253 static int ezusb_init(hermes_t *hw)
1255 struct ezusb_priv *upriv = hw->priv;
1256 int retval;
1258 BUG_ON(in_interrupt());
1259 BUG_ON(!upriv);
1261 upriv->reply_count = 0;
1262 /* Write the MAGIC number on the simulated registers to keep
1263 * orinoco.c happy */
1264 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
1265 hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
1267 usb_kill_urb(upriv->read_urb);
1268 ezusb_submit_in_urb(upriv);
1270 retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
1271 HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
1272 if (retval < 0) {
1273 printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
1274 return retval;
1277 retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
1278 if (retval < 0) {
1279 printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
1280 return retval;
1283 return 0;
1286 static void ezusb_bulk_in_callback(struct urb *urb, struct pt_regs *regs)
1288 struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
1289 struct ezusb_packet *ans = urb->transfer_buffer;
1290 u16 crc;
1291 u16 hermes_rid;
1293 if (upriv->udev == NULL) {
1294 dbg("disconnected");
1295 return;
1298 if (urb->status == -ETIMEDOUT) {
1299 /* When a device gets unplugged we get this every time
1300 * we resubmit, flooding the logs. Since we don't use
1301 * USB timeouts, it shouldn't happen any other time*/
1302 warn("%s: urb timed out, not resubmiting", __FUNCTION__);
1303 return;
1305 if (urb->status == -ECONNABORTED) {
1306 warn("%s: connection abort, resubmiting urb",
1307 __FUNCTION__);
1308 goto resubmit;
1310 if ((urb->status == -EILSEQ)
1311 || (urb->status == -ENOENT)
1312 || (urb->status == -ECONNRESET)) {
1313 dbg("status %d, not resubmiting", urb->status);
1314 return;
1316 if (urb->status)
1317 dbg("status: %d length: %d",
1318 urb->status, urb->actual_length);
1319 if (urb->actual_length < sizeof(*ans)) {
1320 err("%s: short read, ignoring", __FUNCTION__);
1321 goto resubmit;
1323 crc = build_crc(ans);
1324 if (le16_to_cpu(ans->crc) != crc) {
1325 err("CRC error, ignoring packet");
1326 goto resubmit;
1329 hermes_rid = le16_to_cpu(ans->hermes_rid);
1330 if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
1331 ezusb_request_in_callback(upriv, urb);
1332 } else if (upriv->dev) {
1333 struct net_device *dev = upriv->dev;
1334 struct orinoco_private *priv = netdev_priv(dev);
1335 hermes_t *hw = &priv->hw;
1337 if (hermes_rid == EZUSB_RID_RX) {
1338 __orinoco_ev_rx(dev, hw);
1339 } else {
1340 hermes_write_regn(hw, INFOFID,
1341 le16_to_cpu(ans->hermes_rid));
1342 __orinoco_ev_info(dev, hw);
1346 resubmit:
1347 if (upriv->udev)
1348 ezusb_submit_in_urb(upriv);
1351 static inline void ezusb_delete(struct ezusb_priv *upriv)
1353 struct net_device *dev;
1354 struct list_head *item;
1355 struct list_head *tmp_item;
1356 unsigned long flags;
1358 BUG_ON(in_interrupt());
1359 BUG_ON(!upriv);
1361 dev = upriv->dev;
1362 down(&upriv->sem);
1364 upriv->udev = NULL; /* No timer will be rearmed from here */
1366 usb_kill_urb(upriv->read_urb);
1368 restart_list:
1369 spin_lock_irqsave(&upriv->req_lock, flags);
1370 list_for_each_safe(item, tmp_item, &upriv->req_active) {
1371 struct request_context *ctx;
1373 ctx = list_entry(item, struct request_context, list);
1374 atomic_inc(&ctx->refcount);
1376 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
1377 if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
1378 spin_unlock_irqrestore(&upriv->req_lock, flags);
1379 wait_for_completion(&ctx->done);
1380 } else {
1381 spin_unlock_irqrestore(&upriv->req_lock, flags);
1383 del_timer_sync(&ctx->timer);
1384 /* FIXME: there is an slight chance for the irq handler to
1385 * be running */
1386 if (!list_empty(&ctx->list))
1387 ezusb_ctx_complete(ctx);
1389 ezusb_request_context_put(ctx);
1390 goto restart_list;
1392 spin_unlock_irqrestore(&upriv->req_lock, flags);
1394 list_for_each_safe(item, tmp_item, &upriv->req_pending)
1395 ezusb_ctx_complete(list_entry(item,
1396 struct request_context, list));
1398 if (upriv->read_urb->status == -EINPROGRESS) {
1399 printk(KERN_ERR PFX "Some URB in progress\n");
1401 up(&upriv->sem);
1403 kfree(upriv->read_urb->transfer_buffer);
1404 if (upriv->bap_buf != NULL)
1405 kfree(upriv->bap_buf);
1406 if (upriv->read_urb != NULL)
1407 usb_free_urb(upriv->read_urb);
1408 if (upriv->dev) {
1409 unregister_netdev(upriv->dev);
1410 free_orinocodev(upriv->dev);
1414 static const struct hermes_ops ezusb_ops = {
1415 .init = ezusb_init,
1416 .docmd_wait = ezusb_docmd_wait,
1417 .allocate = ezusb_allocate,
1418 .read_ltv = ezusb_read_ltv,
1419 .write_ltv = ezusb_write_ltv,
1420 .bap_pread = ezusb_bap_pread
1423 static int ezusb_probe(struct usb_interface *interface,
1424 const struct usb_device_id *id)
1426 struct usb_device *udev = interface_to_usbdev(interface);
1427 struct orinoco_private *priv;
1428 struct net_device *dev;
1429 hermes_t *hw;
1430 struct ezusb_priv *upriv = NULL;
1431 struct usb_interface_descriptor *iface_desc;
1432 struct usb_endpoint_descriptor *ep;
1433 const struct firmware *fw_entry;
1434 int retval = 0;
1435 int i;
1437 dev = alloc_orinocodev(sizeof(*upriv), ezusb_hard_reset);
1438 if (!dev) {
1439 err("Couldn't allocate dev");
1440 goto exit;
1443 priv = netdev_priv(dev);
1444 hw = &priv->hw;
1446 upriv = priv->card;
1448 init_MUTEX(&upriv->sem);
1449 spin_lock_init(&upriv->reply_count_lock);
1451 spin_lock_init(&upriv->req_lock);
1452 INIT_LIST_HEAD(&upriv->req_pending);
1453 INIT_LIST_HEAD(&upriv->req_active);
1455 upriv->udev = udev;
1457 hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake;
1458 hw->reg_spacing = HERMES_16BIT_REGSPACING;
1459 hw->priv = upriv;
1460 hw->ops = &ezusb_ops;
1461 dev->hard_start_xmit = ezusb_xmit;
1463 priv->irq_no_disable = 1;
1465 /* set up the endpoint information */
1466 /* check out the endpoints */
1468 iface_desc = &interface->altsetting[0].desc;
1469 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1470 ep = &interface->altsetting[0].endpoint[i].desc;
1472 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1473 == USB_DIR_IN) &&
1474 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1475 == USB_ENDPOINT_XFER_BULK)) {
1476 /* we found a bulk in endpoint */
1477 if (upriv->read_urb != NULL) {
1478 warn("Found a second bulk in ep, ignored");
1479 continue;
1482 upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
1483 if (!upriv->read_urb) {
1484 err("No free urbs available");
1485 goto error;
1487 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1488 warn("bulk in: wMaxPacketSize!= 64");
1489 if (ep->bEndpointAddress != (2 | USB_DIR_IN))
1490 warn("bulk in: bEndpointAddress: %d",
1491 ep->bEndpointAddress);
1492 upriv->read_pipe = usb_rcvbulkpipe(udev,
1493 ep->
1494 bEndpointAddress);
1495 upriv->read_urb->transfer_buffer =
1496 kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1497 if (!upriv->read_urb->transfer_buffer) {
1498 err("Couldn't allocate IN buffer");
1499 goto error;
1503 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1504 == USB_DIR_OUT) &&
1505 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1506 == USB_ENDPOINT_XFER_BULK)) {
1507 /* we found a bulk out endpoint */
1508 if (upriv->bap_buf != NULL) {
1509 warn("Found a second bulk out ep, ignored");
1510 continue;
1513 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1514 warn("bulk out: wMaxPacketSize != 64");
1515 if (ep->bEndpointAddress != 2)
1516 warn("bulk out: bEndpointAddress: %d",
1517 ep->bEndpointAddress);
1518 upriv->write_pipe = usb_sndbulkpipe(udev,
1519 ep->
1520 bEndpointAddress);
1521 upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1522 if (!upriv->bap_buf) {
1523 err("Couldn't allocate bulk_out_buffer");
1524 goto error;
1528 if (!upriv->bap_buf || !upriv->read_urb) {
1529 err("Didn't find the required bulk endpoints");
1530 goto error;
1533 if (request_firmware(&fw_entry, "orinoco_ezusb_fw",
1534 &interface->dev) == 0) {
1535 firmware.size = fw_entry->size;
1536 firmware.code = fw_entry->data;
1538 if (firmware.size && firmware.code) {
1539 ezusb_firmware_download(upriv, &firmware);
1540 } else {
1541 err("No firmware to download");
1542 goto error;
1545 if (ezusb_hard_reset(priv) < 0) {
1546 err("Cannot reset the device");
1547 goto error;
1550 /* If the firmware is already downloaded orinoco.c will call
1551 * ezusb_init but if the firmware is not already there, that will make
1552 * the kernel very unstable, so we try initializing here and quit in
1553 * case of error */
1554 if (ezusb_init(hw) < 0) {
1555 err("Couldn't initialize the device");
1556 err("Firmware may not be downloaded or may be wrong.");
1557 goto error;
1560 SET_MODULE_OWNER(dev);
1561 SET_NETDEV_DEV(dev, &interface->dev);
1562 upriv->dev = dev;
1563 if (register_netdev(dev) != 0) {
1564 upriv->dev = NULL;
1565 err("%s: register_netdev() failed", __FUNCTION__);
1566 goto error;
1568 goto exit;
1570 error:
1571 ezusb_delete(upriv);
1572 if (dev) {
1573 /* upriv->dev was 0, so ezusb_delete() didn't free it */
1574 free_orinocodev(dev);
1576 upriv = NULL;
1577 retval = -EFAULT;
1578 exit:
1579 if (fw_entry) {
1580 firmware.code = NULL;
1581 firmware.size = 0;
1582 release_firmware(fw_entry);
1584 usb_set_intfdata(interface, upriv);
1585 return retval;
1589 static void ezusb_disconnect(struct usb_interface *intf)
1591 struct ezusb_priv *upriv = usb_get_intfdata(intf);
1592 usb_set_intfdata(intf, NULL);
1593 ezusb_delete(upriv);
1594 printk(KERN_INFO PFX "Disconnected\n");
1598 /* usb specific object needed to register this driver with the usb subsystem */
1599 static struct usb_driver orinoco_driver = {
1600 .name = DRIVER_NAME,
1601 .probe = ezusb_probe,
1602 .disconnect = ezusb_disconnect,
1603 .id_table = ezusb_table,
1606 /* Can't be declared "const" or the whole __initdata section will
1607 * become const */
1608 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
1609 " (Manuel Estrada Sainz)";
1611 static int __init ezusb_module_init(void)
1613 int err;
1615 printk(KERN_DEBUG "%s\n", version);
1617 /* register this driver with the USB subsystem */
1618 err = usb_register(&orinoco_driver);
1619 if (err < 0) {
1620 printk(KERN_ERR PFX "usb_register failed, error %d\n",
1621 err);
1622 return err;
1625 return 0;
1628 static void __exit ezusb_module_exit(void)
1630 /* deregister this driver with the USB subsystem */
1631 usb_deregister(&orinoco_driver);
1635 module_init(ezusb_module_init);
1636 module_exit(ezusb_module_exit);
1638 MODULE_AUTHOR("Manuel Estrada Sainz");
1639 MODULE_DESCRIPTION
1640 ("Driver for Orinoco wireless LAN cards using EZUSB bridge");
1641 MODULE_LICENSE("Dual MPL/GPL");