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