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