usb.c 37.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2011 Broadcom Corporation
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/firmware.h>
#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 31
#include "core.h"
#include "common.h"
32

33

34
#define IOCTL_RESP_TIMEOUT		msecs_to_jiffies(2000)
35

36 37
#define BRCMF_USB_RESET_GETVER_SPINWAIT	100	/* in unit of ms */
#define BRCMF_USB_RESET_GETVER_LOOP_CNT	10
38 39 40

#define BRCMF_POSTBOOT_ID		0xA123  /* ID to detect if dongle
						   has boot up */
41 42
#define BRCMF_USB_NRXQ			50
#define BRCMF_USB_NTXQ			50
43

44 45 46
#define BRCMF_USB_CBCTL_WRITE		0
#define BRCMF_USB_CBCTL_READ		1
#define BRCMF_USB_MAX_PKT_SIZE		1600
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61
BRCMF_FW_DEF(43143, "brcmfmac43143.bin");
BRCMF_FW_DEF(43236B, "brcmfmac43236b.bin");
BRCMF_FW_DEF(43242A, "brcmfmac43242a.bin");
BRCMF_FW_DEF(43569, "brcmfmac43569.bin");

static struct brcmf_firmware_mapping brcmf_usb_fwnames[] = {
	BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
	BRCMF_FW_ENTRY(BRCM_CC_43235_CHIP_ID, 0x00000008, 43236B),
	BRCMF_FW_ENTRY(BRCM_CC_43236_CHIP_ID, 0x00000008, 43236B),
	BRCMF_FW_ENTRY(BRCM_CC_43238_CHIP_ID, 0x00000008, 43236B),
	BRCMF_FW_ENTRY(BRCM_CC_43242_CHIP_ID, 0xFFFFFFFF, 43242A),
	BRCMF_FW_ENTRY(BRCM_CC_43566_CHIP_ID, 0xFFFFFFFF, 43569),
	BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43569)
};
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#define TRX_MAGIC		0x30524448	/* "HDR0" */
#define TRX_MAX_OFFSET		3		/* Max number of file offsets */
#define TRX_UNCOMP_IMAGE	0x20		/* Trx holds uncompressed img */
#define TRX_RDL_CHUNK		1500		/* size of each dl transfer */
#define TRX_OFFSETS_DLFWLEN_IDX	0

/* Control messages: bRequest values */
#define DL_GETSTATE	0	/* returns the rdl_state_t struct */
#define DL_CHECK_CRC	1	/* currently unused */
#define DL_GO		2	/* execute downloaded image */
#define DL_START	3	/* initialize dl state */
#define DL_REBOOT	4	/* reboot the device in 2 seconds */
#define DL_GETVER	5	/* returns the bootrom_id_t struct */
#define DL_GO_PROTECTED	6	/* execute the downloaded code and set reset
				 * event to occur in 2 seconds.  It is the
				 * responsibility of the downloaded code to
				 * clear this event
				 */
#define DL_EXEC		7	/* jump to a supplied address */
#define DL_RESETCFG	8	/* To support single enum on dongle
				 * - Not used by bootloader
				 */
#define DL_DEFER_RESP_OK 9	/* Potentially defer the response to setup
				 * if resp unavailable
				 */

/* states */
#define DL_WAITING	0	/* waiting to rx first pkt */
#define DL_READY	1	/* hdr was good, waiting for more of the
				 * compressed image
				 */
#define DL_BAD_HDR	2	/* hdr was corrupted */
#define DL_BAD_CRC	3	/* compressed image was corrupted */
#define DL_RUNNABLE	4	/* download was successful,waiting for go cmd */
#define DL_START_FAIL	5	/* failed to initialize correctly */
#define DL_NVRAM_TOOBIG	6	/* host specified nvram data exceeds DL_NVRAM
				 * value
				 */
#define DL_IMAGE_TOOBIG	7	/* firmware image too big */


struct trx_header_le {
	__le32 magic;		/* "HDR0" */
	__le32 len;		/* Length of file including header */
	__le32 crc32;		/* CRC from flag_version to end of file */
	__le32 flag_version;	/* 0:15 flags, 16:31 version */
	__le32 offsets[TRX_MAX_OFFSET];	/* Offsets of partitions from start of
					 * header
					 */
};

struct rdl_state_le {
	__le32 state;
	__le32 bytes;
};

struct bootrom_id_le {
	__le32 chip;		/* Chip id */
	__le32 chiprev;		/* Chip rev */
	__le32 ramsize;		/* Size of  RAM */
	__le32 remapbase;	/* Current remap base address */
	__le32 boardtype;	/* Type of board */
	__le32 boardrev;	/* Board revision */
};

128
struct brcmf_usb_image {
129 130 131 132
	struct list_head list;
	s8 *fwname;
	u8 *image;
	int image_len;
133 134 135 136 137 138 139 140 141
};

struct brcmf_usbdev_info {
	struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
	spinlock_t qlock;
	struct list_head rx_freeq;
	struct list_head rx_postq;
	struct list_head tx_freeq;
	struct list_head tx_postq;
142
	uint rx_pipe, tx_pipe;
143 144 145 146

	int rx_low_watermark;
	int tx_low_watermark;
	int tx_high_watermark;
147 148
	int tx_freecount;
	bool tx_flowblock;
149
	spinlock_t tx_flowblock_lock;
150 151 152 153

	struct brcmf_usbreq *tx_reqs;
	struct brcmf_usbreq *rx_reqs;

154
	char fw_name[BRCMF_FW_NAME_LEN];
155
	const u8 *image;	/* buffer for combine fw and nvram */
156 157 158 159
	int image_len;

	struct usb_device *usbdev;
	struct device *dev;
160
	struct mutex dev_init_lock;
161 162 163 164 165 166 167 168 169 170

