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