Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 Broadcom Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 11 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 13 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 14 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kthread.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/ethtool.h> |
| 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/firmware.h> |
| 30 | #include <linux/usb.h> |
Hauke Mehrtens | edb9bc9 | 2012-05-18 20:22:53 +0200 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 32 | #include <net/cfg80211.h> |
| 33 | |
| 34 | #include <defs.h> |
| 35 | #include <brcmu_utils.h> |
| 36 | #include <brcmu_wifi.h> |
| 37 | #include <dhd_bus.h> |
| 38 | #include <dhd_dbg.h> |
| 39 | |
| 40 | #include "usb_rdl.h" |
| 41 | #include "usb.h" |
| 42 | |
| 43 | #define IOCTL_RESP_TIMEOUT 2000 |
| 44 | |
| 45 | #define BRCMF_USB_SYNC_TIMEOUT 300 /* ms */ |
| 46 | #define BRCMF_USB_DLIMAGE_SPINWAIT 100 /* in unit of ms */ |
| 47 | #define BRCMF_USB_DLIMAGE_LIMIT 500 /* spinwait limit (ms) */ |
| 48 | |
| 49 | #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle |
| 50 | has boot up */ |
| 51 | #define BRCMF_USB_RESETCFG_SPINWAIT 1 /* wait after resetcfg (ms) */ |
| 52 | |
| 53 | #define BRCMF_USB_NRXQ 50 |
| 54 | #define BRCMF_USB_NTXQ 50 |
| 55 | |
| 56 | #define CONFIGDESC(usb) (&((usb)->actconfig)->desc) |
| 57 | #define IFPTR(usb, idx) ((usb)->actconfig->interface[(idx)]) |
| 58 | #define IFALTS(usb, idx) (IFPTR((usb), (idx))->altsetting[0]) |
| 59 | #define IFDESC(usb, idx) IFALTS((usb), (idx)).desc |
| 60 | #define IFEPDESC(usb, idx, ep) (IFALTS((usb), (idx)).endpoint[(ep)]).desc |
| 61 | |
| 62 | #define CONTROL_IF 0 |
| 63 | #define BULK_IF 0 |
| 64 | |
| 65 | #define BRCMF_USB_CBCTL_WRITE 0 |
| 66 | #define BRCMF_USB_CBCTL_READ 1 |
| 67 | #define BRCMF_USB_MAX_PKT_SIZE 1600 |
| 68 | |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 69 | #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin" |
Rafał Miłecki | fda8241 | 2012-02-24 07:22:51 +0100 | [diff] [blame] | 70 | #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin" |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 71 | #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin" |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 72 | |
| 73 | enum usbdev_suspend_state { |
| 74 | USBOS_SUSPEND_STATE_DEVICE_ACTIVE = 0, /* Device is busy, won't allow |
| 75 | suspend */ |
| 76 | USBOS_SUSPEND_STATE_SUSPEND_PENDING, /* Device is idle, can be |
| 77 | * suspended. Wating PM to |
| 78 | * suspend the device |
| 79 | */ |
| 80 | USBOS_SUSPEND_STATE_SUSPENDED /* Device suspended */ |
| 81 | }; |
| 82 | |
| 83 | struct brcmf_usb_probe_info { |
| 84 | void *usbdev_info; |
| 85 | struct usb_device *usb; /* USB device pointer from OS */ |
| 86 | uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2; |
| 87 | int intr_size; /* Size of interrupt message */ |
| 88 | int interval; /* Interrupt polling interval */ |
| 89 | int vid; |
| 90 | int pid; |
| 91 | enum usb_device_speed device_speed; |
| 92 | enum usbdev_suspend_state suspend_state; |
| 93 | struct usb_interface *intf; |
| 94 | }; |
| 95 | static struct brcmf_usb_probe_info usbdev_probe_info; |
| 96 | |
| 97 | struct brcmf_usb_image { |
| 98 | void *data; |
| 99 | u32 len; |
| 100 | }; |
| 101 | static struct brcmf_usb_image g_image = { NULL, 0 }; |
| 102 | |
| 103 | struct intr_transfer_buf { |
| 104 | u32 notification; |
| 105 | u32 reserved; |
| 106 | }; |
| 107 | |
| 108 | struct brcmf_usbdev_info { |
| 109 | struct brcmf_usbdev bus_pub; /* MUST BE FIRST */ |
| 110 | spinlock_t qlock; |
| 111 | struct list_head rx_freeq; |
| 112 | struct list_head rx_postq; |
| 113 | struct list_head tx_freeq; |
| 114 | struct list_head tx_postq; |
| 115 | enum usbdev_suspend_state suspend_state; |
| 116 | uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2; |
| 117 | |
| 118 | bool activity; |
| 119 | int rx_low_watermark; |
| 120 | int tx_low_watermark; |
| 121 | int tx_high_watermark; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 122 | int tx_freecount; |
| 123 | bool tx_flowblock; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 124 | |
| 125 | struct brcmf_usbreq *tx_reqs; |
| 126 | struct brcmf_usbreq *rx_reqs; |
| 127 | |
| 128 | u8 *image; /* buffer for combine fw and nvram */ |
| 129 | int image_len; |
| 130 | |
| 131 | wait_queue_head_t wait; |
| 132 | bool waitdone; |
| 133 | int sync_urb_status; |
| 134 | |
| 135 | struct usb_device *usbdev; |
| 136 | struct device *dev; |
| 137 | enum usb_device_speed device_speed; |
| 138 | |
| 139 | int ctl_in_pipe, ctl_out_pipe; |
| 140 | struct urb *ctl_urb; /* URB for control endpoint */ |
| 141 | struct usb_ctrlrequest ctl_write; |
| 142 | struct usb_ctrlrequest ctl_read; |
| 143 | u32 ctl_urb_actual_length; |
| 144 | int ctl_urb_status; |
| 145 | int ctl_completed; |
| 146 | wait_queue_head_t ioctl_resp_wait; |
| 147 | wait_queue_head_t ctrl_wait; |
| 148 | ulong ctl_op; |
| 149 | |
| 150 | bool rxctl_deferrespok; |
| 151 | |
| 152 | struct urb *bulk_urb; /* used for FW download */ |
| 153 | struct urb *intr_urb; /* URB for interrupt endpoint */ |
| 154 | int intr_size; /* Size of interrupt message */ |
| 155 | int interval; /* Interrupt polling interval */ |
| 156 | struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */ |
| 157 | |
| 158 | struct brcmf_usb_probe_info probe_info; |
| 159 | |
| 160 | }; |
| 161 | |
| 162 | static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo, |
| 163 | struct brcmf_usbreq *req); |
| 164 | |
| 165 | MODULE_AUTHOR("Broadcom Corporation"); |
| 166 | MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver."); |
| 167 | MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards"); |
| 168 | MODULE_LICENSE("Dual BSD/GPL"); |
| 169 | |
| 170 | static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev) |
| 171 | { |
| 172 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 173 | return bus_if->bus_priv.usb; |
| 174 | } |
| 175 | |
| 176 | static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev) |
| 177 | { |
| 178 | return brcmf_usb_get_buspub(dev)->devinfo; |
| 179 | } |
| 180 | |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 181 | static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo, |
| 182 | uint *condition, bool *pending) |
| 183 | { |
| 184 | DECLARE_WAITQUEUE(wait, current); |
| 185 | int timeout = IOCTL_RESP_TIMEOUT; |
| 186 | |
| 187 | /* Convert timeout in millsecond to jiffies */ |
| 188 | timeout = msecs_to_jiffies(timeout); |
| 189 | /* Wait until control frame is available */ |
| 190 | add_wait_queue(&devinfo->ioctl_resp_wait, &wait); |
| 191 | set_current_state(TASK_INTERRUPTIBLE); |
| 192 | |
| 193 | smp_mb(); |
| 194 | while (!(*condition) && (!signal_pending(current) && timeout)) { |
| 195 | timeout = schedule_timeout(timeout); |
| 196 | /* Wait until control frame is available */ |
| 197 | smp_mb(); |
| 198 | } |
| 199 | |
| 200 | if (signal_pending(current)) |
| 201 | *pending = true; |
| 202 | |
| 203 | set_current_state(TASK_RUNNING); |
| 204 | remove_wait_queue(&devinfo->ioctl_resp_wait, &wait); |
| 205 | |
| 206 | return timeout; |
| 207 | } |
| 208 | |
| 209 | static int brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo) |
| 210 | { |
| 211 | if (waitqueue_active(&devinfo->ioctl_resp_wait)) |
| 212 | wake_up_interruptible(&devinfo->ioctl_resp_wait); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status) |
| 219 | { |
| 220 | |
| 221 | if (unlikely(devinfo == NULL)) |
| 222 | return; |
| 223 | |
| 224 | if (type == BRCMF_USB_CBCTL_READ) { |
| 225 | if (status == 0) |
| 226 | devinfo->bus_pub.stats.rx_ctlpkts++; |
| 227 | else |
| 228 | devinfo->bus_pub.stats.rx_ctlerrs++; |
| 229 | } else if (type == BRCMF_USB_CBCTL_WRITE) { |
| 230 | if (status == 0) |
| 231 | devinfo->bus_pub.stats.tx_ctlpkts++; |
| 232 | else |
| 233 | devinfo->bus_pub.stats.tx_ctlerrs++; |
| 234 | } |
| 235 | |
| 236 | devinfo->ctl_urb_status = status; |
| 237 | devinfo->ctl_completed = true; |
| 238 | brcmf_usb_ioctl_resp_wake(devinfo); |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | brcmf_usb_ctlread_complete(struct urb *urb) |
| 243 | { |
| 244 | struct brcmf_usbdev_info *devinfo = |
| 245 | (struct brcmf_usbdev_info *)urb->context; |
| 246 | |
| 247 | devinfo->ctl_urb_actual_length = urb->actual_length; |
| 248 | brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ, |
| 249 | urb->status); |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | brcmf_usb_ctlwrite_complete(struct urb *urb) |
| 254 | { |
| 255 | struct brcmf_usbdev_info *devinfo = |
| 256 | (struct brcmf_usbdev_info *)urb->context; |
| 257 | |
| 258 | brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE, |
| 259 | urb->status); |
| 260 | } |
| 261 | |
| 262 | static int brcmf_usb_pnp(struct brcmf_usbdev_info *devinfo, uint state) |
| 263 | { |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int |
| 268 | brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len) |
| 269 | { |
| 270 | int ret; |
| 271 | u16 size; |
| 272 | |
| 273 | if (devinfo == NULL || buf == NULL || |
| 274 | len == 0 || devinfo->ctl_urb == NULL) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | /* If the USB/HSIC bus in sleep state, wake it up */ |
| 278 | if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED) |
| 279 | if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) { |
| 280 | brcmf_dbg(ERROR, "Could not Resume the bus!\n"); |
| 281 | return -EIO; |
| 282 | } |
| 283 | |
| 284 | devinfo->activity = true; |
| 285 | size = len; |
| 286 | devinfo->ctl_write.wLength = cpu_to_le16p(&size); |
| 287 | devinfo->ctl_urb->transfer_buffer_length = size; |
| 288 | devinfo->ctl_urb_status = 0; |
| 289 | devinfo->ctl_urb_actual_length = 0; |
| 290 | |
| 291 | usb_fill_control_urb(devinfo->ctl_urb, |
| 292 | devinfo->usbdev, |
| 293 | devinfo->ctl_out_pipe, |
| 294 | (unsigned char *) &devinfo->ctl_write, |
| 295 | buf, size, |
| 296 | (usb_complete_t)brcmf_usb_ctlwrite_complete, |
| 297 | devinfo); |
| 298 | |
| 299 | ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); |
| 300 | if (ret < 0) |
| 301 | brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret); |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static int |
| 307 | brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len) |
| 308 | { |
| 309 | int ret; |
| 310 | u16 size; |
| 311 | |
| 312 | if ((devinfo == NULL) || (buf == NULL) || (len == 0) |
| 313 | || (devinfo->ctl_urb == NULL)) |
| 314 | return -EINVAL; |
| 315 | |
| 316 | size = len; |
| 317 | devinfo->ctl_read.wLength = cpu_to_le16p(&size); |
| 318 | devinfo->ctl_urb->transfer_buffer_length = size; |
| 319 | |
| 320 | if (devinfo->rxctl_deferrespok) { |
| 321 | /* BMAC model */ |
| 322 | devinfo->ctl_read.bRequestType = USB_DIR_IN |
| 323 | | USB_TYPE_VENDOR | USB_RECIP_INTERFACE; |
| 324 | devinfo->ctl_read.bRequest = DL_DEFER_RESP_OK; |
| 325 | } else { |
| 326 | /* full dongle model */ |
| 327 | devinfo->ctl_read.bRequestType = USB_DIR_IN |
| 328 | | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 329 | devinfo->ctl_read.bRequest = 1; |
| 330 | } |
| 331 | |
| 332 | usb_fill_control_urb(devinfo->ctl_urb, |
| 333 | devinfo->usbdev, |
| 334 | devinfo->ctl_in_pipe, |
| 335 | (unsigned char *) &devinfo->ctl_read, |
| 336 | buf, size, |
| 337 | (usb_complete_t)brcmf_usb_ctlread_complete, |
| 338 | devinfo); |
| 339 | |
| 340 | ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); |
| 341 | if (ret < 0) |
| 342 | brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret); |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len) |
| 348 | { |
| 349 | int err = 0; |
| 350 | int timeout = 0; |
| 351 | bool pending; |
| 352 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); |
| 353 | |
| 354 | if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) { |
| 355 | /* TODO: handle suspend/resume */ |
| 356 | return -EIO; |
| 357 | } |
| 358 | |
| 359 | if (test_and_set_bit(0, &devinfo->ctl_op)) |
| 360 | return -EIO; |
| 361 | |
Hante Meuleman | a77f574 | 2012-08-30 19:42:58 +0200 | [diff] [blame] | 362 | devinfo->ctl_completed = false; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 363 | err = brcmf_usb_send_ctl(devinfo, buf, len); |
| 364 | if (err) { |
| 365 | brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len); |
| 366 | return err; |
| 367 | } |
| 368 | |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 369 | timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed, |
| 370 | &pending); |
| 371 | clear_bit(0, &devinfo->ctl_op); |
| 372 | if (!timeout) { |
| 373 | brcmf_dbg(ERROR, "Txctl wait timed out\n"); |
| 374 | err = -EIO; |
| 375 | } |
| 376 | return err; |
| 377 | } |
| 378 | |
| 379 | static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len) |
| 380 | { |
| 381 | int err = 0; |
| 382 | int timeout = 0; |
| 383 | bool pending; |
| 384 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); |
| 385 | |
| 386 | if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) { |
| 387 | /* TODO: handle suspend/resume */ |
| 388 | return -EIO; |
| 389 | } |
| 390 | if (test_and_set_bit(0, &devinfo->ctl_op)) |
| 391 | return -EIO; |
| 392 | |
| 393 | err = brcmf_usb_recv_ctl(devinfo, buf, len); |
| 394 | if (err) { |
| 395 | brcmf_dbg(ERROR, "fail %d bytes: %d\n", err, len); |
| 396 | return err; |
| 397 | } |
| 398 | devinfo->ctl_completed = false; |
| 399 | timeout = brcmf_usb_ioctl_resp_wait(devinfo, &devinfo->ctl_completed, |
| 400 | &pending); |
| 401 | err = devinfo->ctl_urb_status; |
| 402 | clear_bit(0, &devinfo->ctl_op); |
| 403 | if (!timeout) { |
| 404 | brcmf_dbg(ERROR, "rxctl wait timed out\n"); |
| 405 | err = -EIO; |
| 406 | } |
| 407 | if (!err) |
| 408 | return devinfo->ctl_urb_actual_length; |
| 409 | else |
| 410 | return err; |
| 411 | } |
| 412 | |
| 413 | static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo, |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 414 | struct list_head *q, int *counter) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 415 | { |
| 416 | unsigned long flags; |
| 417 | struct brcmf_usbreq *req; |
| 418 | spin_lock_irqsave(&devinfo->qlock, flags); |
| 419 | if (list_empty(q)) { |
| 420 | spin_unlock_irqrestore(&devinfo->qlock, flags); |
| 421 | return NULL; |
| 422 | } |
| 423 | req = list_entry(q->next, struct brcmf_usbreq, list); |
| 424 | list_del_init(q->next); |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 425 | if (counter) |
| 426 | (*counter)--; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 427 | spin_unlock_irqrestore(&devinfo->qlock, flags); |
| 428 | return req; |
| 429 | |
| 430 | } |
| 431 | |
| 432 | static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo, |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 433 | struct list_head *q, struct brcmf_usbreq *req, |
| 434 | int *counter) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 435 | { |
| 436 | unsigned long flags; |
| 437 | spin_lock_irqsave(&devinfo->qlock, flags); |
| 438 | list_add_tail(&req->list, q); |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 439 | if (counter) |
| 440 | (*counter)++; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 441 | spin_unlock_irqrestore(&devinfo->qlock, flags); |
| 442 | } |
| 443 | |
| 444 | static struct brcmf_usbreq * |
| 445 | brcmf_usbdev_qinit(struct list_head *q, int qsize) |
| 446 | { |
| 447 | int i; |
| 448 | struct brcmf_usbreq *req, *reqs; |
| 449 | |
| 450 | reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC); |
| 451 | if (reqs == NULL) { |
| 452 | brcmf_dbg(ERROR, "fail to allocate memory!\n"); |
| 453 | return NULL; |
| 454 | } |
| 455 | req = reqs; |
| 456 | |
| 457 | for (i = 0; i < qsize; i++) { |
| 458 | req->urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 459 | if (!req->urb) |
| 460 | goto fail; |
| 461 | |
| 462 | INIT_LIST_HEAD(&req->list); |
| 463 | list_add_tail(&req->list, q); |
| 464 | req++; |
| 465 | } |
| 466 | return reqs; |
| 467 | fail: |
| 468 | brcmf_dbg(ERROR, "fail!\n"); |
| 469 | while (!list_empty(q)) { |
| 470 | req = list_entry(q->next, struct brcmf_usbreq, list); |
| 471 | if (req && req->urb) |
| 472 | usb_free_urb(req->urb); |
| 473 | list_del(q->next); |
| 474 | } |
| 475 | return NULL; |
| 476 | |
| 477 | } |
| 478 | |
| 479 | static void brcmf_usb_free_q(struct list_head *q, bool pending) |
| 480 | { |
| 481 | struct brcmf_usbreq *req, *next; |
| 482 | int i = 0; |
| 483 | list_for_each_entry_safe(req, next, q, list) { |
Dan Carpenter | d4ca009 | 2012-02-24 09:22:27 +0300 | [diff] [blame] | 484 | if (!req->urb) { |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 485 | brcmf_dbg(ERROR, "bad req\n"); |
| 486 | break; |
| 487 | } |
| 488 | i++; |
| 489 | if (pending) { |
| 490 | usb_kill_urb(req->urb); |
| 491 | } else { |
| 492 | usb_free_urb(req->urb); |
| 493 | list_del_init(&req->list); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo, |
| 499 | struct brcmf_usbreq *req) |
| 500 | { |
| 501 | unsigned long flags; |
| 502 | |
| 503 | spin_lock_irqsave(&devinfo->qlock, flags); |
| 504 | list_del_init(&req->list); |
| 505 | spin_unlock_irqrestore(&devinfo->qlock, flags); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | static void brcmf_usb_tx_complete(struct urb *urb) |
| 510 | { |
| 511 | struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context; |
| 512 | struct brcmf_usbdev_info *devinfo = req->devinfo; |
| 513 | |
| 514 | brcmf_usb_del_fromq(devinfo, req); |
| 515 | if (urb->status == 0) |
Arend van Spriel | 1d9c179 | 2012-03-02 22:55:47 +0100 | [diff] [blame] | 516 | devinfo->bus_pub.bus->dstats.tx_packets++; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 517 | else |
Arend van Spriel | 1d9c179 | 2012-03-02 22:55:47 +0100 | [diff] [blame] | 518 | devinfo->bus_pub.bus->dstats.tx_errors++; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 519 | |
Arend van Spriel | 01e3331 | 2012-08-30 10:05:34 +0200 | [diff] [blame] | 520 | brcmu_pkt_buf_free_skb(req->skb); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 521 | req->skb = NULL; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 522 | brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount); |
| 523 | if (devinfo->tx_freecount > devinfo->tx_high_watermark && |
| 524 | devinfo->tx_flowblock) { |
| 525 | brcmf_txflowblock(devinfo->dev, false); |
| 526 | devinfo->tx_flowblock = false; |
| 527 | } |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void brcmf_usb_rx_complete(struct urb *urb) |
| 531 | { |
| 532 | struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context; |
| 533 | struct brcmf_usbdev_info *devinfo = req->devinfo; |
| 534 | struct sk_buff *skb; |
| 535 | int ifidx = 0; |
| 536 | |
| 537 | brcmf_usb_del_fromq(devinfo, req); |
| 538 | skb = req->skb; |
| 539 | req->skb = NULL; |
| 540 | |
| 541 | if (urb->status == 0) { |
Arend van Spriel | 1d9c179 | 2012-03-02 22:55:47 +0100 | [diff] [blame] | 542 | devinfo->bus_pub.bus->dstats.rx_packets++; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 543 | } else { |
Arend van Spriel | 1d9c179 | 2012-03-02 22:55:47 +0100 | [diff] [blame] | 544 | devinfo->bus_pub.bus->dstats.rx_errors++; |
Arend van Spriel | 01e3331 | 2012-08-30 10:05:34 +0200 | [diff] [blame] | 545 | brcmu_pkt_buf_free_skb(skb); |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 546 | brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | |
| 550 | if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP) { |
| 551 | skb_put(skb, urb->actual_length); |
| 552 | if (brcmf_proto_hdrpull(devinfo->dev, &ifidx, skb) != 0) { |
| 553 | brcmf_dbg(ERROR, "rx protocol error\n"); |
| 554 | brcmu_pkt_buf_free_skb(skb); |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 555 | brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 556 | devinfo->bus_pub.bus->dstats.rx_errors++; |
| 557 | } else { |
| 558 | brcmf_rx_packet(devinfo->dev, ifidx, skb); |
| 559 | brcmf_usb_rx_refill(devinfo, req); |
| 560 | } |
| 561 | } else { |
Arend van Spriel | 01e3331 | 2012-08-30 10:05:34 +0200 | [diff] [blame] | 562 | brcmu_pkt_buf_free_skb(skb); |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 563 | brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 564 | } |
| 565 | return; |
| 566 | |
| 567 | } |
| 568 | |
| 569 | static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo, |
| 570 | struct brcmf_usbreq *req) |
| 571 | { |
| 572 | struct sk_buff *skb; |
| 573 | int ret; |
| 574 | |
| 575 | if (!req || !devinfo) |
| 576 | return; |
| 577 | |
| 578 | skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu); |
| 579 | if (!skb) { |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 580 | brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 581 | return; |
| 582 | } |
| 583 | req->skb = skb; |
| 584 | |
| 585 | usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe, |
| 586 | skb->data, skb_tailroom(skb), brcmf_usb_rx_complete, |
| 587 | req); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 588 | req->devinfo = devinfo; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 589 | brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 590 | |
| 591 | ret = usb_submit_urb(req->urb, GFP_ATOMIC); |
Hante Meuleman | 2e875ac | 2012-08-30 10:05:36 +0200 | [diff] [blame] | 592 | if (ret) { |
| 593 | brcmf_usb_del_fromq(devinfo, req); |
Arend van Spriel | 01e3331 | 2012-08-30 10:05:34 +0200 | [diff] [blame] | 594 | brcmu_pkt_buf_free_skb(req->skb); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 595 | req->skb = NULL; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 596 | brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 597 | } |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo) |
| 602 | { |
| 603 | struct brcmf_usbreq *req; |
| 604 | |
| 605 | if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) { |
| 606 | brcmf_dbg(ERROR, "bus is not up\n"); |
| 607 | return; |
| 608 | } |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 609 | while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 610 | brcmf_usb_rx_refill(devinfo, req); |
| 611 | } |
| 612 | |
| 613 | static void |
| 614 | brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state) |
| 615 | { |
| 616 | struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus; |
| 617 | int old_state; |
| 618 | |
| 619 | |
| 620 | if (devinfo->bus_pub.state == state) |
| 621 | return; |
| 622 | |
| 623 | old_state = devinfo->bus_pub.state; |
| 624 | brcmf_dbg(TRACE, "dbus state change from %d to to %d\n", |
| 625 | old_state, state); |
| 626 | |
| 627 | /* Don't update state if it's PnP firmware re-download */ |
| 628 | if (state != BCMFMAC_USB_STATE_PNP_FWDL) /* TODO */ |
| 629 | devinfo->bus_pub.state = state; |
| 630 | |
| 631 | if ((old_state == BCMFMAC_USB_STATE_SLEEP) |
| 632 | && (state == BCMFMAC_USB_STATE_UP)) { |
| 633 | brcmf_usb_rx_fill_all(devinfo); |
| 634 | } |
| 635 | |
| 636 | /* update state of upper layer */ |
| 637 | if (state == BCMFMAC_USB_STATE_DOWN) { |
| 638 | brcmf_dbg(INFO, "DBUS is down\n"); |
| 639 | bcmf_bus->state = BRCMF_BUS_DOWN; |
| 640 | } else { |
| 641 | brcmf_dbg(INFO, "DBUS current state=%d\n", state); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | static void |
| 646 | brcmf_usb_intr_complete(struct urb *urb) |
| 647 | { |
| 648 | struct brcmf_usbdev_info *devinfo = |
| 649 | (struct brcmf_usbdev_info *)urb->context; |
| 650 | bool killed; |
| 651 | |
| 652 | if (devinfo == NULL) |
| 653 | return; |
| 654 | |
| 655 | if (unlikely(urb->status)) { |
| 656 | if (devinfo->suspend_state == |
| 657 | USBOS_SUSPEND_STATE_SUSPEND_PENDING) |
| 658 | killed = true; |
| 659 | |
| 660 | if ((urb->status == -ENOENT && (!killed)) |
| 661 | || urb->status == -ESHUTDOWN || |
| 662 | urb->status == -ENODEV) { |
| 663 | brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN) { |
| 668 | brcmf_dbg(ERROR, "intr cb when DBUS down, ignoring\n"); |
| 669 | return; |
| 670 | } |
| 671 | |
| 672 | if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP) |
| 673 | usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC); |
| 674 | } |
| 675 | |
| 676 | static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb) |
| 677 | { |
| 678 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); |
| 679 | struct brcmf_usbreq *req; |
| 680 | int ret; |
| 681 | |
| 682 | if (devinfo->bus_pub.state != BCMFMAC_USB_STATE_UP) { |
| 683 | /* TODO: handle suspend/resume */ |
| 684 | return -EIO; |
| 685 | } |
| 686 | |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 687 | req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq, |
| 688 | &devinfo->tx_freecount); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 689 | if (!req) { |
Hante Meuleman | 2e875ac | 2012-08-30 10:05:36 +0200 | [diff] [blame] | 690 | brcmu_pkt_buf_free_skb(skb); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 691 | brcmf_dbg(ERROR, "no req to send\n"); |
| 692 | return -ENOMEM; |
| 693 | } |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 694 | |
| 695 | req->skb = skb; |
| 696 | req->devinfo = devinfo; |
| 697 | usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe, |
| 698 | skb->data, skb->len, brcmf_usb_tx_complete, req); |
| 699 | req->urb->transfer_flags |= URB_ZERO_PACKET; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 700 | brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 701 | ret = usb_submit_urb(req->urb, GFP_ATOMIC); |
Hante Meuleman | 2e875ac | 2012-08-30 10:05:36 +0200 | [diff] [blame] | 702 | if (ret) { |
| 703 | brcmf_dbg(ERROR, "brcmf_usb_tx usb_submit_urb FAILED\n"); |
| 704 | brcmf_usb_del_fromq(devinfo, req); |
| 705 | brcmu_pkt_buf_free_skb(req->skb); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 706 | req->skb = NULL; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 707 | brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, |
| 708 | &devinfo->tx_freecount); |
| 709 | } else { |
| 710 | if (devinfo->tx_freecount < devinfo->tx_low_watermark && |
| 711 | !devinfo->tx_flowblock) { |
| 712 | brcmf_txflowblock(dev, true); |
| 713 | devinfo->tx_flowblock = true; |
| 714 | } |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | static int brcmf_usb_up(struct device *dev) |
| 722 | { |
| 723 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); |
| 724 | u16 ifnum; |
| 725 | |
Dan Carpenter | d4ca009 | 2012-02-24 09:22:27 +0300 | [diff] [blame] | 726 | if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_UP) |
| 727 | return 0; |
| 728 | |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 729 | /* If the USB/HSIC bus in sleep state, wake it up */ |
| 730 | if (devinfo->suspend_state == USBOS_SUSPEND_STATE_SUSPENDED) { |
| 731 | if (brcmf_usb_pnp(devinfo, BCMFMAC_USB_PNP_RESUME) != 0) { |
| 732 | brcmf_dbg(ERROR, "Could not Resume the bus!\n"); |
| 733 | return -EIO; |
| 734 | } |
| 735 | } |
| 736 | devinfo->activity = true; |
| 737 | |
| 738 | /* Success, indicate devinfo is fully up */ |
| 739 | brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_UP); |
| 740 | |
| 741 | if (devinfo->intr_urb) { |
| 742 | int ret; |
| 743 | |
| 744 | usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev, |
| 745 | devinfo->intr_pipe, |
| 746 | &devinfo->intr, |
| 747 | devinfo->intr_size, |
| 748 | (usb_complete_t)brcmf_usb_intr_complete, |
| 749 | devinfo, |
| 750 | devinfo->interval); |
| 751 | |
| 752 | ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC); |
| 753 | if (ret) { |
| 754 | brcmf_dbg(ERROR, "USB_SUBMIT_URB failed with status %d\n", |
| 755 | ret); |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (devinfo->ctl_urb) { |
| 761 | devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0); |
| 762 | devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0); |
| 763 | |
| 764 | ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber; |
| 765 | |
| 766 | /* CTL Write */ |
| 767 | devinfo->ctl_write.bRequestType = |
| 768 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 769 | devinfo->ctl_write.bRequest = 0; |
| 770 | devinfo->ctl_write.wValue = cpu_to_le16(0); |
| 771 | devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum); |
| 772 | |
| 773 | /* CTL Read */ |
| 774 | devinfo->ctl_read.bRequestType = |
| 775 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 776 | devinfo->ctl_read.bRequest = 1; |
| 777 | devinfo->ctl_read.wValue = cpu_to_le16(0); |
| 778 | devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum); |
| 779 | } |
| 780 | brcmf_usb_rx_fill_all(devinfo); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static void brcmf_usb_down(struct device *dev) |
| 785 | { |
| 786 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); |
| 787 | |
| 788 | if (devinfo == NULL) |
| 789 | return; |
| 790 | |
| 791 | brcmf_dbg(TRACE, "enter\n"); |
| 792 | if (devinfo->bus_pub.state == BCMFMAC_USB_STATE_DOWN) |
| 793 | return; |
| 794 | |
| 795 | brcmf_usb_state_change(devinfo, BCMFMAC_USB_STATE_DOWN); |
| 796 | if (devinfo->intr_urb) |
| 797 | usb_kill_urb(devinfo->intr_urb); |
| 798 | |
| 799 | if (devinfo->ctl_urb) |
| 800 | usb_kill_urb(devinfo->ctl_urb); |
| 801 | |
| 802 | if (devinfo->bulk_urb) |
| 803 | usb_kill_urb(devinfo->bulk_urb); |
| 804 | brcmf_usb_free_q(&devinfo->tx_postq, true); |
| 805 | |
| 806 | brcmf_usb_free_q(&devinfo->rx_postq, true); |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | brcmf_usb_sync_wait(struct brcmf_usbdev_info *devinfo, u16 time) |
| 811 | { |
| 812 | int ret; |
| 813 | int err = 0; |
| 814 | int ms = time; |
| 815 | |
| 816 | ret = wait_event_interruptible_timeout(devinfo->wait, |
| 817 | devinfo->waitdone == true, (ms * HZ / 1000)); |
| 818 | |
| 819 | if ((devinfo->waitdone == false) || (devinfo->sync_urb_status)) { |
| 820 | brcmf_dbg(ERROR, "timeout(%d) or urb err=%d\n", |
| 821 | ret, devinfo->sync_urb_status); |
| 822 | err = -EINVAL; |
| 823 | } |
| 824 | devinfo->waitdone = false; |
| 825 | return err; |
| 826 | } |
| 827 | |
| 828 | static void |
| 829 | brcmf_usb_sync_complete(struct urb *urb) |
| 830 | { |
| 831 | struct brcmf_usbdev_info *devinfo = |
| 832 | (struct brcmf_usbdev_info *)urb->context; |
| 833 | |
| 834 | devinfo->waitdone = true; |
| 835 | wake_up_interruptible(&devinfo->wait); |
| 836 | devinfo->sync_urb_status = urb->status; |
| 837 | } |
| 838 | |
| 839 | static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd, |
| 840 | void *buffer, int buflen) |
| 841 | { |
| 842 | int ret = 0; |
| 843 | char *tmpbuf; |
| 844 | u16 size; |
| 845 | |
| 846 | if ((!devinfo) || (devinfo->ctl_urb == NULL)) |
| 847 | return false; |
| 848 | |
| 849 | tmpbuf = kmalloc(buflen, GFP_ATOMIC); |
| 850 | if (!tmpbuf) |
| 851 | return false; |
| 852 | |
| 853 | size = buflen; |
| 854 | devinfo->ctl_urb->transfer_buffer_length = size; |
| 855 | |
| 856 | devinfo->ctl_read.wLength = cpu_to_le16p(&size); |
| 857 | devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR | |
| 858 | USB_RECIP_INTERFACE; |
| 859 | devinfo->ctl_read.bRequest = cmd; |
| 860 | |
| 861 | usb_fill_control_urb(devinfo->ctl_urb, |
| 862 | devinfo->usbdev, |
| 863 | usb_rcvctrlpipe(devinfo->usbdev, 0), |
| 864 | (unsigned char *) &devinfo->ctl_read, |
| 865 | (void *) tmpbuf, size, |
| 866 | (usb_complete_t)brcmf_usb_sync_complete, devinfo); |
| 867 | |
| 868 | ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); |
| 869 | if (ret < 0) { |
| 870 | brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret); |
| 871 | kfree(tmpbuf); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT); |
| 876 | memcpy(buffer, tmpbuf, buflen); |
| 877 | kfree(tmpbuf); |
| 878 | |
| 879 | return (ret == 0); |
| 880 | } |
| 881 | |
| 882 | static bool |
| 883 | brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo) |
| 884 | { |
| 885 | struct bootrom_id_le id; |
| 886 | u32 chipid, chiprev; |
| 887 | |
| 888 | brcmf_dbg(TRACE, "enter\n"); |
| 889 | |
| 890 | if (devinfo == NULL) |
| 891 | return false; |
| 892 | |
| 893 | /* Check if firmware downloaded already by querying runtime ID */ |
| 894 | id.chip = cpu_to_le32(0xDEAD); |
| 895 | brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, |
| 896 | sizeof(struct bootrom_id_le)); |
| 897 | |
| 898 | chipid = le32_to_cpu(id.chip); |
| 899 | chiprev = le32_to_cpu(id.chiprev); |
| 900 | |
| 901 | if ((chipid & 0x4300) == 0x4300) |
| 902 | brcmf_dbg(INFO, "chip %x rev 0x%x\n", chipid, chiprev); |
| 903 | else |
| 904 | brcmf_dbg(INFO, "chip %d rev 0x%x\n", chipid, chiprev); |
| 905 | if (chipid == BRCMF_POSTBOOT_ID) { |
| 906 | brcmf_dbg(INFO, "firmware already downloaded\n"); |
| 907 | brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, |
| 908 | sizeof(struct bootrom_id_le)); |
| 909 | return false; |
| 910 | } else { |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 911 | devinfo->bus_pub.devid = chipid; |
| 912 | devinfo->bus_pub.chiprev = chiprev; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 913 | } |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | static int |
| 918 | brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo) |
| 919 | { |
| 920 | struct bootrom_id_le id; |
| 921 | u16 wait = 0, wait_time; |
| 922 | |
| 923 | brcmf_dbg(TRACE, "enter\n"); |
| 924 | |
| 925 | if (devinfo == NULL) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | /* Give dongle chance to boot */ |
| 929 | wait_time = BRCMF_USB_DLIMAGE_SPINWAIT; |
| 930 | while (wait < BRCMF_USB_DLIMAGE_LIMIT) { |
| 931 | mdelay(wait_time); |
| 932 | wait += wait_time; |
| 933 | id.chip = cpu_to_le32(0xDEAD); /* Get the ID */ |
| 934 | brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, |
| 935 | sizeof(struct bootrom_id_le)); |
| 936 | if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) { |
| 941 | brcmf_dbg(INFO, "download done %d ms postboot chip 0x%x/rev 0x%x\n", |
| 942 | wait, le32_to_cpu(id.chip), le32_to_cpu(id.chiprev)); |
| 943 | |
| 944 | brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, |
| 945 | sizeof(struct bootrom_id_le)); |
| 946 | |
| 947 | /* XXX this wait may not be necessary */ |
| 948 | mdelay(BRCMF_USB_RESETCFG_SPINWAIT); |
| 949 | return 0; |
| 950 | } else { |
| 951 | brcmf_dbg(ERROR, "Cannot talk to Dongle. Firmware is not UP, %d ms\n", |
| 952 | wait); |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | |
| 958 | static int |
| 959 | brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len) |
| 960 | { |
| 961 | int ret; |
| 962 | |
| 963 | if ((devinfo == NULL) || (devinfo->bulk_urb == NULL)) |
| 964 | return -EINVAL; |
| 965 | |
| 966 | /* Prepare the URB */ |
| 967 | usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev, |
| 968 | devinfo->tx_pipe, buffer, len, |
| 969 | (usb_complete_t)brcmf_usb_sync_complete, devinfo); |
| 970 | |
| 971 | devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET; |
| 972 | |
| 973 | ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC); |
| 974 | if (ret) { |
| 975 | brcmf_dbg(ERROR, "usb_submit_urb failed %d\n", ret); |
| 976 | return ret; |
| 977 | } |
| 978 | ret = brcmf_usb_sync_wait(devinfo, BRCMF_USB_SYNC_TIMEOUT); |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | static int |
| 983 | brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen) |
| 984 | { |
| 985 | unsigned int sendlen, sent, dllen; |
| 986 | char *bulkchunk = NULL, *dlpos; |
| 987 | struct rdl_state_le state; |
| 988 | u32 rdlstate, rdlbytes; |
| 989 | int err = 0; |
| 990 | brcmf_dbg(TRACE, "fw %p, len %d\n", fw, fwlen); |
| 991 | |
| 992 | bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC); |
| 993 | if (bulkchunk == NULL) { |
| 994 | err = -ENOMEM; |
| 995 | goto fail; |
| 996 | } |
| 997 | |
| 998 | /* 1) Prepare USB boot loader for runtime image */ |
| 999 | brcmf_usb_dl_cmd(devinfo, DL_START, &state, |
| 1000 | sizeof(struct rdl_state_le)); |
| 1001 | |
| 1002 | rdlstate = le32_to_cpu(state.state); |
| 1003 | rdlbytes = le32_to_cpu(state.bytes); |
| 1004 | |
| 1005 | /* 2) Check we are in the Waiting state */ |
| 1006 | if (rdlstate != DL_WAITING) { |
| 1007 | brcmf_dbg(ERROR, "Failed to DL_START\n"); |
| 1008 | err = -EINVAL; |
| 1009 | goto fail; |
| 1010 | } |
| 1011 | sent = 0; |
| 1012 | dlpos = fw; |
| 1013 | dllen = fwlen; |
| 1014 | |
| 1015 | /* Get chip id and rev */ |
| 1016 | while (rdlbytes != dllen) { |
| 1017 | /* Wait until the usb device reports it received all |
| 1018 | * the bytes we sent */ |
| 1019 | if ((rdlbytes == sent) && (rdlbytes != dllen)) { |
| 1020 | if ((dllen-sent) < RDL_CHUNK) |
| 1021 | sendlen = dllen-sent; |
| 1022 | else |
| 1023 | sendlen = RDL_CHUNK; |
| 1024 | |
| 1025 | /* simply avoid having to send a ZLP by ensuring we |
| 1026 | * never have an even |
| 1027 | * multiple of 64 |
| 1028 | */ |
| 1029 | if (!(sendlen % 64)) |
| 1030 | sendlen -= 4; |
| 1031 | |
| 1032 | /* send data */ |
| 1033 | memcpy(bulkchunk, dlpos, sendlen); |
| 1034 | if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk, |
| 1035 | sendlen)) { |
| 1036 | brcmf_dbg(ERROR, "send_bulk failed\n"); |
| 1037 | err = -EINVAL; |
| 1038 | goto fail; |
| 1039 | } |
| 1040 | |
| 1041 | dlpos += sendlen; |
| 1042 | sent += sendlen; |
| 1043 | } |
| 1044 | if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, |
| 1045 | sizeof(struct rdl_state_le))) { |
| 1046 | brcmf_dbg(ERROR, "DL_GETSTATE Failed xxxx\n"); |
| 1047 | err = -EINVAL; |
| 1048 | goto fail; |
| 1049 | } |
| 1050 | |
| 1051 | rdlstate = le32_to_cpu(state.state); |
| 1052 | rdlbytes = le32_to_cpu(state.bytes); |
| 1053 | |
| 1054 | /* restart if an error is reported */ |
| 1055 | if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) { |
| 1056 | brcmf_dbg(ERROR, "Bad Hdr or Bad CRC state %d\n", |
| 1057 | rdlstate); |
| 1058 | err = -EINVAL; |
| 1059 | goto fail; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | fail: |
| 1064 | kfree(bulkchunk); |
| 1065 | brcmf_dbg(TRACE, "err=%d\n", err); |
| 1066 | return err; |
| 1067 | } |
| 1068 | |
| 1069 | static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len) |
| 1070 | { |
| 1071 | int err; |
| 1072 | |
| 1073 | brcmf_dbg(TRACE, "enter\n"); |
| 1074 | |
| 1075 | if (devinfo == NULL) |
| 1076 | return -EINVAL; |
| 1077 | |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1078 | if (devinfo->bus_pub.devid == 0xDEAD) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1079 | return -EINVAL; |
| 1080 | |
| 1081 | err = brcmf_usb_dl_writeimage(devinfo, fw, len); |
| 1082 | if (err == 0) |
| 1083 | devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_DONE; |
| 1084 | else |
| 1085 | devinfo->bus_pub.state = BCMFMAC_USB_STATE_DL_PENDING; |
| 1086 | brcmf_dbg(TRACE, "exit: err=%d\n", err); |
| 1087 | |
| 1088 | return err; |
| 1089 | } |
| 1090 | |
| 1091 | static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo) |
| 1092 | { |
| 1093 | struct rdl_state_le state; |
| 1094 | |
| 1095 | brcmf_dbg(TRACE, "enter\n"); |
| 1096 | if (!devinfo) |
| 1097 | return -EINVAL; |
| 1098 | |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1099 | if (devinfo->bus_pub.devid == 0xDEAD) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1100 | return -EINVAL; |
| 1101 | |
| 1102 | /* Check we are runnable */ |
| 1103 | brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, |
| 1104 | sizeof(struct rdl_state_le)); |
| 1105 | |
| 1106 | /* Start the image */ |
| 1107 | if (state.state == cpu_to_le32(DL_RUNNABLE)) { |
| 1108 | if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state, |
| 1109 | sizeof(struct rdl_state_le))) |
| 1110 | return -ENODEV; |
| 1111 | if (brcmf_usb_resetcfg(devinfo)) |
| 1112 | return -ENODEV; |
| 1113 | /* The Dongle may go for re-enumeration. */ |
| 1114 | } else { |
| 1115 | brcmf_dbg(ERROR, "Dongle not runnable\n"); |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | brcmf_dbg(TRACE, "exit\n"); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | static bool brcmf_usb_chip_support(int chipid, int chiprev) |
| 1123 | { |
| 1124 | switch(chipid) { |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 1125 | case 43143: |
| 1126 | return true; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1127 | case 43235: |
| 1128 | case 43236: |
| 1129 | case 43238: |
| 1130 | return (chiprev == 3); |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1131 | case 43242: |
| 1132 | return true; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1133 | default: |
| 1134 | break; |
| 1135 | } |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
| 1139 | static int |
| 1140 | brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo) |
| 1141 | { |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1142 | int devid, chiprev; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1143 | int err; |
| 1144 | |
| 1145 | brcmf_dbg(TRACE, "enter\n"); |
| 1146 | if (devinfo == NULL) |
| 1147 | return -ENODEV; |
| 1148 | |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1149 | devid = devinfo->bus_pub.devid; |
| 1150 | chiprev = devinfo->bus_pub.chiprev; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1151 | |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1152 | if (!brcmf_usb_chip_support(devid, chiprev)) { |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1153 | brcmf_dbg(ERROR, "unsupported chip %d rev %d\n", |
Arend van Spriel | ac94f19 | 2012-03-02 22:55:46 +0100 | [diff] [blame] | 1154 | devid, chiprev); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | |
| 1158 | if (!devinfo->image) { |
| 1159 | brcmf_dbg(ERROR, "No firmware!\n"); |
| 1160 | return -ENOENT; |
| 1161 | } |
| 1162 | |
| 1163 | err = brcmf_usb_dlstart(devinfo, |
| 1164 | devinfo->image, devinfo->image_len); |
| 1165 | if (err == 0) |
| 1166 | err = brcmf_usb_dlrun(devinfo); |
| 1167 | return err; |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | static void brcmf_usb_detach(const struct brcmf_usbdev *bus_pub) |
| 1172 | { |
| 1173 | struct brcmf_usbdev_info *devinfo = |
| 1174 | (struct brcmf_usbdev_info *)bus_pub; |
| 1175 | |
| 1176 | brcmf_dbg(TRACE, "devinfo %p\n", devinfo); |
| 1177 | |
| 1178 | /* store the image globally */ |
| 1179 | g_image.data = devinfo->image; |
| 1180 | g_image.len = devinfo->image_len; |
| 1181 | |
| 1182 | /* free the URBS */ |
| 1183 | brcmf_usb_free_q(&devinfo->rx_freeq, false); |
| 1184 | brcmf_usb_free_q(&devinfo->tx_freeq, false); |
| 1185 | |
| 1186 | usb_free_urb(devinfo->intr_urb); |
| 1187 | usb_free_urb(devinfo->ctl_urb); |
| 1188 | usb_free_urb(devinfo->bulk_urb); |
| 1189 | |
| 1190 | kfree(devinfo->tx_reqs); |
| 1191 | kfree(devinfo->rx_reqs); |
| 1192 | kfree(devinfo); |
| 1193 | } |
| 1194 | |
| 1195 | #define TRX_MAGIC 0x30524448 /* "HDR0" */ |
| 1196 | #define TRX_VERSION 1 /* Version 1 */ |
| 1197 | #define TRX_MAX_LEN 0x3B0000 /* Max length */ |
| 1198 | #define TRX_NO_HEADER 1 /* Do not write TRX header */ |
| 1199 | #define TRX_MAX_OFFSET 3 /* Max number of individual files */ |
| 1200 | #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */ |
| 1201 | |
| 1202 | struct trx_header_le { |
| 1203 | __le32 magic; /* "HDR0" */ |
| 1204 | __le32 len; /* Length of file including header */ |
| 1205 | __le32 crc32; /* CRC from flag_version to end of file */ |
| 1206 | __le32 flag_version; /* 0:15 flags, 16:31 version */ |
| 1207 | __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of |
| 1208 | * header */ |
| 1209 | }; |
| 1210 | |
| 1211 | static int check_file(const u8 *headers) |
| 1212 | { |
| 1213 | struct trx_header_le *trx; |
| 1214 | int actual_len = -1; |
| 1215 | |
| 1216 | /* Extract trx header */ |
| 1217 | trx = (struct trx_header_le *) headers; |
| 1218 | if (trx->magic != cpu_to_le32(TRX_MAGIC)) |
| 1219 | return -1; |
| 1220 | |
| 1221 | headers += sizeof(struct trx_header_le); |
| 1222 | |
| 1223 | if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) { |
| 1224 | actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]); |
| 1225 | return actual_len + sizeof(struct trx_header_le); |
| 1226 | } |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo) |
| 1231 | { |
| 1232 | s8 *fwname; |
| 1233 | const struct firmware *fw; |
| 1234 | int err; |
| 1235 | |
| 1236 | devinfo->image = g_image.data; |
| 1237 | devinfo->image_len = g_image.len; |
| 1238 | |
| 1239 | /* |
| 1240 | * if we have an image we can leave here. |
| 1241 | */ |
| 1242 | if (devinfo->image) |
| 1243 | return 0; |
| 1244 | |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1245 | switch (devinfo->bus_pub.devid) { |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 1246 | case 43143: |
| 1247 | fwname = BRCMF_USB_43143_FW_NAME; |
| 1248 | break; |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1249 | case 43235: |
| 1250 | case 43236: |
| 1251 | case 43238: |
| 1252 | fwname = BRCMF_USB_43236_FW_NAME; |
| 1253 | break; |
| 1254 | case 43242: |
| 1255 | fwname = BRCMF_USB_43242_FW_NAME; |
| 1256 | break; |
| 1257 | default: |
| 1258 | return -EINVAL; |
| 1259 | break; |
| 1260 | } |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1261 | |
| 1262 | err = request_firmware(&fw, fwname, devinfo->dev); |
| 1263 | if (!fw) { |
| 1264 | brcmf_dbg(ERROR, "fail to request firmware %s\n", fwname); |
| 1265 | return err; |
| 1266 | } |
| 1267 | if (check_file(fw->data) < 0) { |
| 1268 | brcmf_dbg(ERROR, "invalid firmware %s\n", fwname); |
| 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | |
Hauke Mehrtens | edb9bc9 | 2012-05-18 20:22:53 +0200 | [diff] [blame] | 1272 | devinfo->image = vmalloc(fw->size); /* plus nvram */ |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1273 | if (!devinfo->image) |
| 1274 | return -ENOMEM; |
| 1275 | |
| 1276 | memcpy(devinfo->image, fw->data, fw->size); |
| 1277 | devinfo->image_len = fw->size; |
| 1278 | |
| 1279 | release_firmware(fw); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | static |
| 1285 | struct brcmf_usbdev *brcmf_usb_attach(int nrxq, int ntxq, struct device *dev) |
| 1286 | { |
| 1287 | struct brcmf_usbdev_info *devinfo; |
| 1288 | |
| 1289 | devinfo = kzalloc(sizeof(struct brcmf_usbdev_info), GFP_ATOMIC); |
| 1290 | if (devinfo == NULL) |
| 1291 | return NULL; |
| 1292 | |
| 1293 | devinfo->bus_pub.nrxq = nrxq; |
| 1294 | devinfo->rx_low_watermark = nrxq / 2; |
| 1295 | devinfo->bus_pub.devinfo = devinfo; |
| 1296 | devinfo->bus_pub.ntxq = ntxq; |
| 1297 | |
| 1298 | /* flow control when too many tx urbs posted */ |
| 1299 | devinfo->tx_low_watermark = ntxq / 4; |
| 1300 | devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3; |
| 1301 | devinfo->dev = dev; |
| 1302 | devinfo->usbdev = usbdev_probe_info.usb; |
| 1303 | devinfo->tx_pipe = usbdev_probe_info.tx_pipe; |
| 1304 | devinfo->rx_pipe = usbdev_probe_info.rx_pipe; |
| 1305 | devinfo->rx_pipe2 = usbdev_probe_info.rx_pipe2; |
| 1306 | devinfo->intr_pipe = usbdev_probe_info.intr_pipe; |
| 1307 | |
| 1308 | devinfo->interval = usbdev_probe_info.interval; |
| 1309 | devinfo->intr_size = usbdev_probe_info.intr_size; |
| 1310 | |
| 1311 | memcpy(&devinfo->probe_info, &usbdev_probe_info, |
| 1312 | sizeof(struct brcmf_usb_probe_info)); |
| 1313 | devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE; |
| 1314 | |
| 1315 | /* Initialize other structure content */ |
| 1316 | init_waitqueue_head(&devinfo->ioctl_resp_wait); |
| 1317 | |
| 1318 | /* Initialize the spinlocks */ |
| 1319 | spin_lock_init(&devinfo->qlock); |
| 1320 | |
| 1321 | INIT_LIST_HEAD(&devinfo->rx_freeq); |
| 1322 | INIT_LIST_HEAD(&devinfo->rx_postq); |
| 1323 | |
| 1324 | INIT_LIST_HEAD(&devinfo->tx_freeq); |
| 1325 | INIT_LIST_HEAD(&devinfo->tx_postq); |
| 1326 | |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 1327 | devinfo->tx_flowblock = false; |
| 1328 | |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1329 | devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq); |
| 1330 | if (!devinfo->rx_reqs) |
| 1331 | goto error; |
| 1332 | |
| 1333 | devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq); |
| 1334 | if (!devinfo->tx_reqs) |
| 1335 | goto error; |
Hante Meuleman | c6ab429 | 2012-09-11 21:18:48 +0200 | [diff] [blame^] | 1336 | devinfo->tx_freecount = ntxq; |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1337 | |
| 1338 | devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1339 | if (!devinfo->intr_urb) { |
| 1340 | brcmf_dbg(ERROR, "usb_alloc_urb (intr) failed\n"); |
| 1341 | goto error; |
| 1342 | } |
| 1343 | devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1344 | if (!devinfo->ctl_urb) { |
| 1345 | brcmf_dbg(ERROR, "usb_alloc_urb (ctl) failed\n"); |
| 1346 | goto error; |
| 1347 | } |
| 1348 | devinfo->rxctl_deferrespok = 0; |
| 1349 | |
| 1350 | devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1351 | if (!devinfo->bulk_urb) { |
| 1352 | brcmf_dbg(ERROR, "usb_alloc_urb (bulk) failed\n"); |
| 1353 | goto error; |
| 1354 | } |
| 1355 | |
| 1356 | init_waitqueue_head(&devinfo->wait); |
| 1357 | if (!brcmf_usb_dlneeded(devinfo)) |
| 1358 | return &devinfo->bus_pub; |
| 1359 | |
| 1360 | brcmf_dbg(TRACE, "start fw downloading\n"); |
| 1361 | if (brcmf_usb_get_fw(devinfo)) |
| 1362 | goto error; |
| 1363 | |
| 1364 | if (brcmf_usb_fw_download(devinfo)) |
| 1365 | goto error; |
| 1366 | |
| 1367 | return &devinfo->bus_pub; |
| 1368 | |
| 1369 | error: |
| 1370 | brcmf_dbg(ERROR, "failed!\n"); |
| 1371 | brcmf_usb_detach(&devinfo->bus_pub); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | |
| 1375 | static int brcmf_usb_probe_cb(struct device *dev, const char *desc, |
| 1376 | u32 bustype, u32 hdrlen) |
| 1377 | { |
| 1378 | struct brcmf_bus *bus = NULL; |
| 1379 | struct brcmf_usbdev *bus_pub = NULL; |
| 1380 | int ret; |
| 1381 | |
| 1382 | |
| 1383 | bus_pub = brcmf_usb_attach(BRCMF_USB_NRXQ, BRCMF_USB_NTXQ, dev); |
| 1384 | if (!bus_pub) { |
| 1385 | ret = -ENODEV; |
| 1386 | goto fail; |
| 1387 | } |
| 1388 | |
| 1389 | bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC); |
| 1390 | if (!bus) { |
| 1391 | ret = -ENOMEM; |
| 1392 | goto fail; |
| 1393 | } |
| 1394 | |
| 1395 | bus_pub->bus = bus; |
| 1396 | bus->brcmf_bus_txdata = brcmf_usb_tx; |
| 1397 | bus->brcmf_bus_init = brcmf_usb_up; |
| 1398 | bus->brcmf_bus_stop = brcmf_usb_down; |
| 1399 | bus->brcmf_bus_txctl = brcmf_usb_tx_ctlpkt; |
| 1400 | bus->brcmf_bus_rxctl = brcmf_usb_rx_ctlpkt; |
| 1401 | bus->type = bustype; |
| 1402 | bus->bus_priv.usb = bus_pub; |
| 1403 | dev_set_drvdata(dev, bus); |
| 1404 | |
| 1405 | /* Attach to the common driver interface */ |
| 1406 | ret = brcmf_attach(hdrlen, dev); |
| 1407 | if (ret) { |
| 1408 | brcmf_dbg(ERROR, "dhd_attach failed\n"); |
| 1409 | goto fail; |
| 1410 | } |
| 1411 | |
| 1412 | ret = brcmf_bus_start(dev); |
| 1413 | if (ret == -ENOLINK) { |
| 1414 | brcmf_dbg(ERROR, "dongle is not responding\n"); |
| 1415 | brcmf_detach(dev); |
| 1416 | goto fail; |
| 1417 | } |
| 1418 | |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1419 | return 0; |
| 1420 | fail: |
| 1421 | /* Release resources in reverse order */ |
| 1422 | if (bus_pub) |
| 1423 | brcmf_usb_detach(bus_pub); |
| 1424 | kfree(bus); |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | static void |
| 1429 | brcmf_usb_disconnect_cb(struct brcmf_usbdev *bus_pub) |
| 1430 | { |
| 1431 | if (!bus_pub) |
| 1432 | return; |
| 1433 | brcmf_dbg(TRACE, "enter: bus_pub %p\n", bus_pub); |
| 1434 | |
| 1435 | brcmf_detach(bus_pub->devinfo->dev); |
| 1436 | kfree(bus_pub->bus); |
| 1437 | brcmf_usb_detach(bus_pub); |
| 1438 | |
| 1439 | } |
| 1440 | |
| 1441 | static int |
| 1442 | brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 1443 | { |
| 1444 | int ep; |
| 1445 | struct usb_endpoint_descriptor *endpoint; |
| 1446 | int ret = 0; |
| 1447 | struct usb_device *usb = interface_to_usbdev(intf); |
| 1448 | int num_of_eps; |
| 1449 | u8 endpoint_num; |
| 1450 | |
| 1451 | brcmf_dbg(TRACE, "enter\n"); |
| 1452 | |
| 1453 | usbdev_probe_info.usb = usb; |
| 1454 | usbdev_probe_info.intf = intf; |
| 1455 | |
| 1456 | if (id != NULL) { |
| 1457 | usbdev_probe_info.vid = id->idVendor; |
| 1458 | usbdev_probe_info.pid = id->idProduct; |
| 1459 | } |
| 1460 | |
| 1461 | usb_set_intfdata(intf, &usbdev_probe_info); |
| 1462 | |
| 1463 | /* Check that the device supports only one configuration */ |
| 1464 | if (usb->descriptor.bNumConfigurations != 1) { |
| 1465 | ret = -1; |
| 1466 | goto fail; |
| 1467 | } |
| 1468 | |
| 1469 | if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) { |
| 1470 | ret = -1; |
| 1471 | goto fail; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Only the BDC interface configuration is supported: |
| 1476 | * Device class: USB_CLASS_VENDOR_SPEC |
| 1477 | * if0 class: USB_CLASS_VENDOR_SPEC |
| 1478 | * if0/ep0: control |
| 1479 | * if0/ep1: bulk in |
| 1480 | * if0/ep2: bulk out (ok if swapped with bulk in) |
| 1481 | */ |
| 1482 | if (CONFIGDESC(usb)->bNumInterfaces != 1) { |
| 1483 | ret = -1; |
| 1484 | goto fail; |
| 1485 | } |
| 1486 | |
| 1487 | /* Check interface */ |
| 1488 | if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC || |
| 1489 | IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 || |
| 1490 | IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) { |
| 1491 | brcmf_dbg(ERROR, "invalid control interface: class %d, subclass %d, proto %d\n", |
| 1492 | IFDESC(usb, CONTROL_IF).bInterfaceClass, |
| 1493 | IFDESC(usb, CONTROL_IF).bInterfaceSubClass, |
| 1494 | IFDESC(usb, CONTROL_IF).bInterfaceProtocol); |
| 1495 | ret = -1; |
| 1496 | goto fail; |
| 1497 | } |
| 1498 | |
| 1499 | /* Check control endpoint */ |
| 1500 | endpoint = &IFEPDESC(usb, CONTROL_IF, 0); |
| 1501 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 1502 | != USB_ENDPOINT_XFER_INT) { |
| 1503 | brcmf_dbg(ERROR, "invalid control endpoint %d\n", |
| 1504 | endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); |
| 1505 | ret = -1; |
| 1506 | goto fail; |
| 1507 | } |
| 1508 | |
| 1509 | endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 1510 | usbdev_probe_info.intr_pipe = usb_rcvintpipe(usb, endpoint_num); |
| 1511 | |
| 1512 | usbdev_probe_info.rx_pipe = 0; |
| 1513 | usbdev_probe_info.rx_pipe2 = 0; |
| 1514 | usbdev_probe_info.tx_pipe = 0; |
| 1515 | num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1; |
| 1516 | |
| 1517 | /* Check data endpoints and get pipes */ |
| 1518 | for (ep = 1; ep <= num_of_eps; ep++) { |
| 1519 | endpoint = &IFEPDESC(usb, BULK_IF, ep); |
| 1520 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != |
| 1521 | USB_ENDPOINT_XFER_BULK) { |
| 1522 | brcmf_dbg(ERROR, "invalid data endpoint %d\n", ep); |
| 1523 | ret = -1; |
| 1524 | goto fail; |
| 1525 | } |
| 1526 | |
| 1527 | endpoint_num = endpoint->bEndpointAddress & |
| 1528 | USB_ENDPOINT_NUMBER_MASK; |
| 1529 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
| 1530 | == USB_DIR_IN) { |
| 1531 | if (!usbdev_probe_info.rx_pipe) { |
| 1532 | usbdev_probe_info.rx_pipe = |
| 1533 | usb_rcvbulkpipe(usb, endpoint_num); |
| 1534 | } else { |
| 1535 | usbdev_probe_info.rx_pipe2 = |
| 1536 | usb_rcvbulkpipe(usb, endpoint_num); |
| 1537 | } |
| 1538 | } else { |
| 1539 | usbdev_probe_info.tx_pipe = |
| 1540 | usb_sndbulkpipe(usb, endpoint_num); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /* Allocate interrupt URB and data buffer */ |
| 1545 | /* RNDIS says 8-byte intr, our old drivers used 4-byte */ |
| 1546 | if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16)) |
| 1547 | usbdev_probe_info.intr_size = 8; |
| 1548 | else |
| 1549 | usbdev_probe_info.intr_size = 4; |
| 1550 | |
| 1551 | usbdev_probe_info.interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval; |
| 1552 | |
| 1553 | usbdev_probe_info.device_speed = usb->speed; |
| 1554 | if (usb->speed == USB_SPEED_HIGH) |
| 1555 | brcmf_dbg(INFO, "Broadcom high speed USB wireless device detected\n"); |
| 1556 | else |
| 1557 | brcmf_dbg(INFO, "Broadcom full speed USB wireless device detected\n"); |
| 1558 | |
| 1559 | ret = brcmf_usb_probe_cb(&usb->dev, "", USB_BUS, 0); |
| 1560 | if (ret) |
| 1561 | goto fail; |
| 1562 | |
| 1563 | /* Success */ |
| 1564 | return 0; |
| 1565 | |
| 1566 | fail: |
| 1567 | brcmf_dbg(ERROR, "failed with errno %d\n", ret); |
| 1568 | usb_set_intfdata(intf, NULL); |
| 1569 | return ret; |
| 1570 | |
| 1571 | } |
| 1572 | |
| 1573 | static void |
| 1574 | brcmf_usb_disconnect(struct usb_interface *intf) |
| 1575 | { |
| 1576 | struct usb_device *usb = interface_to_usbdev(intf); |
| 1577 | |
| 1578 | brcmf_dbg(TRACE, "enter\n"); |
| 1579 | brcmf_usb_disconnect_cb(brcmf_usb_get_buspub(&usb->dev)); |
| 1580 | usb_set_intfdata(intf, NULL); |
| 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * only need to signal the bus being down and update the suspend state. |
| 1585 | */ |
| 1586 | static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state) |
| 1587 | { |
| 1588 | struct usb_device *usb = interface_to_usbdev(intf); |
| 1589 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev); |
| 1590 | |
| 1591 | brcmf_dbg(TRACE, "enter\n"); |
| 1592 | devinfo->bus_pub.state = BCMFMAC_USB_STATE_DOWN; |
| 1593 | devinfo->suspend_state = USBOS_SUSPEND_STATE_SUSPENDED; |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * mark suspend state active and crank up the bus. |
| 1599 | */ |
| 1600 | static int brcmf_usb_resume(struct usb_interface *intf) |
| 1601 | { |
| 1602 | struct usb_device *usb = interface_to_usbdev(intf); |
| 1603 | struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev); |
| 1604 | |
| 1605 | brcmf_dbg(TRACE, "enter\n"); |
| 1606 | devinfo->suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE; |
| 1607 | brcmf_bus_start(&usb->dev); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | #define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 1612 | #define BRCMF_USB_DEVICE_ID_43143 0xbd1e |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1613 | #define BRCMF_USB_DEVICE_ID_43236 0xbd17 |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1614 | #define BRCMF_USB_DEVICE_ID_43242 0xbd1f |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1615 | #define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc |
| 1616 | |
| 1617 | static struct usb_device_id brcmf_usb_devid_table[] = { |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 1618 | { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) }, |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1619 | { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) }, |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1620 | { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) }, |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1621 | /* special entry for device with firmware loaded and running */ |
| 1622 | { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) }, |
| 1623 | { } |
| 1624 | }; |
| 1625 | MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table); |
Hante Meuleman | 70f0822 | 2012-08-30 19:43:01 +0200 | [diff] [blame] | 1626 | MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME); |
Rafał Miłecki | fda8241 | 2012-02-24 07:22:51 +0100 | [diff] [blame] | 1627 | MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME); |
Hante Meuleman | 1212d37 | 2012-08-30 19:43:00 +0200 | [diff] [blame] | 1628 | MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* TODO: suspend and resume entries */ |
| 1631 | static struct usb_driver brcmf_usbdrvr = { |
| 1632 | .name = KBUILD_MODNAME, |
| 1633 | .probe = brcmf_usb_probe, |
| 1634 | .disconnect = brcmf_usb_disconnect, |
| 1635 | .id_table = brcmf_usb_devid_table, |
| 1636 | .suspend = brcmf_usb_suspend, |
| 1637 | .resume = brcmf_usb_resume, |
Sarah Sharp | c51fa66 | 2012-05-21 05:34:24 -0700 | [diff] [blame] | 1638 | .supports_autosuspend = 1, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 1639 | .disable_hub_initiated_lpm = 1, |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1640 | }; |
| 1641 | |
| 1642 | void brcmf_usb_exit(void) |
| 1643 | { |
| 1644 | usb_deregister(&brcmf_usbdrvr); |
Hauke Mehrtens | edb9bc9 | 2012-05-18 20:22:53 +0200 | [diff] [blame] | 1645 | vfree(g_image.data); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1646 | g_image.data = NULL; |
| 1647 | g_image.len = 0; |
| 1648 | } |
| 1649 | |
Arend van Spriel | 549040a | 2012-03-02 22:55:48 +0100 | [diff] [blame] | 1650 | void brcmf_usb_init(void) |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1651 | { |
Arend van Spriel | 549040a | 2012-03-02 22:55:48 +0100 | [diff] [blame] | 1652 | usb_register(&brcmf_usbdrvr); |
Arend van Spriel | 71bb244 | 2012-02-09 21:09:08 +0100 | [diff] [blame] | 1653 | } |