	int ctl_in_pipe, ctl_out_pipe;
	struct urb *ctl_urb; /* URB for control endpoint */
	struct usb_ctrlrequest ctl_write;
	struct usb_ctrlrequest ctl_read;
	u32 ctl_urb_actual_length;
	int ctl_urb_status;
	int ctl_completed;
	wait_queue_head_t ioctl_resp_wait;
	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 180 181 182 183 184 185 186 187 188 189 190 191 192
};

static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
				struct brcmf_usbreq  *req);

static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
{
	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
	return bus_if->bus_priv.usb;
}

static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
{
	return brcmf_usb_get_buspub(dev)->devinfo;
}

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 205 206
}

static void
brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
{
207
	brcmf_dbg(USB, "Enter, status=%d\n", status);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

	if (unlikely(devinfo == NULL))
		return;

	if (type == BRCMF_USB_CBCTL_READ) {
		if (status == 0)
			devinfo->bus_pub.stats.rx_ctlpkts++;
		else
			devinfo->bus_pub.stats.rx_ctlerrs++;
	} else if (type == BRCMF_USB_CBCTL_WRITE) {
		if (status == 0)
			devinfo->bus_pub.stats.tx_ctlpkts++;
		else
			devinfo->bus_pub.stats.tx_ctlerrs++;
	}

	devinfo->ctl_urb_status = status;
	devinfo->ctl_completed = true;
	brcmf_usb_ioctl_resp_wake(devinfo);
}

static void
brcmf_usb_ctlread_complete(struct urb *urb)
{
	struct brcmf_usbdev_info *devinfo =
		(struct brcmf_usbdev_info *)urb->context;

235
	brcmf_dbg(USB, "Enter\n");
236 237 238 239 240 241 242 243 244 245 246
	devinfo->ctl_urb_actual_length = urb->actual_length;
	brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
		urb->status);
}

static void
brcmf_usb_ctlwrite_complete(struct urb *urb)
{
	struct brcmf_usbdev_info *devinfo =
		(struct brcmf_usbdev_info *)urb->context;

247
	brcmf_dbg(USB, "Enter\n");
248 249 250 251 252 253 254 255 256 257
	brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
		urb->status);
}

static int
brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
{
	int ret;
	u16 size;

258
	brcmf_dbg(USB, "Enter\n");
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	if (devinfo == NULL || buf == NULL ||
	    len == 0 || devinfo->ctl_urb == NULL)
		return -EINVAL;

	size = len;
	devinfo->ctl_write.wLength = cpu_to_le16p(&size);
	devinfo->ctl_urb->transfer_buffer_length = size;
	devinfo->ctl_urb_status = 0;
	devinfo->ctl_urb_actual_length = 0;

	usb_fill_control_urb(devinfo->ctl_urb,
		devinfo->usbdev,
		devinfo->ctl_out_pipe,
		(unsigned char *) &devinfo->ctl_write,
		buf, size,
		(usb_complete_t)brcmf_usb_ctlwrite_complete,
		devinfo);

	ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
	if (ret < 0)
279
		brcmf_err("usb_submit_urb failed %d\n", ret);
280 281 282 283 284 285 286 287 288 289

	return ret;
}

static int
brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
{
	int ret;
	u16 size;

290
	brcmf_dbg(USB, "Enter\n");
291 292 293 294 295 296 297 298
	if ((devinfo == NULL) || (buf == NULL) || (len == 0)
		|| (devinfo->ctl_urb == NULL))
		return -EINVAL;

	size = len;
	devinfo->ctl_read.wLength = cpu_to_le16p(&size);
	devinfo->ctl_urb->transfer_buffer_length = size;

299 300 301
	devinfo->ctl_read.bRequestType = USB_DIR_IN
		| USB_TYPE_CLASS | USB_RECIP_INTERFACE;
	devinfo->ctl_read.bRequest = 1;
302 303 304 305 306 307 308 309 310 311 312

	usb_fill_control_urb(devinfo->ctl_urb,
		devinfo->usbdev,
		devinfo->ctl_in_pipe,
		(unsigned char *) &devinfo->ctl_read,
		buf, size,
		(usb_complete_t)brcmf_usb_ctlread_complete,
		devinfo);

	ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
	if (ret < 0)
313
		brcmf_err("usb_submit_urb failed %d\n", ret);
314 315 316 317 318 319 320 321 322 323

	return ret;
}

static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
{
	int err = 0;
	int timeout = 0;
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);

324
	brcmf_dbg(USB, "Enter\n");
H
Hante Meuleman 已提交
325
	if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
326 327 328 329 330
		return -EIO;

	if (test_and_set_bit(0, &devinfo->ctl_op))
		return -EIO;

331
	devinfo->ctl_completed = false;
332 333
	err = brcmf_usb_send_ctl(devinfo, buf, len);
	if (err) {
334
		brcmf_err("fail %d bytes: %d\n", err, len);
335
		clear_bit(0, &devinfo->ctl_op);
336 337
		return err;
	}
338
	timeout = brcmf_usb_ioctl_resp_wait(devinfo);
339 340
	clear_bit(0, &devinfo->ctl_op);
	if (!timeout) {
341
		brcmf_err("Txctl wait timed out\n");
342 343 344 345 346 347 348 349 350 351 352
		err = -EIO;
	}
	return err;
}

static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
{
	int err = 0;
	int timeout = 0;
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);

353
	brcmf_dbg(USB, "Enter\n");
H
Hante Meuleman 已提交
354
	if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
355
		return -EIO;
H
Hante Meuleman 已提交
356

357 358 359
	if (test_and_set_bit(0, &devinfo->ctl_op))
		return -EIO;

