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/kernel.h> 18 #include <linux/module.h> 19 #include <linux/firmware.h> 20 #include <linux/usb.h> 21 #include <linux/vmalloc.h> 22 23 #include <brcmu_utils.h> 24 #include <brcm_hw_ids.h> 25 #include <brcmu_wifi.h> 26 #include "bus.h" 27 #include "debug.h" 28 #include "firmware.h" 29 #include "usb.h" 30 #include "core.h" 31 #include "common.h" 32 33 34 #define IOCTL_RESP_TIMEOUT msecs_to_jiffies(2000) 35 36 #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */ 37 #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10 38 39 #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle 40 has boot up */ 41 #define BRCMF_USB_NRXQ 50 42 #define BRCMF_USB_NTXQ 50 43 44 #define BRCMF_USB_CBCTL_WRITE 0 45 #define BRCMF_USB_CBCTL_READ 1 46 #define BRCMF_USB_MAX_PKT_SIZE 1600 47 48 BRCMF_FW_DEF(43143, "brcmfmac43143.bin"); 49 BRCMF_FW_DEF(43236B, "brcmfmac43236b.bin"); 50 BRCMF_FW_DEF(43242A, "brcmfmac43242a.bin"); 51 BRCMF_FW_DEF(43569, "brcmfmac43569.bin"); 52 53 static struct brcmf_firmware_mapping brcmf_usb_fwnames[] = { 54 BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143), 55 BRCMF_FW_ENTRY(BRCM_CC_43235_CHIP_ID, 0x00000008, 43236B), 56 BRCMF_FW_ENTRY(BRCM_CC_43236_CHIP_ID, 0x00000008, 43236B), 57 BRCMF_FW_ENTRY(BRCM_CC_43238_CHIP_ID, 0x00000008, 43236B), 58 BRCMF_FW_ENTRY(BRCM_CC_43242_CHIP_ID, 0xFFFFFFFF, 43242A), 59 BRCMF_FW_ENTRY(BRCM_CC_43566_CHIP_ID, 0xFFFFFFFF, 43569), 60 BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43569) 61 }; 62 63 #define TRX_MAGIC 0x30524448 /* "HDR0" */ 64 #define TRX_MAX_OFFSET 3 /* Max number of file offsets */ 65 #define TRX_UNCOMP_IMAGE 0x20 /* Trx holds uncompressed img */ 66 #define TRX_RDL_CHUNK 1500 /* size of each dl transfer */ 67 #define TRX_OFFSETS_DLFWLEN_IDX 0 68 69 /* Control messages: bRequest values */ 70 #define DL_GETSTATE 0 /* returns the rdl_state_t struct */ 71 #define DL_CHECK_CRC 1 /* currently unused */ 72 #define DL_GO 2 /* execute downloaded image */ 73 #define DL_START 3 /* initialize dl state */ 74 #define DL_REBOOT 4 /* reboot the device in 2 seconds */ 75 #define DL_GETVER 5 /* returns the bootrom_id_t struct */ 76 #define DL_GO_PROTECTED 6 /* execute the downloaded code and set reset 77 * event to occur in 2 seconds. It is the 78 * responsibility of the downloaded code to 79 * clear this event 80 */ 81 #define DL_EXEC 7 /* jump to a supplied address */ 82 #define DL_RESETCFG 8 /* To support single enum on dongle 83 * - Not used by bootloader 84 */ 85 #define DL_DEFER_RESP_OK 9 /* Potentially defer the response to setup 86 * if resp unavailable 87 */ 88 89 /* states */ 90 #define DL_WAITING 0 /* waiting to rx first pkt */ 91 #define DL_READY 1 /* hdr was good, waiting for more of the 92 * compressed image 93 */ 94 #define DL_BAD_HDR 2 /* hdr was corrupted */ 95 #define DL_BAD_CRC 3 /* compressed image was corrupted */ 96 #define DL_RUNNABLE 4 /* download was successful,waiting for go cmd */ 97 #define DL_START_FAIL 5 /* failed to initialize correctly */ 98 #define DL_NVRAM_TOOBIG 6 /* host specified nvram data exceeds DL_NVRAM 99 * value 100 */ 101 #define DL_IMAGE_TOOBIG 7 /* firmware image too big */ 102 103 104 struct trx_header_le { 105 __le32 magic; /* "HDR0" */ 106 __le32 len; /* Length of file including header */ 107 __le32 crc32; /* CRC from flag_version to end of file */ 108 __le32 flag_version; /* 0:15 flags, 16:31 version */ 109 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of 110 * header 111 */ 112 }; 113 114 struct rdl_state_le { 115 __le32 state; 116 __le32 bytes; 117 }; 118 119 struct bootrom_id_le { 120 __le32 chip; /* Chip id */ 121 __le32 chiprev; /* Chip rev */ 122 __le32 ramsize; /* Size of RAM */ 123 __le32 remapbase; /* Current remap base address */ 124 __le32 boardtype; /* Type of board */ 125 __le32 boardrev; /* Board revision */ 126 }; 127 128 struct brcmf_usb_image { 129 struct list_head list; 130 s8 *fwname; 131 u8 *image; 132 int image_len; 133 }; 134 135 struct brcmf_usbdev_info { 136 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */ 137 spinlock_t qlock; 138 struct list_head rx_freeq; 139 struct list_head rx_postq; 140 struct list_head tx_freeq; 141 struct list_head tx_postq; 142 uint rx_pipe, tx_pipe; 143 144 int rx_low_watermark; 145 int tx_low_watermark; 146 int tx_high_watermark; 147 int tx_freecount; 148 bool tx_flowblock; 149 spinlock_t tx_flowblock_lock; 150 151 struct brcmf_usbreq *tx_reqs; 152 struct brcmf_usbreq *rx_reqs; 153 154 char fw_name[BRCMF_FW_NAME_LEN]; 155 const u8 *image; /* buffer for combine fw and nvram */ 156 int image_len; 157 158 struct usb_device *usbdev; 159 struct device *dev; 160 struct mutex dev_init_lock; 161 162 int ctl_in_pipe, ctl_out_pipe; 163 struct urb *ctl_urb; /* URB for control endpoint */ 164 struct usb_ctrlrequest ctl_write; 165 struct usb_ctrlrequest ctl_read; 166 u32 ctl_urb_actual_length; 167 int ctl_urb_status; 168 int ctl_completed; 169 wait_queue_head_t ioctl_resp_wait; 170 ulong ctl_op; 171 u8 ifnum; 172 173 struct urb *bulk_urb; /* used for FW download */ 174 175 bool wowl_enabled; 176 struct brcmf_mp_device *settings; 177 }; 178 179 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo, 180 struct brcmf_usbreq *req); 181 182 static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev) 183 { 184 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 185 return bus_if->bus_priv.usb; 186 } 187 188 static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev) 189 { 190 return brcmf_usb_get_buspub(dev)->devinfo; 191 } 192 193 static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo) 194 { 195 return wait_event_timeout(devinfo->ioctl_resp_wait, 196 devinfo->ctl_completed, IOCTL_RESP_TIMEOUT); 197 } 198 199 static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo) 200 { 201 wake_up(&devinfo->ioctl_resp_wait); 202 } 203 204 static void 205 brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status) 206 { 207 brcmf_dbg(USB, "Enter, status=%d\n", status); 208 209 if (unlikely(devinfo == NULL)) 210 return; 211 212 if (type == BRCMF_USB_CBCTL_READ) { 213 if (status == 0) 214 devinfo->bus_pub.stats.rx_ctlpkts++; 215 else 216 devinfo->bus_pub.stats.rx_ctlerrs++; 217 } else if (type == BRCMF_USB_CBCTL_WRITE) { 218 if (status == 0) 219 devinfo->bus_pub.stats.tx_ctlpkts++; 220 else 221 devinfo->bus_pub.stats.tx_ctlerrs++; 222 } 223 224 devinfo->ctl_urb_status = status; 225 devinfo->ctl_completed = true; 226 brcmf_usb_ioctl_resp_wake(devinfo); 227 } 228 229 static void 230 brcmf_usb_ctlread_complete(struct urb *urb) 231 { 232 struct brcmf_usbdev_info *devinfo = 233 (struct brcmf_usbdev_info *)urb->context; 234 235 brcmf_dbg(USB, "Enter\n"); 236 devinfo->ctl_urb_actual_length = urb->actual_length; 237 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ, 238 urb->status); 239 } 240 241 static void 242 brcmf_usb_ctlwrite_complete(struct urb *urb) 243 { 244 struct brcmf_usbdev_info *devinfo = 245 (struct brcmf_usbdev_info *)urb->context; 246 247 brcmf_dbg(USB, "Enter\n"); 248 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE, 249 urb->status); 250 } 251 252 static int 253 brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len) 254 { 255 int ret; 256 u16 size; 257 258 brcmf_dbg(USB, "Enter\n"); 259 if (devinfo == NULL || buf == NULL || 260 len == 0 || devinfo->ctl_urb == NULL) 261 return -EINVAL; 262 263 size = len; 264 devinfo->ctl_write.wLength = cpu_to_le16p(&size); 265 devinfo->ctl_urb->transfer_buffer_length = size; 266 devinfo->ctl_urb_status = 0; 267 devinfo->ctl_urb_actual_length = 0; 268 269 usb_fill_control_urb(devinfo->ctl_urb, 270 devinfo->usbdev, 271 devinfo->ctl_out_pipe, 272 (unsigned char *) &devinfo->ctl_write, 273 buf, size, 274 (usb_complete_t)brcmf_usb_ctlwrite_complete, 275 devinfo); 276 277 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); 278 if (ret < 0) 279 brcmf_err("usb_submit_urb failed %d\n", ret); 280 281 return ret; 282 } 283 284 static int 285 brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len) 286 { 287 int ret; 288 u16 size; 289 290 brcmf_dbg(USB, "Enter\n"); 291 if ((devinfo == NULL) || (buf == NULL) || (len == 0) 292 || (devinfo->ctl_urb == NULL)) 293 return -EINVAL; 294 295 size = len; 296 devinfo->ctl_read.wLength = cpu_to_le16p(&size); 297 devinfo->ctl_urb->transfer_buffer_length = size; 298 299 devinfo->ctl_read.bRequestType = USB_DIR_IN 300 | USB_TYPE_CLASS | USB_RECIP_INTERFACE; 301 devinfo->ctl_read.bRequest = 1; 302 303 usb_fill_control_urb(devinfo->ctl_urb, 304 devinfo->usbdev, 305 devinfo->ctl_in_pipe, 306 (unsigned char *) &devinfo->ctl_read, 307 buf, size, 308 (usb_complete_t)brcmf_usb_ctlread_complete, 309 devinfo); 310 311 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); 312 if (ret < 0) 313 brcmf_err("usb_submit_urb failed %d\n", ret); 314 315 return ret; 316 } 317 318 static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len) 319 { 320 int err = 0; 321 int timeout = 0; 322 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 323 324 brcmf_dbg(USB, "Enter\n"); 325 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) 326 return -EIO; 327 328 if (test_and_set_bit(0, &devinfo->ctl_op)) 329 return -EIO; 330 331 devinfo->ctl_completed = false; 332 err = brcmf_usb_send_ctl(devinfo, buf, len); 333 if (err) { 334 brcmf_err("fail %d bytes: %d\n", err, len); 335 clear_bit(0, &devinfo->ctl_op); 336 return err; 337 } 338 timeout = brcmf_usb_ioctl_resp_wait(devinfo); 339 clear_bit(0, &devinfo->ctl_op); 340 if (!timeout) { 341 brcmf_err("Txctl wait timed out\n"); 342 err = -EIO; 343 } 344 return err; 345 } 346 347 static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len) 348 { 349 int err = 0; 350 int timeout = 0; 351 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 352 353 brcmf_dbg(USB, "Enter\n"); 354 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) 355 return -EIO; 356 357 if (test_and_set_bit(0, &devinfo->ctl_op)) 358 return -EIO; 359 360 devinfo->ctl_completed = false; 361 err = brcmf_usb_recv_ctl(devinfo, buf, len); 362 if (err) { 363 brcmf_err("fail %d bytes: %d\n", err, len); 364 clear_bit(0, &devinfo->ctl_op); 365 return err; 366 } 367 timeout = brcmf_usb_ioctl_resp_wait(devinfo); 368 err = devinfo->ctl_urb_status; 369 clear_bit(0, &devinfo->ctl_op); 370 if (!timeout) { 371 brcmf_err("rxctl wait timed out\n"); 372 err = -EIO; 373 } 374 if (!err) 375 return devinfo->ctl_urb_actual_length; 376 else 377 return err; 378 } 379 380 static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo, 381 struct list_head *q, int *counter) 382 { 383 unsigned long flags; 384 struct brcmf_usbreq *req; 385 spin_lock_irqsave(&devinfo->qlock, flags); 386 if (list_empty(q)) { 387 spin_unlock_irqrestore(&devinfo->qlock, flags); 388 return NULL; 389 } 390 req = list_entry(q->next, struct brcmf_usbreq, list); 391 list_del_init(q->next); 392 if (counter) 393 (*counter)--; 394 spin_unlock_irqrestore(&devinfo->qlock, flags); 395 return req; 396 397 } 398 399 static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo, 400 struct list_head *q, struct brcmf_usbreq *req, 401 int *counter) 402 { 403 unsigned long flags; 404 spin_lock_irqsave(&devinfo->qlock, flags); 405 list_add_tail(&req->list, q); 406 if (counter) 407 (*counter)++; 408 spin_unlock_irqrestore(&devinfo->qlock, flags); 409 } 410 411 static struct brcmf_usbreq * 412 brcmf_usbdev_qinit(struct list_head *q, int qsize) 413 { 414 int i; 415 struct brcmf_usbreq *req, *reqs; 416 417 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC); 418 if (reqs == NULL) 419 return NULL; 420 421 req = reqs; 422 423 for (i = 0; i < qsize; i++) { 424 req->urb = usb_alloc_urb(0, GFP_ATOMIC); 425 if (!req->urb) 426 goto fail; 427 428 INIT_LIST_HEAD(&req->list); 429 list_add_tail(&req->list, q); 430 req++; 431 } 432 return reqs; 433 fail: 434 brcmf_err("fail!\n"); 435 while (!list_empty(q)) { 436 req = list_entry(q->next, struct brcmf_usbreq, list); 437 if (req) 438 usb_free_urb(req->urb); 439 list_del(q->next); 440 } 441 return NULL; 442 443 } 444 445 static void brcmf_usb_free_q(struct list_head *q, bool pending) 446 { 447 struct brcmf_usbreq *req, *next; 448 int i = 0; 449 list_for_each_entry_safe(req, next, q, list) { 450 if (!req->urb) { 451 brcmf_err("bad req\n"); 452 break; 453 } 454 i++; 455 if (pending) { 456 usb_kill_urb(req->urb); 457 } else { 458 usb_free_urb(req->urb); 459 list_del_init(&req->list); 460 } 461 } 462 } 463 464 static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo, 465 struct brcmf_usbreq *req) 466 { 467 unsigned long flags; 468 469 spin_lock_irqsave(&devinfo->qlock, flags); 470 list_del_init(&req->list); 471 spin_unlock_irqrestore(&devinfo->qlock, flags); 472 } 473 474 475 static void brcmf_usb_tx_complete(struct urb *urb) 476 { 477 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context; 478 struct brcmf_usbdev_info *devinfo = req->devinfo; 479 unsigned long flags; 480 481 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status, 482 req->skb); 483 brcmf_usb_del_fromq(devinfo, req); 484 485 brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0); 486 req->skb = NULL; 487 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount); 488 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags); 489 if (devinfo->tx_freecount > devinfo->tx_high_watermark && 490 devinfo->tx_flowblock) { 491 brcmf_txflowblock(devinfo->dev, false); 492 devinfo->tx_flowblock = false; 493 } 494 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags); 495 } 496 497 static void brcmf_usb_rx_complete(struct urb *urb) 498 { 499 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context; 500 struct brcmf_usbdev_info *devinfo = req->devinfo; 501 struct sk_buff *skb; 502 503 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status); 504 brcmf_usb_del_fromq(devinfo, req); 505 skb = req->skb; 506 req->skb = NULL; 507 508 /* zero lenght packets indicate usb "failure". Do not refill */ 509 if (urb->status != 0 || !urb->actual_length) { 510 brcmu_pkt_buf_free_skb(skb); 511 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); 512 return; 513 } 514 515 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) { 516 skb_put(skb, urb->actual_length); 517 brcmf_rx_frame(devinfo->dev, skb, true); 518 brcmf_usb_rx_refill(devinfo, req); 519 } else { 520 brcmu_pkt_buf_free_skb(skb); 521 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); 522 } 523 return; 524 525 } 526 527 static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo, 528 struct brcmf_usbreq *req) 529 { 530 struct sk_buff *skb; 531 int ret; 532 533 if (!req || !devinfo) 534 return; 535 536 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu); 537 if (!skb) { 538 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); 539 return; 540 } 541 req->skb = skb; 542 543 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe, 544 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete, 545 req); 546 req->devinfo = devinfo; 547 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL); 548 549 ret = usb_submit_urb(req->urb, GFP_ATOMIC); 550 if (ret) { 551 brcmf_usb_del_fromq(devinfo, req); 552 brcmu_pkt_buf_free_skb(req->skb); 553 req->skb = NULL; 554 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL); 555 } 556 return; 557 } 558 559 static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo) 560 { 561 struct brcmf_usbreq *req; 562 563 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) { 564 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state); 565 return; 566 } 567 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL) 568 brcmf_usb_rx_refill(devinfo, req); 569 } 570 571 static void 572 brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state) 573 { 574 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus; 575 int old_state; 576 577 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n", 578 devinfo->bus_pub.state, state); 579 580 if (devinfo->bus_pub.state == state) 581 return; 582 583 old_state = devinfo->bus_pub.state; 584 devinfo->bus_pub.state = state; 585 586 /* update state of upper layer */ 587 if (state == BRCMFMAC_USB_STATE_DOWN) { 588 brcmf_dbg(USB, "DBUS is down\n"); 589 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN); 590 } else if (state == BRCMFMAC_USB_STATE_UP) { 591 brcmf_dbg(USB, "DBUS is up\n"); 592 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP); 593 } else { 594 brcmf_dbg(USB, "DBUS current state=%d\n", state); 595 } 596 } 597 598 static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb) 599 { 600 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 601 struct brcmf_usbreq *req; 602 int ret; 603 unsigned long flags; 604 605 brcmf_dbg(USB, "Enter, skb=%p\n", skb); 606 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) { 607 ret = -EIO; 608 goto fail; 609 } 610 611 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq, 612 &devinfo->tx_freecount); 613 if (!req) { 614 brcmf_err("no req to send\n"); 615 ret = -ENOMEM; 616 goto fail; 617 } 618 619 req->skb = skb; 620 req->devinfo = devinfo; 621 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe, 622 skb->data, skb->len, brcmf_usb_tx_complete, req); 623 req->urb->transfer_flags |= URB_ZERO_PACKET; 624 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL); 625 ret = usb_submit_urb(req->urb, GFP_ATOMIC); 626 if (ret) { 627 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n"); 628 brcmf_usb_del_fromq(devinfo, req); 629 req->skb = NULL; 630 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, 631 &devinfo->tx_freecount); 632 goto fail; 633 } 634 635 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags); 636 if (devinfo->tx_freecount < devinfo->tx_low_watermark && 637 !devinfo->tx_flowblock) { 638 brcmf_txflowblock(dev, true); 639 devinfo->tx_flowblock = true; 640 } 641 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags); 642 return 0; 643 644 fail: 645 return ret; 646 } 647 648 649 static int brcmf_usb_up(struct device *dev) 650 { 651 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 652 653 brcmf_dbg(USB, "Enter\n"); 654 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) 655 return 0; 656 657 /* Success, indicate devinfo is fully up */ 658 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP); 659 660 if (devinfo->ctl_urb) { 661 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0); 662 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0); 663 664 /* CTL Write */ 665 devinfo->ctl_write.bRequestType = 666 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE; 667 devinfo->ctl_write.bRequest = 0; 668 devinfo->ctl_write.wValue = cpu_to_le16(0); 669 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum); 670 671 /* CTL Read */ 672 devinfo->ctl_read.bRequestType = 673 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; 674 devinfo->ctl_read.bRequest = 1; 675 devinfo->ctl_read.wValue = cpu_to_le16(0); 676 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum); 677 } 678 brcmf_usb_rx_fill_all(devinfo); 679 return 0; 680 } 681 682 static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo) 683 { 684 if (devinfo->ctl_urb) 685 usb_kill_urb(devinfo->ctl_urb); 686 if (devinfo->bulk_urb) 687 usb_kill_urb(devinfo->bulk_urb); 688 brcmf_usb_free_q(&devinfo->tx_postq, true); 689 brcmf_usb_free_q(&devinfo->rx_postq, true); 690 } 691 692 static void brcmf_usb_down(struct device *dev) 693 { 694 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 695 696 brcmf_dbg(USB, "Enter\n"); 697 if (devinfo == NULL) 698 return; 699 700 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN) 701 return; 702 703 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN); 704 705 brcmf_cancel_all_urbs(devinfo); 706 } 707 708 static void 709 brcmf_usb_sync_complete(struct urb *urb) 710 { 711 struct brcmf_usbdev_info *devinfo = 712 (struct brcmf_usbdev_info *)urb->context; 713 714 devinfo->ctl_completed = true; 715 brcmf_usb_ioctl_resp_wake(devinfo); 716 } 717 718 static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd, 719 void *buffer, int buflen) 720 { 721 int ret; 722 char *tmpbuf; 723 u16 size; 724 725 if ((!devinfo) || (devinfo->ctl_urb == NULL)) 726 return -EINVAL; 727 728 tmpbuf = kmalloc(buflen, GFP_ATOMIC); 729 if (!tmpbuf) 730 return -ENOMEM; 731 732 size = buflen; 733 devinfo->ctl_urb->transfer_buffer_length = size; 734 735 devinfo->ctl_read.wLength = cpu_to_le16p(&size); 736 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR | 737 USB_RECIP_INTERFACE; 738 devinfo->ctl_read.bRequest = cmd; 739 740 usb_fill_control_urb(devinfo->ctl_urb, 741 devinfo->usbdev, 742 usb_rcvctrlpipe(devinfo->usbdev, 0), 743 (unsigned char *) &devinfo->ctl_read, 744 (void *) tmpbuf, size, 745 (usb_complete_t)brcmf_usb_sync_complete, devinfo); 746 747 devinfo->ctl_completed = false; 748 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC); 749 if (ret < 0) { 750 brcmf_err("usb_submit_urb failed %d\n", ret); 751 goto finalize; 752 } 753 754 if (!brcmf_usb_ioctl_resp_wait(devinfo)) { 755 usb_kill_urb(devinfo->ctl_urb); 756 ret = -ETIMEDOUT; 757 } else { 758 memcpy(buffer, tmpbuf, buflen); 759 } 760 761 finalize: 762 kfree(tmpbuf); 763 return ret; 764 } 765 766 static bool 767 brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo) 768 { 769 struct bootrom_id_le id; 770 u32 chipid, chiprev; 771 772 brcmf_dbg(USB, "Enter\n"); 773 774 if (devinfo == NULL) 775 return false; 776 777 /* Check if firmware downloaded already by querying runtime ID */ 778 id.chip = cpu_to_le32(0xDEAD); 779 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id)); 780 781 chipid = le32_to_cpu(id.chip); 782 chiprev = le32_to_cpu(id.chiprev); 783 784 if ((chipid & 0x4300) == 0x4300) 785 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev); 786 else 787 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev); 788 if (chipid == BRCMF_POSTBOOT_ID) { 789 brcmf_dbg(USB, "firmware already downloaded\n"); 790 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id)); 791 return false; 792 } else { 793 devinfo->bus_pub.devid = chipid; 794 devinfo->bus_pub.chiprev = chiprev; 795 } 796 return true; 797 } 798 799 static int 800 brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo) 801 { 802 struct bootrom_id_le id; 803 u32 loop_cnt; 804 int err; 805 806 brcmf_dbg(USB, "Enter\n"); 807 808 loop_cnt = 0; 809 do { 810 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT); 811 loop_cnt++; 812 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */ 813 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id)); 814 if ((err) && (err != -ETIMEDOUT)) 815 return err; 816 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) 817 break; 818 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT); 819 820 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) { 821 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n", 822 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev)); 823 824 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id)); 825 return 0; 826 } else { 827 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n", 828 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt); 829 return -EINVAL; 830 } 831 } 832 833 834 static int 835 brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len) 836 { 837 int ret; 838 839 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL)) 840 return -EINVAL; 841 842 /* Prepare the URB */ 843 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev, 844 devinfo->tx_pipe, buffer, len, 845 (usb_complete_t)brcmf_usb_sync_complete, devinfo); 846 847 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET; 848 849 devinfo->ctl_completed = false; 850 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC); 851 if (ret) { 852 brcmf_err("usb_submit_urb failed %d\n", ret); 853 return ret; 854 } 855 ret = brcmf_usb_ioctl_resp_wait(devinfo); 856 return (ret == 0); 857 } 858 859 static int 860 brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen) 861 { 862 unsigned int sendlen, sent, dllen; 863 char *bulkchunk = NULL, *dlpos; 864 struct rdl_state_le state; 865 u32 rdlstate, rdlbytes; 866 int err = 0; 867 868 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen); 869 870 bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC); 871 if (bulkchunk == NULL) { 872 err = -ENOMEM; 873 goto fail; 874 } 875 876 /* 1) Prepare USB boot loader for runtime image */ 877 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state)); 878 879 rdlstate = le32_to_cpu(state.state); 880 rdlbytes = le32_to_cpu(state.bytes); 881 882 /* 2) Check we are in the Waiting state */ 883 if (rdlstate != DL_WAITING) { 884 brcmf_err("Failed to DL_START\n"); 885 err = -EINVAL; 886 goto fail; 887 } 888 sent = 0; 889 dlpos = fw; 890 dllen = fwlen; 891 892 /* Get chip id and rev */ 893 while (rdlbytes != dllen) { 894 /* Wait until the usb device reports it received all 895 * the bytes we sent */ 896 if ((rdlbytes == sent) && (rdlbytes != dllen)) { 897 if ((dllen-sent) < TRX_RDL_CHUNK) 898 sendlen = dllen-sent; 899 else 900 sendlen = TRX_RDL_CHUNK; 901 902 /* simply avoid having to send a ZLP by ensuring we 903 * never have an even 904 * multiple of 64 905 */ 906 if (!(sendlen % 64)) 907 sendlen -= 4; 908 909 /* send data */ 910 memcpy(bulkchunk, dlpos, sendlen); 911 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk, 912 sendlen)) { 913 brcmf_err("send_bulk failed\n"); 914 err = -EINVAL; 915 goto fail; 916 } 917 918 dlpos += sendlen; 919 sent += sendlen; 920 } 921 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, 922 sizeof(state)); 923 if (err) { 924 brcmf_err("DL_GETSTATE Failed\n"); 925 goto fail; 926 } 927 928 rdlstate = le32_to_cpu(state.state); 929 rdlbytes = le32_to_cpu(state.bytes); 930 931 /* restart if an error is reported */ 932 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) { 933 brcmf_err("Bad Hdr or Bad CRC state %d\n", 934 rdlstate); 935 err = -EINVAL; 936 goto fail; 937 } 938 } 939 940 fail: 941 kfree(bulkchunk); 942 brcmf_dbg(USB, "Exit, err=%d\n", err); 943 return err; 944 } 945 946 static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len) 947 { 948 int err; 949 950 brcmf_dbg(USB, "Enter\n"); 951 952 if (devinfo == NULL) 953 return -EINVAL; 954 955 if (devinfo->bus_pub.devid == 0xDEAD) 956 return -EINVAL; 957 958 err = brcmf_usb_dl_writeimage(devinfo, fw, len); 959 if (err == 0) 960 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE; 961 else 962 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL; 963 brcmf_dbg(USB, "Exit, err=%d\n", err); 964 965 return err; 966 } 967 968 static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo) 969 { 970 struct rdl_state_le state; 971 972 brcmf_dbg(USB, "Enter\n"); 973 if (!devinfo) 974 return -EINVAL; 975 976 if (devinfo->bus_pub.devid == 0xDEAD) 977 return -EINVAL; 978 979 /* Check we are runnable */ 980 state.state = 0; 981 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state)); 982 983 /* Start the image */ 984 if (state.state == cpu_to_le32(DL_RUNNABLE)) { 985 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state))) 986 return -ENODEV; 987 if (brcmf_usb_resetcfg(devinfo)) 988 return -ENODEV; 989 /* The Dongle may go for re-enumeration. */ 990 } else { 991 brcmf_err("Dongle not runnable\n"); 992 return -EINVAL; 993 } 994 brcmf_dbg(USB, "Exit\n"); 995 return 0; 996 } 997 998 static int 999 brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo) 1000 { 1001 int err; 1002 1003 brcmf_dbg(USB, "Enter\n"); 1004 if (devinfo == NULL) 1005 return -ENODEV; 1006 1007 if (!devinfo->image) { 1008 brcmf_err("No firmware!\n"); 1009 return -ENOENT; 1010 } 1011 1012 err = brcmf_usb_dlstart(devinfo, 1013 (u8 *)devinfo->image, devinfo->image_len); 1014 if (err == 0) 1015 err = brcmf_usb_dlrun(devinfo); 1016 return err; 1017 } 1018 1019 1020 static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo) 1021 { 1022 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo); 1023 1024 /* free the URBS */ 1025 brcmf_usb_free_q(&devinfo->rx_freeq, false); 1026 brcmf_usb_free_q(&devinfo->tx_freeq, false); 1027 1028 usb_free_urb(devinfo->ctl_urb); 1029 usb_free_urb(devinfo->bulk_urb); 1030 1031 kfree(devinfo->tx_reqs); 1032 kfree(devinfo->rx_reqs); 1033 1034 if (devinfo->settings) 1035 brcmf_release_module_param(devinfo->settings); 1036 } 1037 1038 1039 static int check_file(const u8 *headers) 1040 { 1041 struct trx_header_le *trx; 1042 int actual_len = -1; 1043 1044 brcmf_dbg(USB, "Enter\n"); 1045 /* Extract trx header */ 1046 trx = (struct trx_header_le *) headers; 1047 if (trx->magic != cpu_to_le32(TRX_MAGIC)) 1048 return -1; 1049 1050 headers += sizeof(struct trx_header_le); 1051 1052 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) { 1053 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]); 1054 return actual_len + sizeof(struct trx_header_le); 1055 } 1056 return -1; 1057 } 1058 1059 1060 static 1061 struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo, 1062 int nrxq, int ntxq) 1063 { 1064 brcmf_dbg(USB, "Enter\n"); 1065 1066 devinfo->bus_pub.nrxq = nrxq; 1067 devinfo->rx_low_watermark = nrxq / 2; 1068 devinfo->bus_pub.devinfo = devinfo; 1069 devinfo->bus_pub.ntxq = ntxq; 1070 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN; 1071 1072 /* flow control when too many tx urbs posted */ 1073 devinfo->tx_low_watermark = ntxq / 4; 1074 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3; 1075 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE; 1076 1077 /* Initialize other structure content */ 1078 init_waitqueue_head(&devinfo->ioctl_resp_wait); 1079 1080 /* Initialize the spinlocks */ 1081 spin_lock_init(&devinfo->qlock); 1082 spin_lock_init(&devinfo->tx_flowblock_lock); 1083 1084 INIT_LIST_HEAD(&devinfo->rx_freeq); 1085 INIT_LIST_HEAD(&devinfo->rx_postq); 1086 1087 INIT_LIST_HEAD(&devinfo->tx_freeq); 1088 INIT_LIST_HEAD(&devinfo->tx_postq); 1089 1090 devinfo->tx_flowblock = false; 1091 1092 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq); 1093 if (!devinfo->rx_reqs) 1094 goto error; 1095 1096 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq); 1097 if (!devinfo->tx_reqs) 1098 goto error; 1099 devinfo->tx_freecount = ntxq; 1100 1101 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC); 1102 if (!devinfo->ctl_urb) 1103 goto error; 1104 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC); 1105 if (!devinfo->bulk_urb) 1106 goto error; 1107 1108 return &devinfo->bus_pub; 1109 1110 error: 1111 brcmf_err("failed!\n"); 1112 brcmf_usb_detach(devinfo); 1113 return NULL; 1114 } 1115 1116 static void brcmf_usb_wowl_config(struct device *dev, bool enabled) 1117 { 1118 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev); 1119 1120 brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled); 1121 devinfo->wowl_enabled = enabled; 1122 if (enabled) 1123 device_set_wakeup_enable(devinfo->dev, true); 1124 else 1125 device_set_wakeup_enable(devinfo->dev, false); 1126 } 1127 1128 static const struct brcmf_bus_ops brcmf_usb_bus_ops = { 1129 .txdata = brcmf_usb_tx, 1130 .stop = brcmf_usb_down, 1131 .txctl = brcmf_usb_tx_ctlpkt, 1132 .rxctl = brcmf_usb_rx_ctlpkt, 1133 .wowl_config = brcmf_usb_wowl_config, 1134 }; 1135 1136 static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo) 1137 { 1138 int ret; 1139 1140 /* Attach to the common driver interface */ 1141 ret = brcmf_attach(devinfo->dev, devinfo->settings); 1142 if (ret) { 1143 brcmf_err("brcmf_attach failed\n"); 1144 return ret; 1145 } 1146 1147 ret = brcmf_usb_up(devinfo->dev); 1148 if (ret) 1149 goto fail; 1150 1151 ret = brcmf_bus_started(devinfo->dev); 1152 if (ret) 1153 goto fail; 1154 1155 return 0; 1156 fail: 1157 brcmf_detach(devinfo->dev); 1158 return ret; 1159 } 1160 1161 static void brcmf_usb_probe_phase2(struct device *dev, 1162 const struct firmware *fw, 1163 void *nvram, u32 nvlen) 1164 { 1165 struct brcmf_bus *bus = dev_get_drvdata(dev); 1166 struct brcmf_usbdev_info *devinfo; 1167 int ret; 1168 1169 brcmf_dbg(USB, "Start fw downloading\n"); 1170 1171 devinfo = bus->bus_priv.usb->devinfo; 1172 ret = check_file(fw->data); 1173 if (ret < 0) { 1174 brcmf_err("invalid firmware\n"); 1175 release_firmware(fw); 1176 goto error; 1177 } 1178 1179 devinfo->image = fw->data; 1180 devinfo->image_len = fw->size; 1181 1182 ret = brcmf_usb_fw_download(devinfo); 1183 release_firmware(fw); 1184 if (ret) 1185 goto error; 1186 1187 ret = brcmf_usb_bus_setup(devinfo); 1188 if (ret) 1189 goto error; 1190 1191 mutex_unlock(&devinfo->dev_init_lock); 1192 return; 1193 error: 1194 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret); 1195 mutex_unlock(&devinfo->dev_init_lock); 1196 device_release_driver(dev); 1197 } 1198 1199 static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo) 1200 { 1201 struct brcmf_bus *bus = NULL; 1202 struct brcmf_usbdev *bus_pub = NULL; 1203 struct device *dev = devinfo->dev; 1204 int ret; 1205 1206 brcmf_dbg(USB, "Enter\n"); 1207 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ); 1208 if (!bus_pub) 1209 return -ENODEV; 1210 1211 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC); 1212 if (!bus) { 1213 ret = -ENOMEM; 1214 goto fail; 1215 } 1216 1217 bus->dev = dev; 1218 bus_pub->bus = bus; 1219 bus->bus_priv.usb = bus_pub; 1220 dev_set_drvdata(dev, bus); 1221 bus->ops = &brcmf_usb_bus_ops; 1222 bus->proto_type = BRCMF_PROTO_BCDC; 1223 bus->always_use_fws_queue = true; 1224 #ifdef CONFIG_PM 1225 bus->wowl_supported = true; 1226 #endif 1227 1228 devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB, 1229 bus_pub->devid, 1230 bus_pub->chiprev); 1231 if (!devinfo->settings) { 1232 ret = -ENOMEM; 1233 goto fail; 1234 } 1235 1236 if (!brcmf_usb_dlneeded(devinfo)) { 1237 ret = brcmf_usb_bus_setup(devinfo); 1238 if (ret) 1239 goto fail; 1240 /* we are done */ 1241 mutex_unlock(&devinfo->dev_init_lock); 1242 return 0; 1243 } 1244 bus->chip = bus_pub->devid; 1245 bus->chiprev = bus_pub->chiprev; 1246 1247 ret = brcmf_fw_map_chip_to_name(bus_pub->devid, bus_pub->chiprev, 1248 brcmf_usb_fwnames, 1249 ARRAY_SIZE(brcmf_usb_fwnames), 1250 devinfo->fw_name, NULL); 1251 if (ret) 1252 goto fail; 1253 1254 /* request firmware here */ 1255 ret = brcmf_fw_get_firmwares(dev, 0, devinfo->fw_name, NULL, 1256 brcmf_usb_probe_phase2); 1257 if (ret) { 1258 brcmf_err("firmware request failed: %d\n", ret); 1259 goto fail; 1260 } 1261 1262 return 0; 1263 1264 fail: 1265 /* Release resources in reverse order */ 1266 kfree(bus); 1267 brcmf_usb_detach(devinfo); 1268 return ret; 1269 } 1270 1271 static void 1272 brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo) 1273 { 1274 if (!devinfo) 1275 return; 1276 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo); 1277 1278 brcmf_detach(devinfo->dev); 1279 kfree(devinfo->bus_pub.bus); 1280 brcmf_usb_detach(devinfo); 1281 } 1282 1283 static int 1284 brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 1285 { 1286 struct usb_device *usb = interface_to_usbdev(intf); 1287 struct brcmf_usbdev_info *devinfo; 1288 struct usb_interface_descriptor *desc; 1289 struct usb_endpoint_descriptor *endpoint; 1290 int ret = 0; 1291 u32 num_of_eps; 1292 u8 endpoint_num, ep; 1293 1294 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct); 1295 1296 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC); 1297 if (devinfo == NULL) 1298 return -ENOMEM; 1299 1300 devinfo->usbdev = usb; 1301 devinfo->dev = &usb->dev; 1302 /* Take an init lock, to protect for disconnect while still loading. 1303 * Necessary because of the asynchronous firmware load construction 1304 */ 1305 mutex_init(&devinfo->dev_init_lock); 1306 mutex_lock(&devinfo->dev_init_lock); 1307 1308 usb_set_intfdata(intf, devinfo); 1309 1310 /* Check that the device supports only one configuration */ 1311 if (usb->descriptor.bNumConfigurations != 1) { 1312 brcmf_err("Number of configurations: %d not supported\n", 1313 usb->descriptor.bNumConfigurations); 1314 ret = -ENODEV; 1315 goto fail; 1316 } 1317 1318 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) && 1319 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) && 1320 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) { 1321 brcmf_err("Device class: 0x%x not supported\n", 1322 usb->descriptor.bDeviceClass); 1323 ret = -ENODEV; 1324 goto fail; 1325 } 1326 1327 desc = &intf->altsetting[0].desc; 1328 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || 1329 (desc->bInterfaceSubClass != 2) || 1330 (desc->bInterfaceProtocol != 0xff)) { 1331 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n", 1332 desc->bInterfaceNumber, desc->bInterfaceClass, 1333 desc->bInterfaceSubClass, desc->bInterfaceProtocol); 1334 ret = -ENODEV; 1335 goto fail; 1336 } 1337 1338 num_of_eps = desc->bNumEndpoints; 1339 for (ep = 0; ep < num_of_eps; ep++) { 1340 endpoint = &intf->altsetting[0].endpoint[ep].desc; 1341 endpoint_num = usb_endpoint_num(endpoint); 1342 if (!usb_endpoint_xfer_bulk(endpoint)) 1343 continue; 1344 if (usb_endpoint_dir_in(endpoint)) { 1345 if (!devinfo->rx_pipe) 1346 devinfo->rx_pipe = 1347 usb_rcvbulkpipe(usb, endpoint_num); 1348 } else { 1349 if (!devinfo->tx_pipe) 1350 devinfo->tx_pipe = 1351 usb_sndbulkpipe(usb, endpoint_num); 1352 } 1353 } 1354 if (devinfo->rx_pipe == 0) { 1355 brcmf_err("No RX (in) Bulk EP found\n"); 1356 ret = -ENODEV; 1357 goto fail; 1358 } 1359 if (devinfo->tx_pipe == 0) { 1360 brcmf_err("No TX (out) Bulk EP found\n"); 1361 ret = -ENODEV; 1362 goto fail; 1363 } 1364 1365 devinfo->ifnum = desc->bInterfaceNumber; 1366 1367 if (usb->speed == USB_SPEED_SUPER_PLUS) 1368 brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n"); 1369 else if (usb->speed == USB_SPEED_SUPER) 1370 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n"); 1371 else if (usb->speed == USB_SPEED_HIGH) 1372 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n"); 1373 else 1374 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n"); 1375 1376 ret = brcmf_usb_probe_cb(devinfo); 1377 if (ret) 1378 goto fail; 1379 1380 /* Success */ 1381 return 0; 1382 1383 fail: 1384 mutex_unlock(&devinfo->dev_init_lock); 1385 kfree(devinfo); 1386 usb_set_intfdata(intf, NULL); 1387 return ret; 1388 } 1389 1390 static void 1391 brcmf_usb_disconnect(struct usb_interface *intf) 1392 { 1393 struct brcmf_usbdev_info *devinfo; 1394 1395 brcmf_dbg(USB, "Enter\n"); 1396 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf); 1397 1398 if (devinfo) { 1399 mutex_lock(&devinfo->dev_init_lock); 1400 /* Make sure that devinfo still exists. Firmware probe routines 1401 * may have released the device and cleared the intfdata. 1402 */ 1403 if (!usb_get_intfdata(intf)) 1404 goto done; 1405 1406 brcmf_usb_disconnect_cb(devinfo); 1407 kfree(devinfo); 1408 } 1409 done: 1410 brcmf_dbg(USB, "Exit\n"); 1411 } 1412 1413 /* 1414 * only need to signal the bus being down and update the state. 1415 */ 1416 static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state) 1417 { 1418 struct usb_device *usb = interface_to_usbdev(intf); 1419 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev); 1420 1421 brcmf_dbg(USB, "Enter\n"); 1422 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP; 1423 if (devinfo->wowl_enabled) 1424 brcmf_cancel_all_urbs(devinfo); 1425 else 1426 brcmf_detach(&usb->dev); 1427 return 0; 1428 } 1429 1430 /* 1431 * (re-) start the bus. 1432 */ 1433 static int brcmf_usb_resume(struct usb_interface *intf) 1434 { 1435 struct usb_device *usb = interface_to_usbdev(intf); 1436 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev); 1437 1438 brcmf_dbg(USB, "Enter\n"); 1439 if (!devinfo->wowl_enabled) 1440 return brcmf_usb_bus_setup(devinfo); 1441 1442 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP; 1443 brcmf_usb_rx_fill_all(devinfo); 1444 return 0; 1445 } 1446 1447 static int brcmf_usb_reset_resume(struct usb_interface *intf) 1448 { 1449 struct usb_device *usb = interface_to_usbdev(intf); 1450 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev); 1451 1452 brcmf_dbg(USB, "Enter\n"); 1453 1454 return brcmf_fw_get_firmwares(&usb->dev, 0, devinfo->fw_name, NULL, 1455 brcmf_usb_probe_phase2); 1456 } 1457 1458 #define BRCMF_USB_DEVICE(dev_id) \ 1459 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) } 1460 1461 #define LINKSYS_USB_DEVICE(dev_id) \ 1462 { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) } 1463 1464 static struct usb_device_id brcmf_usb_devid_table[] = { 1465 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID), 1466 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID), 1467 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID), 1468 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID), 1469 LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID), 1470 { USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) }, 1471 /* special entry for device with firmware loaded and running */ 1472 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID), 1473 { /* end: all zeroes */ } 1474 }; 1475 1476 MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table); 1477 1478 static struct usb_driver brcmf_usbdrvr = { 1479 .name = KBUILD_MODNAME, 1480 .probe = brcmf_usb_probe, 1481 .disconnect = brcmf_usb_disconnect, 1482 .id_table = brcmf_usb_devid_table, 1483 .suspend = brcmf_usb_suspend, 1484 .resume = brcmf_usb_resume, 1485 .reset_resume = brcmf_usb_reset_resume, 1486 .disable_hub_initiated_lpm = 1, 1487 }; 1488 1489 static int brcmf_usb_reset_device(struct device *dev, void *notused) 1490 { 1491 /* device past is the usb interface so we 1492 * need to use parent here. 1493 */ 1494 brcmf_dev_reset(dev->parent); 1495 return 0; 1496 } 1497 1498 void brcmf_usb_exit(void) 1499 { 1500 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver; 1501 int ret; 1502 1503 brcmf_dbg(USB, "Enter\n"); 1504 ret = driver_for_each_device(drv, NULL, NULL, 1505 brcmf_usb_reset_device); 1506 usb_deregister(&brcmf_usbdrvr); 1507 } 1508 1509 void brcmf_usb_register(void) 1510 { 1511 brcmf_dbg(USB, "Enter\n"); 1512 usb_register(&brcmf_usbdrvr); 1513 } 1514