1 |
/* |
2 |
* USB Orinoco driver |
3 |
* |
4 |
* Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org> |
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 |
#include <linux/config.h> |
47 |
#include <linux/module.h> |
48 |
#include <linux/kernel.h> |
49 |
#include <linux/sched.h> |
50 |
#include <linux/signal.h> |
51 |
#include <linux/errno.h> |
52 |
#include <linux/poll.h> |
53 |
#include <linux/init.h> |
54 |
#include <linux/slab.h> |
55 |
#include <linux/fcntl.h> |
56 |
#include <linux/module.h> |
57 |
#include <linux/spinlock.h> |
58 |
#include <linux/list.h> |
59 |
#include <linux/smp_lock.h> |
60 |
#include <linux/usb.h> |
61 |
#include <linux/timer.h> |
62 |
#include <asm/io.h> |
63 |
|
64 |
#include <linux/netdevice.h> |
65 |
#include <linux/if_arp.h> |
66 |
#include <linux/etherdevice.h> |
67 |
#include <linux/wireless.h> |
68 |
|
69 |
#include "hermes.h" |
70 |
#include "hermes_rid.h" |
71 |
#include "orinoco.h" |
72 |
|
73 |
#include "firmware.h" |
74 |
#include "orinoco_usb.h" |
75 |
#include "orinoco_usb_fw.h" |
76 |
#include "kcompat.h" |
77 |
|
78 |
#ifdef CONFIG_USB_DEBUG |
79 |
static int debug = 1; |
80 |
#else |
81 |
static int debug; |
82 |
#endif |
83 |
|
84 |
/* Use our own dbg macro */ |
85 |
#undef dbg |
86 |
#define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__":%s: " format "\n", __FUNCTION__ , ## arg); } while (0) |
87 |
|
88 |
#define ndbg(format, arg...) |
89 |
#define dump(a, b) bridge_dump_data(__FUNCTION__, a, b) |
90 |
#define ndump(a, b) |
91 |
|
92 |
#define UNUSED __attribute__((unused)) |
93 |
|
94 |
/* Version Information */ |
95 |
#define DRIVER_VERSION "v0.2.1" |
96 |
|
97 |
/* Module paramaters */ |
98 |
MODULE_PARM(debug, "i"); |
99 |
MODULE_PARM_DESC(debug, "Debug enabled or not"); |
100 |
|
101 |
/* Some times, the card gets stuck and stops paying |
102 |
* attention to the world until we do something to it. Setting |
103 |
* watchdog_secs to a nonzero value will set a timer on the USB |
104 |
* callbacks that will trigger within watchdog_secs seconds of |
105 |
* inactivity. The method to get the card back to work is just to send an |
106 |
* INQ_TALLIES command which should be harmless */ |
107 |
static int watchdog_secs = 0; |
108 |
MODULE_PARM(watchdog_secs, "i"); |
109 |
MODULE_PARM_DESC(watchdog_secs, "0 to disable"); |
110 |
|
111 |
static int force_unsupported = 0; |
112 |
MODULE_PARM(force_unsupported, "i"); |
113 |
MODULE_PARM_DESC(force_unsupported, "Try to handle untested hardware."); |
114 |
|
115 |
static int default_card_variant = 0; |
116 |
MODULE_PARM(default_card_variant, "i"); |
117 |
MODULE_PARM_DESC(default_card_variant, "Card variant to use as a first guess"); |
118 |
/* Most of this Device/Vendor pairs are untested */ |
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_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */ |
123 |
#define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */ |
124 |
|
125 |
#define USB_MELCO_VENDOR_ID 0x0411 |
126 |
#define USB_BUFFALO_AIRSTA_ID 0x000B /* Buffalo airstation usb */ |
127 |
|
128 |
#define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */ |
129 |
#define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB |
130 |
Client */ |
131 |
#define USB_AVAYA8_VENDOR_ID 0x0D98 |
132 |
#define USB_AVAYAE_VENDOR_ID 0x0D98 |
133 |
#define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */ |
134 |
|
135 |
#define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */ |
136 |
#define USB_AGERE_ORINOCO_ID 0x1000 /*Wireless USB Card Model 0801*/ |
137 |
#define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */ |
138 |
|
139 |
#define USB_ELSA_VERNDOR_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 /* LEGEND Joynet WLAN USB Card */ |
144 |
|
145 |
#define USB_SAMSUNG_VENDOR_ID 0x04E8 |
146 |
#define USB_SAMSUNG_SEW2001U_ID 0x5002 /* Samsung SEW-2001u Card */ |
147 |
#define USB_SANSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */ |
148 |
|
149 |
#define USB_IGATE_VENDOR_ID 0x0681 |
150 |
#define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */ |
151 |
|
152 |
#define USB_FUJITSU_VENDOR_ID 0x0BF |
153 |
#define USB_FUJITSU_E1100_ID 0x1002 /* Fujitsu Siemens Computers |
154 |
connect2AIR WLAN E-1100 USB */ |
155 |
#define USB_2WIRE_VENDOR_ID 0x1630 |
156 |
#define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */ |
157 |
|
158 |
|
159 |
/* table of devices that may work with this driver */ |
160 |
/* We will refuse to touch any untested device unless force_unsupported is set |
161 |
*/ |
162 |
static struct usb_device_id bridge_table [] = { |
163 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID) }, |
164 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID) }, |
165 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID) }, |
166 |
{ USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_AIRSTA_ID) }, |
167 |
{ USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID) }, |
168 |
{ USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID) }, |
169 |
{ USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID) }, |
170 |
{ USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_ORINOCO_ID) }, |
171 |
{ USB_DEVICE(USB_ELSA_VERNDOR_ID, USB_ELSA_AIRLANCER_ID) }, |
172 |
{ USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID) }, |
173 |
{ USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U_ID, |
174 |
0, 0) }, |
175 |
{ USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SANSUNG_SEW2001U2_ID) }, |
176 |
{ USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID) }, |
177 |
{ USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID) }, |
178 |
{ USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID) }, |
179 |
{ USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID) }, |
180 |
{ } /* Terminating entry */ |
181 |
}; |
182 |
|
183 |
MODULE_DEVICE_TABLE (usb, bridge_table); |
184 |
|
185 |
/* Devices should be added to 'bridge_table' and one of |
186 |
* bridge_varian1_table or bridge_varian2_table. Devices on the |
187 |
* 'bridge_table' but not on any of 'bridge_varian1_table' or |
188 |
* 'bridge_varian2_table' will require force_unsupported=1. */ |
189 |
|
190 |
/* Variant 1 has a PCMCIA card inside */ |
191 |
static struct usb_device_id bridge_varian1_table [] = { |
192 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID) }, |
193 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID) }, |
194 |
{ USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID) }, |
195 |
{ USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID) }, |
196 |
{ USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_AIRSTA_ID) }, |
197 |
{ USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_ORINOCO_ID) }, |
198 |
{ } |
199 |
}; |
200 |
/* Variant 2 is all build in a single board */ |
201 |
static struct usb_device_id bridge_varian2_table [] = { |
202 |
{ } |
203 |
}; |
204 |
|
205 |
struct bridge_read { |
206 |
struct urb *urb; |
207 |
u8 pipe; |
208 |
int avail_status; |
209 |
}; |
210 |
struct bridge_write { |
211 |
u8 pipe; |
212 |
}; |
213 |
enum bridge_priv_flags { |
214 |
ORINOCO_USB_NEEDS_AC_REQ=1, |
215 |
}; |
216 |
struct request_queue { |
217 |
spinlock_t lock; |
218 |
struct list_head pending; |
219 |
struct list_head active; |
220 |
}; |
221 |
/* Structure to hold all of our device specific stuff */ |
222 |
struct bridge_priv { |
223 |
struct usb_device *udev; |
224 |
struct usb_interface *interface; |
225 |
struct net_device *net_dev; |
226 |
struct bridge_read read; |
227 |
struct bridge_write write; |
228 |
u8 reply_count; |
229 |
struct timer_list watchdog_timer; |
230 |
struct semaphore sem; |
231 |
spinlock_t lock; |
232 |
u8 card_variant; |
233 |
u16 hermes_reg_fake [0x40]; |
234 |
int (*orinoco_stop)(struct net_device *dev); |
235 |
int (*orinoco_open)(struct net_device *dev); |
236 |
u32 flags; |
237 |
|
238 |
struct request_queue ctxq; |
239 |
struct { |
240 |
u8 *buf; |
241 |
int fill; |
242 |
} bap; |
243 |
}; |
244 |
|
245 |
struct request_context |
246 |
{ |
247 |
struct list_head list; |
248 |
atomic_t refcount; |
249 |
struct completion done; /* Signals that CTLX is dead */ |
250 |
int killed; |
251 |
struct urb *outurb; /* OUT for req pkt */ |
252 |
/* the RID that we will wait for */ |
253 |
u16 out_rid; |
254 |
u16 in_rid; |
255 |
struct bridge_priv *dev; |
256 |
union { |
257 |
struct bridge_req_packet out; |
258 |
struct bridge_ans_packet in; |
259 |
} *buf; |
260 |
int buf_actual_length; |
261 |
struct timer_list timer; /* Timeout handling */ |
262 |
volatile u32 state; /* Tracks running state */ |
263 |
}; |
264 |
|
265 |
enum { |
266 |
BRIDGE_CTX_START, |
267 |
BRIDGE_CTX_QUEUED, |
268 |
BRIDGE_CTX_REQ_SUBMITTED, |
269 |
BRIDGE_CTX_REQ_COMPLETE, |
270 |
BRIDGE_CTX_RESP_RECEIVED, |
271 |
BRIDGE_CTX_REQ_TIMEOUT, |
272 |
BRIDGE_CTX_REQ_FAILED, |
273 |
BRIDGE_CTX_RESP_TIMEOUT, |
274 |
BRIDGE_CTX_REQSUBMIT_FAIL, |
275 |
BRIDGE_CTX_COMPLETE, |
276 |
}; |
277 |
|
278 |
static __inline__ u8 bridge_reply_inc(u8 count); |
279 |
static void bridge_ctx_complete(struct request_context *ctx); |
280 |
|
281 |
static inline void |
282 |
bridge_mod_timer(struct bridge_priv *dev, |
283 |
struct timer_list *timer, unsigned long expire) |
284 |
{ |
285 |
if (!dev->udev) |
286 |
return; |
287 |
mod_timer(timer, expire); |
288 |
} |
289 |
|
290 |
static void bridge_request_context_put(struct request_context *ctx); |
291 |
|
292 |
static void |
293 |
bridge_request_timerfn(u_long _dev) |
294 |
{ |
295 |
struct bridge_priv *dev = (void *)_dev; |
296 |
LIST_HEAD (killed_ctx); |
297 |
struct list_head *item; |
298 |
struct list_head *tmp_item; |
299 |
unsigned long flags; |
300 |
struct request_context *ctx; |
301 |
|
302 |
dbg("ENTER"); |
303 |
|
304 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
305 |
list_for_each_safe (item, tmp_item, &dev->ctxq.active) { |
306 |
ctx = list_entry(item, struct request_context, list); |
307 |
|
308 |
if (timer_pending(&ctx->timer)) |
309 |
/* let it's own timer expire */ |
310 |
continue; |
311 |
|
312 |
ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; |
313 |
if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) { |
314 |
ctx->state = BRIDGE_CTX_REQ_TIMEOUT; |
315 |
} else { |
316 |
ctx->state = BRIDGE_CTX_RESP_TIMEOUT; |
317 |
dbg("couldn't unlink"); |
318 |
atomic_inc(&ctx->refcount); |
319 |
ctx->killed = 1; |
320 |
list_move(&ctx->list, &killed_ctx); |
321 |
} |
322 |
} |
323 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
324 |
|
325 |
list_for_each_safe (item, tmp_item, &killed_ctx) { |
326 |
ctx = list_entry(item, struct request_context, list); |
327 |
bridge_ctx_complete(ctx); |
328 |
bridge_request_context_put(ctx); |
329 |
} |
330 |
}; |
331 |
|
332 |
static int |
333 |
bridge_request_context_alloc(struct request_context **ctx_p, |
334 |
struct bridge_priv *dev, u16 out_rid, u16 in_rid) |
335 |
{ |
336 |
struct request_context *ctx; |
337 |
|
338 |
ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
339 |
if (!ctx) { |
340 |
return -ENOMEM; |
341 |
} |
342 |
memset(ctx, 0, sizeof(*ctx)); |
343 |
|
344 |
ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC); |
345 |
if (!ctx->buf) { |
346 |
kfree(ctx); |
347 |
return -ENOMEM; |
348 |
} |
349 |
ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC); |
350 |
if (!ctx->outurb) { |
351 |
kfree(ctx->buf); |
352 |
kfree(ctx); |
353 |
return -ENOMEM; |
354 |
} |
355 |
|
356 |
*ctx_p = ctx; |
357 |
|
358 |
ctx->dev = dev; |
359 |
ctx->state = 0; |
360 |
ctx->out_rid = out_rid; |
361 |
ctx->in_rid = in_rid; |
362 |
|
363 |
atomic_set(&ctx->refcount, 1); |
364 |
init_completion(&ctx->done); |
365 |
//usb_init_urb(&ctx->outurb); |
366 |
//ctx->outurb->timeout = DEF_TIMEOUT; |
367 |
|
368 |
init_timer(&ctx->timer); |
369 |
ctx->timer.function = bridge_request_timerfn; |
370 |
ctx->timer.data = (u_long)dev; |
371 |
return 0; |
372 |
} |
373 |
|
374 |
static void |
375 |
bridge_request_context_put(struct request_context *ctx) |
376 |
{ |
377 |
if (!ctx) |
378 |
return; |
379 |
if (atomic_dec_and_test(&ctx->refcount)) { |
380 |
ndbg("freeing %p:%p", ctx->buf, ctx); |
381 |
WARN_ON(!ctx->done.done); |
382 |
BUG_ON(ctx->outurb->status == -EINPROGRESS); |
383 |
BUG_ON(timer_pending(&ctx->timer)); |
384 |
usb_free_urb(ctx->outurb); |
385 |
kfree(ctx->buf); |
386 |
kfree(ctx); |
387 |
} |
388 |
} |
389 |
static void bridge_req_queue_run(struct request_queue *ctxq); |
390 |
|
391 |
/** |
392 |
* bridge_ctx_complete: |
393 |
* Description: |
394 |
* |
395 |
**/ |
396 |
static void |
397 |
bridge_ctx_complete(struct request_context *ctx) |
398 |
{ |
399 |
struct bridge_priv *dev = ctx->dev; |
400 |
struct net_device *net_dev = dev->net_dev; |
401 |
struct orinoco_private *priv = net_dev->priv; |
402 |
struct net_device_stats *stats = &priv->stats; |
403 |
unsigned long flags; |
404 |
|
405 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
406 |
|
407 |
list_del_init(&ctx->list); |
408 |
if (dev->udev) { |
409 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
410 |
bridge_req_queue_run(&dev->ctxq); |
411 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
412 |
} |
413 |
|
414 |
switch(ctx->state) { |
415 |
case BRIDGE_CTX_COMPLETE: |
416 |
case BRIDGE_CTX_REQSUBMIT_FAIL: |
417 |
case BRIDGE_CTX_REQ_FAILED: |
418 |
case BRIDGE_CTX_REQ_TIMEOUT: |
419 |
case BRIDGE_CTX_RESP_TIMEOUT: |
420 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
421 |
|
422 |
switch (ctx->out_rid) { |
423 |
case BRIDGE_RID_TX: |
424 |
ndbg("BRIDGE_RID_TX status:%d", |
425 |
ctx->outurb->status); |
426 |
if (ctx->state != BRIDGE_CTX_COMPLETE ){ |
427 |
stats->tx_errors++; |
428 |
} else { |
429 |
stats->tx_packets++; |
430 |
} |
431 |
netif_wake_queue(net_dev); |
432 |
break; |
433 |
} |
434 |
complete_all(&ctx->done); |
435 |
bridge_request_context_put(ctx); |
436 |
break; |
437 |
|
438 |
default: |
439 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
440 |
if (!dev->udev) { |
441 |
/* This is normal, as all request contexts get flushed |
442 |
* when the device is disconnected */ |
443 |
err("Called, CTLX not terminating, but device gone"); |
444 |
complete_all(&ctx->done); |
445 |
bridge_request_context_put(ctx); |
446 |
break; |
447 |
} |
448 |
|
449 |
err("Called, CTLX not in terminating state."); |
450 |
/* Things are really bad if this happens. Just leak |
451 |
* the CTLX because it may still be linked to the |
452 |
* queue or the OUT urb may still be active. |
453 |
* Just leaking at least prevents an Oops or Panic. |
454 |
*/ |
455 |
break; |
456 |
} |
457 |
} |
458 |
|
459 |
/** |
460 |
* bridge_req_queue_run: |
461 |
* Description: |
462 |
* Note: Only one active CTX at any one time, because there's no |
463 |
* other (reliable) way to match the response URB to the correct |
464 |
* CTX. |
465 |
**/ |
466 |
|
467 |
static void |
468 |
bridge_req_queue_run(struct request_queue *ctxq) |
469 |
{ |
470 |
unsigned long flags; |
471 |
|
472 |
spin_lock_irqsave(&ctxq->lock, flags); |
473 |
|
474 |
if (list_empty(&ctxq->active) && !list_empty(&ctxq->pending)) { |
475 |
struct request_context *head; |
476 |
int result; |
477 |
|
478 |
head = list_entry(ctxq->pending.next, struct request_context, |
479 |
list); |
480 |
|
481 |
if (!head->dev->udev) |
482 |
goto unlock; |
483 |
|
484 |
/* We need to split this off to avoid a race condition */ |
485 |
list_move_tail(&head->list, &ctxq->active); |
486 |
|
487 |
if (head->state == BRIDGE_CTX_QUEUED) { |
488 |
atomic_inc(&head->refcount); |
489 |
result = usb_submit_urb(head->outurb, GFP_ATOMIC); |
490 |
if (result) { |
491 |
head->state = BRIDGE_CTX_REQSUBMIT_FAIL; |
492 |
|
493 |
spin_unlock_irqrestore(&ctxq->lock, flags); |
494 |
|
495 |
err("Fatal, failed to submit command urb." |
496 |
" error=%d\n", result); |
497 |
|
498 |
bridge_ctx_complete(head); |
499 |
bridge_request_context_put(head); |
500 |
goto done; |
501 |
} |
502 |
|
503 |
head->state = BRIDGE_CTX_REQ_SUBMITTED; |
504 |
bridge_mod_timer(head->dev, |
505 |
&head->timer, jiffies + DEF_TIMEOUT); |
506 |
} |
507 |
} |
508 |
|
509 |
unlock: |
510 |
spin_unlock_irqrestore(&ctxq->lock, flags); |
511 |
|
512 |
done: |
513 |
return; |
514 |
} |
515 |
|
516 |
static void |
517 |
bridge_req_enqueue_run(struct request_queue *ctxq, struct request_context *ctx) |
518 |
{ |
519 |
unsigned long flags; |
520 |
|
521 |
spin_lock_irqsave(&ctxq->lock, flags); |
522 |
|
523 |
if (!ctx->dev->udev) { |
524 |
spin_unlock_irqrestore(&ctxq->lock, flags); |
525 |
goto done; |
526 |
} |
527 |
atomic_inc(&ctx->refcount); |
528 |
list_add_tail(&ctx->list, &ctxq->pending); |
529 |
ctx->state = BRIDGE_CTX_QUEUED; |
530 |
|
531 |
spin_unlock_irqrestore(&ctxq->lock, flags); |
532 |
|
533 |
bridge_req_queue_run(ctxq); |
534 |
|
535 |
done: |
536 |
return; |
537 |
} |
538 |
|
539 |
static void |
540 |
bridge_request_out_callback(struct urb *urb, struct pt_regs *pt_regs) |
541 |
{ |
542 |
unsigned long flags; |
543 |
unsigned int state; |
544 |
struct request_context *ctx = urb->context; |
545 |
struct bridge_priv *dev = ctx->dev; |
546 |
|
547 |
|
548 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
549 |
|
550 |
del_timer(&ctx->timer); |
551 |
|
552 |
if (ctx->killed) { |
553 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
554 |
warn("interrupt called with dead ctx"); |
555 |
goto out; |
556 |
} |
557 |
|
558 |
state = ctx->state; |
559 |
|
560 |
if(watchdog_secs) |
561 |
bridge_mod_timer(dev, &dev->watchdog_timer, |
562 |
jiffies+watchdog_secs*HZ); |
563 |
|
564 |
if ( urb->status == 0 ) { |
565 |
switch ( state ) { |
566 |
case BRIDGE_CTX_REQ_SUBMITTED: |
567 |
if (ctx->in_rid) { |
568 |
ctx->state = BRIDGE_CTX_REQ_COMPLETE; |
569 |
/* reply URB still pending */ |
570 |
bridge_mod_timer(dev, &ctx->timer, |
571 |
jiffies + DEF_TIMEOUT); |
572 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
573 |
break; |
574 |
} |
575 |
/* fall through */ |
576 |
case BRIDGE_CTX_RESP_RECEIVED: |
577 |
/* IN already received before this OUT-ACK */ |
578 |
ctx->state = BRIDGE_CTX_COMPLETE; |
579 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
580 |
bridge_ctx_complete(ctx); |
581 |
break; |
582 |
|
583 |
default: |
584 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
585 |
err("Illegal CTLX success state(0x%x, %d)" |
586 |
" in OUT URB\n", state, urb->status); |
587 |
break; |
588 |
} |
589 |
} else { |
590 |
/* If someone cancels the OUT URB then its status |
591 |
* should be either -ECONNRESET or -ENOENT. |
592 |
*/ |
593 |
switch ( state ) { |
594 |
case BRIDGE_CTX_REQ_SUBMITTED: |
595 |
case BRIDGE_CTX_RESP_RECEIVED: |
596 |
ctx->state = BRIDGE_CTX_REQ_FAILED; |
597 |
/* fall through */ |
598 |
|
599 |
case BRIDGE_CTX_REQ_FAILED: |
600 |
case BRIDGE_CTX_REQ_TIMEOUT: |
601 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
602 |
|
603 |
bridge_ctx_complete(ctx); |
604 |
break; |
605 |
|
606 |
default: |
607 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
608 |
|
609 |
err("Illegal CTLX error state(0x%x, %d) in OUT URB", |
610 |
state, urb->status); |
611 |
break; |
612 |
} |
613 |
} |
614 |
out: |
615 |
bridge_request_context_put(ctx); |
616 |
} |
617 |
static int |
618 |
bridge_request_in_callback(struct bridge_priv *dev, struct urb *urb) |
619 |
{ |
620 |
struct bridge_ans_packet *ans = urb->transfer_buffer; |
621 |
struct request_context *ctx = NULL; |
622 |
unsigned int state = BRIDGE_CTX_START; /* for clobbering */ |
623 |
unsigned long flags; |
624 |
|
625 |
/* Find the CTLX on the active queue that requested this URB */ |
626 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
627 |
if ( dev->udev ) { |
628 |
struct list_head *item; |
629 |
|
630 |
list_for_each(item, &dev->ctxq.active) { |
631 |
struct request_context *c; |
632 |
int reply_count; |
633 |
|
634 |
c = list_entry(item, struct request_context, list); |
635 |
reply_count = bridge_reply_inc(c->buf->out.reply_count); |
636 |
if ((ans->reply_count == reply_count) |
637 |
&& (ans->hermes_rid == c->in_rid)) { |
638 |
ctx = c; |
639 |
state = ctx->state; |
640 |
break; |
641 |
} |
642 |
dbg("Skiped (0x%x/0x%x) (%d/%d)", ans->hermes_rid, |
643 |
c->in_rid, ans->reply_count, reply_count); |
644 |
} |
645 |
} |
646 |
|
647 |
if ( ctx == NULL ) { |
648 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
649 |
warn("Could not match IN URB(0x%x,%d) to CTLX - ignored", |
650 |
ans->hermes_rid, urb->status); |
651 |
bridge_req_queue_run(&dev->ctxq); |
652 |
return -ENOENT; |
653 |
} else { |
654 |
/* The data we want is in the in buffer, exchange */ |
655 |
urb->transfer_buffer = ctx->buf; |
656 |
ctx->buf = (void *)ans; |
657 |
ctx->buf_actual_length = urb->actual_length; |
658 |
} |
659 |
|
660 |
switch ( state ) { |
661 |
case BRIDGE_CTX_REQ_SUBMITTED: |
662 |
/* We have received our response URB before |
663 |
* our request has been acknowledged. Do NOT |
664 |
* destroy our CTX yet, because our OUT URB |
665 |
* is still alive ... |
666 |
*/ |
667 |
ctx->state = BRIDGE_CTX_RESP_RECEIVED; |
668 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
669 |
|
670 |
/* Copy the buffer to ctx */ |
671 |
//memcpy(&ctx->buf->in, ans, sizeof(ctx->buf->in)); |
672 |
|
673 |
/* Let the machine continue running. */ |
674 |
break; |
675 |
|
676 |
case BRIDGE_CTX_REQ_COMPLETE: |
677 |
/* This is the usual path: our request |
678 |
* has already been acknowledged, and |
679 |
* we have now received the reply. |
680 |
*/ |
681 |
ctx->state = BRIDGE_CTX_COMPLETE; |
682 |
|
683 |
/* Stop the intimer */ |
684 |
del_timer(&ctx->timer); |
685 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
686 |
|
687 |
/* Copy the buffer to ctx */ |
688 |
//memcpy(&ctx->buf->in, ans, sizeof(ctx->buf->in)); |
689 |
|
690 |
/* Call the completion handler */ |
691 |
bridge_ctx_complete(ctx); |
692 |
break; |
693 |
|
694 |
default: |
695 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
696 |
|
697 |
warn("Matched IN URB, CTX in invalid state(0x%x). Discarded.", |
698 |
state); |
699 |
/* Throw this CTX away and try submitting another */ |
700 |
del_timer(&ctx->timer); |
701 |
ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; |
702 |
usb_unlink_urb(ctx->outurb); |
703 |
bridge_req_queue_run(&dev->ctxq); |
704 |
break; |
705 |
} /* switch */ |
706 |
return 0; |
707 |
} |
708 |
|
709 |
|
710 |
|
711 |
static void |
712 |
bridge_req_ctx_wait(struct bridge_priv *dev, struct request_context *ctx) |
713 |
{ |
714 |
unsigned long flags; |
715 |
|
716 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
717 |
switch(ctx->state) { |
718 |
case BRIDGE_CTX_COMPLETE: |
719 |
case BRIDGE_CTX_REQSUBMIT_FAIL: |
720 |
case BRIDGE_CTX_REQ_TIMEOUT: |
721 |
case BRIDGE_CTX_REQ_FAILED: |
722 |
case BRIDGE_CTX_RESP_TIMEOUT: |
723 |
dbg("Already done, skipping sleep."); |
724 |
/* fall through */ |
725 |
case BRIDGE_CTX_START: |
726 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
727 |
break; |
728 |
default: |
729 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
730 |
dbg("Sleeping..."); |
731 |
if (in_interrupt()) { |
732 |
/* If we get called from a timer, timeout timers don't |
733 |
* get the chance to run themselfs. So we make sure |
734 |
* that we don't sleep for ever */ |
735 |
int msecs = DEF_TIMEOUT * (1000/HZ); |
736 |
while(!ctx->done.done && msecs--) |
737 |
udelay(1000); |
738 |
} else { |
739 |
wait_event_interruptible(ctx->done.wait, |
740 |
ctx->done.done); |
741 |
} |
742 |
dbg("Done sleeping"); |
743 |
break; |
744 |
} |
745 |
} |
746 |
|
747 |
/* local function prototypes */ |
748 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
749 |
static void * bridge_probe(struct usb_device *dev, unsigned int ifnum, |
750 |
const struct usb_device_id *id); |
751 |
static void bridge_disconnect(struct usb_device *dev, void *ptr); |
752 |
#else |
753 |
static int bridge_probe(struct usb_interface *interface, |
754 |
const struct usb_device_id *id); |
755 |
static void bridge_disconnect(struct usb_interface *interface); |
756 |
#endif |
757 |
|
758 |
|
759 |
static void bridge_bulk_in_callback (struct urb *urb, struct pt_regs *); |
760 |
|
761 |
static inline void |
762 |
bridge_dump_data(const char *function, const void *data, int size); |
763 |
|
764 |
static char *rid_name(u16 rid); |
765 |
|
766 |
/* lock to protect the minor_table structure */ |
767 |
static DECLARE_MUTEX (minor_table_mutex); |
768 |
/* usb specific object needed to register this driver with the usb subsystem */ |
769 |
static struct usb_driver orinoco_driver = { |
770 |
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,19)) |
771 |
.owner = THIS_MODULE, |
772 |
#endif |
773 |
.name = "Orinoco USB", |
774 |
.probe = bridge_probe, |
775 |
.disconnect = bridge_disconnect, |
776 |
.id_table = bridge_table, |
777 |
}; |
778 |
|
779 |
/* |
780 |
* Orinoco helper functions |
781 |
*/ |
782 |
|
783 |
static __inline__ u16 build_crc(u8 *data) |
784 |
{ |
785 |
u16 crc = 0; |
786 |
int i; |
787 |
for (i=0; i<8; i++){ |
788 |
crc = (crc<<1) + data[i]; |
789 |
} |
790 |
return crc; |
791 |
} |
792 |
|
793 |
static __inline__ u8 bridge_reply_inc(u8 count) |
794 |
{ |
795 |
if (count < 0x7F) |
796 |
return count+1; |
797 |
else |
798 |
return 1; |
799 |
} |
800 |
|
801 |
static inline void |
802 |
bridge_req_le_to_cpu(struct bridge_ans_packet *req) |
803 |
{ |
804 |
le16_to_cpus(req->x00); |
805 |
le16_to_cpus(req->x04); |
806 |
le16_to_cpus(req->size); |
807 |
le16_to_cpus(req->crc); |
808 |
le16_to_cpus(req->hermes_len); |
809 |
le16_to_cpus(req->hermes_rid); |
810 |
} |
811 |
|
812 |
/** |
813 |
* fill_bridge_req: |
814 |
* |
815 |
* if data == NULL and length > 0 the data is assumed to be already in |
816 |
* the target buffer and only the header is filled. |
817 |
* |
818 |
*/ |
819 |
static int |
820 |
fill_bridge_req(struct bridge_req_packet *req, u16 length, u16 rid, |
821 |
const void *data, u16 x04, u8 reply_count) |
822 |
{ |
823 |
int total_size = sizeof(*req)+length; |
824 |
|
825 |
BUG_ON(total_size > BULK_BUF_SIZE); |
826 |
|
827 |
req->x00 = cpu_to_le16(0x0210); |
828 |
req->reply_count = reply_count; |
829 |
req->x03 = 0x00; |
830 |
req->x04 = x04; |
831 |
req->x05 = 0x00; |
832 |
req->size= cpu_to_le16(length + 4); |
833 |
req->crc = cpu_to_le16(build_crc((u8*)req)); |
834 |
req->hermes_len = cpu_to_le16((length+3)/2); |
835 |
req->hermes_rid = cpu_to_le16(rid); |
836 |
if(data) |
837 |
memcpy(req->data, data, length); |
838 |
return total_size; |
839 |
} |
840 |
|
841 |
static int bridge_submit_in_urb(struct bridge_priv *dev, |
842 |
struct request_context *ctx) |
843 |
{ |
844 |
int retval = 0; |
845 |
void *cur_buf = dev->read.urb->transfer_buffer; |
846 |
ndbg("ENTER"); |
847 |
if(dev->read.urb->status == -EINPROGRESS){ |
848 |
dbg("urb busy, not resubmiting"); |
849 |
retval = -EBUSY; |
850 |
goto exit; |
851 |
} |
852 |
if(ctx){ |
853 |
void *tmp = ctx->buf; |
854 |
ctx->buf = cur_buf; |
855 |
cur_buf = tmp; |
856 |
ctx->buf_actual_length = dev->read.urb->actual_length; |
857 |
} |
858 |
dev->read.avail_status = dev->read.urb->status; |
859 |
usb_fill_bulk_urb(dev->read.urb, dev->udev, |
860 |
usb_rcvbulkpipe(dev->udev, dev->read.pipe), |
861 |
cur_buf, BULK_BUF_SIZE, |
862 |
bridge_bulk_in_callback, dev); |
863 |
dev->read.urb->timeout = 0; //300*HZ; |
864 |
dev->read.urb->transfer_flags = 0; //USB_ASYNC_UNLINK; |
865 |
retval = usb_submit_urb(dev->read.urb, GFP_ATOMIC); |
866 |
if (retval) { |
867 |
err("%s submit failed %d", __FUNCTION__, retval); |
868 |
goto exit; |
869 |
} |
870 |
exit: |
871 |
ndbg("EXIT"); |
872 |
return retval; |
873 |
} |
874 |
static int bridge_remove_in_urb(struct bridge_priv *dev) |
875 |
{ |
876 |
int retval = 0; |
877 |
ndbg("ENTER"); |
878 |
if (dev->read.urb->status != -EINPROGRESS){ |
879 |
dbg("no urb to remove"); |
880 |
retval = -ENOENT; |
881 |
goto exit; |
882 |
} |
883 |
retval = usb_unlink_urb(dev->read.urb); |
884 |
exit: |
885 |
ndbg("EXIT"); |
886 |
return retval; |
887 |
} |
888 |
static __inline__ int ezusb_8051_cpucs(struct bridge_priv *dev, u8 reset) |
889 |
{ |
890 |
if(!dev->udev){ |
891 |
err("%s: !dev->udev", __FUNCTION__); |
892 |
return -EFAULT; |
893 |
} |
894 |
return usb_control_msg(dev->udev, |
895 |
usb_sndctrlpipe(dev->udev, 0), |
896 |
EZUSB_REQUEST_FW_TRANS, |
897 |
USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_OUT, |
898 |
EZUSB_CPUCS_REG, 0x0, |
899 |
&reset, sizeof(reset), |
900 |
DEF_TIMEOUT); |
901 |
} |
902 |
|
903 |
static int bridge_reset(struct bridge_priv *dev) |
904 |
{ |
905 |
int retval = 0; |
906 |
ndbg("ENTER"); |
907 |
retval = ezusb_8051_cpucs(dev, 1); |
908 |
if (retval < 0){ |
909 |
err("Failed to reset"); |
910 |
goto exit; |
911 |
} |
912 |
|
913 |
retval = ezusb_8051_cpucs(dev, 0); |
914 |
if (retval < 0){ |
915 |
err("Failed to unreset"); |
916 |
goto exit; |
917 |
} |
918 |
exit: |
919 |
ndbg("EXIT"); |
920 |
return retval; |
921 |
} |
922 |
|
923 |
static int find_fw_variant_offset(struct ez_usb_fw *fw) |
924 |
{ |
925 |
int offset; |
926 |
offset = *(short *)(fw->code+1); |
927 |
offset = be16_to_cpu(offset); |
928 |
for(; offset < fw->size; offset++){ |
929 |
if (memcmp("\x02\x03\x58", &fw->code[offset], 3)==0) |
930 |
break; |
931 |
} |
932 |
if (offset > (fw->size-4)){ |
933 |
dbg("couldn't find card_variant_offset"); |
934 |
return -1; |
935 |
} |
936 |
fw->card_variant_offset = offset + 3; |
937 |
dbg("card_variant_offset = 0x%04X", fw->card_variant_offset); |
938 |
return 0; |
939 |
} |
940 |
|
941 |
static int bridge_firmware_download(struct bridge_priv *dev, |
942 |
struct ez_usb_fw *fw) |
943 |
{ |
944 |
int retval, addr; |
945 |
int packet_size = dev->udev->descriptor.bMaxPacketSize0; |
946 |
int variant_offset = fw->card_variant_offset%packet_size; |
947 |
int variant_base = fw->card_variant_offset - variant_offset; |
948 |
u8 *transfer_buffer = kmalloc (packet_size, GFP_KERNEL); |
949 |
|
950 |
dbg("ENTER"); |
951 |
ndbg("packet size: %d", packet_size); |
952 |
if (!transfer_buffer) { |
953 |
err("orinoco_usb: kmalloc(%d) failed.", packet_size); |
954 |
retval = -ENOMEM; |
955 |
goto fail; |
956 |
} |
957 |
|
958 |
retval = ezusb_8051_cpucs(dev, 1); |
959 |
if(retval < 0) |
960 |
goto fail; |
961 |
for(addr = 0; addr < fw->size; addr += packet_size){ |
962 |
if((addr >= 0x100) && (addr < 0x300)){ |
963 |
/* 0x100-0x300 should be left alone, it |
964 |
* contains card specific data, like USB |
965 |
* enumeration information */ |
966 |
ndbg("Skiping 0x100-0x300 (0x%04X)", fw_rec->Address); |
967 |
continue; |
968 |
} |
969 |
memcpy (transfer_buffer, &fw->code[addr], packet_size); |
970 |
if(addr == variant_base){ |
971 |
dbg("Patching card_variant byte 0x%02X at 0x%04X", |
972 |
dev->card_variant, fw->card_variant_offset); |
973 |
transfer_buffer[variant_offset] = dev->card_variant; |
974 |
} |
975 |
retval = usb_control_msg(dev->udev, |
976 |
usb_sndctrlpipe(dev->udev, 0), |
977 |
EZUSB_REQUEST_FW_TRANS, |
978 |
USB_TYPE_VENDOR|USB_RECIP_DEVICE |
979 |
|USB_DIR_OUT, |
980 |
addr, 0x0, |
981 |
transfer_buffer, packet_size, |
982 |
DEF_TIMEOUT); |
983 |
|
984 |
if(retval < 0) |
985 |
goto fail; |
986 |
} |
987 |
retval = ezusb_8051_cpucs(dev, 0); |
988 |
goto exit; |
989 |
fail: |
990 |
err("orinoco_usb: fw upload: retval=%d", retval); |
991 |
exit: |
992 |
if(transfer_buffer) |
993 |
kfree (transfer_buffer); |
994 |
dbg("EXIT"); |
995 |
return retval; |
996 |
} |
997 |
static int bridge_access_ltv(struct bridge_priv *dev, |
998 |
struct request_context *ctx, |
999 |
u16 length, const void *data, u16 x04, |
1000 |
void *ans_buff, int ans_size, |
1001 |
u16 *ans_length) |
1002 |
{ |
1003 |
struct net_device *net_dev = dev->net_dev; |
1004 |
struct orinoco_private *priv = net_dev->priv; |
1005 |
int req_size; |
1006 |
int retval = 0; |
1007 |
unsigned long flags=0; |
1008 |
|
1009 |
ndbg("ENTER"); |
1010 |
BUG_ON(!ctx); |
1011 |
|
1012 |
if(!dev->udev){ |
1013 |
dbg("Device disconnected"); |
1014 |
return -ENODEV; |
1015 |
} |
1016 |
|
1017 |
if(dev->read.urb->status != -EINPROGRESS){ |
1018 |
err("%s: in urb not pending", __FUNCTION__); |
1019 |
} |
1020 |
|
1021 |
spin_lock_irqsave(&dev->lock, flags); |
1022 |
/* need to be atomic to get reply_count right */ |
1023 |
req_size = fill_bridge_req(&ctx->buf->out, length, ctx->out_rid, data, |
1024 |
x04, dev->reply_count); |
1025 |
usb_fill_bulk_urb(ctx->outurb, dev->udev, |
1026 |
usb_sndbulkpipe(dev->udev, dev->write.pipe), |
1027 |
&ctx->buf->out, req_size, |
1028 |
bridge_request_out_callback, ctx); |
1029 |
|
1030 |
if (ctx->in_rid) |
1031 |
dev->reply_count = bridge_reply_inc(dev->reply_count); |
1032 |
|
1033 |
bridge_req_enqueue_run(&dev->ctxq, ctx); |
1034 |
|
1035 |
spin_unlock_irqrestore(&dev->lock, flags); |
1036 |
|
1037 |
if (ctx->in_rid) { |
1038 |
int hw_unavailable = priv->hw_unavailable; |
1039 |
#warning hw_unavailable is probably not right, but it works for now |
1040 |
if(!hw_unavailable) |
1041 |
spin_unlock_bh(&priv->lock); |
1042 |
|
1043 |
bridge_req_ctx_wait(dev, ctx); |
1044 |
|
1045 |
if(!hw_unavailable) |
1046 |
spin_lock_bh(&priv->lock); |
1047 |
} |
1048 |
|
1049 |
switch (ctx->state){ |
1050 |
case BRIDGE_CTX_COMPLETE: |
1051 |
retval = ctx->outurb->status; |
1052 |
break; |
1053 |
|
1054 |
case BRIDGE_CTX_QUEUED: |
1055 |
case BRIDGE_CTX_REQ_SUBMITTED: |
1056 |
if(!ctx->in_rid) |
1057 |
break; |
1058 |
default: |
1059 |
err("Non specific state %d", ctx->state); |
1060 |
/* fall though */ |
1061 |
case BRIDGE_CTX_REQ_TIMEOUT: |
1062 |
case BRIDGE_CTX_REQ_FAILED: |
1063 |
case BRIDGE_CTX_RESP_TIMEOUT: |
1064 |
case BRIDGE_CTX_REQSUBMIT_FAIL: |
1065 |
err("orinoco_usb: access failed(%d)" |
1066 |
" reply_count %d (resetting)", |
1067 |
ctx->state, dev->reply_count); |
1068 |
dev->reply_count = 0; |
1069 |
if (ctx->state == BRIDGE_CTX_REQ_TIMEOUT |
1070 |
|| ctx->state == BRIDGE_CTX_RESP_TIMEOUT) { |
1071 |
err("orinoco_usb: ctx timed out"); |
1072 |
retval = -ETIMEDOUT; |
1073 |
} else { |
1074 |
err("orinoco_usb: ctx failed"); |
1075 |
retval = -EFAULT; |
1076 |
} |
1077 |
goto exit; |
1078 |
break; |
1079 |
} |
1080 |
if (ctx->in_rid) { |
1081 |
struct bridge_ans_packet *ans = &ctx->buf->in; |
1082 |
ndump((__u8 *)ans, ctx->buf_actual_length); |
1083 |
if(((ans->hermes_len+1)*2+10) != ctx->buf_actual_length) |
1084 |
err("%s: hermes_len=%d actual_length=%d", __FUNCTION__, |
1085 |
ans->hermes_len, ctx->buf_actual_length); |
1086 |
if(ans_buff) |
1087 |
memcpy(ans_buff, ans->data, |
1088 |
min_t(int, ctx->buf_actual_length-sizeof(*ans), |
1089 |
ans_size)); |
1090 |
if(ans_length) |
1091 |
*ans_length = ans->hermes_len; |
1092 |
} |
1093 |
exit: |
1094 |
bridge_request_context_put(ctx); |
1095 |
ndbg("EXIT"); |
1096 |
return retval; |
1097 |
} |
1098 |
|
1099 |
int bridge_write_ltv(hermes_t *hw, int bap, u16 rid, |
1100 |
u16 length, const void *data) |
1101 |
{ |
1102 |
struct bridge_priv *dev = hw->priv; |
1103 |
u16 x04 = 0x02; |
1104 |
struct request_context *ctx; |
1105 |
int err; |
1106 |
|
1107 |
if (length == 0) |
1108 |
return -EINVAL; |
1109 |
ndbg("length: %d", length); |
1110 |
length = HERMES_RECLEN_TO_BYTES(length); |
1111 |
ndbg("length: %d", length); |
1112 |
if (length == 0){ |
1113 |
/* On memory mapped devices |
1114 |
* HERMES_RID_CNFGROUPADDRESSES can be set to be |
1115 |
* empty, but the USB bridge doesn't like it */ |
1116 |
ndbg("zero size data, make them think that we did it"); |
1117 |
return 0; |
1118 |
} |
1119 |
err = bridge_request_context_alloc(&ctx, dev, rid, BRIDGE_RID_ACK); |
1120 |
if (err) |
1121 |
return err; |
1122 |
|
1123 |
ndbg("0x%04X", rid); |
1124 |
if (rid == BRIDGE_RID_TX) |
1125 |
x04 = 0x01; |
1126 |
|
1127 |
return bridge_access_ltv(dev, ctx, length, data, x04, |
1128 |
NULL, 0, NULL); |
1129 |
} |
1130 |
|
1131 |
static int bridge_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize, |
1132 |
u16 *length, void *buf) |
1133 |
{ |
1134 |
struct bridge_priv *dev = hw->priv; |
1135 |
struct request_context *ctx; |
1136 |
int err; |
1137 |
ndbg("0x%04X", rid); |
1138 |
|
1139 |
if ( (bufsize < 0) || (bufsize % 2) ) |
1140 |
return -EINVAL; |
1141 |
|
1142 |
err = bridge_request_context_alloc(&ctx, dev, rid, rid); |
1143 |
if (err) |
1144 |
return err; |
1145 |
|
1146 |
return bridge_access_ltv(dev, ctx, 0, NULL, 0x02, |
1147 |
buf, bufsize, length); |
1148 |
} |
1149 |
static int bridge_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0, |
1150 |
hermes_response_t * resp) |
1151 |
{ |
1152 |
struct bridge_priv *dev = hw->priv; |
1153 |
struct request_context *ctx; |
1154 |
int err; |
1155 |
|
1156 |
struct { |
1157 |
u16 cmd; |
1158 |
u16 parm0; |
1159 |
u16 parm1; |
1160 |
u16 parm2; |
1161 |
} data = { |
1162 |
.cmd = cmd, |
1163 |
.parm0 = parm0, |
1164 |
.parm1 = 0, |
1165 |
.parm2 = 0, |
1166 |
}; |
1167 |
dbg("0x%04X, parm0 0x%04X", cmd, parm0); |
1168 |
ndump((void *)&data, sizeof(data)); |
1169 |
err = bridge_request_context_alloc(&ctx, dev, |
1170 |
BRIDGE_RID_DOCMD, BRIDGE_RID_ACK); |
1171 |
if (err) |
1172 |
return err; |
1173 |
|
1174 |
return bridge_access_ltv(dev, ctx, sizeof(data), &data, 0x02, |
1175 |
NULL, 0, NULL); |
1176 |
} |
1177 |
static char *rid_name(u16 rid) |
1178 |
{ |
1179 |
switch (rid){ |
1180 |
case HERMES_INQ_LINKSTATUS: |
1181 |
return "HERMES_INQ_LINKSTATUS"; |
1182 |
case HERMES_INQ_TALLIES: |
1183 |
return "HERMES_INQ_TALLIES"; |
1184 |
case HERMES_INQ_SCAN: |
1185 |
return "HERMES_INQ_SCAN"; |
1186 |
case BRIDGE_RID_RX: |
1187 |
return "BRIDGE_RID_RX"; |
1188 |
case BRIDGE_RID_TX: |
1189 |
return "BRIDGE_RID_TX"; |
1190 |
default: |
1191 |
return ""; |
1192 |
} |
1193 |
} |
1194 |
static int bridge_bap_pread(struct hermes *hw, int bap, |
1195 |
void *buf, unsigned len, |
1196 |
u16 id, u16 offset) |
1197 |
{ |
1198 |
struct bridge_priv *dev = hw->priv; |
1199 |
struct bridge_ans_packet *ans = |
1200 |
(void *)dev->read.urb->transfer_buffer; |
1201 |
int actual_length = dev->read.urb->actual_length; |
1202 |
|
1203 |
ndbg("bap: %d, id: 0x%04X(%s), offset: 0x%04X, len: %d, size: %d", |
1204 |
bap, id, rid_name(id), offset, len, actual_length); |
1205 |
if (id != BRIDGE_RID_RX){ |
1206 |
ndbg("id == %d != BRIDGE_RID_RX", id); |
1207 |
} |
1208 |
if(offset == 0){ |
1209 |
ndump((void *)ans, actual_length); |
1210 |
} |
1211 |
switch(id){ |
1212 |
case HERMES_INQ_LINKSTATUS: |
1213 |
case HERMES_INQ_TALLIES: |
1214 |
case HERMES_INQ_SCAN: |
1215 |
/* FIXME: this should be the default */ |
1216 |
if ((sizeof(*ans)+offset+len-4) > actual_length){ |
1217 |
err("orinoco_usb: bap read beyond end actual_length"); |
1218 |
return -EFAULT; |
1219 |
} |
1220 |
/* Should include the LENGTH/TYPE part */ |
1221 |
memcpy(buf, (ans->data-4)+offset, len); |
1222 |
ndbg("dev->read.bap.offset: %d", dev->read.bap.offset); |
1223 |
break; |
1224 |
default: |
1225 |
if ((sizeof(*ans)+offset+len) > actual_length){ |
1226 |
err("orinoco_usb: bap read beyond end actual_length"); |
1227 |
return -EFAULT; |
1228 |
} |
1229 |
memcpy(buf, ans->data+offset, len); |
1230 |
} |
1231 |
ndump(buf, len); |
1232 |
return 0; |
1233 |
} |
1234 |
|
1235 |
static int bridge_bap_pwrite(struct hermes *hw, int bap, const void *buf, |
1236 |
unsigned len, u16 id, u16 offset) |
1237 |
{ |
1238 |
struct bridge_priv *dev = hw->priv; |
1239 |
struct bridge_req_packet *req = (void *)dev->bap.buf; |
1240 |
|
1241 |
dbg("bap: %d, id: 0x%04X, offset: %d, len: %d", |
1242 |
bap, id, offset, len); |
1243 |
|
1244 |
BUG(); |
1245 |
|
1246 |
if(offset == 0){ |
1247 |
memset(dev->bap.buf, 0, BULK_BUF_SIZE); |
1248 |
ndbg("clearing bap"); |
1249 |
} |
1250 |
memcpy(req->data+offset, buf, len); |
1251 |
dev->bap.fill = max_t(int, sizeof(*req)+offset+len, dev->bap.fill); |
1252 |
return 0; |
1253 |
} |
1254 |
|
1255 |
static int |
1256 |
bridge_xmit(struct sk_buff *skb, struct net_device *net_dev) |
1257 |
{ |
1258 |
struct orinoco_private *priv = (struct orinoco_private *)net_dev->priv; |
1259 |
struct net_device_stats *stats = &priv->stats; |
1260 |
hermes_t *hw = &priv->hw; |
1261 |
struct bridge_priv *dev = hw->priv; |
1262 |
int err = 0; |
1263 |
char *p; |
1264 |
struct ethhdr *eh; |
1265 |
int len, data_len, data_off; |
1266 |
u16 tx_control; |
1267 |
unsigned long flags; |
1268 |
struct request_context *ctx; |
1269 |
u8 *buf; |
1270 |
int tx_size; |
1271 |
|
1272 |
TRACE_ENTER(net_dev->name); |
1273 |
|
1274 |
if (! netif_running(net_dev)) { |
1275 |
printk(KERN_ERR "%s: Tx on stopped device!\n", |
1276 |
net_dev->name); |
1277 |
TRACE_EXIT(net_dev->name); |
1278 |
return 1; |
1279 |
} |
1280 |
|
1281 |
if (netif_queue_stopped(net_dev)) { |
1282 |
printk(KERN_DEBUG "%s: Tx while transmitter busy!\n", |
1283 |
net_dev->name); |
1284 |
TRACE_EXIT(net_dev->name); |
1285 |
return 1; |
1286 |
} |
1287 |
|
1288 |
if (orinoco_lock(priv, &flags) != 0) { |
1289 |
printk(KERN_ERR "%s: orinoco_xmit() called while hw_unavailable\n", |
1290 |
net_dev->name); |
1291 |
TRACE_EXIT(net_dev->name); |
1292 |
/* BUG(); */ |
1293 |
return 1; |
1294 |
} |
1295 |
|
1296 |
if (! netif_carrier_ok(net_dev)) { |
1297 |
/* Oops, the firmware hasn't established a connection, |
1298 |
silently drop the packet (this seems to be the |
1299 |
safest approach). */ |
1300 |
stats->tx_errors++; |
1301 |
orinoco_unlock(priv, &flags); |
1302 |
dev_kfree_skb(skb); |
1303 |
TRACE_EXIT(net_dev->name); |
1304 |
return 0; |
1305 |
} |
1306 |
|
1307 |
err = bridge_request_context_alloc(&ctx, dev, BRIDGE_RID_TX, 0); |
1308 |
if (err) |
1309 |
goto fail; |
1310 |
memset(ctx->buf, 0, BULK_BUF_SIZE); |
1311 |
buf = ctx->buf->out.data; |
1312 |
|
1313 |
/* Length of the packet body */ |
1314 |
/* FIXME: what if the skb is smaller than this? */ |
1315 |
len = max_t(int,skb->len - ETH_HLEN, ETH_ZLEN - ETH_HLEN); |
1316 |
|
1317 |
eh = (struct ethhdr *)skb->data; |
1318 |
|
1319 |
tx_control = cpu_to_le16(0); |
1320 |
memcpy(buf, &tx_control, sizeof(tx_control)); |
1321 |
buf += sizeof(tx_control); |
1322 |
/* Encapsulate Ethernet-II frames */ |
1323 |
if (ntohs(eh->h_proto) > 1500) { /* Ethernet-II frame */ |
1324 |
struct header_struct *hdr = (void *)buf; |
1325 |
buf += sizeof(*hdr); |
1326 |
data_len = len; |
1327 |
data_off = sizeof(tx_control) + sizeof(*hdr); |
1328 |
p = skb->data + ETH_HLEN; |
1329 |
|
1330 |
/* 802.3 header */ |
1331 |
memcpy(hdr->dest, eh->h_dest, ETH_ALEN); |
1332 |
memcpy(hdr->src, eh->h_source, ETH_ALEN); |
1333 |
hdr->len = htons(data_len + ENCAPS_OVERHEAD); |
1334 |
|
1335 |
/* 802.2 header */ |
1336 |
memcpy(&hdr->dsap, &encaps_hdr, sizeof(encaps_hdr)); |
1337 |
|
1338 |
hdr->ethertype = eh->h_proto; |
1339 |
} else { /* IEEE 802.3 frame */ |
1340 |
data_len = len + ETH_HLEN; |
1341 |
data_off = sizeof(tx_control); |
1342 |
p = skb->data; |
1343 |
} |
1344 |
|
1345 |
memcpy(buf, p, data_len); |
1346 |
buf += data_len; |
1347 |
|
1348 |
/* Finally, we actually initiate the send */ |
1349 |
netif_stop_queue(net_dev); |
1350 |
|
1351 |
/* The card may behave better if we send evenly sized usb transfers */ |
1352 |
tx_size = RUP_EVEN(buf - ctx->buf->out.data); |
1353 |
|
1354 |
err = bridge_access_ltv(dev, ctx, tx_size, NULL, 0x01, NULL, 0, NULL); |
1355 |
|
1356 |
if (err) { |
1357 |
netif_start_queue(net_dev); |
1358 |
printk(KERN_ERR "%s: Error %d transmitting packet\n", |
1359 |
net_dev->name, err); |
1360 |
stats->tx_errors++; |
1361 |
goto fail; |
1362 |
} |
1363 |
|
1364 |
net_dev->trans_start = jiffies; |
1365 |
stats->tx_bytes += data_off + data_len; |
1366 |
|
1367 |
orinoco_unlock(priv, &flags); |
1368 |
|
1369 |
dev_kfree_skb(skb); |
1370 |
|
1371 |
TRACE_EXIT(net_dev->name); |
1372 |
|
1373 |
return 0; |
1374 |
fail: |
1375 |
TRACE_EXIT(net_dev->name); |
1376 |
|
1377 |
orinoco_unlock(priv, &flags); |
1378 |
return err; |
1379 |
} |
1380 |
|
1381 |
static int bridge_allocate(struct hermes *hw, u16 size, u16 *fid) |
1382 |
{ |
1383 |
struct bridge_priv *dev = hw->priv; |
1384 |
struct net_device *net_dev = dev->net_dev; |
1385 |
|
1386 |
*fid = BRIDGE_RID_TX; |
1387 |
netif_wake_queue(net_dev); |
1388 |
return 0; |
1389 |
} |
1390 |
|
1391 |
static int bridge_init(hermes_t *hw) |
1392 |
{ |
1393 |
struct bridge_priv *dev = hw->priv; |
1394 |
int retval = 0; |
1395 |
dbg("ENTER"); |
1396 |
if(in_interrupt()){ |
1397 |
err("bridge_init called from interrupt"); |
1398 |
retval = -EFAULT; |
1399 |
goto exit; |
1400 |
} |
1401 |
if(!dev){ |
1402 |
err("NULL dev"); |
1403 |
retval = -EFAULT; |
1404 |
goto exit; |
1405 |
} |
1406 |
dev->reply_count=0; |
1407 |
/* Write the MAGIC number on the simulated registers to keep |
1408 |
* orinoco.c happy */ |
1409 |
hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC); |
1410 |
hermes_write_regn(hw, RXFID, BRIDGE_RID_RX); |
1411 |
retval = bridge_reset(dev); |
1412 |
if (retval < 0) |
1413 |
goto exit; |
1414 |
dbg("sending control message"); |
1415 |
retval = usb_control_msg(dev->udev, |
1416 |
usb_sndctrlpipe(dev->udev, 0), |
1417 |
EZUSB_REQUEST_TRIGER, |
1418 |
USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_OUT, |
1419 |
0x0, 0x0, |
1420 |
NULL, 0, |
1421 |
DEF_TIMEOUT); |
1422 |
if (retval < 0){ |
1423 |
err("EZUSB_REQUEST_TRIGER failed retval %d", retval); |
1424 |
goto exit; |
1425 |
} |
1426 |
if (dev->flags && ORINOCO_USB_NEEDS_AC_REQ){ |
1427 |
dbg("Sending EZUSB_REQUEST_TRIG_AC"); |
1428 |
retval = usb_control_msg(dev->udev, |
1429 |
usb_sndctrlpipe(dev->udev, 0), |
1430 |
EZUSB_REQUEST_TRIG_AC, |
1431 |
USB_TYPE_VENDOR|USB_RECIP_DEVICE |
1432 |
|USB_DIR_OUT, |
1433 |
0x00FA, 0x0, |
1434 |
NULL, 0, |
1435 |
DEF_TIMEOUT); |
1436 |
if (retval < 0){ |
1437 |
err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval); |
1438 |
goto exit; |
1439 |
} |
1440 |
} |
1441 |
bridge_remove_in_urb(dev); |
1442 |
bridge_submit_in_urb(dev, NULL); |
1443 |
|
1444 |
retval = bridge_write_ltv(hw, 0, BRIDGE_RID_INIT1, |
1445 |
HERMES_BYTES_TO_RECLEN(2), "\x10\x00"); |
1446 |
if (retval < 0){ |
1447 |
err("orinoco_usb: BRIDGE_RID_INIT1 retval %d", retval); |
1448 |
goto exit; |
1449 |
} |
1450 |
retval = hermes_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL); |
1451 |
if (retval < 0){ |
1452 |
err("orinoco_usb: HERMES_CMD_INIT retval %d", retval); |
1453 |
goto exit; |
1454 |
} |
1455 |
|
1456 |
exit: |
1457 |
if(retval < 0){ |
1458 |
if (bridge_reset(dev) < 0){ |
1459 |
err("orinoco_usb: ERROR RESETTING DEVICE"); |
1460 |
} |
1461 |
bridge_remove_in_urb(dev); |
1462 |
} |
1463 |
dbg("EXIT"); |
1464 |
return retval; |
1465 |
} |
1466 |
|
1467 |
static int bridge_netdev_stop(struct net_device *net_dev) |
1468 |
{ |
1469 |
struct orinoco_private *priv = net_dev->priv; |
1470 |
struct bridge_priv *dev = priv->card; |
1471 |
|
1472 |
if(watchdog_secs) |
1473 |
del_timer(&dev->watchdog_timer); |
1474 |
return dev->orinoco_stop(net_dev); |
1475 |
} |
1476 |
static int bridge_netdev_open(struct net_device *net_dev) |
1477 |
{ |
1478 |
struct orinoco_private *priv = net_dev->priv; |
1479 |
struct bridge_priv *dev = priv->card; |
1480 |
int retval; |
1481 |
|
1482 |
retval = dev->orinoco_open(net_dev); |
1483 |
if(watchdog_secs) |
1484 |
bridge_mod_timer(dev, &dev->watchdog_timer, |
1485 |
jiffies+watchdog_secs*HZ); |
1486 |
return retval; |
1487 |
} |
1488 |
|
1489 |
static inline void |
1490 |
bridge_dump_data (const char *function, const void *data_p, int size) |
1491 |
{ |
1492 |
int i; |
1493 |
const u8 *data = data_p; |
1494 |
|
1495 |
if (!debug) |
1496 |
return; |
1497 |
|
1498 |
printk (KERN_DEBUG __FILE__": %s - length: %d", |
1499 |
function, size); |
1500 |
|
1501 |
printk("\n"KERN_DEBUG" "); |
1502 |
for (i=0; i<0x10; i++) |
1503 |
printk("%02X ", i); |
1504 |
for (i=0; i<size; i++){ |
1505 |
if((i%0x10) == 0) |
1506 |
printk("\n"KERN_DEBUG"0x%04X: ", i/0x10); |
1507 |
printk("%02X ", data[i]); |
1508 |
} |
1509 |
printk("\n"); |
1510 |
|
1511 |
|
1512 |
} |
1513 |
|
1514 |
|
1515 |
static inline void bridge_delete (struct bridge_priv *dev) |
1516 |
{ |
1517 |
//struct net_device *net_dev = dev->net_dev; |
1518 |
//struct orinoco_private *priv = net_dev->priv; |
1519 |
|
1520 |
dbg("ENTER"); |
1521 |
if(in_interrupt()){ |
1522 |
err("orinoco_usb: %s: In interrupt", __FUNCTION__); |
1523 |
return; |
1524 |
} |
1525 |
if(!dev){ |
1526 |
err("%s: dev == NULL", __FUNCTION__); |
1527 |
return; |
1528 |
} |
1529 |
if (dev->net_dev) |
1530 |
unregister_netdev(dev->net_dev); |
1531 |
kfree (dev->read.urb->transfer_buffer); |
1532 |
if (dev->bap.buf != NULL) |
1533 |
kfree (dev->bap.buf); |
1534 |
if (dev->read.urb != NULL) |
1535 |
usb_free_urb(dev->read.urb); |
1536 |
if (dev->net_dev) |
1537 |
kfree(dev->net_dev); |
1538 |
dbg("EXIT"); |
1539 |
} |
1540 |
|
1541 |
static void |
1542 |
bridge_bulk_in_callback (struct urb *urb, struct pt_regs *regs) |
1543 |
{ |
1544 |
struct bridge_priv *dev = (struct bridge_priv *)urb->context; |
1545 |
struct net_device *net_dev = dev->net_dev; |
1546 |
struct orinoco_private *priv = net_dev->priv; |
1547 |
hermes_t *hw = &priv->hw; |
1548 |
struct bridge_ans_packet *ans = urb->transfer_buffer; |
1549 |
|
1550 |
if (dev->udev == NULL){ |
1551 |
dbg("disconnected"); |
1552 |
return; |
1553 |
} |
1554 |
|
1555 |
if(watchdog_secs) |
1556 |
bridge_mod_timer(dev, &dev->watchdog_timer, |
1557 |
jiffies+watchdog_secs*HZ); |
1558 |
|
1559 |
if (urb->status == -ETIMEDOUT){ |
1560 |
/* When a device gets unplugged we get this every time |
1561 |
* we resubmit, flooding the logs. Since we don't use |
1562 |
* USB timeouts, it shouldn't happen any other time*/ |
1563 |
warn("%s: urb timed out, not resubmiting", __FUNCTION__); |
1564 |
goto exit; |
1565 |
} |
1566 |
if (urb->status == -ECONNABORTED){ |
1567 |
warn("%s: connection abort, resubmiting urb", __FUNCTION__); |
1568 |
goto resubmit; |
1569 |
} |
1570 |
if ((urb->status == -EILSEQ) |
1571 |
|| (urb->status == -ENOENT) |
1572 |
|| (urb->status == -ECONNRESET)){ |
1573 |
dbg("status %d, not resubmiting", urb->status); |
1574 |
goto exit; |
1575 |
} |
1576 |
if(urb->status) |
1577 |
dbg("status: %d length: %d", |
1578 |
urb->status, urb->actual_length); |
1579 |
if (urb->actual_length < sizeof(*ans)){ |
1580 |
err("%s: short read, ignoring", __FUNCTION__); |
1581 |
goto resubmit; |
1582 |
} |
1583 |
bridge_req_le_to_cpu(ans); |
1584 |
if (ans->crc != build_crc((u8*)ans)) { |
1585 |
err("CRC error, ignoring packet"); |
1586 |
goto resubmit; |
1587 |
} |
1588 |
|
1589 |
switch (ans->hermes_rid) { |
1590 |
case BRIDGE_RID_RX: |
1591 |
ndbg("got a data frame"); |
1592 |
__orinoco_ev_rx(net_dev, hw); |
1593 |
break; |
1594 |
case HERMES_INQ_LINKSTATUS: |
1595 |
case HERMES_INQ_TALLIES: |
1596 |
case HERMES_INQ_SCAN: |
1597 |
ndbg("got an INQ frame %s", rid_name(ans->hermes_rid)); |
1598 |
ndump(urb->transfer_buffer, urb->actual_length); |
1599 |
hermes_write_regn(hw, INFOFID, ans->hermes_rid); |
1600 |
__orinoco_ev_info(net_dev, hw); |
1601 |
break; |
1602 |
default: |
1603 |
if (bridge_request_in_callback(dev, urb) == -ENOENT) |
1604 |
err("%s: got unexpected RID: 0x%04X(%s)", __FUNCTION__, |
1605 |
ans->hermes_rid, rid_name(ans->hermes_rid)); |
1606 |
} |
1607 |
resubmit: |
1608 |
//if ((dev->net_dev->flags&IFF_UP) || !dev->read.done){ |
1609 |
if (dev->udev){ |
1610 |
ndbg("resubmiting IN urb"); |
1611 |
bridge_submit_in_urb(dev, NULL); |
1612 |
} else { |
1613 |
dbg("NOT resubmiting IN urb"); |
1614 |
} |
1615 |
exit: |
1616 |
return; |
1617 |
} |
1618 |
static void watchdog_timer_func(u_long data) |
1619 |
{ |
1620 |
struct bridge_priv *dev = (struct bridge_priv *)data; |
1621 |
struct net_device *net_dev = dev->net_dev; |
1622 |
struct orinoco_private *priv = net_dev->priv; |
1623 |
hermes_t *hw = &priv->hw; |
1624 |
unsigned long flags; |
1625 |
int error; |
1626 |
|
1627 |
if (!netif_running(net_dev)){ |
1628 |
/* Some lost timer, of course there is no traffic if the |
1629 |
* interface is not running */ |
1630 |
dbg("!netif_running(net_dev): ignoring timer"); |
1631 |
return; |
1632 |
} |
1633 |
if (orinoco_lock(priv, &flags) != 0) { |
1634 |
dbg("called when hw_unavailable\n"); |
1635 |
mod_timer(&dev->watchdog_timer, jiffies+watchdog_secs*HZ); |
1636 |
return; |
1637 |
} |
1638 |
if(!list_empty(&dev->ctxq.active)){ |
1639 |
err("card stuck? active request (rescheduling)."); |
1640 |
mod_timer(&dev->watchdog_timer, jiffies+watchdog_secs*HZ); |
1641 |
orinoco_unlock(priv, &flags); |
1642 |
return; |
1643 |
} |
1644 |
WARN_ON(!list_empty(&dev->ctxq.pending)); |
1645 |
error = hermes_inquire(hw, HERMES_INQ_TALLIES); |
1646 |
orinoco_unlock(priv, &flags); |
1647 |
err("card stuck? sending an INQ command to get it going (ret:%d)", |
1648 |
error); |
1649 |
|
1650 |
#if 0 |
1651 |
/* The timer get's restarted often enough without this, and resetting |
1652 |
* it here, makes it a little too agresive, altough it does work */ |
1653 |
if(watchdog_secs){ |
1654 |
mod_timer(&dev->watchdog_timer, jiffies+watchdog_secs*HZ); |
1655 |
} |
1656 |
#endif |
1657 |
} |
1658 |
|
1659 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
1660 |
static void * bridge_probe(struct usb_device *udev, unsigned int ifnum, |
1661 |
const struct usb_device_id *id) |
1662 |
{ |
1663 |
struct usb_interface *interface = &udev->actconfig->interface[ifnum]; |
1664 |
char buf[FIRMWARE_NAME_MAX]; |
1665 |
#else |
1666 |
static int bridge_probe(struct usb_interface *interface, |
1667 |
const struct usb_device_id *id) |
1668 |
{ |
1669 |
struct usb_device *udev = interface_to_usbdev(interface); |
1670 |
#endif |
1671 |
struct orinoco_private *priv; |
1672 |
struct net_device *net_dev; |
1673 |
hermes_t *hw; |
1674 |
struct bridge_priv *dev = NULL; |
1675 |
struct usb_interface_descriptor *iface_desc; |
1676 |
struct usb_endpoint_descriptor *ep; |
1677 |
int card_variant = -1; |
1678 |
const struct firmware *fw_entry; |
1679 |
int retval = 0; |
1680 |
|
1681 |
int i; |
1682 |
|
1683 |
/* See if the device offered us matches what we can accept */ |
1684 |
|
1685 |
if (usb_match_id(udev, interface, bridge_varian1_table)) |
1686 |
card_variant = 0; |
1687 |
if (usb_match_id(udev, interface, bridge_varian2_table)) |
1688 |
card_variant = 1; |
1689 |
|
1690 |
if(force_unsupported) |
1691 |
card_variant = -1; |
1692 |
|
1693 |
if (card_variant == -1) { |
1694 |
err("Device is not supported (you may want to set" |
1695 |
" force_unsupported=1)"); |
1696 |
if(!force_unsupported) |
1697 |
return_probe(NULL, -ENODEV); |
1698 |
err("Trying to handle device anyway as requested"); |
1699 |
} |
1700 |
|
1701 |
dbg("ENTER"); |
1702 |
down (&minor_table_mutex); |
1703 |
|
1704 |
net_dev = alloc_orinocodev(sizeof(*dev), NULL); |
1705 |
if (!net_dev){ |
1706 |
err("Couldn't allocate net_dev"); |
1707 |
goto exit; |
1708 |
} |
1709 |
|
1710 |
|
1711 |
priv = net_dev->priv; |
1712 |
hw = &priv->hw; |
1713 |
|
1714 |
dev = priv->card; |
1715 |
dev->net_dev = net_dev; |
1716 |
|
1717 |
|
1718 |
init_MUTEX (&dev->sem); |
1719 |
spin_lock_init(&dev->lock); |
1720 |
dev->watchdog_timer.function = watchdog_timer_func; |
1721 |
dev->watchdog_timer.data = (u_long)dev; |
1722 |
init_timer(&dev->watchdog_timer); |
1723 |
|
1724 |
spin_lock_init(&dev->ctxq.lock); |
1725 |
INIT_LIST_HEAD(&dev->ctxq.pending); |
1726 |
INIT_LIST_HEAD(&dev->ctxq.active); |
1727 |
|
1728 |
dev->udev = udev; |
1729 |
dev->interface = interface; |
1730 |
|
1731 |
hermes_struct_init(hw, (ulong)&dev->hermes_reg_fake, |
1732 |
HERMES_MEM, HERMES_16BIT_REGSPACING); |
1733 |
hw->priv = dev; |
1734 |
|
1735 |
hw->init = bridge_init; |
1736 |
hw->docmd_wait = bridge_docmd_wait; |
1737 |
hw->allocate = bridge_allocate; |
1738 |
hw->read_ltv = bridge_read_ltv; |
1739 |
hw->write_ltv = bridge_write_ltv; |
1740 |
hw->bap_pread = bridge_bap_pread; |
1741 |
hw->bap_pwrite = bridge_bap_pwrite; |
1742 |
//net_dev->init = orinoco_init; |
1743 |
dev->orinoco_stop = net_dev->stop; |
1744 |
net_dev->stop = bridge_netdev_stop; |
1745 |
dev->orinoco_open = net_dev->open; |
1746 |
net_dev->open = bridge_netdev_open; |
1747 |
net_dev->hard_start_xmit = bridge_xmit; |
1748 |
//net_dev->set_multicast_list = NULL; |
1749 |
|
1750 |
priv->irq_no_disable = 1; |
1751 |
|
1752 |
#if 0 |
1753 |
net_dev->base_addr = (ulong)&dev->hermes_reg_fake; |
1754 |
net_dev->irq = -1; |
1755 |
#endif |
1756 |
SET_MODULE_OWNER(net_dev); |
1757 |
strcpy(net_dev->name, "wlan%d"); |
1758 |
|
1759 |
/* set up the endpoint information */ |
1760 |
/* check out the endpoints */ |
1761 |
|
1762 |
iface_desc = get_iface_desc(interface, 0); |
1763 |
for (i = 0; i < iface_desc->bNumEndpoints; ++i) { |
1764 |
ep = get_iface_ep_desc(interface, 0, i); |
1765 |
|
1766 |
if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
1767 |
== USB_DIR_IN) && |
1768 |
((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
1769 |
== USB_ENDPOINT_XFER_BULK)) { |
1770 |
/* we found a bulk in endpoint */ |
1771 |
if (dev->read.urb != NULL) { |
1772 |
warn("Found a second bulk in ep, ignored"); |
1773 |
continue; |
1774 |
} |
1775 |
|
1776 |
dev->read.urb = usb_alloc_urb(0, GFP_KERNEL); |
1777 |
if (!dev->read.urb) { |
1778 |
err("No free urbs available"); |
1779 |
goto error; |
1780 |
} |
1781 |
if (ep->wMaxPacketSize != 64) |
1782 |
warn("bulk in: wMaxPacketSize!= 64"); |
1783 |
dev->read.pipe = ep->bEndpointAddress; |
1784 |
if (dev->read.pipe != (2|USB_DIR_IN)) |
1785 |
warn("bulk in: bEndpointAddress: %d", |
1786 |
dev->read.pipe); |
1787 |
dev->read.urb->transfer_buffer = |
1788 |
kmalloc (BULK_BUF_SIZE, GFP_KERNEL); |
1789 |
if (!dev->read.urb->transfer_buffer) { |
1790 |
err("Couldn't allocate IN buffer"); |
1791 |
goto error; |
1792 |
} |
1793 |
usb_fill_bulk_urb( |
1794 |
dev->read.urb, udev, |
1795 |
usb_sndbulkpipe(udev, ep->bEndpointAddress), |
1796 |
dev->read.urb->transfer_buffer, BULK_BUF_SIZE, |
1797 |
bridge_bulk_in_callback, dev); |
1798 |
} |
1799 |
|
1800 |
if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
1801 |
== USB_DIR_OUT) && |
1802 |
((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
1803 |
== USB_ENDPOINT_XFER_BULK)) { |
1804 |
/* we found a bulk out endpoint */ |
1805 |
if (dev->bap.buf != NULL) { |
1806 |
warn("Found a second bulk out ep, ignored"); |
1807 |
continue; |
1808 |
} |
1809 |
|
1810 |
if (ep->wMaxPacketSize != 64) |
1811 |
warn("bulk out: wMaxPacketSize != 64"); |
1812 |
dev->write.pipe = ep->bEndpointAddress; |
1813 |
if (dev->write.pipe != 2) |
1814 |
warn("bulk in: bEndpointAddress: %d", |
1815 |
dev->read.pipe); |
1816 |
dev->bap.buf = kmalloc (BULK_BUF_SIZE, GFP_KERNEL); |
1817 |
if (!dev->bap.buf) { |
1818 |
err("Couldn't allocate bulk_out_buffer"); |
1819 |
goto error; |
1820 |
} |
1821 |
} |
1822 |
} |
1823 |
if (!dev->bap.buf || !dev->read.urb){ |
1824 |
err("Didn't find the required bulk endpoints"); |
1825 |
goto error; |
1826 |
} |
1827 |
|
1828 |
if(card_variant != -1) |
1829 |
dev->card_variant = card_variant; |
1830 |
else |
1831 |
dev->card_variant = default_card_variant; |
1832 |
|
1833 |
if (request_firmware(&fw_entry, "orinoco_ezusb_fw", |
1834 |
fw_dev_param(dev->udev, buf))==0) { |
1835 |
firmware.size = fw_entry->size; |
1836 |
firmware.code = fw_entry->data; |
1837 |
find_fw_variant_offset(&firmware); |
1838 |
} |
1839 |
if (firmware.size && firmware.code) { |
1840 |
bridge_firmware_download(dev, &firmware); |
1841 |
} else { |
1842 |
err("No firmware to download"); |
1843 |
goto error; |
1844 |
} |
1845 |
|
1846 |
/* If the firmware is already downloaded orinoco.c will call |
1847 |
* bridge_init but if the firmware is not already there, that will make |
1848 |
* the kernel very unstable, so we try initializing here and quit in |
1849 |
* case of error */ |
1850 |
if (bridge_init(hw) < 0){ |
1851 |
err("Couldn't initialize the device"); |
1852 |
err("Firmware may not be downloaded or may be wrong."); |
1853 |
if(card_variant == -1){ |
1854 |
err("Replug the device to pretend it is the other" |
1855 |
" variant"); |
1856 |
default_card_variant = !default_card_variant; |
1857 |
} |
1858 |
goto error; |
1859 |
} |
1860 |
if (card_variant == -1){ |
1861 |
err("Your device variant seams to be 0x%02X, please send this" |
1862 |
" message and the output of 'lsusb -v' to" |
1863 |
" orinoco-usb-devel@ranty.ddts.net", dev->card_variant); |
1864 |
} |
1865 |
|
1866 |
ndbg("registering net_dev"); |
1867 |
if (register_netdev(net_dev) != 0) { |
1868 |
err("%s: register_netdev() failed", __FUNCTION__); |
1869 |
goto error; |
1870 |
} |
1871 |
ndbg("net_dev registered"); |
1872 |
|
1873 |
/* let the user know what node this device is now attached to */ |
1874 |
info ("Orinoco USB device now attached %s", net_dev->name); |
1875 |
goto exit; |
1876 |
|
1877 |
error: |
1878 |
bridge_remove_in_urb(dev); |
1879 |
bridge_delete (dev); |
1880 |
dev = NULL; |
1881 |
retval = -EFAULT; |
1882 |
exit: |
1883 |
if(fw_entry){ |
1884 |
firmware.code = NULL; |
1885 |
firmware.size = 0; |
1886 |
release_firmware(fw_entry); |
1887 |
} |
1888 |
up (&minor_table_mutex); |
1889 |
dbg("EXIT"); |
1890 |
usb_set_intfdata (interface, dev); |
1891 |
return_probe(dev, retval); |
1892 |
} |
1893 |
|
1894 |
|
1895 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
1896 |
static void bridge_disconnect(struct usb_device *udev, void *ptr) |
1897 |
{ |
1898 |
struct bridge_priv *dev = ptr; |
1899 |
#else |
1900 |
static void bridge_disconnect(struct usb_interface *intf) |
1901 |
{ |
1902 |
struct bridge_priv *dev = usb_get_intfdata (intf); |
1903 |
#endif |
1904 |
struct net_device *net_dev = NULL; |
1905 |
struct orinoco_private *priv = NULL; |
1906 |
struct list_head *item; |
1907 |
struct list_head *tmp_item; |
1908 |
unsigned long flags; |
1909 |
int retval; |
1910 |
|
1911 |
usb_set_intfdata (intf, NULL); |
1912 |
|
1913 |
if(!dev){ |
1914 |
dbg("NULL device pointer"); |
1915 |
return; |
1916 |
} |
1917 |
net_dev = dev->net_dev; |
1918 |
priv = net_dev->priv; |
1919 |
down (&minor_table_mutex); |
1920 |
down (&dev->sem); |
1921 |
|
1922 |
|
1923 |
if(in_interrupt()){ |
1924 |
dbg("in_interrupt"); |
1925 |
} |
1926 |
dev->udev = NULL; /* No timer will be rearmed from here */ |
1927 |
//priv->hw_unavailable = 1; |
1928 |
|
1929 |
retval = bridge_remove_in_urb(dev); |
1930 |
if (retval) { |
1931 |
dbg("retval %d status %d", retval, dev->read.urb->status); |
1932 |
} |
1933 |
del_timer_sync(&dev->watchdog_timer); |
1934 |
|
1935 |
restart_list: |
1936 |
spin_lock_irqsave(&dev->ctxq.lock, flags); |
1937 |
list_for_each_safe(item, tmp_item, &dev->ctxq.active){ |
1938 |
struct request_context *ctx; |
1939 |
|
1940 |
ctx = list_entry(item, struct request_context, list); |
1941 |
atomic_inc(&ctx->refcount); |
1942 |
|
1943 |
ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK; |
1944 |
if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) { |
1945 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
1946 |
wait_for_completion(&ctx->done); |
1947 |
} else { |
1948 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
1949 |
} |
1950 |
del_timer_sync(&ctx->timer); |
1951 |
#warning there is an slight chance for the irq handler to be running. |
1952 |
if (!list_empty(&ctx->list)) |
1953 |
bridge_ctx_complete(ctx); |
1954 |
|
1955 |
bridge_request_context_put(ctx); |
1956 |
goto restart_list; |
1957 |
} |
1958 |
spin_unlock_irqrestore(&dev->ctxq.lock, flags); |
1959 |
|
1960 |
list_for_each_safe(item, tmp_item, &dev->ctxq.pending) |
1961 |
bridge_ctx_complete(list_entry(item, |
1962 |
struct request_context, list)); |
1963 |
|
1964 |
if (dev->read.urb->status == -EINPROGRESS) { |
1965 |
err("orinoco_usb: Some URB in progress"); |
1966 |
} |
1967 |
up (&dev->sem); |
1968 |
bridge_delete (dev); |
1969 |
|
1970 |
info("Orinoco USB device now disconnected"); |
1971 |
up (&minor_table_mutex); |
1972 |
} |
1973 |
|
1974 |
static int __init bridge_module_init(void) |
1975 |
{ |
1976 |
int result; |
1977 |
|
1978 |
if(firmware.size){ |
1979 |
find_fw_variant_offset(&firmware); |
1980 |
register_firmware("orinoco_ezusb_fw", firmware.code, |
1981 |
firmware.size); |
1982 |
} |
1983 |
/* register this driver with the USB subsystem */ |
1984 |
result = usb_register(&orinoco_driver); |
1985 |
if (result < 0) { |
1986 |
err("usb_register failed for the " |
1987 |
__FILE__" driver. Error number %d", |
1988 |
result); |
1989 |
return -1; |
1990 |
} |
1991 |
|
1992 |
printk(KERN_INFO "orinoco_usb.c " DRIVER_VERSION |
1993 |
" (Manuel Estrada Sainz <ranty@debian.org>)\n"); |
1994 |
return 0; |
1995 |
} |
1996 |
|
1997 |
|
1998 |
static void __exit bridge_module_exit(void) |
1999 |
{ |
2000 |
/* deregister this driver with the USB subsystem */ |
2001 |
usb_deregister(&orinoco_driver); |
2002 |
} |
2003 |
|
2004 |
|
2005 |
module_init (bridge_module_init); |
2006 |
module_exit (bridge_module_exit); |
2007 |
|
2008 |
MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>"); |
2009 |
MODULE_DESCRIPTION("Orinoco USB Bridge Driver"); |
2010 |
MODULE_LICENSE("GPL"); |
2011 |
|