360
	devinfo->ctl_completed = false;
361 362
	err = brcmf_usb_recv_ctl(devinfo, buf, len);
	if (err) {
363
		brcmf_err("fail %d bytes: %d\n", err, len);
364
		clear_bit(0, &devinfo->ctl_op);
365 366
		return err;
	}
367
	timeout = brcmf_usb_ioctl_resp_wait(devinfo);
368 369 370
	err = devinfo->ctl_urb_status;
	clear_bit(0, &devinfo->ctl_op);
	if (!timeout) {
371
		brcmf_err("rxctl wait timed out\n");
372 373 374 375 376 377 378 379 380
		err = -EIO;
	}
	if (!err)
		return devinfo->ctl_urb_actual_length;
	else
		return err;
}

static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
381
					  struct list_head *q, int *counter)
382 383 384 385 386 387 388 389 390 391
{
	unsigned long flags;
	struct brcmf_usbreq  *req;
	spin_lock_irqsave(&devinfo->qlock, flags);
	if (list_empty(q)) {
		spin_unlock_irqrestore(&devinfo->qlock, flags);
		return NULL;
	}
	req = list_entry(q->next, struct brcmf_usbreq, list);
	list_del_init(q->next);
392 393
	if (counter)
		(*counter)--;
394 395 396 397 398 399
	spin_unlock_irqrestore(&devinfo->qlock, flags);
	return req;

}

static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
400 401
			  struct list_head *q, struct brcmf_usbreq *req,
			  int *counter)
402 403 404 405
{
	unsigned long flags;
	spin_lock_irqsave(&devinfo->qlock, flags);
	list_add_tail(&req->list, q);
406 407
	if (counter)
		(*counter)++;
408 409 410 411 412 413 414 415 416
	spin_unlock_irqrestore(&devinfo->qlock, flags);
}

static struct brcmf_usbreq *
brcmf_usbdev_qinit(struct list_head *q, int qsize)
{
	int i;
	struct brcmf_usbreq *req, *reqs;

417 418
	reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
	if (reqs == NULL)
419
		return NULL;
420

421 422 423 424 425 426 427 428 429 430 431 432 433
	req = reqs;

	for (i = 0; i < qsize; i++) {
		req->urb = usb_alloc_urb(0, GFP_ATOMIC);
		if (!req->urb)
			goto fail;

		INIT_LIST_HEAD(&req->list);
		list_add_tail(&req->list, q);
		req++;
	}
	return reqs;
fail:
434
	brcmf_err("fail!\n");
435 436
	while (!list_empty(q)) {
		req = list_entry(q->next, struct brcmf_usbreq, list);
437
		if (req)
438 439 440 441 442 443 444 445 446 447 448 449
			usb_free_urb(req->urb);
		list_del(q->next);
	}
	return NULL;

}

static void brcmf_usb_free_q(struct list_head *q, bool pending)
{
	struct brcmf_usbreq *req, *next;
	int i = 0;
	list_for_each_entry_safe(req, next, q, list) {
450
		if (!req->urb) {
451
			brcmf_err("bad req\n");
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
			break;
		}
		i++;
		if (pending) {
			usb_kill_urb(req->urb);
		} else {
			usb_free_urb(req->urb);
			list_del_init(&req->list);
		}
	}
}

static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
				struct brcmf_usbreq *req)
{
	unsigned long flags;

	spin_lock_irqsave(&devinfo->qlock, flags);
	list_del_init(&req->list);
	spin_unlock_irqrestore(&devinfo->qlock, flags);
}


static void brcmf_usb_tx_complete(struct urb *urb)
{
	struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
	struct brcmf_usbdev_info *devinfo = req->devinfo;
479
	unsigned long flags;
480

481 482
	brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
		  req->skb);
483 484
	brcmf_usb_del_fromq(devinfo, req);

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 490 491 492 493
	if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
		devinfo->tx_flowblock) {
		brcmf_txflowblock(devinfo->dev, false);
		devinfo->tx_flowblock = false;
	}
494
	spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
495 496 497 498 499 500 501 502
}

static void brcmf_usb_rx_complete(struct urb *urb)
{
	struct brcmf_usbreq  *req = (struct brcmf_usbreq *)urb->context;
	struct brcmf_usbdev_info *devinfo = req->devinfo;
	struct sk_buff *skb;

503
	brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
504 505 506 507
	brcmf_usb_del_fromq(devinfo, req);
	skb = req->skb;
	req->skb = NULL;

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 513 514
		return;
	}

H
Hante Meuleman 已提交
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	}
	return;

}

static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
				struct brcmf_usbreq  *req)
{
	struct sk_buff *skb;
	int ret;

	if (!req || !devinfo)
		return;

	skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
	if (!skb) {
538
		brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
539 540 541 542 543 544 545 546
		return;
	}
	req->skb = skb;

	usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
			  skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
			  req);
	req->devinfo = devinfo;
547
	brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
548 549

	ret = usb_submit_urb(req->urb, GFP_ATOMIC);
550 551
	if (ret) {
		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 557 558 559 560 561 562
	}
	return;
}

static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
{
	struct brcmf_usbreq *req;

H
Hante Meuleman 已提交
563
	if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
564
		brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
565 566
		return;
	}
567
	while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
568 569 570 571 572 573 574 575 576
		brcmf_usb_rx_refill(devinfo, req);
}

static void
brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
{
	struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
	int old_state;

577 578
	brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
		  devinfo->bus_pub.state, state);
579 580 581 582 583

	if (devinfo->bus_pub.state == state)
		return;

	old_state = devinfo->bus_pub.state;
H
Hante Meuleman 已提交
584
	devinfo->bus_pub.state = state;
