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