svn-1299
[orinoco_usb.git] / drivers / net / wireless / orinoco_usb.c
blob3046e65db5b4ba3557854d9c0760b02f818243b6
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);
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)
557 unsigned long flags;
558 ezusb_state_t state;
559 struct request_context *ctx = urb->context;
560 struct ezusb_priv *upriv = ctx->upriv;
562 spin_lock_irqsave(&upriv->req_lock, flags);
564 del_timer(&ctx->timer);
566 if (ctx->killed) {
567 spin_unlock_irqrestore(&upriv->req_lock, flags);
568 warn("interrupt called with dead ctx");
569 goto out;
572 state = ctx->state;
574 if (urb->status == 0) {
575 switch (state) {
576 case EZUSB_CTX_REQ_SUBMITTED:
577 if (ctx->in_rid) {
578 ctx->state = EZUSB_CTX_REQ_COMPLETE;
579 /* reply URB still pending */
580 ezusb_mod_timer(upriv, &ctx->timer,
581 jiffies + DEF_TIMEOUT);
582 spin_unlock_irqrestore(&upriv->req_lock,
583 flags);
584 break;
586 /* fall through */
587 case EZUSB_CTX_RESP_RECEIVED:
588 /* IN already received before this OUT-ACK */
589 ctx->state = EZUSB_CTX_COMPLETE;
590 spin_unlock_irqrestore(&upriv->req_lock, flags);
591 ezusb_ctx_complete(ctx);
592 break;
594 default:
595 spin_unlock_irqrestore(&upriv->req_lock, flags);
596 err("Unexpected state(0x%x, %d) in OUT URB",
597 state, urb->status);
598 break;
600 } else {
601 /* If someone cancels the OUT URB then its status
602 * should be either -ECONNRESET or -ENOENT.
604 switch (state) {
605 case EZUSB_CTX_REQ_SUBMITTED:
606 case EZUSB_CTX_RESP_RECEIVED:
607 ctx->state = EZUSB_CTX_REQ_FAILED;
608 /* fall through */
610 case EZUSB_CTX_REQ_FAILED:
611 case EZUSB_CTX_REQ_TIMEOUT:
612 spin_unlock_irqrestore(&upriv->req_lock, flags);
614 ezusb_ctx_complete(ctx);
615 break;
617 default:
618 spin_unlock_irqrestore(&upriv->req_lock, flags);
620 err("Unexpected state(0x%x, %d) in OUT URB",
621 state, urb->status);
622 break;
625 out:
626 ezusb_request_context_put(ctx);
629 static void ezusb_request_in_callback(struct ezusb_priv *upriv,
630 struct urb *urb)
632 struct ezusb_packet *ans = urb->transfer_buffer;
633 struct request_context *ctx = NULL;
634 ezusb_state_t state;
635 unsigned long flags;
637 /* Find the CTLX on the active queue that requested this URB */
638 spin_lock_irqsave(&upriv->req_lock, flags);
639 if (upriv->udev) {
640 struct list_head *item;
642 list_for_each(item, &upriv->req_active) {
643 struct request_context *c;
644 int reply_count;
646 c = list_entry(item, struct request_context, list);
647 reply_count =
648 ezusb_reply_inc(c->buf->req_reply_count);
649 if ((ans->ans_reply_count == reply_count)
650 && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) {
651 ctx = c;
652 break;
654 dbg("Skipped (0x%x/0x%x) (%d/%d)",
655 le16_to_cpu(ans->hermes_rid),
656 c->in_rid, ans->ans_reply_count, reply_count);
660 if (ctx == NULL) {
661 spin_unlock_irqrestore(&upriv->req_lock, flags);
662 err("%s: got unexpected RID: 0x%04X", __FUNCTION__,
663 le16_to_cpu(ans->hermes_rid));
664 ezusb_req_queue_run(upriv);
665 return;
668 /* The data we want is in the in buffer, exchange */
669 urb->transfer_buffer = ctx->buf;
670 ctx->buf = (void *) ans;
671 ctx->buf_length = urb->actual_length;
673 state = ctx->state;
674 switch (state) {
675 case EZUSB_CTX_REQ_SUBMITTED:
676 /* We have received our response URB before
677 * our request has been acknowledged. Do NOT
678 * destroy our CTX yet, because our OUT URB
679 * is still alive ...
681 ctx->state = EZUSB_CTX_RESP_RECEIVED;
682 spin_unlock_irqrestore(&upriv->req_lock, flags);
684 /* Let the machine continue running. */
685 break;
687 case EZUSB_CTX_REQ_COMPLETE:
688 /* This is the usual path: our request
689 * has already been acknowledged, and
690 * we have now received the reply.
692 ctx->state = EZUSB_CTX_COMPLETE;
694 /* Stop the intimer */
695 del_timer(&ctx->timer);
696 spin_unlock_irqrestore(&upriv->req_lock, flags);
698 /* Call the completion handler */
699 ezusb_ctx_complete(ctx);
700 break;
702 default:
703 spin_unlock_irqrestore(&upriv->req_lock, flags);
705 warn("Matched IN URB, unexpected context state(0x%x)",
706 state);
707 /* Throw this CTX away and try submitting another */
708 del_timer(&ctx->timer);
709 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
710 usb_unlink_urb(ctx->outurb);
711 ezusb_req_queue_run(upriv);
712 break;
713 } /* switch */
717 static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
718 struct request_context *ctx)
720 switch (ctx->state) {
721 case EZUSB_CTX_QUEUED:
722 case EZUSB_CTX_REQ_SUBMITTED:
723 case EZUSB_CTX_REQ_COMPLETE:
724 case EZUSB_CTX_RESP_RECEIVED:
725 if (in_atomic()) {
726 /* If we get called from a timer, timeout timers don't
727 * get the chance to run themselfs. So we make sure
728 * that we don't sleep for ever */
729 int msecs = DEF_TIMEOUT * (1000 / HZ);
730 while (!ctx->done.done && msecs--)
731 udelay(1000);
732 } else {
733 wait_event_interruptible(ctx->done.wait,
734 ctx->done.done);
736 break;
737 default:
738 /* Done or failed - nothing to wait for */
739 break;
743 static inline u16 build_crc(struct ezusb_packet *data)
745 u16 crc = 0;
746 u8 *bytes = (u8 *)data;
747 int i;
749 for (i = 0; i < 8; i++)
750 crc = (crc << 1) + bytes[i];
752 return crc;
756 * ezusb_fill_req:
758 * if data == NULL and length > 0 the data is assumed to be already in
759 * the target buffer and only the header is filled.
762 static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid,
763 const void *data, u16 frame_type, u8 reply_count)
765 int total_size = sizeof(*req) + length;
767 BUG_ON(total_size > BULK_BUF_SIZE);
769 req->magic = cpu_to_le16(EZUSB_MAGIC);
770 req->req_reply_count = reply_count;
771 req->ans_reply_count = 0;
772 req->frame_type = cpu_to_le16(frame_type);
773 req->size = cpu_to_le16(length + 4);
774 req->crc = cpu_to_le16(build_crc(req));
775 req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length));
776 req->hermes_rid = cpu_to_le16(rid);
777 if (data)
778 memcpy(req->data, data, length);
779 return total_size;
782 static int ezusb_submit_in_urb(struct ezusb_priv *upriv)
784 int retval = 0;
785 void *cur_buf = upriv->read_urb->transfer_buffer;
787 if (upriv->read_urb->status == -EINPROGRESS) {
788 dbg("urb busy, not resubmiting");
789 retval = -EBUSY;
790 goto exit;
792 usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe,
793 cur_buf, BULK_BUF_SIZE,
794 ezusb_bulk_in_callback, upriv);
795 upriv->read_urb->transfer_flags = 0;
796 retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC);
797 if (retval)
798 err("%s submit failed %d", __FUNCTION__, retval);
800 exit:
801 return retval;
804 static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset)
806 u8 res_val = reset; /* avoid argument promotion */
808 if (!upriv->udev) {
809 err("%s: !upriv->udev", __FUNCTION__);
810 return -EFAULT;
812 return usb_control_msg(upriv->udev,
813 usb_sndctrlpipe(upriv->udev, 0),
814 EZUSB_REQUEST_FW_TRANS,
815 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
816 USB_DIR_OUT, EZUSB_CPUCS_REG, 0, &res_val,
817 sizeof(res_val), DEF_TIMEOUT);
820 static int ezusb_firmware_download(struct ezusb_priv *upriv,
821 struct ez_usb_fw *fw)
823 u8 fw_buffer[FW_BUF_SIZE];
824 int retval, addr;
825 int variant_offset;
828 * This byte is 1 and should be replaced with 0. The offset is
829 * 0x10AD in version 0.0.6. The byte in question should follow
830 * the end of the code pointed to by the jump in the beginning
831 * of the firmware. Also, it is read by code located at 0x358.
833 variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
834 if (variant_offset >= fw->size) {
835 printk(KERN_ERR PFX "Invalid firmware variant offset: "
836 "0x%04x\n", variant_offset);
837 retval = -EINVAL;
838 goto fail;
841 retval = ezusb_8051_cpucs(upriv, 1);
842 if (retval < 0)
843 goto fail;
844 for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
845 /* 0x100-0x300 should be left alone, it contains card
846 * specific data, like USB enumeration information */
847 if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
848 continue;
850 memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
851 if (variant_offset >= addr &&
852 variant_offset < addr + FW_BUF_SIZE) {
853 dbg("Patching card_variant byte at 0x%04X",
854 variant_offset);
855 fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
857 retval = usb_control_msg(upriv->udev,
858 usb_sndctrlpipe(upriv->udev, 0),
859 EZUSB_REQUEST_FW_TRANS,
860 USB_TYPE_VENDOR | USB_RECIP_DEVICE
861 | USB_DIR_OUT,
862 addr, 0x0,
863 fw_buffer, FW_BUF_SIZE,
864 DEF_TIMEOUT);
866 if (retval < 0)
867 goto fail;
869 retval = ezusb_8051_cpucs(upriv, 0);
870 if (retval < 0)
871 goto fail;
873 goto exit;
874 fail:
875 printk(KERN_ERR PFX "Firmware download failed, error %d\n",
876 retval);
877 exit:
878 return retval;
881 static int ezusb_access_ltv(struct ezusb_priv *upriv,
882 struct request_context *ctx,
883 u16 length, const void *data, u16 frame_type,
884 void *ans_buff, int ans_size, u16 *ans_length)
886 int req_size;
887 int retval = 0;
888 ezusb_state_t state;
890 BUG_ON(in_irq());
892 if (!upriv->udev) {
893 dbg("Device disconnected");
894 return -ENODEV;
897 if (upriv->read_urb->status != -EINPROGRESS) {
898 err("%s: in urb not pending", __FUNCTION__);
901 /* protect upriv->reply_count, guarantee sequential numbers */
902 spin_lock_bh(&upriv->reply_count_lock);
903 req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
904 frame_type, upriv->reply_count);
905 usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
906 ctx->buf, req_size,
907 ezusb_request_out_callback, ctx);
909 if (ctx->in_rid)
910 upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
912 ezusb_req_enqueue_run(upriv, ctx);
914 spin_unlock_bh(&upriv->reply_count_lock);
916 if (ctx->in_rid)
917 ezusb_req_ctx_wait(upriv, ctx);
919 state = ctx->state;
920 switch (state) {
921 case EZUSB_CTX_COMPLETE:
922 retval = ctx->outurb->status;
923 break;
925 case EZUSB_CTX_QUEUED:
926 case EZUSB_CTX_REQ_SUBMITTED:
927 if (!ctx->in_rid)
928 break;
929 default:
930 err("%s: Unexpected context state %d", __FUNCTION__,
931 state);
932 /* fall though */
933 case EZUSB_CTX_REQ_TIMEOUT:
934 case EZUSB_CTX_REQ_FAILED:
935 case EZUSB_CTX_RESP_TIMEOUT:
936 case EZUSB_CTX_REQSUBMIT_FAIL:
937 printk(KERN_ERR PFX "Access failed, resetting (state %d,"
938 " reply_count %d)\n", state, upriv->reply_count);
939 upriv->reply_count = 0;
940 if (state == EZUSB_CTX_REQ_TIMEOUT
941 || state == EZUSB_CTX_RESP_TIMEOUT) {
942 printk(KERN_ERR PFX "ctx timed out\n");
943 retval = -ETIMEDOUT;
944 } else {
945 printk(KERN_ERR PFX "ctx failed\n");
946 retval = -EFAULT;
948 goto exit;
949 break;
951 if (ctx->in_rid) {
952 struct ezusb_packet *ans = ctx->buf;
953 int exp_len;
955 if (ans->hermes_len != 0)
956 exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
957 else
958 exp_len = 14;
960 if (exp_len != ctx->buf_length) {
961 err("%s: lenght mismatch for RID 0x%04x: "
962 "expected %d, got %d", __FUNCTION__,
963 ctx->in_rid, exp_len, ctx->buf_length);
964 retval = -EIO;
965 goto exit;
968 if (ans_buff)
969 memcpy(ans_buff, ans->data,
970 min_t(int, exp_len, ans_size));
971 if (ans_length)
972 *ans_length = le16_to_cpu(ans->hermes_len);
974 exit:
975 ezusb_request_context_put(ctx);
976 return retval;
979 static int ezusb_write_ltv(hermes_t *hw, int bap, u16 rid,
980 u16 length, const void *data)
982 struct ezusb_priv *upriv = hw->priv;
983 u16 frame_type;
984 struct request_context *ctx;
986 if (length == 0)
987 return -EINVAL;
989 length = HERMES_RECLEN_TO_BYTES(length);
991 /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
992 * set to be empty, but the USB bridge doesn't like it */
993 if (length == 0)
994 return 0;
996 ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
997 if (!ctx)
998 return -ENOMEM;
1000 if (rid == EZUSB_RID_TX)
1001 frame_type = EZUSB_FRAME_DATA;
1002 else
1003 frame_type = EZUSB_FRAME_CONTROL;
1005 return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
1006 NULL, 0, NULL);
1009 static int ezusb_read_ltv(hermes_t *hw, int bap, u16 rid,
1010 unsigned bufsize, u16 *length, void *buf)
1012 struct ezusb_priv *upriv = hw->priv;
1013 struct request_context *ctx;
1015 if ((bufsize < 0) || (bufsize % 2))
1016 return -EINVAL;
1018 ctx = ezusb_alloc_ctx(upriv, rid, rid);
1019 if (!ctx)
1020 return -ENOMEM;
1022 return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
1023 buf, bufsize, length);
1026 static int ezusb_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
1027 struct hermes_response *resp)
1029 struct ezusb_priv *upriv = hw->priv;
1030 struct request_context *ctx;
1032 __le16 data[4] = {
1033 cpu_to_le16(cmd),
1034 cpu_to_le16(parm0),
1038 dbg("0x%04X, parm0 0x%04X", cmd, parm0);
1039 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
1040 if (!ctx)
1041 return -ENOMEM;
1043 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1044 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1047 static int ezusb_bap_pread(struct hermes *hw, int bap,
1048 void *buf, int len, u16 id, u16 offset)
1050 struct ezusb_priv *upriv = hw->priv;
1051 struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
1052 int actual_length = upriv->read_urb->actual_length;
1054 if (id == EZUSB_RID_RX) {
1055 if ((sizeof(*ans) + offset + len) > actual_length) {
1056 printk(KERN_ERR PFX "BAP read beyond buffer end "
1057 "in rx frame\n");
1058 return -EINVAL;
1060 memcpy(buf, ans->data + offset, len);
1061 return 0;
1064 if (EZUSB_IS_INFO(id)) {
1065 /* Include 4 bytes for length/type */
1066 if ((sizeof(*ans) + offset + len - 4) > actual_length) {
1067 printk(KERN_ERR PFX "BAP read beyond buffer end "
1068 "in info frame\n");
1069 return -EFAULT;
1071 memcpy(buf, ans->data + offset - 4, len);
1072 } else {
1073 printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
1074 return -EINVAL;
1077 return 0;
1080 static int ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
1082 struct orinoco_private *priv =
1083 (struct orinoco_private *) netdev_priv(dev);
1084 struct net_device_stats *stats = &priv->stats;
1085 struct ezusb_priv *upriv = priv->card;
1086 int err = 0;
1087 char *p;
1088 struct ethhdr *eh;
1089 int len, data_len, data_off;
1090 __le16 tx_control;
1091 unsigned long flags;
1092 struct request_context *ctx;
1093 u8 *buf;
1094 int tx_size;
1096 if (!netif_running(dev)) {
1097 printk(KERN_ERR "%s: Tx on stopped device!\n",
1098 dev->name);
1099 return 1;
1102 if (netif_queue_stopped(dev)) {
1103 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
1104 dev->name);
1105 return 1;
1108 if (orinoco_lock(priv, &flags) != 0) {
1109 printk(KERN_ERR
1110 "%s: orinoco_xmit() called while hw_unavailable\n",
1111 dev->name);
1112 return 1;
1115 if (!netif_carrier_ok(dev) ||
1116 (priv->iw_mode == IW_MODE_MONITOR)) {
1117 /* Oops, the firmware hasn't established a connection,
1118 silently drop the packet (this seems to be the
1119 safest approach). */
1120 stats->tx_errors++;
1121 orinoco_unlock(priv, &flags);
1122 dev_kfree_skb(skb);
1123 return 0;
1126 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
1127 if (!ctx) {
1128 err = -ENOMEM;
1129 goto fail;
1131 memset(ctx->buf, 0, BULK_BUF_SIZE);
1132 buf = ctx->buf->data;
1134 /* Length of the packet body */
1135 /* FIXME: what if the skb is smaller than this? */
1136 len = max_t(int, skb->len - ETH_HLEN, ETH_ZLEN - ETH_HLEN);
1138 eh = (struct ethhdr *) skb->data;
1140 tx_control = cpu_to_le16(0);
1141 memcpy(buf, &tx_control, sizeof(tx_control));
1142 buf += sizeof(tx_control);
1143 /* Encapsulate Ethernet-II frames */
1144 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
1145 struct header_struct *hdr = (void *) buf;
1146 buf += sizeof(*hdr);
1147 data_len = len;
1148 data_off = sizeof(tx_control) + sizeof(*hdr);
1149 p = skb->data + ETH_HLEN;
1151 /* 802.3 header */
1152 memcpy(hdr->dest, eh->h_dest, ETH_ALEN);
1153 memcpy(hdr->src, eh->h_source, ETH_ALEN);
1154 hdr->len = htons(data_len + ENCAPS_OVERHEAD);
1156 /* 802.2 header */
1157 memcpy(&hdr->dsap, &encaps_hdr, sizeof(encaps_hdr));
1159 hdr->ethertype = eh->h_proto;
1160 } else { /* IEEE 802.3 frame */
1161 data_len = len + ETH_HLEN;
1162 data_off = sizeof(tx_control);
1163 p = skb->data;
1166 memcpy(buf, p, data_len);
1167 buf += data_len;
1169 /* Finally, we actually initiate the send */
1170 netif_stop_queue(dev);
1172 /* The card may behave better if we send evenly sized usb transfers */
1173 tx_size = ALIGN(buf - ctx->buf->data, 2);
1175 err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
1176 EZUSB_FRAME_DATA, NULL, 0, NULL);
1178 if (err) {
1179 netif_start_queue(dev);
1180 if (net_ratelimit())
1181 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1182 dev->name, err);
1183 stats->tx_errors++;
1184 goto fail;
1187 dev->trans_start = jiffies;
1188 stats->tx_bytes += data_off + data_len;
1190 orinoco_unlock(priv, &flags);
1192 dev_kfree_skb(skb);
1194 return 0;
1195 fail:
1196 orinoco_unlock(priv, &flags);
1197 return err;
1200 static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
1202 *fid = EZUSB_RID_TX;
1203 return 0;
1207 static int ezusb_hard_reset(struct orinoco_private *priv)
1209 struct ezusb_priv *upriv = priv->card;
1210 int retval = ezusb_8051_cpucs(upriv, 1);
1212 if (retval < 0) {
1213 err("Failed to reset");
1214 return retval;
1217 retval = ezusb_8051_cpucs(upriv, 0);
1218 if (retval < 0) {
1219 err("Failed to unreset");
1220 return retval;
1223 dbg("sending control message");
1224 retval = usb_control_msg(upriv->udev,
1225 usb_sndctrlpipe(upriv->udev, 0),
1226 EZUSB_REQUEST_TRIGER,
1227 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1228 USB_DIR_OUT, 0x0, 0x0, NULL, 0,
1229 DEF_TIMEOUT);
1230 if (retval < 0) {
1231 err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
1232 return retval;
1234 #if 0
1235 dbg("Sending EZUSB_REQUEST_TRIG_AC");
1236 retval = usb_control_msg(upriv->udev,
1237 usb_sndctrlpipe(upriv->udev, 0),
1238 EZUSB_REQUEST_TRIG_AC,
1239 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1240 USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
1241 DEF_TIMEOUT);
1242 if (retval < 0) {
1243 err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
1244 return retval;
1246 #endif
1248 return 0;
1252 static int ezusb_init(hermes_t *hw)
1254 struct ezusb_priv *upriv = hw->priv;
1255 int retval;
1257 BUG_ON(in_interrupt());
1258 BUG_ON(!upriv);
1260 upriv->reply_count = 0;
1261 /* Write the MAGIC number on the simulated registers to keep
1262 * orinoco.c happy */
1263 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
1264 hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
1266 usb_kill_urb(upriv->read_urb);
1267 ezusb_submit_in_urb(upriv);
1269 retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
1270 HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
1271 if (retval < 0) {
1272 printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
1273 return retval;
1276 retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
1277 if (retval < 0) {
1278 printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
1279 return retval;
1282 return 0;
1285 static void ezusb_bulk_in_callback(struct urb *urb)
1287 struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
1288 struct ezusb_packet *ans = urb->transfer_buffer;
1289 u16 crc;
1290 u16 hermes_rid;
1292 if (upriv->udev == NULL) {
1293 dbg("disconnected");
1294 return;
1297 if (urb->status == -ETIMEDOUT) {
1298 /* When a device gets unplugged we get this every time
1299 * we resubmit, flooding the logs. Since we don't use
1300 * USB timeouts, it shouldn't happen any other time*/
1301 warn("%s: urb timed out, not resubmiting", __FUNCTION__);
1302 return;
1304 if (urb->status == -ECONNABORTED) {
1305 warn("%s: connection abort, resubmiting urb",
1306 __FUNCTION__);
1307 goto resubmit;
1309 if ((urb->status == -EILSEQ)
1310 || (urb->status == -ENOENT)
1311 || (urb->status == -ECONNRESET)) {
1312 dbg("status %d, not resubmiting", urb->status);
1313 return;
1315 if (urb->status)
1316 dbg("status: %d length: %d",
1317 urb->status, urb->actual_length);
1318 if (urb->actual_length < sizeof(*ans)) {
1319 err("%s: short read, ignoring", __FUNCTION__);
1320 goto resubmit;
1322 crc = build_crc(ans);
1323 if (le16_to_cpu(ans->crc) != crc) {
1324 err("CRC error, ignoring packet");
1325 goto resubmit;
1328 hermes_rid = le16_to_cpu(ans->hermes_rid);
1329 if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
1330 ezusb_request_in_callback(upriv, urb);
1331 } else if (upriv->dev) {
1332 struct net_device *dev = upriv->dev;
1333 struct orinoco_private *priv = netdev_priv(dev);
1334 hermes_t *hw = &priv->hw;
1336 if (hermes_rid == EZUSB_RID_RX) {
1337 __orinoco_ev_rx(dev, hw);
1338 } else {
1339 hermes_write_regn(hw, INFOFID,
1340 le16_to_cpu(ans->hermes_rid));
1341 __orinoco_ev_info(dev, hw);
1345 resubmit:
1346 if (upriv->udev)
1347 ezusb_submit_in_urb(upriv);
1350 static inline void ezusb_delete(struct ezusb_priv *upriv)
1352 struct net_device *dev;
1353 struct list_head *item;
1354 struct list_head *tmp_item;
1355 unsigned long flags;
1357 BUG_ON(in_interrupt());
1358 BUG_ON(!upriv);
1360 dev = upriv->dev;
1361 down(&upriv->sem);
1363 upriv->udev = NULL; /* No timer will be rearmed from here */
1365 usb_kill_urb(upriv->read_urb);
1367 restart_list:
1368 spin_lock_irqsave(&upriv->req_lock, flags);
1369 list_for_each_safe(item, tmp_item, &upriv->req_active) {
1370 struct request_context *ctx;
1372 ctx = list_entry(item, struct request_context, list);
1373 atomic_inc(&ctx->refcount);
1375 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
1376 if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
1377 spin_unlock_irqrestore(&upriv->req_lock, flags);
1378 wait_for_completion(&ctx->done);
1379 } else {
1380 spin_unlock_irqrestore(&upriv->req_lock, flags);
1382 del_timer_sync(&ctx->timer);
1383 /* FIXME: there is an slight chance for the irq handler to
1384 * be running */
1385 if (!list_empty(&ctx->list))
1386 ezusb_ctx_complete(ctx);
1388 ezusb_request_context_put(ctx);
1389 goto restart_list;
1391 spin_unlock_irqrestore(&upriv->req_lock, flags);
1393 list_for_each_safe(item, tmp_item, &upriv->req_pending)
1394 ezusb_ctx_complete(list_entry(item,
1395 struct request_context, list));
1397 if (upriv->read_urb->status == -EINPROGRESS) {
1398 printk(KERN_ERR PFX "Some URB in progress\n");
1400 up(&upriv->sem);
1402 kfree(upriv->read_urb->transfer_buffer);
1403 if (upriv->bap_buf != NULL)
1404 kfree(upriv->bap_buf);
1405 if (upriv->read_urb != NULL)
1406 usb_free_urb(upriv->read_urb);
1407 if (upriv->dev) {
1408 unregister_netdev(upriv->dev);
1409 free_orinocodev(upriv->dev);
1413 static const struct hermes_ops ezusb_ops = {
1414 .init = ezusb_init,
1415 .docmd_wait = ezusb_docmd_wait,
1416 .allocate = ezusb_allocate,
1417 .read_ltv = ezusb_read_ltv,
1418 .write_ltv = ezusb_write_ltv,
1419 .bap_pread = ezusb_bap_pread
1422 static int ezusb_probe(struct usb_interface *interface,
1423 const struct usb_device_id *id)
1425 struct usb_device *udev = interface_to_usbdev(interface);
1426 struct orinoco_private *priv;
1427 struct net_device *dev;
1428 hermes_t *hw;
1429 struct ezusb_priv *upriv = NULL;
1430 struct usb_interface_descriptor *iface_desc;
1431 struct usb_endpoint_descriptor *ep;
1432 const struct firmware *fw_entry;
1433 int retval = 0;
1434 int i;
1436 dev = alloc_orinocodev(sizeof(*upriv), ezusb_hard_reset);
1437 if (!dev) {
1438 err("Couldn't allocate dev");
1439 goto exit;
1442 priv = netdev_priv(dev);
1443 hw = &priv->hw;
1445 upriv = priv->card;
1447 init_MUTEX(&upriv->sem);
1448 spin_lock_init(&upriv->reply_count_lock);
1450 spin_lock_init(&upriv->req_lock);
1451 INIT_LIST_HEAD(&upriv->req_pending);
1452 INIT_LIST_HEAD(&upriv->req_active);
1454 upriv->udev = udev;
1456 hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake;
1457 hw->reg_spacing = HERMES_16BIT_REGSPACING;
1458 hw->priv = upriv;
1459 hw->ops = &ezusb_ops;
1460 dev->hard_start_xmit = ezusb_xmit;
1462 priv->irq_no_disable = 1;
1464 /* set up the endpoint information */
1465 /* check out the endpoints */
1467 iface_desc = &interface->altsetting[0].desc;
1468 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1469 ep = &interface->altsetting[0].endpoint[i].desc;
1471 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1472 == USB_DIR_IN) &&
1473 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1474 == USB_ENDPOINT_XFER_BULK)) {
1475 /* we found a bulk in endpoint */
1476 if (upriv->read_urb != NULL) {
1477 warn("Found a second bulk in ep, ignored");
1478 continue;
1481 upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
1482 if (!upriv->read_urb) {
1483 err("No free urbs available");
1484 goto error;
1486 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1487 warn("bulk in: wMaxPacketSize!= 64");
1488 if (ep->bEndpointAddress != (2 | USB_DIR_IN))
1489 warn("bulk in: bEndpointAddress: %d",
1490 ep->bEndpointAddress);
1491 upriv->read_pipe = usb_rcvbulkpipe(udev,
1492 ep->
1493 bEndpointAddress);
1494 upriv->read_urb->transfer_buffer =
1495 kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1496 if (!upriv->read_urb->transfer_buffer) {
1497 err("Couldn't allocate IN buffer");
1498 goto error;
1502 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1503 == USB_DIR_OUT) &&
1504 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1505 == USB_ENDPOINT_XFER_BULK)) {
1506 /* we found a bulk out endpoint */
1507 if (upriv->bap_buf != NULL) {
1508 warn("Found a second bulk out ep, ignored");
1509 continue;
1512 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1513 warn("bulk out: wMaxPacketSize != 64");
1514 if (ep->bEndpointAddress != 2)
1515 warn("bulk out: bEndpointAddress: %d",
1516 ep->bEndpointAddress);
1517 upriv->write_pipe = usb_sndbulkpipe(udev,
1518 ep->
1519 bEndpointAddress);
1520 upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1521 if (!upriv->bap_buf) {
1522 err("Couldn't allocate bulk_out_buffer");
1523 goto error;
1527 if (!upriv->bap_buf || !upriv->read_urb) {
1528 err("Didn't find the required bulk endpoints");
1529 goto error;
1532 if (request_firmware(&fw_entry, "orinoco_ezusb_fw",
1533 &interface->dev) == 0) {
1534 firmware.size = fw_entry->size;
1535 firmware.code = fw_entry->data;
1537 if (firmware.size && firmware.code) {
1538 ezusb_firmware_download(upriv, &firmware);
1539 } else {
1540 err("No firmware to download");
1541 goto error;
1544 if (ezusb_hard_reset(priv) < 0) {
1545 err("Cannot reset the device");
1546 goto error;
1549 /* If the firmware is already downloaded orinoco.c will call
1550 * ezusb_init but if the firmware is not already there, that will make
1551 * the kernel very unstable, so we try initializing here and quit in
1552 * case of error */
1553 if (ezusb_init(hw) < 0) {
1554 err("Couldn't initialize the device");
1555 err("Firmware may not be downloaded or may be wrong.");
1556 goto error;
1559 SET_NETDEV_DEV(dev, &interface->dev);
1560 upriv->dev = dev;
1561 if (register_netdev(dev) != 0) {
1562 upriv->dev = NULL;
1563 err("%s: register_netdev() failed", __FUNCTION__);
1564 goto error;
1566 goto exit;
1568 error:
1569 ezusb_delete(upriv);
1570 if (dev) {
1571 /* upriv->dev was 0, so ezusb_delete() didn't free it */
1572 free_orinocodev(dev);
1574 upriv = NULL;
1575 retval = -EFAULT;
1576 exit:
1577 if (fw_entry) {
1578 firmware.code = NULL;
1579 firmware.size = 0;
1580 release_firmware(fw_entry);
1582 usb_set_intfdata(interface, upriv);
1583 return retval;
1587 static void ezusb_disconnect(struct usb_interface *intf)
1589 struct ezusb_priv *upriv = usb_get_intfdata(intf);
1590 usb_set_intfdata(intf, NULL);
1591 ezusb_delete(upriv);
1592 printk(KERN_INFO PFX "Disconnected\n");
1596 /* usb specific object needed to register this driver with the usb subsystem */
1597 static struct usb_driver orinoco_driver = {
1598 .name = DRIVER_NAME,
1599 .probe = ezusb_probe,
1600 .disconnect = ezusb_disconnect,
1601 .id_table = ezusb_table,
1604 /* Can't be declared "const" or the whole __initdata section will
1605 * become const */
1606 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
1607 " (Manuel Estrada Sainz)";
1609 static int __init ezusb_module_init(void)
1611 int err;
1613 printk(KERN_DEBUG "%s\n", version);
1615 /* register this driver with the USB subsystem */
1616 err = usb_register(&orinoco_driver);
1617 if (err < 0) {
1618 printk(KERN_ERR PFX "usb_register failed, error %d\n",
1619 err);
1620 return err;
1623 return 0;
1626 static void __exit ezusb_module_exit(void)
1628 /* deregister this driver with the USB subsystem */
1629 usb_deregister(&orinoco_driver);
1633 module_init(ezusb_module_init);
1634 module_exit(ezusb_module_exit);
1636 MODULE_AUTHOR("Manuel Estrada Sainz");
1637 MODULE_DESCRIPTION
1638 ("Driver for Orinoco wireless LAN cards using EZUSB bridge");
1639 MODULE_LICENSE("Dual MPL/GPL");