585 586

	/* update state of upper layer */
H
Hante Meuleman 已提交
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);
H
Hante Meuleman 已提交
590 591
	} else if (state == BRCMFMAC_USB_STATE_UP) {
		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 599 600 601 602
	}
}

static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
{
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
	struct brcmf_usbreq  *req;
	int ret;
603
	unsigned long flags;
604

605
	brcmf_dbg(USB, "Enter, skb=%p\n", skb);
606 607 608 609
	if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
		ret = -EIO;
		goto fail;
	}
610

611 612
	req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
					&devinfo->tx_freecount);
613
	if (!req) {
614
		brcmf_err("no req to send\n");
615 616
		ret = -ENOMEM;
		goto fail;
617 618 619 620 621 622 623
	}

	req->skb = skb;
	req->devinfo = devinfo;
	usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
			  skb->data, skb->len, brcmf_usb_tx_complete, req);
	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 632
			      &devinfo->tx_freecount);
		goto fail;
633 634
	}

635
	spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
636 637 638 639 640
	if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
	    !devinfo->tx_flowblock) {
		brcmf_txflowblock(dev, true);
		devinfo->tx_flowblock = true;
	}
641
	spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
642 643 644
	return 0;

fail:
645 646 647 648 649 650 651 652
	return ret;
}


static int brcmf_usb_up(struct device *dev)
{
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);

653
	brcmf_dbg(USB, "Enter\n");
H
Hante Meuleman 已提交
654
	if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
655 656
		return 0;

657
	/* Success, indicate devinfo is fully up */
H
Hante Meuleman 已提交
658
	brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
659 660 661 662 663 664 665 666 667 668

	if (devinfo->ctl_urb) {
		devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
		devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);

		/* CTL Write */
		devinfo->ctl_write.bRequestType =
			USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
		devinfo->ctl_write.bRequest = 0;
		devinfo->ctl_write.wValue = cpu_to_le16(0);
669
		devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
670 671 672 673 674 675

		/* CTL Read */
		devinfo->ctl_read.bRequestType =
			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
		devinfo->ctl_read.bRequest = 1;
		devinfo->ctl_read.wValue = cpu_to_le16(0);
676
		devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
677 678 679 680 681
	}
	brcmf_usb_rx_fill_all(devinfo);
	return 0;
}

682 683 684 685 686 687 688 689 690 691
static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
{
	if (devinfo->ctl_urb)
		usb_kill_urb(devinfo->ctl_urb);
	if (devinfo->bulk_urb)
		usb_kill_urb(devinfo->bulk_urb);
	brcmf_usb_free_q(&devinfo->tx_postq, true);
	brcmf_usb_free_q(&devinfo->rx_postq, true);
}

692 693 694 695
static void brcmf_usb_down(struct device *dev)
{
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);

696
	brcmf_dbg(USB, "Enter\n");
697 698 699
	if (devinfo == NULL)
		return;

H
Hante Meuleman 已提交
700
	if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
701 702
		return;

H
Hante Meuleman 已提交
703
	brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
704

705
	brcmf_cancel_all_urbs(devinfo);
706 707 708 709 710 711 712 713
}

static void
brcmf_usb_sync_complete(struct urb *urb)
{
	struct brcmf_usbdev_info *devinfo =
			(struct brcmf_usbdev_info *)urb->context;

714 715
	devinfo->ctl_completed = true;
	brcmf_usb_ioctl_resp_wake(devinfo);
716 717
}

718 719
static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
			    void *buffer, int buflen)
720
{
721
	int ret;
722 723 724 725
	char *tmpbuf;
	u16 size;

	if ((!devinfo) || (devinfo->ctl_urb == NULL))
726
		return -EINVAL;
727 728 729

	tmpbuf = kmalloc(buflen, GFP_ATOMIC);
	if (!tmpbuf)
730
		return -ENOMEM;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

	size = buflen;
	devinfo->ctl_urb->transfer_buffer_length = size;

	devinfo->ctl_read.wLength = cpu_to_le16p(&size);
	devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
		USB_RECIP_INTERFACE;
	devinfo->ctl_read.bRequest = cmd;

	usb_fill_control_urb(devinfo->ctl_urb,
		devinfo->usbdev,
		usb_rcvctrlpipe(devinfo->usbdev, 0),
		(unsigned char *) &devinfo->ctl_read,
		(void *) tmpbuf, size,
		(usb_complete_t)brcmf_usb_sync_complete, devinfo);

747
	devinfo->ctl_completed = false;
748 749
	ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
	if (ret < 0) {
750
		brcmf_err("usb_submit_urb failed %d\n", ret);
751
		goto finalize;
752 753
	}

754 755
	if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
		usb_kill_urb(devinfo->ctl_urb);
756
		ret = -ETIMEDOUT;
757
	} else {
758
		memcpy(buffer, tmpbuf, buflen);
759
	}
760

761 762
finalize:
	kfree(tmpbuf);
763
	return ret;
764 765 766 767 768 769 770 771
}

static bool
brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
{
	struct bootrom_id_le id;
	u32 chipid, chiprev;

772
	brcmf_dbg(USB, "Enter\n");
773 774 775 776 777 778

	if (devinfo == NULL)
		return false;

	/* Check if firmware downloaded already by querying runtime ID */
	id.chip = cpu_to_le32(0xDEAD);
779
	brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
780 781 782 783 784

	chipid = le32_to_cpu(id.chip);
	chiprev = le32_to_cpu(id.chiprev);

	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 792
		return false;
	} else {
793 794
		devinfo->bus_pub.devid = chipid;
		devinfo->bus_pub.chiprev = chiprev;
795 796 797 798 799 800 801 802
	}
	return true;
}

