/[orinoco]/orinoco/orinoco_usb.c
ViewVC logotype

Contents of /orinoco/orinoco_usb.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.92 - (show annotations) (download)
Thu Oct 21 22:12:32 2004 UTC (19 years, 7 months ago) by proski
Branch: MAIN
Changes since 1.91: +0 -1 lines
File MIME type: text/plain
Fix compilation - the timeout field in struct urb was removed in Linux 2.6.9.
Reported by Thomas Vollmer <thomas-ml@vollmeronline.de>

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

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26