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