static int
brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
{
	struct bootrom_id_le id;
803
	u32 loop_cnt;
804
	int err;
805

806
	brcmf_dbg(USB, "Enter\n");
807

808 809 810 811
	loop_cnt = 0;
	do {
		mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
		loop_cnt++;
812
		id.chip = cpu_to_le32(0xDEAD);       /* Get the ID */
813 814 815
		err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
		if ((err) && (err != -ETIMEDOUT))
			return err;
816 817
		if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
			break;
818
	} while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
819 820

	if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
821 822
		brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
			  le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
823

824
		brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
825 826
		return 0;
	} else {
827
		brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
828
			  BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		return -EINVAL;
	}
}


static int
brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
{
	int ret;

	if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
		return -EINVAL;

	/* Prepare the URB */
	usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
			  devinfo->tx_pipe, buffer, len,
			  (usb_complete_t)brcmf_usb_sync_complete, devinfo);

	devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;

849
	devinfo->ctl_completed = false;
850 851
	ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
	if (ret) {
852
		brcmf_err("usb_submit_urb failed %d\n", ret);
853 854
		return ret;
	}
855 856
	ret = brcmf_usb_ioctl_resp_wait(devinfo);
	return (ret == 0);
857 858 859 860 861 862 863 864 865 866
}

static int
brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
{
	unsigned int sendlen, sent, dllen;
	char *bulkchunk = NULL, *dlpos;
	struct rdl_state_le state;
	u32 rdlstate, rdlbytes;
	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 872 873 874 875 876
	if (bulkchunk == NULL) {
		err = -ENOMEM;
		goto fail;
	}

	/* 1) Prepare USB boot loader for runtime image */
877
	brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
878 879 880 881 882 883

	rdlstate = le32_to_cpu(state.state);
	rdlbytes = le32_to_cpu(state.bytes);

	/* 2) Check we are in the Waiting state */
	if (rdlstate != DL_WAITING) {
884
		brcmf_err("Failed to DL_START\n");
885 886 887 888 889 890 891 892 893 894 895 896
		err = -EINVAL;
		goto fail;
	}
	sent = 0;
	dlpos = fw;
	dllen = fwlen;

	/* Get chip id and rev */
	while (rdlbytes != dllen) {
		/* Wait until the usb device reports it received all
		 * the bytes we sent */
		if ((rdlbytes == sent) && (rdlbytes != dllen)) {
897
			if ((dllen-sent) < TRX_RDL_CHUNK)
898 899
				sendlen = dllen-sent;
			else
900
				sendlen = TRX_RDL_CHUNK;
901 902 903 904 905 906 907 908 909 910 911 912

			/* simply avoid having to send a ZLP by ensuring we
			 * never have an even
			 * multiple of 64
			 */
			if (!(sendlen % 64))
				sendlen -= 4;

			/* send data */
			memcpy(bulkchunk, dlpos, sendlen);
			if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
						   sendlen)) {
913
				brcmf_err("send_bulk failed\n");
914 915 916 917 918 919 920
				err = -EINVAL;
				goto fail;
			}

			dlpos += sendlen;
			sent += sendlen;
		}
921 922 923 924
		err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
				       sizeof(state));
		if (err) {
			brcmf_err("DL_GETSTATE Failed\n");
925 926 927 928 929 930 931 932
			goto fail;
		}

		rdlstate = le32_to_cpu(state.state);
		rdlbytes = le32_to_cpu(state.bytes);

		/* restart if an error is reported */
		if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
933
			brcmf_err("Bad Hdr or Bad CRC state %d\n",
934 935 936 937 938 939 940 941
				  rdlstate);
			err = -EINVAL;
			goto fail;
		}
	}

fail:
	kfree(bulkchunk);
942
	brcmf_dbg(USB, "Exit, err=%d\n", err);
943 944 945 946 947 948 949
	return err;
}

static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
{
	int err;

950
	brcmf_dbg(USB, "Enter\n");
951 952 953 954

	if (devinfo == NULL)
		return -EINVAL;

955
	if (devinfo->bus_pub.devid == 0xDEAD)
956 957 958 959
		return -EINVAL;

	err = brcmf_usb_dl_writeimage(devinfo, fw, len);
	if (err == 0)
H
Hante Meuleman 已提交
960
		devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
961
	else
H
Hante Meuleman 已提交
962
		devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
963
	brcmf_dbg(USB, "Exit, err=%d\n", err);
964 965 966 967 968 969 970 971

	return err;
}

static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
{
	struct rdl_state_le state;

972
	brcmf_dbg(USB, "Enter\n");
973 974 975
	if (!devinfo)
		return -EINVAL;

976
	if (devinfo->bus_pub.devid == 0xDEAD)
977 978 979
		return -EINVAL;

	/* Check we are runnable */
980 981
	state.state = 0;
	brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
982 983 984

	/* Start the image */
	if (state.state == cpu_to_le32(DL_RUNNABLE)) {
985
		if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
986 987 988 989 990
			return -ENODEV;
		if (brcmf_usb_resetcfg(devinfo))
			return -ENODEV;
		/* The Dongle may go for re-enumeration. */
	} else {
991
		brcmf_err("Dongle not runnable\n");
992 993
		return -EINVAL;
	}
994
	brcmf_dbg(USB, "Exit\n");
995 996 997 998 999 1000 1001 1002
	return 0;
}

static int
brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
{
	int err;

1003
	brcmf_dbg(USB, "Enter\n");
1004 1005 1006 1007
	if (devinfo == NULL)
		return -ENODEV;

	if (!devinfo->image) {
1008
		brcmf_err("No firmware!\n");
1009 1010 1011 1012
		return -ENOENT;
	}

	err = brcmf_usb_dlstart(devinfo,
1013
		(u8 *)devinfo->image, devinfo->image_len);
1014 1015 1016 1017 1018 1019
	if (err == 0)
		err = brcmf_usb_dlrun(devinfo);
	return err;
}


