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