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