1020
static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1021
{
1022
	brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

	/* free the URBS */
	brcmf_usb_free_q(&devinfo->rx_freeq, false);
	brcmf_usb_free_q(&devinfo->tx_freeq, false);

	usb_free_urb(devinfo->ctl_urb);
	usb_free_urb(devinfo->bulk_urb);

	kfree(devinfo->tx_reqs);
	kfree(devinfo->rx_reqs);
1033 1034 1035

	if (devinfo->settings)
		brcmf_release_module_param(devinfo->settings);
1036 1037 1038 1039 1040 1041 1042 1043
}


static int check_file(const u8 *headers)
{
	struct trx_header_le *trx;
	int actual_len = -1;

1044
	brcmf_dbg(USB, "Enter\n");
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	/* Extract trx header */
	trx = (struct trx_header_le *) headers;
	if (trx->magic != cpu_to_le32(TRX_MAGIC))
		return -1;

	headers += sizeof(struct trx_header_le);

	if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
		actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
		return actual_len + sizeof(struct trx_header_le);
	}
	return -1;
}


static
1061 1062
struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
				      int nrxq, int ntxq)
1063
{
1064 1065
	brcmf_dbg(USB, "Enter\n");

1066 1067 1068 1069
	devinfo->bus_pub.nrxq = nrxq;
	devinfo->rx_low_watermark = nrxq / 2;
	devinfo->bus_pub.devinfo = devinfo;
	devinfo->bus_pub.ntxq = ntxq;
H
Hante Meuleman 已提交
1070
	devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

	/* flow control when too many tx urbs posted */
	devinfo->tx_low_watermark = ntxq / 4;
	devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
	devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;

	/* Initialize other structure content */
	init_waitqueue_head(&devinfo->ioctl_resp_wait);

	/* Initialize the spinlocks */
	spin_lock_init(&devinfo->qlock);
1082
	spin_lock_init(&devinfo->tx_flowblock_lock);
1083 1084 1085 1086 1087 1088 1089

	INIT_LIST_HEAD(&devinfo->rx_freeq);
	INIT_LIST_HEAD(&devinfo->rx_postq);

	INIT_LIST_HEAD(&devinfo->tx_freeq);
	INIT_LIST_HEAD(&devinfo->tx_postq);

1090 1091
	devinfo->tx_flowblock = false;

1092 1093 1094 1095 1096 1097 1098
	devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
	if (!devinfo->rx_reqs)
		goto error;

	devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
	if (!devinfo->tx_reqs)
		goto error;
1099
	devinfo->tx_freecount = ntxq;
1100 1101

	devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1102
	if (!devinfo->ctl_urb)
1103 1104
		goto error;
	devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1105
	if (!devinfo->bulk_urb)
1106 1107 1108 1109 1110
		goto error;

	return &devinfo->bus_pub;

error:
1111
	brcmf_err("failed!\n");
1112
	brcmf_usb_detach(devinfo);
1113 1114 1115
	return NULL;
}

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
static void brcmf_usb_wowl_config(struct device *dev, bool enabled)
{
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);

	brcmf_dbg(USB, "Configuring WOWL, enabled=%d\n", enabled);
	devinfo->wowl_enabled = enabled;
	if (enabled)
		device_set_wakeup_enable(devinfo->dev, true);
	else
		device_set_wakeup_enable(devinfo->dev, false);
}

1128
static const struct brcmf_bus_ops brcmf_usb_bus_ops = {
A
Arend van Spriel 已提交
1129 1130 1131 1132
	.txdata = brcmf_usb_tx,
	.stop = brcmf_usb_down,
	.txctl = brcmf_usb_tx_ctlpkt,
	.rxctl = brcmf_usb_rx_ctlpkt,
1133
	.wowl_config = brcmf_usb_wowl_config,
A
Arend van Spriel 已提交
1134 1135
};

1136 1137 1138 1139 1140
static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
{
	int ret;

	/* Attach to the common driver interface */
1141
	ret = brcmf_attach(devinfo->dev, devinfo->settings);
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	if (ret) {
		brcmf_err("brcmf_attach failed\n");
		return ret;
	}

	ret = brcmf_usb_up(devinfo->dev);
	if (ret)
		goto fail;

	ret = brcmf_bus_start(devinfo->dev);
	if (ret)
		goto fail;

	return 0;
fail:
	brcmf_detach(devinfo->dev);
	return ret;
}

1161 1162 1163 1164 1165 1166 1167 1168 1169
static void brcmf_usb_probe_phase2(struct device *dev,
				   const struct firmware *fw,
				   void *nvram, u32 nvlen)
{
	struct brcmf_bus *bus = dev_get_drvdata(dev);
	struct brcmf_usbdev_info *devinfo;
	int ret;

	brcmf_dbg(USB, "Start fw downloading\n");
1170 1171

	devinfo = bus->bus_priv.usb->devinfo;
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	ret = check_file(fw->data);
	if (ret < 0) {
		brcmf_err("invalid firmware\n");
		release_firmware(fw);
		goto error;
	}

	devinfo->image = fw->data;
	devinfo->image_len = fw->size;

	ret = brcmf_usb_fw_download(devinfo);
	release_firmware(fw);
	if (ret)
		goto error;

	ret = brcmf_usb_bus_setup(devinfo);
	if (ret)
		goto error;

1191
	mutex_unlock(&devinfo->dev_init_lock);
1192 1193 1194
	return;
error:
	brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1195
	mutex_unlock(&devinfo->dev_init_lock);
1196 1197 1198
	device_release_driver(dev);
}

