orinoco_usb: implement fw download
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / orinoco / orinoco_usb.c
blobe22093359f3ea71cb9c74657f3b2b065b775946a
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 const 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 __func__ , ## 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");
123 MODULE_FIRMWARE("orinoco_ezusb_fw");
126 * Under some conditions, the card gets stuck and stops paying attention
127 * to the world (i.e. data communication stalls) until we do something to
128 * it. Sending an INQ_TALLIES command seems to be enough and should be
129 * harmless otherwise. This behaviour has been observed when using the
130 * driver on a systemimager client during installation. In the past a
131 * timer was used to send INQ_TALLIES commands when there was no other
132 * activity, but it was troublesome and was removed.
135 #define USB_COMPAQ_VENDOR_ID 0x049f /* Compaq Computer Corp. */
136 #define USB_COMPAQ_WL215_ID 0x001f /* Compaq WL215 USB Adapter */
137 #define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */
138 #define USB_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */
140 #define USB_MELCO_VENDOR_ID 0x0411
141 #define USB_BUFFALO_L11_ID 0x0006 /* BUFFALO WLI-USB-L11 */
142 #define USB_BUFFALO_L11G_WR_ID 0x000B /* BUFFALO WLI-USB-L11G-WR */
143 #define USB_BUFFALO_L11G_ID 0x000D /* BUFFALO WLI-USB-L11G */
145 #define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */
146 #define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB Client */
148 #define USB_AVAYA8_VENDOR_ID 0x0D98
149 #define USB_AVAYAE_VENDOR_ID 0x0D9E
150 #define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */
152 #define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */
153 #define USB_AGERE_MODEL0801_ID 0x1000 /* Wireless USB Card Model 0801 */
154 #define USB_AGERE_MODEL0802_ID 0x1001 /* Wireless USB Card Model 0802 */
155 #define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */
157 #define USB_ELSA_VENDOR_ID 0x05CC
158 #define USB_ELSA_AIRLANCER_ID 0x3100 /* ELSA AirLancer USB-11 */
160 #define USB_LEGEND_VENDOR_ID 0x0E7C
161 #define USB_LEGEND_JOYNET_ID 0x0300 /* Joynet WLAN USB Card */
163 #define USB_SAMSUNG_VENDOR_ID 0x04E8
164 #define USB_SAMSUNG_SEW2001U1_ID 0x5002 /* Samsung SEW-2001u Card */
165 #define USB_SAMSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */
166 #define USB_SAMSUNG_SEW2003U_ID 0x7011 /* Samsung SEW-2003U Card */
168 #define USB_IGATE_VENDOR_ID 0x0681
169 #define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */
171 #define USB_FUJITSU_VENDOR_ID 0x0BF8
172 #define USB_FUJITSU_E1100_ID 0x1002 /* connect2AIR WLAN E-1100 USB */
174 #define USB_2WIRE_VENDOR_ID 0x1630
175 #define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */
178 #define EZUSB_REQUEST_FW_TRANS 0xA0
179 #define EZUSB_REQUEST_TRIGER 0xAA
180 #define EZUSB_REQUEST_TRIG_AC 0xAC
181 #define EZUSB_CPUCS_REG 0x7F92
183 #define EZUSB_RID_TX 0x0700
184 #define EZUSB_RID_RX 0x0701
185 #define EZUSB_RID_INIT1 0x0702
186 #define EZUSB_RID_ACK 0x0710
187 #define EZUSB_RID_READ_PDA 0x0800
188 #define EZUSB_RID_PROG_INIT 0x0852
189 #define EZUSB_RID_PROG_SET_ADDR 0x0853
190 #define EZUSB_RID_PROG_BYTES 0x0854
191 #define EZUSB_RID_PROG_END 0x0855
192 #define EZUSB_RID_DOCMD 0x0860
194 /* Recognize info frames */
195 #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF))
197 #define EZUSB_MAGIC 0x0210
199 #define EZUSB_FRAME_DATA 1
200 #define EZUSB_FRAME_CONTROL 2
202 #define DEF_TIMEOUT (3*HZ)
204 #define BULK_BUF_SIZE 2048
206 #define MAX_DL_SIZE (BULK_BUF_SIZE - sizeof(struct ezusb_packet))
208 #define FW_BUF_SIZE 64
209 #define FW_VAR_OFFSET_PTR 0x359
210 #define FW_VAR_VALUE 0
211 #define FW_HOLE_START 0x100
212 #define FW_HOLE_END 0x300
214 struct ezusb_packet {
215 __le16 magic; /* 0x0210 */
216 u8 req_reply_count;
217 u8 ans_reply_count;
218 __le16 frame_type; /* 0x01 for data frames, 0x02 otherwise */
219 __le16 size; /* transport size */
220 __le16 crc; /* CRC up to here */
221 __le16 hermes_len;
222 __le16 hermes_rid;
223 u8 data[0];
224 } __attribute__ ((packed));
226 /* Table of devices that work or may work with this driver */
227 static struct usb_device_id ezusb_table[] = {
228 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID)},
229 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID)},
230 {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID)},
231 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11_ID)},
232 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_WR_ID)},
233 {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_ID)},
234 {USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID)},
235 {USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
236 {USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
237 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0801_ID)},
238 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0802_ID)},
239 {USB_DEVICE(USB_ELSA_VENDOR_ID, USB_ELSA_AIRLANCER_ID)},
240 {USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID)},
241 {USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U1_ID,
242 0, 0)},
243 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U2_ID)},
244 {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2003U_ID)},
245 {USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID)},
246 {USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID)},
247 {USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID)},
248 {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID)},
249 {} /* Terminating entry */
252 MODULE_DEVICE_TABLE(usb, ezusb_table);
254 /* Structure to hold all of our device specific stuff */
255 struct ezusb_priv {
256 struct usb_device *udev;
257 struct net_device *dev;
258 struct mutex mtx;
259 spinlock_t req_lock;
260 struct list_head req_pending;
261 struct list_head req_active;
262 spinlock_t reply_count_lock;
263 u16 hermes_reg_fake[0x40];
264 u8 *bap_buf;
265 struct urb *read_urb;
266 int read_pipe;
267 int write_pipe;
268 u8 reply_count;
271 enum ezusb_state {
272 EZUSB_CTX_START,
273 EZUSB_CTX_QUEUED,
274 EZUSB_CTX_REQ_SUBMITTED,
275 EZUSB_CTX_REQ_COMPLETE,
276 EZUSB_CTX_RESP_RECEIVED,
277 EZUSB_CTX_REQ_TIMEOUT,
278 EZUSB_CTX_REQ_FAILED,
279 EZUSB_CTX_RESP_TIMEOUT,
280 EZUSB_CTX_REQSUBMIT_FAIL,
281 EZUSB_CTX_COMPLETE,
284 struct request_context {
285 struct list_head list;
286 atomic_t refcount;
287 struct completion done; /* Signals that CTX is dead */
288 int killed;
289 struct urb *outurb; /* OUT for req pkt */
290 struct ezusb_priv *upriv;
291 struct ezusb_packet *buf;
292 int buf_length;
293 struct timer_list timer; /* Timeout handling */
294 enum ezusb_state state; /* Current state */
295 /* the RID that we will wait for */
296 u16 out_rid;
297 u16 in_rid;
301 /* Forward declarations */
302 static void ezusb_ctx_complete(struct request_context *ctx);
303 static void ezusb_req_queue_run(struct ezusb_priv *upriv);
304 static void ezusb_bulk_in_callback(struct urb *urb);
306 static inline u8 ezusb_reply_inc(u8 count)
308 if (count < 0x7F)
309 return count + 1;
310 else
311 return 1;
314 static void ezusb_request_context_put(struct request_context *ctx)
316 if (!atomic_dec_and_test(&ctx->refcount))
317 return;
319 WARN_ON(!ctx->done.done);
320 BUG_ON(ctx->outurb->status == -EINPROGRESS);
321 BUG_ON(timer_pending(&ctx->timer));
322 usb_free_urb(ctx->outurb);
323 kfree(ctx->buf);
324 kfree(ctx);
327 static inline void ezusb_mod_timer(struct ezusb_priv *upriv,
328 struct timer_list *timer,
329 unsigned long expire)
331 if (!upriv->udev)
332 return;
333 mod_timer(timer, expire);
336 static void ezusb_request_timerfn(u_long _ctx)
338 struct request_context *ctx = (void *) _ctx;
340 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
341 if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
342 ctx->state = EZUSB_CTX_REQ_TIMEOUT;
343 } else {
344 ctx->state = EZUSB_CTX_RESP_TIMEOUT;
345 dbg("couldn't unlink");
346 atomic_inc(&ctx->refcount);
347 ctx->killed = 1;
348 ezusb_ctx_complete(ctx);
349 ezusb_request_context_put(ctx);
353 static struct request_context *ezusb_alloc_ctx(struct ezusb_priv *upriv,
354 u16 out_rid, u16 in_rid)
356 struct request_context *ctx;
358 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
359 if (!ctx)
360 return NULL;
362 memset(ctx, 0, sizeof(*ctx));
364 ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC);
365 if (!ctx->buf) {
366 kfree(ctx);
367 return NULL;
369 ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC);
370 if (!ctx->outurb) {
371 kfree(ctx->buf);
372 kfree(ctx);
373 return NULL;
376 ctx->upriv = upriv;
377 ctx->state = EZUSB_CTX_START;
378 ctx->out_rid = out_rid;
379 ctx->in_rid = in_rid;
381 atomic_set(&ctx->refcount, 1);
382 init_completion(&ctx->done);
384 init_timer(&ctx->timer);
385 ctx->timer.function = ezusb_request_timerfn;
386 ctx->timer.data = (u_long) ctx;
387 return ctx;
391 /* Hopefully the real complete_all will soon be exported, in the mean
392 * while this should work. */
393 static inline void ezusb_complete_all(struct completion *comp)
395 complete(comp);
396 complete(comp);
397 complete(comp);
398 complete(comp);
401 static void ezusb_ctx_complete(struct request_context *ctx)
403 struct ezusb_priv *upriv = ctx->upriv;
404 unsigned long flags;
406 spin_lock_irqsave(&upriv->req_lock, flags);
408 list_del_init(&ctx->list);
409 if (upriv->udev) {
410 spin_unlock_irqrestore(&upriv->req_lock, flags);
411 ezusb_req_queue_run(upriv);
412 spin_lock_irqsave(&upriv->req_lock, flags);
415 switch (ctx->state) {
416 case EZUSB_CTX_COMPLETE:
417 case EZUSB_CTX_REQSUBMIT_FAIL:
418 case EZUSB_CTX_REQ_FAILED:
419 case EZUSB_CTX_REQ_TIMEOUT:
420 case EZUSB_CTX_RESP_TIMEOUT:
421 spin_unlock_irqrestore(&upriv->req_lock, flags);
423 if ((ctx->out_rid == EZUSB_RID_TX) && upriv->dev) {
424 struct net_device *dev = upriv->dev;
425 struct orinoco_private *priv = ndev_priv(dev);
426 struct net_device_stats *stats = &priv->stats;
428 if (ctx->state != EZUSB_CTX_COMPLETE)
429 stats->tx_errors++;
430 else
431 stats->tx_packets++;
433 netif_wake_queue(dev);
435 ezusb_complete_all(&ctx->done);
436 ezusb_request_context_put(ctx);
437 break;
439 default:
440 spin_unlock_irqrestore(&upriv->req_lock, flags);
441 if (!upriv->udev) {
442 /* This is normal, as all request contexts get flushed
443 * when the device is disconnected */
444 err("Called, CTX not terminating, but device gone");
445 ezusb_complete_all(&ctx->done);
446 ezusb_request_context_put(ctx);
447 break;
450 err("Called, CTX not in terminating state.");
451 /* Things are really bad if this happens. Just leak
452 * the CTX because it may still be linked to the
453 * queue or the OUT urb may still be active.
454 * Just leaking at least prevents an Oops or Panic.
456 break;
461 * ezusb_req_queue_run:
462 * Description:
463 * Note: Only one active CTX at any one time, because there's no
464 * other (reliable) way to match the response URB to the correct
465 * CTX.
467 static void ezusb_req_queue_run(struct ezusb_priv *upriv)
469 unsigned long flags;
470 struct request_context *ctx;
471 int result;
473 spin_lock_irqsave(&upriv->req_lock, flags);
475 if (!list_empty(&upriv->req_active))
476 goto unlock;
478 if (list_empty(&upriv->req_pending))
479 goto unlock;
481 ctx =
482 list_entry(upriv->req_pending.next, struct request_context,
483 list);
485 if (!ctx->upriv->udev)
486 goto unlock;
488 /* We need to split this off to avoid a race condition */
489 list_move_tail(&ctx->list, &upriv->req_active);
491 if (ctx->state == EZUSB_CTX_QUEUED) {
492 atomic_inc(&ctx->refcount);
493 result = usb_submit_urb(ctx->outurb, GFP_ATOMIC);
494 if (result) {
495 ctx->state = EZUSB_CTX_REQSUBMIT_FAIL;
497 spin_unlock_irqrestore(&upriv->req_lock, flags);
499 err("Fatal, failed to submit command urb."
500 " error=%d\n", result);
502 ezusb_ctx_complete(ctx);
503 ezusb_request_context_put(ctx);
504 goto done;
507 ctx->state = EZUSB_CTX_REQ_SUBMITTED;
508 ezusb_mod_timer(ctx->upriv, &ctx->timer,
509 jiffies + DEF_TIMEOUT);
512 unlock:
513 spin_unlock_irqrestore(&upriv->req_lock, flags);
515 done:
516 return;
519 static void ezusb_req_enqueue_run(struct ezusb_priv *upriv,
520 struct request_context *ctx)
522 unsigned long flags;
524 spin_lock_irqsave(&upriv->req_lock, flags);
526 if (!ctx->upriv->udev) {
527 spin_unlock_irqrestore(&upriv->req_lock, flags);
528 goto done;
530 atomic_inc(&ctx->refcount);
531 list_add_tail(&ctx->list, &upriv->req_pending);
532 spin_unlock_irqrestore(&upriv->req_lock, flags);
534 ctx->state = EZUSB_CTX_QUEUED;
535 ezusb_req_queue_run(upriv);
537 done:
538 return;
541 static void ezusb_request_out_callback(struct urb *urb)
543 unsigned long flags;
544 enum ezusb_state state;
545 struct request_context *ctx = urb->context;
546 struct ezusb_priv *upriv = ctx->upriv;
548 spin_lock_irqsave(&upriv->req_lock, flags);
550 del_timer(&ctx->timer);
552 if (ctx->killed) {
553 spin_unlock_irqrestore(&upriv->req_lock, flags);
554 pr_warning("interrupt called with dead ctx");
555 goto out;
558 state = ctx->state;
560 if (urb->status == 0) {
561 switch (state) {
562 case EZUSB_CTX_REQ_SUBMITTED:
563 if (ctx->in_rid) {
564 ctx->state = EZUSB_CTX_REQ_COMPLETE;
565 /* reply URB still pending */
566 ezusb_mod_timer(upriv, &ctx->timer,
567 jiffies + DEF_TIMEOUT);
568 spin_unlock_irqrestore(&upriv->req_lock,
569 flags);
570 break;
572 /* fall through */
573 case EZUSB_CTX_RESP_RECEIVED:
574 /* IN already received before this OUT-ACK */
575 ctx->state = EZUSB_CTX_COMPLETE;
576 spin_unlock_irqrestore(&upriv->req_lock, flags);
577 ezusb_ctx_complete(ctx);
578 break;
580 default:
581 spin_unlock_irqrestore(&upriv->req_lock, flags);
582 err("Unexpected state(0x%x, %d) in OUT URB",
583 state, urb->status);
584 break;
586 } else {
587 /* If someone cancels the OUT URB then its status
588 * should be either -ECONNRESET or -ENOENT.
590 switch (state) {
591 case EZUSB_CTX_REQ_SUBMITTED:
592 case EZUSB_CTX_RESP_RECEIVED:
593 ctx->state = EZUSB_CTX_REQ_FAILED;
594 /* fall through */
596 case EZUSB_CTX_REQ_FAILED:
597 case EZUSB_CTX_REQ_TIMEOUT:
598 spin_unlock_irqrestore(&upriv->req_lock, flags);
600 ezusb_ctx_complete(ctx);
601 break;
603 default:
604 spin_unlock_irqrestore(&upriv->req_lock, flags);
606 err("Unexpected state(0x%x, %d) in OUT URB",
607 state, urb->status);
608 break;
611 out:
612 ezusb_request_context_put(ctx);
615 static void ezusb_request_in_callback(struct ezusb_priv *upriv,
616 struct urb *urb)
618 struct ezusb_packet *ans = urb->transfer_buffer;
619 struct request_context *ctx = NULL;
620 enum ezusb_state state;
621 unsigned long flags;
623 /* Find the CTX on the active queue that requested this URB */
624 spin_lock_irqsave(&upriv->req_lock, flags);
625 if (upriv->udev) {
626 struct list_head *item;
628 list_for_each(item, &upriv->req_active) {
629 struct request_context *c;
630 int reply_count;
632 c = list_entry(item, struct request_context, list);
633 reply_count =
634 ezusb_reply_inc(c->buf->req_reply_count);
635 if ((ans->ans_reply_count == reply_count)
636 && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) {
637 ctx = c;
638 break;
640 dbg("Skipped (0x%x/0x%x) (%d/%d)",
641 le16_to_cpu(ans->hermes_rid),
642 c->in_rid, ans->ans_reply_count, reply_count);
646 if (ctx == NULL) {
647 spin_unlock_irqrestore(&upriv->req_lock, flags);
648 err("%s: got unexpected RID: 0x%04X", __func__,
649 le16_to_cpu(ans->hermes_rid));
650 ezusb_req_queue_run(upriv);
651 return;
654 /* The data we want is in the in buffer, exchange */
655 urb->transfer_buffer = ctx->buf;
656 ctx->buf = (void *) ans;
657 ctx->buf_length = urb->actual_length;
659 state = ctx->state;
660 switch (state) {
661 case EZUSB_CTX_REQ_SUBMITTED:
662 /* We have received our response URB before
663 * our request has been acknowledged. Do NOT
664 * destroy our CTX yet, because our OUT URB
665 * is still alive ...
667 ctx->state = EZUSB_CTX_RESP_RECEIVED;
668 spin_unlock_irqrestore(&upriv->req_lock, flags);
670 /* Let the machine continue running. */
671 break;
673 case EZUSB_CTX_REQ_COMPLETE:
674 /* This is the usual path: our request
675 * has already been acknowledged, and
676 * we have now received the reply.
678 ctx->state = EZUSB_CTX_COMPLETE;
680 /* Stop the intimer */
681 del_timer(&ctx->timer);
682 spin_unlock_irqrestore(&upriv->req_lock, flags);
684 /* Call the completion handler */
685 ezusb_ctx_complete(ctx);
686 break;
688 default:
689 spin_unlock_irqrestore(&upriv->req_lock, flags);
691 pr_warning("Matched IN URB, unexpected context state(0x%x)",
692 state);
693 /* Throw this CTX away and try submitting another */
694 del_timer(&ctx->timer);
695 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
696 usb_unlink_urb(ctx->outurb);
697 ezusb_req_queue_run(upriv);
698 break;
699 } /* switch */
703 static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
704 struct request_context *ctx)
706 switch (ctx->state) {
707 case EZUSB_CTX_QUEUED:
708 case EZUSB_CTX_REQ_SUBMITTED:
709 case EZUSB_CTX_REQ_COMPLETE:
710 case EZUSB_CTX_RESP_RECEIVED:
711 if (in_softirq()) {
712 /* If we get called from a timer, timeout timers don't
713 * get the chance to run themselves. So we make sure
714 * that we don't sleep for ever */
715 int msecs = DEF_TIMEOUT * (1000 / HZ);
716 while (!ctx->done.done && msecs--)
717 udelay(1000);
718 } else {
719 wait_event_interruptible(ctx->done.wait,
720 ctx->done.done);
722 break;
723 default:
724 /* Done or failed - nothing to wait for */
725 break;
729 static inline u16 build_crc(struct ezusb_packet *data)
731 u16 crc = 0;
732 u8 *bytes = (u8 *)data;
733 int i;
735 for (i = 0; i < 8; i++)
736 crc = (crc << 1) + bytes[i];
738 return crc;
742 * ezusb_fill_req:
744 * if data == NULL and length > 0 the data is assumed to be already in
745 * the target buffer and only the header is filled.
748 static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid,
749 const void *data, u16 frame_type, u8 reply_count)
751 int total_size = sizeof(*req) + length;
753 BUG_ON(total_size > BULK_BUF_SIZE);
755 req->magic = cpu_to_le16(EZUSB_MAGIC);
756 req->req_reply_count = reply_count;
757 req->ans_reply_count = 0;
758 req->frame_type = cpu_to_le16(frame_type);
759 req->size = cpu_to_le16(length + 4);
760 req->crc = cpu_to_le16(build_crc(req));
761 req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length));
762 req->hermes_rid = cpu_to_le16(rid);
763 if (data)
764 memcpy(req->data, data, length);
765 return total_size;
768 static int ezusb_submit_in_urb(struct ezusb_priv *upriv)
770 int retval = 0;
771 void *cur_buf = upriv->read_urb->transfer_buffer;
773 if (upriv->read_urb->status == -EINPROGRESS) {
774 dbg("urb busy, not resubmiting");
775 retval = -EBUSY;
776 goto exit;
778 usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe,
779 cur_buf, BULK_BUF_SIZE,
780 ezusb_bulk_in_callback, upriv);
781 upriv->read_urb->transfer_flags = 0;
782 retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC);
783 if (retval)
784 err("%s submit failed %d", __func__, retval);
786 exit:
787 return retval;
790 static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset)
792 u8 res_val = reset; /* avoid argument promotion */
794 if (!upriv->udev) {
795 err("%s: !upriv->udev", __func__);
796 return -EFAULT;
798 return usb_control_msg(upriv->udev,
799 usb_sndctrlpipe(upriv->udev, 0),
800 EZUSB_REQUEST_FW_TRANS,
801 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
802 USB_DIR_OUT, EZUSB_CPUCS_REG, 0, &res_val,
803 sizeof(res_val), DEF_TIMEOUT);
806 static int ezusb_firmware_download(struct ezusb_priv *upriv,
807 struct ez_usb_fw *fw)
809 u8 fw_buffer[FW_BUF_SIZE];
810 int retval, addr;
811 int variant_offset;
814 * This byte is 1 and should be replaced with 0. The offset is
815 * 0x10AD in version 0.0.6. The byte in question should follow
816 * the end of the code pointed to by the jump in the beginning
817 * of the firmware. Also, it is read by code located at 0x358.
819 variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
820 if (variant_offset >= fw->size) {
821 printk(KERN_ERR PFX "Invalid firmware variant offset: "
822 "0x%04x\n", variant_offset);
823 retval = -EINVAL;
824 goto fail;
827 retval = ezusb_8051_cpucs(upriv, 1);
828 if (retval < 0)
829 goto fail;
830 for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
831 /* 0x100-0x300 should be left alone, it contains card
832 * specific data, like USB enumeration information */
833 if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
834 continue;
836 memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
837 if (variant_offset >= addr &&
838 variant_offset < addr + FW_BUF_SIZE) {
839 dbg("Patching card_variant byte at 0x%04X",
840 variant_offset);
841 fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
843 retval = usb_control_msg(upriv->udev,
844 usb_sndctrlpipe(upriv->udev, 0),
845 EZUSB_REQUEST_FW_TRANS,
846 USB_TYPE_VENDOR | USB_RECIP_DEVICE
847 | USB_DIR_OUT,
848 addr, 0x0,
849 fw_buffer, FW_BUF_SIZE,
850 DEF_TIMEOUT);
852 if (retval < 0)
853 goto fail;
855 retval = ezusb_8051_cpucs(upriv, 0);
856 if (retval < 0)
857 goto fail;
859 goto exit;
860 fail:
861 printk(KERN_ERR PFX "Firmware download failed, error %d\n",
862 retval);
863 exit:
864 return retval;
867 static int ezusb_access_ltv(struct ezusb_priv *upriv,
868 struct request_context *ctx,
869 u16 length, const void *data, u16 frame_type,
870 void *ans_buff, int ans_size, u16 *ans_length)
872 int req_size;
873 int retval = 0;
874 enum ezusb_state state;
876 BUG_ON(in_irq());
878 if (!upriv->udev) {
879 dbg("Device disconnected");
880 return -ENODEV;
883 if (upriv->read_urb->status != -EINPROGRESS)
884 err("%s: in urb not pending", __func__);
886 /* protect upriv->reply_count, guarantee sequential numbers */
887 spin_lock_bh(&upriv->reply_count_lock);
888 req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
889 frame_type, upriv->reply_count);
890 usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
891 ctx->buf, req_size,
892 ezusb_request_out_callback, ctx);
894 if (ctx->in_rid)
895 upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
897 ezusb_req_enqueue_run(upriv, ctx);
899 spin_unlock_bh(&upriv->reply_count_lock);
901 if (ctx->in_rid)
902 ezusb_req_ctx_wait(upriv, ctx);
904 state = ctx->state;
905 switch (state) {
906 case EZUSB_CTX_COMPLETE:
907 retval = ctx->outurb->status;
908 break;
910 case EZUSB_CTX_QUEUED:
911 case EZUSB_CTX_REQ_SUBMITTED:
912 if (!ctx->in_rid)
913 break;
914 default:
915 err("%s: Unexpected context state %d", __func__,
916 state);
917 /* fall though */
918 case EZUSB_CTX_REQ_TIMEOUT:
919 case EZUSB_CTX_REQ_FAILED:
920 case EZUSB_CTX_RESP_TIMEOUT:
921 case EZUSB_CTX_REQSUBMIT_FAIL:
922 printk(KERN_ERR PFX "Access failed, resetting (state %d,"
923 " reply_count %d)\n", state, upriv->reply_count);
924 upriv->reply_count = 0;
925 if (state == EZUSB_CTX_REQ_TIMEOUT
926 || state == EZUSB_CTX_RESP_TIMEOUT) {
927 printk(KERN_ERR PFX "ctx timed out\n");
928 retval = -ETIMEDOUT;
929 } else {
930 printk(KERN_ERR PFX "ctx failed\n");
931 retval = -EFAULT;
933 goto exit;
934 break;
936 if (ctx->in_rid) {
937 struct ezusb_packet *ans = ctx->buf;
938 int exp_len;
940 if (ans->hermes_len != 0)
941 exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
942 else
943 exp_len = 14;
945 if (exp_len != ctx->buf_length) {
946 err("%s: length mismatch for RID 0x%04x: "
947 "expected %d, got %d", __func__,
948 ctx->in_rid, exp_len, ctx->buf_length);
949 retval = -EIO;
950 goto exit;
953 if (ans_buff)
954 memcpy(ans_buff, ans->data,
955 min_t(int, exp_len, ans_size));
956 if (ans_length)
957 *ans_length = le16_to_cpu(ans->hermes_len);
959 exit:
960 ezusb_request_context_put(ctx);
961 return retval;
964 static int ezusb_write_ltv(hermes_t *hw, int bap, u16 rid,
965 u16 length, const void *data)
967 struct ezusb_priv *upriv = hw->priv;
968 u16 frame_type;
969 struct request_context *ctx;
971 if (length == 0)
972 return -EINVAL;
974 length = HERMES_RECLEN_TO_BYTES(length);
976 /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
977 * set to be empty, but the USB bridge doesn't like it */
978 if (length == 0)
979 return 0;
981 ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
982 if (!ctx)
983 return -ENOMEM;
985 if (rid == EZUSB_RID_TX)
986 frame_type = EZUSB_FRAME_DATA;
987 else
988 frame_type = EZUSB_FRAME_CONTROL;
990 return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
991 NULL, 0, NULL);
994 static int ezusb_read_ltv(hermes_t *hw, int bap, u16 rid,
995 unsigned bufsize, u16 *length, void *buf)
997 struct ezusb_priv *upriv = hw->priv;
998 struct request_context *ctx;
1000 if ((bufsize < 0) || (bufsize % 2))
1001 return -EINVAL;
1003 ctx = ezusb_alloc_ctx(upriv, rid, rid);
1004 if (!ctx)
1005 return -ENOMEM;
1007 return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
1008 buf, bufsize, length);
1011 static int ezusb_doicmd_wait(hermes_t *hw, u16 cmd, u16 parm0, u16 parm1,
1012 u16 parm2, struct hermes_response *resp)
1014 struct ezusb_priv *upriv = hw->priv;
1015 struct request_context *ctx;
1017 __le16 data[4] = {
1018 cpu_to_le16(cmd),
1019 cpu_to_le16(parm0),
1020 cpu_to_le16(parm1),
1021 cpu_to_le16(parm2),
1023 dbg("0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X",
1024 cmd, parm0, parm1, parm2);
1025 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
1026 if (!ctx)
1027 return -ENOMEM;
1029 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1030 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1033 static int ezusb_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
1034 struct hermes_response *resp)
1036 struct ezusb_priv *upriv = hw->priv;
1037 struct request_context *ctx;
1039 __le16 data[4] = {
1040 cpu_to_le16(cmd),
1041 cpu_to_le16(parm0),
1045 dbg("0x%04X, parm0 0x%04X", cmd, parm0);
1046 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
1047 if (!ctx)
1048 return -ENOMEM;
1050 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1051 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1054 static int ezusb_bap_pread(struct hermes *hw, int bap,
1055 void *buf, int len, u16 id, u16 offset)
1057 struct ezusb_priv *upriv = hw->priv;
1058 struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
1059 int actual_length = upriv->read_urb->actual_length;
1061 if (id == EZUSB_RID_RX) {
1062 if ((sizeof(*ans) + offset + len) > actual_length) {
1063 printk(KERN_ERR PFX "BAP read beyond buffer end "
1064 "in rx frame\n");
1065 return -EINVAL;
1067 memcpy(buf, ans->data + offset, len);
1068 return 0;
1071 if (EZUSB_IS_INFO(id)) {
1072 /* Include 4 bytes for length/type */
1073 if ((sizeof(*ans) + offset + len - 4) > actual_length) {
1074 printk(KERN_ERR PFX "BAP read beyond buffer end "
1075 "in info frame\n");
1076 return -EFAULT;
1078 memcpy(buf, ans->data + offset - 4, len);
1079 } else {
1080 printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
1081 return -EINVAL;
1084 return 0;
1087 static int ezusb_read_pda(struct hermes *hw, __le16 *pda,
1088 u32 pda_addr, u16 pda_len)
1090 struct ezusb_priv *upriv = hw->priv;
1091 struct request_context *ctx;
1092 __le16 data[] = {
1093 cpu_to_le16(pda_addr & 0xffff),
1094 cpu_to_le16(pda_len - 4)
1096 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_READ_PDA, EZUSB_RID_READ_PDA);
1097 if (!ctx)
1098 return -ENOMEM;
1100 /* wl_lkm does not include PDA size in the PDA area.
1101 * We will pad the information into pda, so other routines
1102 * don't have to be modified */
1103 pda[0] = cpu_to_le16(pda_len - 2);
1104 /* Includes CFG_PROD_DATA but not itself */
1105 pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
1107 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1108 EZUSB_FRAME_CONTROL, &pda[2], pda_len - 4,
1109 NULL);
1112 static int ezusb_program_init(struct hermes *hw, u32 entry_point)
1114 struct ezusb_priv *upriv = hw->priv;
1115 struct request_context *ctx;
1116 __le32 data = cpu_to_le32(entry_point);
1118 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_INIT, EZUSB_RID_ACK);
1119 if (!ctx)
1120 return -ENOMEM;
1122 return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1123 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1126 static int ezusb_program_end(struct hermes *hw)
1128 struct ezusb_priv *upriv = hw->priv;
1129 struct request_context *ctx;
1131 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_END, EZUSB_RID_ACK);
1132 if (!ctx)
1133 return -ENOMEM;
1135 return ezusb_access_ltv(upriv, ctx, 0, NULL,
1136 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1139 static int ezusb_program_bytes(struct hermes *hw, const char *buf,
1140 u32 addr, u32 len)
1142 struct ezusb_priv *upriv = hw->priv;
1143 struct request_context *ctx;
1144 __le32 data = cpu_to_le32(addr);
1145 int err;
1147 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_SET_ADDR, EZUSB_RID_ACK);
1148 if (!ctx)
1149 return -ENOMEM;
1151 err = ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
1152 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1153 if (err)
1154 return err;
1156 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_BYTES, EZUSB_RID_ACK);
1157 if (!ctx)
1158 return -ENOMEM;
1160 return ezusb_access_ltv(upriv, ctx, len, buf,
1161 EZUSB_FRAME_CONTROL, NULL, 0, NULL);
1164 static int ezusb_program(struct hermes *hw, const char *buf,
1165 u32 addr, u32 len)
1167 u32 ch_addr;
1168 u32 ch_len;
1169 int err = 0;
1171 /* We can only send 2048 bytes out of the bulk xmit at a time,
1172 * so we have to split any programming into chunks of <2048
1173 * bytes. */
1175 ch_len = (len < MAX_DL_SIZE) ? len : MAX_DL_SIZE;
1176 ch_addr = addr;
1178 while (ch_addr < (addr + len)) {
1179 pr_debug("Programming subblock of length %d "
1180 "to address 0x%08x. Data @ %p\n",
1181 ch_len, ch_addr, &buf[ch_addr - addr]);
1183 err = ezusb_program_bytes(hw, &buf[ch_addr - addr],
1184 ch_addr, ch_len);
1185 if (err)
1186 break;
1188 ch_addr += ch_len;
1189 ch_len = ((addr + len - ch_addr) < MAX_DL_SIZE) ?
1190 (addr + len - ch_addr) : MAX_DL_SIZE;
1193 return err;
1196 static netdev_tx_t ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
1198 struct orinoco_private *priv = ndev_priv(dev);
1199 struct net_device_stats *stats = &priv->stats;
1200 struct ezusb_priv *upriv = priv->card;
1201 int err = 0;
1202 char *p;
1203 struct ethhdr *eh;
1204 int len, data_len, data_off;
1205 __le16 tx_control;
1206 unsigned long flags;
1207 struct request_context *ctx;
1208 u8 *buf;
1209 int tx_size;
1211 if (!netif_running(dev)) {
1212 printk(KERN_ERR "%s: Tx on stopped device!\n",
1213 dev->name);
1214 return NETDEV_TX_BUSY;
1217 if (netif_queue_stopped(dev)) {
1218 printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
1219 dev->name);
1220 return NETDEV_TX_BUSY;
1223 if (orinoco_lock(priv, &flags) != 0) {
1224 printk(KERN_ERR
1225 "%s: orinoco_xmit() called while hw_unavailable\n",
1226 dev->name);
1227 return NETDEV_TX_BUSY;
1230 if (!netif_carrier_ok(dev) ||
1231 (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
1232 /* Oops, the firmware hasn't established a connection,
1233 silently drop the packet (this seems to be the
1234 safest approach). */
1235 stats->tx_errors++;
1236 orinoco_unlock(priv, &flags);
1237 dev_kfree_skb(skb);
1238 return NETDEV_TX_OK;
1241 ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
1242 if (!ctx)
1243 goto fail;
1245 memset(ctx->buf, 0, BULK_BUF_SIZE);
1246 buf = ctx->buf->data;
1248 /* Length of the packet body */
1249 /* FIXME: what if the skb is smaller than this? */
1250 len = max_t(int, skb->len - ETH_HLEN, ETH_ZLEN - ETH_HLEN);
1252 eh = (struct ethhdr *) skb->data;
1254 tx_control = cpu_to_le16(0);
1255 memcpy(buf, &tx_control, sizeof(tx_control));
1256 buf += sizeof(tx_control);
1257 /* Encapsulate Ethernet-II frames */
1258 if (ntohs(eh->h_proto) > ETH_DATA_LEN) { /* Ethernet-II frame */
1259 struct header_struct *hdr = (void *) buf;
1260 buf += sizeof(*hdr);
1261 data_len = len;
1262 data_off = sizeof(tx_control) + sizeof(*hdr);
1263 p = skb->data + ETH_HLEN;
1265 /* 802.3 header */
1266 memcpy(hdr->dest, eh->h_dest, ETH_ALEN);
1267 memcpy(hdr->src, eh->h_source, ETH_ALEN);
1268 hdr->len = htons(data_len + ENCAPS_OVERHEAD);
1270 /* 802.2 header */
1271 memcpy(&hdr->dsap, &encaps_hdr, sizeof(encaps_hdr));
1273 hdr->ethertype = eh->h_proto;
1274 } else { /* IEEE 802.3 frame */
1275 data_len = len + ETH_HLEN;
1276 data_off = sizeof(tx_control);
1277 p = skb->data;
1280 memcpy(buf, p, data_len);
1281 buf += data_len;
1283 /* Finally, we actually initiate the send */
1284 netif_stop_queue(dev);
1286 /* The card may behave better if we send evenly sized usb transfers */
1287 tx_size = ALIGN(buf - ctx->buf->data, 2);
1289 err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
1290 EZUSB_FRAME_DATA, NULL, 0, NULL);
1292 if (err) {
1293 netif_start_queue(dev);
1294 if (net_ratelimit())
1295 printk(KERN_ERR "%s: Error %d transmitting packet\n",
1296 dev->name, err);
1297 stats->tx_errors++;
1298 goto fail;
1301 dev->trans_start = jiffies;
1302 stats->tx_bytes += data_off + data_len;
1304 orinoco_unlock(priv, &flags);
1306 dev_kfree_skb(skb);
1308 return NETDEV_TX_OK;
1310 fail:
1311 orinoco_unlock(priv, &flags);
1312 return NETDEV_TX_BUSY;
1315 static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
1317 *fid = EZUSB_RID_TX;
1318 return 0;
1322 static int ezusb_hard_reset(struct orinoco_private *priv)
1324 struct ezusb_priv *upriv = priv->card;
1325 int retval = ezusb_8051_cpucs(upriv, 1);
1327 if (retval < 0) {
1328 err("Failed to reset");
1329 return retval;
1332 retval = ezusb_8051_cpucs(upriv, 0);
1333 if (retval < 0) {
1334 err("Failed to unreset");
1335 return retval;
1338 dbg("sending control message");
1339 retval = usb_control_msg(upriv->udev,
1340 usb_sndctrlpipe(upriv->udev, 0),
1341 EZUSB_REQUEST_TRIGER,
1342 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1343 USB_DIR_OUT, 0x0, 0x0, NULL, 0,
1344 DEF_TIMEOUT);
1345 if (retval < 0) {
1346 err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
1347 return retval;
1349 #if 0
1350 dbg("Sending EZUSB_REQUEST_TRIG_AC");
1351 retval = usb_control_msg(upriv->udev,
1352 usb_sndctrlpipe(upriv->udev, 0),
1353 EZUSB_REQUEST_TRIG_AC,
1354 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1355 USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
1356 DEF_TIMEOUT);
1357 if (retval < 0) {
1358 err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
1359 return retval;
1361 #endif
1363 return 0;
1367 static int ezusb_init(hermes_t *hw)
1369 struct ezusb_priv *upriv = hw->priv;
1370 int retval;
1372 BUG_ON(in_interrupt());
1373 BUG_ON(!upriv);
1375 upriv->reply_count = 0;
1376 /* Write the MAGIC number on the simulated registers to keep
1377 * orinoco.c happy */
1378 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
1379 hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
1381 usb_kill_urb(upriv->read_urb);
1382 ezusb_submit_in_urb(upriv);
1384 retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
1385 HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
1386 if (retval < 0) {
1387 printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
1388 return retval;
1391 retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
1392 if (retval < 0) {
1393 printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
1394 return retval;
1397 return 0;
1400 static void ezusb_bulk_in_callback(struct urb *urb)
1402 struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
1403 struct ezusb_packet *ans = urb->transfer_buffer;
1404 u16 crc;
1405 u16 hermes_rid;
1407 if (upriv->udev == NULL) {
1408 dbg("disconnected");
1409 return;
1412 if (urb->status == -ETIMEDOUT) {
1413 /* When a device gets unplugged we get this every time
1414 * we resubmit, flooding the logs. Since we don't use
1415 * USB timeouts, it shouldn't happen any other time*/
1416 pr_warning("%s: urb timed out, not resubmiting", __func__);
1417 return;
1419 if (urb->status == -ECONNABORTED) {
1420 pr_warning("%s: connection abort, resubmiting urb",
1421 __func__);
1422 goto resubmit;
1424 if ((urb->status == -EILSEQ)
1425 || (urb->status == -ENOENT)
1426 || (urb->status == -ECONNRESET)) {
1427 dbg("status %d, not resubmiting", urb->status);
1428 return;
1430 if (urb->status)
1431 dbg("status: %d length: %d",
1432 urb->status, urb->actual_length);
1433 if (urb->actual_length < sizeof(*ans)) {
1434 err("%s: short read, ignoring", __func__);
1435 goto resubmit;
1437 crc = build_crc(ans);
1438 if (le16_to_cpu(ans->crc) != crc) {
1439 err("CRC error, ignoring packet");
1440 goto resubmit;
1443 hermes_rid = le16_to_cpu(ans->hermes_rid);
1444 if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
1445 ezusb_request_in_callback(upriv, urb);
1446 } else if (upriv->dev) {
1447 struct net_device *dev = upriv->dev;
1448 struct orinoco_private *priv = ndev_priv(dev);
1449 hermes_t *hw = &priv->hw;
1451 if (hermes_rid == EZUSB_RID_RX) {
1452 __orinoco_ev_rx(dev, hw);
1453 } else {
1454 hermes_write_regn(hw, INFOFID,
1455 le16_to_cpu(ans->hermes_rid));
1456 __orinoco_ev_info(dev, hw);
1460 resubmit:
1461 if (upriv->udev)
1462 ezusb_submit_in_urb(upriv);
1465 static inline void ezusb_delete(struct ezusb_priv *upriv)
1467 struct net_device *dev;
1468 struct list_head *item;
1469 struct list_head *tmp_item;
1470 unsigned long flags;
1472 BUG_ON(in_interrupt());
1473 BUG_ON(!upriv);
1475 dev = upriv->dev;
1476 mutex_lock(&upriv->mtx);
1478 upriv->udev = NULL; /* No timer will be rearmed from here */
1480 usb_kill_urb(upriv->read_urb);
1482 spin_lock_irqsave(&upriv->req_lock, flags);
1483 list_for_each_safe(item, tmp_item, &upriv->req_active) {
1484 struct request_context *ctx;
1485 int err;
1487 ctx = list_entry(item, struct request_context, list);
1488 atomic_inc(&ctx->refcount);
1490 ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
1491 err = usb_unlink_urb(ctx->outurb);
1493 spin_unlock_irqrestore(&upriv->req_lock, flags);
1494 if (err == -EINPROGRESS)
1495 wait_for_completion(&ctx->done);
1497 del_timer_sync(&ctx->timer);
1498 /* FIXME: there is an slight chance for the irq handler to
1499 * be running */
1500 if (!list_empty(&ctx->list))
1501 ezusb_ctx_complete(ctx);
1503 ezusb_request_context_put(ctx);
1504 spin_lock_irqsave(&upriv->req_lock, flags);
1506 spin_unlock_irqrestore(&upriv->req_lock, flags);
1508 list_for_each_safe(item, tmp_item, &upriv->req_pending)
1509 ezusb_ctx_complete(list_entry(item,
1510 struct request_context, list));
1512 if (upriv->read_urb->status == -EINPROGRESS)
1513 printk(KERN_ERR PFX "Some URB in progress\n");
1515 mutex_unlock(&upriv->mtx);
1517 kfree(upriv->read_urb->transfer_buffer);
1518 if (upriv->bap_buf != NULL)
1519 kfree(upriv->bap_buf);
1520 if (upriv->read_urb != NULL)
1521 usb_free_urb(upriv->read_urb);
1522 if (upriv->dev) {
1523 struct orinoco_private *priv = ndev_priv(upriv->dev);
1524 orinoco_if_del(priv);
1525 free_orinocodev(priv);
1529 static void ezusb_lock_irqsave(spinlock_t *lock,
1530 unsigned long *flags) __acquires(lock)
1532 spin_lock_bh(lock);
1535 static void ezusb_unlock_irqrestore(spinlock_t *lock,
1536 unsigned long *flags) __releases(lock)
1538 spin_unlock_bh(lock);
1541 static void ezusb_lock_irq(spinlock_t *lock) __acquires(lock)
1543 spin_lock_bh(lock);
1546 static void ezusb_unlock_irq(spinlock_t *lock) __releases(lock)
1548 spin_unlock_bh(lock);
1551 static const struct hermes_ops ezusb_ops = {
1552 .init = ezusb_init,
1553 .cmd_wait = ezusb_docmd_wait,
1554 .init_cmd_wait = ezusb_doicmd_wait,
1555 .allocate = ezusb_allocate,
1556 .read_ltv = ezusb_read_ltv,
1557 .write_ltv = ezusb_write_ltv,
1558 .bap_pread = ezusb_bap_pread,
1559 .read_pda = ezusb_read_pda,
1560 .program_init = ezusb_program_init,
1561 .program_end = ezusb_program_end,
1562 .program = ezusb_program,
1563 .lock_irqsave = ezusb_lock_irqsave,
1564 .unlock_irqrestore = ezusb_unlock_irqrestore,
1565 .lock_irq = ezusb_lock_irq,
1566 .unlock_irq = ezusb_unlock_irq,
1569 static const struct net_device_ops ezusb_netdev_ops = {
1570 .ndo_open = orinoco_open,
1571 .ndo_stop = orinoco_stop,
1572 .ndo_start_xmit = ezusb_xmit,
1573 .ndo_set_multicast_list = orinoco_set_multicast_list,
1574 .ndo_change_mtu = orinoco_change_mtu,
1575 .ndo_set_mac_address = eth_mac_addr,
1576 .ndo_validate_addr = eth_validate_addr,
1577 .ndo_tx_timeout = orinoco_tx_timeout,
1578 .ndo_get_stats = orinoco_get_stats,
1581 static int ezusb_probe(struct usb_interface *interface,
1582 const struct usb_device_id *id)
1584 struct usb_device *udev = interface_to_usbdev(interface);
1585 struct orinoco_private *priv;
1586 hermes_t *hw;
1587 struct ezusb_priv *upriv = NULL;
1588 struct usb_interface_descriptor *iface_desc;
1589 struct usb_endpoint_descriptor *ep;
1590 const struct firmware *fw_entry;
1591 int retval = 0;
1592 int i;
1594 priv = alloc_orinocodev(sizeof(*upriv), &udev->dev,
1595 ezusb_hard_reset, NULL);
1596 if (!priv) {
1597 err("Couldn't allocate orinocodev");
1598 goto exit;
1601 hw = &priv->hw;
1603 upriv = priv->card;
1605 mutex_init(&upriv->mtx);
1606 spin_lock_init(&upriv->reply_count_lock);
1608 spin_lock_init(&upriv->req_lock);
1609 INIT_LIST_HEAD(&upriv->req_pending);
1610 INIT_LIST_HEAD(&upriv->req_active);
1612 upriv->udev = udev;
1614 hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake;
1615 hw->reg_spacing = HERMES_16BIT_REGSPACING;
1616 hw->priv = upriv;
1617 hw->ops = &ezusb_ops;
1619 /* set up the endpoint information */
1620 /* check out the endpoints */
1622 iface_desc = &interface->altsetting[0].desc;
1623 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1624 ep = &interface->altsetting[0].endpoint[i].desc;
1626 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1627 == USB_DIR_IN) &&
1628 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1629 == USB_ENDPOINT_XFER_BULK)) {
1630 /* we found a bulk in endpoint */
1631 if (upriv->read_urb != NULL) {
1632 pr_warning("Found a second bulk in ep, ignored");
1633 continue;
1636 upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
1637 if (!upriv->read_urb) {
1638 err("No free urbs available");
1639 goto error;
1641 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1642 pr_warning("bulk in: wMaxPacketSize!= 64");
1643 if (ep->bEndpointAddress != (2 | USB_DIR_IN))
1644 pr_warning("bulk in: bEndpointAddress: %d",
1645 ep->bEndpointAddress);
1646 upriv->read_pipe = usb_rcvbulkpipe(udev,
1647 ep->
1648 bEndpointAddress);
1649 upriv->read_urb->transfer_buffer =
1650 kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1651 if (!upriv->read_urb->transfer_buffer) {
1652 err("Couldn't allocate IN buffer");
1653 goto error;
1657 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1658 == USB_DIR_OUT) &&
1659 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1660 == USB_ENDPOINT_XFER_BULK)) {
1661 /* we found a bulk out endpoint */
1662 if (upriv->bap_buf != NULL) {
1663 pr_warning("Found a second bulk out ep, ignored");
1664 continue;
1667 if (le16_to_cpu(ep->wMaxPacketSize) != 64)
1668 pr_warning("bulk out: wMaxPacketSize != 64");
1669 if (ep->bEndpointAddress != 2)
1670 pr_warning("bulk out: bEndpointAddress: %d",
1671 ep->bEndpointAddress);
1672 upriv->write_pipe = usb_sndbulkpipe(udev,
1673 ep->
1674 bEndpointAddress);
1675 upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
1676 if (!upriv->bap_buf) {
1677 err("Couldn't allocate bulk_out_buffer");
1678 goto error;
1682 if (!upriv->bap_buf || !upriv->read_urb) {
1683 err("Didn't find the required bulk endpoints");
1684 goto error;
1687 if (request_firmware(&fw_entry, "orinoco_ezusb_fw",
1688 &interface->dev) == 0) {
1689 firmware.size = fw_entry->size;
1690 firmware.code = fw_entry->data;
1692 if (firmware.size && firmware.code) {
1693 ezusb_firmware_download(upriv, &firmware);
1694 } else {
1695 err("No firmware to download");
1696 goto error;
1699 if (ezusb_hard_reset(priv) < 0) {
1700 err("Cannot reset the device");
1701 goto error;
1704 /* If the firmware is already downloaded orinoco.c will call
1705 * ezusb_init but if the firmware is not already there, that will make
1706 * the kernel very unstable, so we try initializing here and quit in
1707 * case of error */
1708 if (ezusb_init(hw) < 0) {
1709 err("Couldn't initialize the device");
1710 err("Firmware may not be downloaded or may be wrong.");
1711 goto error;
1714 /* Initialise the main driver */
1715 if (orinoco_init(priv) != 0) {
1716 err("orinoco_init() failed\n");
1717 goto error;
1720 if (orinoco_if_add(priv, 0, 0, &ezusb_netdev_ops) != 0) {
1721 upriv->dev = NULL;
1722 err("%s: orinoco_if_add() failed", __func__);
1723 goto error;
1725 upriv->dev = priv->ndev;
1727 goto exit;
1729 error:
1730 ezusb_delete(upriv);
1731 if (upriv->dev) {
1732 /* upriv->dev was 0, so ezusb_delete() didn't free it */
1733 free_orinocodev(priv);
1735 upriv = NULL;
1736 retval = -EFAULT;
1737 exit:
1738 if (fw_entry) {
1739 firmware.code = NULL;
1740 firmware.size = 0;
1741 release_firmware(fw_entry);
1743 usb_set_intfdata(interface, upriv);
1744 return retval;
1748 static void ezusb_disconnect(struct usb_interface *intf)
1750 struct ezusb_priv *upriv = usb_get_intfdata(intf);
1751 usb_set_intfdata(intf, NULL);
1752 ezusb_delete(upriv);
1753 printk(KERN_INFO PFX "Disconnected\n");
1757 /* usb specific object needed to register this driver with the usb subsystem */
1758 static struct usb_driver orinoco_driver = {
1759 .name = DRIVER_NAME,
1760 .probe = ezusb_probe,
1761 .disconnect = ezusb_disconnect,
1762 .id_table = ezusb_table,
1765 /* Can't be declared "const" or the whole __initdata section will
1766 * become const */
1767 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
1768 " (Manuel Estrada Sainz)";
1770 static int __init ezusb_module_init(void)
1772 int err;
1774 printk(KERN_DEBUG "%s\n", version);
1776 /* register this driver with the USB subsystem */
1777 err = usb_register(&orinoco_driver);
1778 if (err < 0) {
1779 printk(KERN_ERR PFX "usb_register failed, error %d\n",
1780 err);
1781 return err;
1784 return 0;
1787 static void __exit ezusb_module_exit(void)
1789 /* deregister this driver with the USB subsystem */
1790 usb_deregister(&orinoco_driver);
1794 module_init(ezusb_module_init);
1795 module_exit(ezusb_module_exit);
1797 MODULE_AUTHOR("Manuel Estrada Sainz");
1798 MODULE_DESCRIPTION
1799 ("Driver for Orinoco wireless LAN cards using EZUSB bridge");
1800 MODULE_LICENSE("Dual MPL/GPL");