H
Hante Meuleman 已提交
1199
static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1200 1201 1202
{
	struct brcmf_bus *bus = NULL;
	struct brcmf_usbdev *bus_pub = NULL;
1203
	struct device *dev = devinfo->dev;
1204
	int ret;
1205

1206
	brcmf_dbg(USB, "Enter\n");
1207
	bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1208 1209
	if (!bus_pub)
		return -ENODEV;
1210 1211 1212 1213 1214 1215 1216

	bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
	if (!bus) {
		ret = -ENOMEM;
		goto fail;
	}

A
Arend van Spriel 已提交
1217
	bus->dev = dev;
1218 1219 1220
	bus_pub->bus = bus;
	bus->bus_priv.usb = bus_pub;
	dev_set_drvdata(dev, bus);
A
Arend van Spriel 已提交
1221
	bus->ops = &brcmf_usb_bus_ops;
1222
	bus->proto_type = BRCMF_PROTO_BCDC;
1223
	bus->always_use_fws_queue = true;
1224 1225 1226
#ifdef CONFIG_PM
	bus->wowl_supported = true;
#endif
1227

1228 1229 1230 1231 1232 1233 1234 1235
	devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
						   bus_pub->devid,
						   bus_pub->chiprev);
	if (!devinfo->settings) {
		ret = -ENOMEM;
		goto fail;
	}

1236 1237 1238 1239
	if (!brcmf_usb_dlneeded(devinfo)) {
		ret = brcmf_usb_bus_setup(devinfo);
		if (ret)
			goto fail;
1240
		/* we are done */
1241
		mutex_unlock(&devinfo->dev_init_lock);
1242
		return 0;
1243
	}
1244 1245 1246
	bus->chip = bus_pub->devid;
	bus->chiprev = bus_pub->chiprev;

1247 1248 1249 1250 1251 1252 1253
	ret = brcmf_fw_map_chip_to_name(bus_pub->devid, bus_pub->chiprev,
					brcmf_usb_fwnames,
					ARRAY_SIZE(brcmf_usb_fwnames),
					devinfo->fw_name, NULL);
	if (ret)
		goto fail;

1254
	/* request firmware here */
1255 1256
	ret = brcmf_fw_get_firmwares(dev, 0, devinfo->fw_name, NULL,
				     brcmf_usb_probe_phase2);
1257 1258 1259 1260 1261
	if (ret) {
		brcmf_err("firmware request failed: %d\n", ret);
		goto fail;
	}

1262
	return 0;
1263

1264 1265 1266
fail:
	/* Release resources in reverse order */
	kfree(bus);
1267
	brcmf_usb_detach(devinfo);
1268 1269 1270 1271
	return ret;
}

static void
1272
brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1273
{
1274
	if (!devinfo)
1275
		return;
1276
	brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1277

1278 1279 1280
	brcmf_detach(devinfo->dev);
	kfree(devinfo->bus_pub.bus);
	brcmf_usb_detach(devinfo);
1281 1282 1283 1284 1285 1286
}

static int
brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *usb = interface_to_usbdev(intf);
1287
	struct brcmf_usbdev_info *devinfo;
1288 1289 1290 1291 1292
	struct usb_interface_descriptor	*desc;
	struct usb_endpoint_descriptor *endpoint;
	int ret = 0;
	u32 num_of_eps;
	u8 endpoint_num, ep;
1293

1294
	brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1295

1296 1297 1298
	devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
	if (devinfo == NULL)
		return -ENOMEM;
1299

1300 1301
	devinfo->usbdev = usb;
	devinfo->dev = &usb->dev;
1302 1303 1304 1305 1306 1307
	/* Take an init lock, to protect for disconnect while still loading.
	 * Necessary because of the asynchronous firmware load construction
	 */
	mutex_init(&devinfo->dev_init_lock);
	mutex_lock(&devinfo->dev_init_lock);

1308
	usb_set_intfdata(intf, devinfo);
1309 1310 1311

	/* Check that the device supports only one configuration */
	if (usb->descriptor.bNumConfigurations != 1) {
1312 1313 1314
		brcmf_err("Number of configurations: %d not supported\n",
			  usb->descriptor.bNumConfigurations);
		ret = -ENODEV;
1315 1316 1317
		goto fail;
	}

1318 1319 1320 1321 1322 1323
	if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
	    (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
	    (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
		brcmf_err("Device class: 0x%x not supported\n",
			  usb->descriptor.bDeviceClass);
		ret = -ENODEV;
1324 1325 1326
		goto fail;
	}

1327 1328 1329 1330 1331 1332 1333 1334
	desc = &intf->altsetting[0].desc;
	if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
	    (desc->bInterfaceSubClass != 2) ||
	    (desc->bInterfaceProtocol != 0xff)) {
		brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
			  desc->bInterfaceNumber, desc->bInterfaceClass,
			  desc->bInterfaceSubClass, desc->bInterfaceProtocol);
		ret = -ENODEV;
1335 1336 1337
		goto fail;
	}

1338 1339 1340 1341 1342 1343 1344 1345
	num_of_eps = desc->bNumEndpoints;
	for (ep = 0; ep < num_of_eps; ep++) {
		endpoint = &intf->altsetting[0].endpoint[ep].desc;
		endpoint_num = usb_endpoint_num(endpoint);
		if (!usb_endpoint_xfer_bulk(endpoint))
			continue;
		if (usb_endpoint_dir_in(endpoint)) {
			if (!devinfo->rx_pipe)
1346
				devinfo->rx_pipe =
1347 1348
					usb_rcvbulkpipe(usb, endpoint_num);
		} else {
1349 1350 1351
			if (!devinfo->tx_pipe)
				devinfo->tx_pipe =
					usb_sndbulkpipe(usb, endpoint_num);
1352 1353
		}
	}
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	if (devinfo->rx_pipe == 0) {
		brcmf_err("No RX (in) Bulk EP found\n");
		ret = -ENODEV;
		goto fail;
	}
	if (devinfo->tx_pipe == 0) {
		brcmf_err("No TX (out) Bulk EP found\n");
		ret = -ENODEV;
		goto fail;
	}

	devinfo->ifnum = desc->bInterfaceNumber;
1366

O
Oliver Neukum 已提交
1367 1368 1369
	if (usb->speed == USB_SPEED_SUPER_PLUS)
		brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n");
	else if (usb->speed == USB_SPEED_SUPER)
1370
		brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1371
	else if (usb->speed == USB_SPEED_HIGH)
1372
		brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1373
	else
1374
		brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1375

H
Hante Meuleman 已提交
1376
	ret = brcmf_usb_probe_cb(devinfo);
1377 1378 1379 1380 1381 1382 1383
	if (ret)
		goto fail;

	/* Success */
	return 0;

fail:
1384
	mutex_unlock(&devinfo->dev_init_lock);
1385
	kfree(devinfo);
1386 1387 1388 1389 1390 1391 1392
	usb_set_intfdata(intf, NULL);
	return ret;
}

static void
brcmf_usb_disconnect(struct usb_interface *intf)
{
1393
	struct brcmf_usbdev_info *devinfo;
1394

1395
	brcmf_dbg(USB, "Enter\n");
1396
	devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

	if (devinfo) {
		mutex_lock(&devinfo->dev_init_lock);
		/* Make sure that devinfo still exists. Firmware probe routines
		 * may have released the device and cleared the intfdata.
		 */
		if (!usb_get_intfdata(intf))
			goto done;

		brcmf_usb_disconnect_cb(devinfo);
		kfree(devinfo);
	}
done:
1410
	brcmf_dbg(USB, "Exit\n");
1411 1412 1413
}

/*
H
Hante Meuleman 已提交
1414
 * only need to signal the bus being down and update the state.
1415 1416 1417 1418 1419 1420
 */
static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
{
	struct usb_device *usb = interface_to_usbdev(intf);
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);

1421
	brcmf_dbg(USB, "Enter\n");
H
Hante Meuleman 已提交
1422
	devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1423 1424 1425 1426
	if (devinfo->wowl_enabled)
		brcmf_cancel_all_urbs(devinfo);
	else
		brcmf_detach(&usb->dev);
1427 1428 1429 1430
	return 0;
}

/*
H
Hante Meuleman 已提交
1431
 * (re-) start the bus.
1432 1433 1434 1435 1436 1437
 */
static int brcmf_usb_resume(struct usb_interface *intf)
{
	struct usb_device *usb = interface_to_usbdev(intf);
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);

1438
	brcmf_dbg(USB, "Enter\n");
1439 1440 1441 1442 1443 1444
	if (!devinfo->wowl_enabled)
		return brcmf_usb_bus_setup(devinfo);

	devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
	brcmf_usb_rx_fill_all(devinfo);
	return 0;
1445 1446
}

H
Hante Meuleman 已提交
1447 1448 1449 1450
static int brcmf_usb_reset_resume(struct usb_interface *intf)
{
	struct usb_device *usb = interface_to_usbdev(intf);
	struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1451

H
Hante Meuleman 已提交
1452 1453
	brcmf_dbg(USB, "Enter\n");

1454
	return brcmf_fw_get_firmwares(&usb->dev, 0, devinfo->fw_name, NULL,
1455
				      brcmf_usb_probe_phase2);
H
Hante Meuleman 已提交
1456 1457
}

1458 1459
#define BRCMF_USB_DEVICE(dev_id)	\
	{ USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1460

1461 1462 1463
#define LINKSYS_USB_DEVICE(dev_id)	\
	{ USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }

1464
static struct usb_device_id brcmf_usb_devid_table[] = {
1465 1466 1467 1468
	BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
	BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
	BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
	BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1469
	LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID),
1470
	{ USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) },
1471
	/* special entry for device with firmware loaded and running */
1472 1473
	BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
	{ /* end: all zeroes */ }
1474
};
1475

1476 1477 1478 1479 1480 1481 1482 1483 1484
MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);

static struct usb_driver brcmf_usbdrvr = {
	.name = KBUILD_MODNAME,
	.probe = brcmf_usb_probe,
	.disconnect = brcmf_usb_disconnect,
	.id_table = brcmf_usb_devid_table,
	.suspend = brcmf_usb_suspend,
	.resume = brcmf_usb_resume,
H
Hante Meuleman 已提交
1485
	.reset_resume = brcmf_usb_reset_resume,
1486
	.disable_hub_initiated_lpm = 1,
1487 1488
};

1489 1490 1491 1492 1493 1494 1495 1496
static int brcmf_usb_reset_device(struct device *dev, void *notused)
{
	/* device past is the usb interface so we
	 * need to use parent here.
	 */
	brcmf_dev_reset(dev->parent);
	return 0;
}
1497

1498 1499
void brcmf_usb_exit(void)
{
1500 1501 1502
	struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
	int ret;

1503
	brcmf_dbg(USB, "Enter\n");
1504 1505
	ret = driver_for_each_device(drv, NULL, NULL,
				     brcmf_usb_reset_device);
1506 1507 1508
	usb_deregister(&brcmf_usbdrvr);
}

1509
void brcmf_usb_register(void)
1510
{
1511
	brcmf_dbg(USB, "Enter\n");
1512
	usb_register(&brcmf_usbdrvr);
1513
}