blob: dc135915470d2cb70efab5480d11a036a0e00d3f [file] [log] [blame]
Arend van Spriel71bb2442012-02-09 21:09:08 +01001/*
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
Arend van Spriel71bb2442012-02-09 21:09:08 +010017#include <linux/kernel.h>
18#include <linux/module.h>
Arend van Spriel71bb2442012-02-09 21:09:08 +010019#include <linux/firmware.h>
20#include <linux/usb.h>
John W. Linvillee2ff0492012-11-19 11:04:28 -050021#include <linux/vmalloc.h>
Arend van Spriel71bb2442012-02-09 21:09:08 +010022
Arend van Spriel71bb2442012-02-09 21:09:08 +010023#include <brcmu_utils.h>
Hante Meuleman5779ae62014-07-12 08:49:34 +020024#include <brcm_hw_ids.h>
Arend van Spriel71bb2442012-02-09 21:09:08 +010025#include <brcmu_wifi.h>
26#include <dhd_bus.h>
27#include <dhd_dbg.h>
28
Arend van Spriel5b8045d2014-05-27 12:56:23 +020029#include "firmware.h"
Arend van Spriel71bb2442012-02-09 21:09:08 +010030#include "usb_rdl.h"
31#include "usb.h"
32
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020033#define IOCTL_RESP_TIMEOUT 2000
Arend van Spriel71bb2442012-02-09 21:09:08 +010034
Hante Meuleman83bc9c312012-11-14 18:46:06 -080035#define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
36#define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
Arend van Spriel71bb2442012-02-09 21:09:08 +010037
38#define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
39 has boot up */
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020040#define BRCMF_USB_NRXQ 50
41#define BRCMF_USB_NTXQ 50
Arend van Spriel71bb2442012-02-09 21:09:08 +010042
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020043#define BRCMF_USB_CBCTL_WRITE 0
44#define BRCMF_USB_CBCTL_READ 1
45#define BRCMF_USB_MAX_PKT_SIZE 1600
Arend van Spriel71bb2442012-02-09 21:09:08 +010046
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020047#define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
48#define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
49#define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
50#define BRCMF_USB_43569_FW_NAME "brcm/brcmfmac43569.bin"
Arend van Spriel71bb2442012-02-09 21:09:08 +010051
Arend van Spriel71bb2442012-02-09 21:09:08 +010052struct brcmf_usb_image {
Hante Meuleman803599d2012-09-19 22:21:10 +020053 struct list_head list;
54 s8 *fwname;
55 u8 *image;
56 int image_len;
Arend van Spriel71bb2442012-02-09 21:09:08 +010057};
Arend van Spriel71bb2442012-02-09 21:09:08 +010058
Arend van Spriel71bb2442012-02-09 21:09:08 +010059struct brcmf_usbdev_info {
60 struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
61 spinlock_t qlock;
62 struct list_head rx_freeq;
63 struct list_head rx_postq;
64 struct list_head tx_freeq;
65 struct list_head tx_postq;
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020066 uint rx_pipe, tx_pipe;
Arend van Spriel71bb2442012-02-09 21:09:08 +010067
Arend van Spriel71bb2442012-02-09 21:09:08 +010068 int rx_low_watermark;
69 int tx_low_watermark;
70 int tx_high_watermark;
Hante Meulemanc6ab4292012-09-11 21:18:48 +020071 int tx_freecount;
72 bool tx_flowblock;
Hante Meulemandf50f752013-06-06 13:17:48 +020073 spinlock_t tx_flowblock_lock;
Arend van Spriel71bb2442012-02-09 21:09:08 +010074
75 struct brcmf_usbreq *tx_reqs;
76 struct brcmf_usbreq *rx_reqs;
77
Arend van Spriel5b8045d2014-05-27 12:56:23 +020078 const u8 *image; /* buffer for combine fw and nvram */
Arend van Spriel71bb2442012-02-09 21:09:08 +010079 int image_len;
80
Arend van Spriel71bb2442012-02-09 21:09:08 +010081 struct usb_device *usbdev;
82 struct device *dev;
Arend van Spriel71bb2442012-02-09 21:09:08 +010083
84 int ctl_in_pipe, ctl_out_pipe;
85 struct urb *ctl_urb; /* URB for control endpoint */
86 struct usb_ctrlrequest ctl_write;
87 struct usb_ctrlrequest ctl_read;
88 u32 ctl_urb_actual_length;
89 int ctl_urb_status;
90 int ctl_completed;
91 wait_queue_head_t ioctl_resp_wait;
Arend van Spriel71bb2442012-02-09 21:09:08 +010092 ulong ctl_op;
Hante Meuleman51c7f5e2014-06-21 12:11:15 +020093 u8 ifnum;
Arend van Spriel71bb2442012-02-09 21:09:08 +010094
Arend van Spriel71bb2442012-02-09 21:09:08 +010095 struct urb *bulk_urb; /* used for FW download */
Arend van Spriel71bb2442012-02-09 21:09:08 +010096};
97
98static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
99 struct brcmf_usbreq *req);
100
Arend van Spriel71bb2442012-02-09 21:09:08 +0100101static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
102{
103 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
104 return bus_if->bus_priv.usb;
105}
106
107static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
108{
109 return brcmf_usb_get_buspub(dev)->devinfo;
110}
111
Hante Meuleman70398a52012-09-27 14:17:47 +0200112static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100113{
Hante Meuleman70398a52012-09-27 14:17:47 +0200114 return wait_event_timeout(devinfo->ioctl_resp_wait,
115 devinfo->ctl_completed,
116 msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100117}
118
Hante Meuleman70398a52012-09-27 14:17:47 +0200119static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100120{
121 if (waitqueue_active(&devinfo->ioctl_resp_wait))
Hante Meuleman70398a52012-09-27 14:17:47 +0200122 wake_up(&devinfo->ioctl_resp_wait);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100123}
124
125static void
126brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
127{
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800128 brcmf_dbg(USB, "Enter, status=%d\n", status);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100129
130 if (unlikely(devinfo == NULL))
131 return;
132
133 if (type == BRCMF_USB_CBCTL_READ) {
134 if (status == 0)
135 devinfo->bus_pub.stats.rx_ctlpkts++;
136 else
137 devinfo->bus_pub.stats.rx_ctlerrs++;
138 } else if (type == BRCMF_USB_CBCTL_WRITE) {
139 if (status == 0)
140 devinfo->bus_pub.stats.tx_ctlpkts++;
141 else
142 devinfo->bus_pub.stats.tx_ctlerrs++;
143 }
144
145 devinfo->ctl_urb_status = status;
146 devinfo->ctl_completed = true;
147 brcmf_usb_ioctl_resp_wake(devinfo);
148}
149
150static void
151brcmf_usb_ctlread_complete(struct urb *urb)
152{
153 struct brcmf_usbdev_info *devinfo =
154 (struct brcmf_usbdev_info *)urb->context;
155
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800156 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100157 devinfo->ctl_urb_actual_length = urb->actual_length;
158 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
159 urb->status);
160}
161
162static void
163brcmf_usb_ctlwrite_complete(struct urb *urb)
164{
165 struct brcmf_usbdev_info *devinfo =
166 (struct brcmf_usbdev_info *)urb->context;
167
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800168 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100169 brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
170 urb->status);
171}
172
Arend van Spriel71bb2442012-02-09 21:09:08 +0100173static int
174brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
175{
176 int ret;
177 u16 size;
178
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800179 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100180 if (devinfo == NULL || buf == NULL ||
181 len == 0 || devinfo->ctl_urb == NULL)
182 return -EINVAL;
183
Arend van Spriel71bb2442012-02-09 21:09:08 +0100184 size = len;
185 devinfo->ctl_write.wLength = cpu_to_le16p(&size);
186 devinfo->ctl_urb->transfer_buffer_length = size;
187 devinfo->ctl_urb_status = 0;
188 devinfo->ctl_urb_actual_length = 0;
189
190 usb_fill_control_urb(devinfo->ctl_urb,
191 devinfo->usbdev,
192 devinfo->ctl_out_pipe,
193 (unsigned char *) &devinfo->ctl_write,
194 buf, size,
195 (usb_complete_t)brcmf_usb_ctlwrite_complete,
196 devinfo);
197
198 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
199 if (ret < 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100200 brcmf_err("usb_submit_urb failed %d\n", ret);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100201
202 return ret;
203}
204
205static int
206brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
207{
208 int ret;
209 u16 size;
210
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800211 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100212 if ((devinfo == NULL) || (buf == NULL) || (len == 0)
213 || (devinfo->ctl_urb == NULL))
214 return -EINVAL;
215
216 size = len;
217 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
218 devinfo->ctl_urb->transfer_buffer_length = size;
219
Hante Meuleman81643292012-09-19 22:21:14 +0200220 devinfo->ctl_read.bRequestType = USB_DIR_IN
221 | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
222 devinfo->ctl_read.bRequest = 1;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100223
224 usb_fill_control_urb(devinfo->ctl_urb,
225 devinfo->usbdev,
226 devinfo->ctl_in_pipe,
227 (unsigned char *) &devinfo->ctl_read,
228 buf, size,
229 (usb_complete_t)brcmf_usb_ctlread_complete,
230 devinfo);
231
232 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
233 if (ret < 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100234 brcmf_err("usb_submit_urb failed %d\n", ret);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100235
236 return ret;
237}
238
239static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
240{
241 int err = 0;
242 int timeout = 0;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100243 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
244
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800245 brcmf_dbg(USB, "Enter\n");
Hante Meuleman7c38e692012-11-14 18:46:07 -0800246 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100247 return -EIO;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100248
249 if (test_and_set_bit(0, &devinfo->ctl_op))
250 return -EIO;
251
Hante Meulemana77f5742012-08-30 19:42:58 +0200252 devinfo->ctl_completed = false;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100253 err = brcmf_usb_send_ctl(devinfo, buf, len);
254 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100255 brcmf_err("fail %d bytes: %d\n", err, len);
Hante Meuleman6385df22012-09-19 22:21:16 +0200256 clear_bit(0, &devinfo->ctl_op);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100257 return err;
258 }
Hante Meuleman70398a52012-09-27 14:17:47 +0200259 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100260 clear_bit(0, &devinfo->ctl_op);
261 if (!timeout) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100262 brcmf_err("Txctl wait timed out\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100263 err = -EIO;
264 }
265 return err;
266}
267
268static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
269{
270 int err = 0;
271 int timeout = 0;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100272 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
273
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800274 brcmf_dbg(USB, "Enter\n");
Hante Meuleman7c38e692012-11-14 18:46:07 -0800275 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100276 return -EIO;
Hante Meuleman7c38e692012-11-14 18:46:07 -0800277
Arend van Spriel71bb2442012-02-09 21:09:08 +0100278 if (test_and_set_bit(0, &devinfo->ctl_op))
279 return -EIO;
280
Hante Meuleman70398a52012-09-27 14:17:47 +0200281 devinfo->ctl_completed = false;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100282 err = brcmf_usb_recv_ctl(devinfo, buf, len);
283 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100284 brcmf_err("fail %d bytes: %d\n", err, len);
Hante Meuleman6385df22012-09-19 22:21:16 +0200285 clear_bit(0, &devinfo->ctl_op);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100286 return err;
287 }
Hante Meuleman70398a52012-09-27 14:17:47 +0200288 timeout = brcmf_usb_ioctl_resp_wait(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100289 err = devinfo->ctl_urb_status;
290 clear_bit(0, &devinfo->ctl_op);
291 if (!timeout) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100292 brcmf_err("rxctl wait timed out\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100293 err = -EIO;
294 }
295 if (!err)
296 return devinfo->ctl_urb_actual_length;
297 else
298 return err;
299}
300
301static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200302 struct list_head *q, int *counter)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100303{
304 unsigned long flags;
305 struct brcmf_usbreq *req;
306 spin_lock_irqsave(&devinfo->qlock, flags);
307 if (list_empty(q)) {
308 spin_unlock_irqrestore(&devinfo->qlock, flags);
309 return NULL;
310 }
311 req = list_entry(q->next, struct brcmf_usbreq, list);
312 list_del_init(q->next);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200313 if (counter)
314 (*counter)--;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100315 spin_unlock_irqrestore(&devinfo->qlock, flags);
316 return req;
317
318}
319
320static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200321 struct list_head *q, struct brcmf_usbreq *req,
322 int *counter)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100323{
324 unsigned long flags;
325 spin_lock_irqsave(&devinfo->qlock, flags);
326 list_add_tail(&req->list, q);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200327 if (counter)
328 (*counter)++;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100329 spin_unlock_irqrestore(&devinfo->qlock, flags);
330}
331
332static struct brcmf_usbreq *
333brcmf_usbdev_qinit(struct list_head *q, int qsize)
334{
335 int i;
336 struct brcmf_usbreq *req, *reqs;
337
Joe Perches14f8dc42013-02-07 11:46:27 +0000338 reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
339 if (reqs == NULL)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100340 return NULL;
Joe Perches14f8dc42013-02-07 11:46:27 +0000341
Arend van Spriel71bb2442012-02-09 21:09:08 +0100342 req = reqs;
343
344 for (i = 0; i < qsize; i++) {
345 req->urb = usb_alloc_urb(0, GFP_ATOMIC);
346 if (!req->urb)
347 goto fail;
348
349 INIT_LIST_HEAD(&req->list);
350 list_add_tail(&req->list, q);
351 req++;
352 }
353 return reqs;
354fail:
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100355 brcmf_err("fail!\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100356 while (!list_empty(q)) {
357 req = list_entry(q->next, struct brcmf_usbreq, list);
358 if (req && req->urb)
359 usb_free_urb(req->urb);
360 list_del(q->next);
361 }
362 return NULL;
363
364}
365
366static void brcmf_usb_free_q(struct list_head *q, bool pending)
367{
368 struct brcmf_usbreq *req, *next;
369 int i = 0;
370 list_for_each_entry_safe(req, next, q, list) {
Dan Carpenterd4ca0092012-02-24 09:22:27 +0300371 if (!req->urb) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100372 brcmf_err("bad req\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100373 break;
374 }
375 i++;
376 if (pending) {
377 usb_kill_urb(req->urb);
378 } else {
379 usb_free_urb(req->urb);
380 list_del_init(&req->list);
381 }
382 }
383}
384
385static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
386 struct brcmf_usbreq *req)
387{
388 unsigned long flags;
389
390 spin_lock_irqsave(&devinfo->qlock, flags);
391 list_del_init(&req->list);
392 spin_unlock_irqrestore(&devinfo->qlock, flags);
393}
394
395
396static void brcmf_usb_tx_complete(struct urb *urb)
397{
398 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
399 struct brcmf_usbdev_info *devinfo = req->devinfo;
Hante Meulemandf50f752013-06-06 13:17:48 +0200400 unsigned long flags;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100401
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800402 brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
403 req->skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100404 brcmf_usb_del_fromq(devinfo, req);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100405
Hante Meuleman1e2d9582012-09-27 14:17:50 +0200406 brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100407 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200408 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
Hante Meulemandf50f752013-06-06 13:17:48 +0200409 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200410 if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
411 devinfo->tx_flowblock) {
412 brcmf_txflowblock(devinfo->dev, false);
413 devinfo->tx_flowblock = false;
414 }
Hante Meulemandf50f752013-06-06 13:17:48 +0200415 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100416}
417
418static void brcmf_usb_rx_complete(struct urb *urb)
419{
420 struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
421 struct brcmf_usbdev_info *devinfo = req->devinfo;
422 struct sk_buff *skb;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100423
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800424 brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100425 brcmf_usb_del_fromq(devinfo, req);
426 skb = req->skb;
427 req->skb = NULL;
428
Arend van Spriela43af512013-01-02 15:22:43 +0100429 /* zero lenght packets indicate usb "failure". Do not refill */
Hante Meulemanb1a2a412013-02-06 18:40:41 +0100430 if (urb->status != 0 || !urb->actual_length) {
Arend van Spriel01e33312012-08-30 10:05:34 +0200431 brcmu_pkt_buf_free_skb(skb);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200432 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100433 return;
434 }
435
Hante Meuleman7c38e692012-11-14 18:46:07 -0800436 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
Arend van Spriel71bb2442012-02-09 21:09:08 +0100437 skb_put(skb, urb->actual_length);
Arend van Spriel05f38202013-09-25 13:05:46 +0200438 brcmf_rx_frame(devinfo->dev, skb);
Arend van Spriela43af512013-01-02 15:22:43 +0100439 brcmf_usb_rx_refill(devinfo, req);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100440 } else {
Arend van Spriel01e33312012-08-30 10:05:34 +0200441 brcmu_pkt_buf_free_skb(skb);
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200442 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100443 }
444 return;
445
446}
447
448static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
449 struct brcmf_usbreq *req)
450{
451 struct sk_buff *skb;
452 int ret;
453
454 if (!req || !devinfo)
455 return;
456
457 skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
458 if (!skb) {
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200459 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100460 return;
461 }
462 req->skb = skb;
463
464 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
465 skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
466 req);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100467 req->devinfo = devinfo;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200468 brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100469
470 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200471 if (ret) {
472 brcmf_usb_del_fromq(devinfo, req);
Arend van Spriel01e33312012-08-30 10:05:34 +0200473 brcmu_pkt_buf_free_skb(req->skb);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100474 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200475 brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100476 }
477 return;
478}
479
480static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
481{
482 struct brcmf_usbreq *req;
483
Hante Meuleman7c38e692012-11-14 18:46:07 -0800484 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100485 brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100486 return;
487 }
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200488 while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100489 brcmf_usb_rx_refill(devinfo, req);
490}
491
492static void
493brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
494{
495 struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
496 int old_state;
497
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800498 brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
499 devinfo->bus_pub.state, state);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100500
501 if (devinfo->bus_pub.state == state)
502 return;
503
504 old_state = devinfo->bus_pub.state;
Hante Meuleman7c38e692012-11-14 18:46:07 -0800505 devinfo->bus_pub.state = state;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100506
507 /* update state of upper layer */
Hante Meuleman7c38e692012-11-14 18:46:07 -0800508 if (state == BRCMFMAC_USB_STATE_DOWN) {
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800509 brcmf_dbg(USB, "DBUS is down\n");
Arend van Sprielbb350712014-01-13 22:20:29 +0100510 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
Hante Meuleman7c38e692012-11-14 18:46:07 -0800511 } else if (state == BRCMFMAC_USB_STATE_UP) {
512 brcmf_dbg(USB, "DBUS is up\n");
Arend van Sprielbb350712014-01-13 22:20:29 +0100513 brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DATA);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100514 } else {
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800515 brcmf_dbg(USB, "DBUS current state=%d\n", state);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100516 }
517}
518
Arend van Spriel71bb2442012-02-09 21:09:08 +0100519static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
520{
521 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
522 struct brcmf_usbreq *req;
523 int ret;
Hante Meulemandf50f752013-06-06 13:17:48 +0200524 unsigned long flags;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100525
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800526 brcmf_dbg(USB, "Enter, skb=%p\n", skb);
Arend van Spriel6fc9ca12013-03-03 12:45:30 +0100527 if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
528 ret = -EIO;
529 goto fail;
530 }
Arend van Spriel71bb2442012-02-09 21:09:08 +0100531
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200532 req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
533 &devinfo->tx_freecount);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100534 if (!req) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100535 brcmf_err("no req to send\n");
Arend van Spriel6fc9ca12013-03-03 12:45:30 +0100536 ret = -ENOMEM;
537 goto fail;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100538 }
Arend van Spriel71bb2442012-02-09 21:09:08 +0100539
540 req->skb = skb;
541 req->devinfo = devinfo;
542 usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
543 skb->data, skb->len, brcmf_usb_tx_complete, req);
544 req->urb->transfer_flags |= URB_ZERO_PACKET;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200545 brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100546 ret = usb_submit_urb(req->urb, GFP_ATOMIC);
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200547 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100548 brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
Hante Meuleman2e875ac2012-08-30 10:05:36 +0200549 brcmf_usb_del_fromq(devinfo, req);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100550 req->skb = NULL;
Hante Meulemanc6ab4292012-09-11 21:18:48 +0200551 brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
Arend van Spriel6fc9ca12013-03-03 12:45:30 +0100552 &devinfo->tx_freecount);
553 goto fail;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100554 }
555
Hante Meulemandf50f752013-06-06 13:17:48 +0200556 spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
Arend van Spriel6fc9ca12013-03-03 12:45:30 +0100557 if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
558 !devinfo->tx_flowblock) {
559 brcmf_txflowblock(dev, true);
560 devinfo->tx_flowblock = true;
561 }
Hante Meulemandf50f752013-06-06 13:17:48 +0200562 spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
Arend van Spriel6fc9ca12013-03-03 12:45:30 +0100563 return 0;
564
565fail:
Arend van Spriel71bb2442012-02-09 21:09:08 +0100566 return ret;
567}
568
569
570static int brcmf_usb_up(struct device *dev)
571{
572 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100573
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800574 brcmf_dbg(USB, "Enter\n");
Hante Meuleman7c38e692012-11-14 18:46:07 -0800575 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
Dan Carpenterd4ca0092012-02-24 09:22:27 +0300576 return 0;
577
Arend van Spriel71bb2442012-02-09 21:09:08 +0100578 /* Success, indicate devinfo is fully up */
Hante Meuleman7c38e692012-11-14 18:46:07 -0800579 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100580
Arend van Spriel71bb2442012-02-09 21:09:08 +0100581 if (devinfo->ctl_urb) {
582 devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
583 devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
584
Arend van Spriel71bb2442012-02-09 21:09:08 +0100585 /* CTL Write */
586 devinfo->ctl_write.bRequestType =
587 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
588 devinfo->ctl_write.bRequest = 0;
589 devinfo->ctl_write.wValue = cpu_to_le16(0);
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200590 devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100591
592 /* CTL Read */
593 devinfo->ctl_read.bRequestType =
594 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
595 devinfo->ctl_read.bRequest = 1;
596 devinfo->ctl_read.wValue = cpu_to_le16(0);
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200597 devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100598 }
599 brcmf_usb_rx_fill_all(devinfo);
600 return 0;
601}
602
603static void brcmf_usb_down(struct device *dev)
604{
605 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
606
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800607 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100608 if (devinfo == NULL)
609 return;
610
Hante Meuleman7c38e692012-11-14 18:46:07 -0800611 if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100612 return;
613
Hante Meuleman7c38e692012-11-14 18:46:07 -0800614 brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100615
616 if (devinfo->ctl_urb)
617 usb_kill_urb(devinfo->ctl_urb);
618
619 if (devinfo->bulk_urb)
620 usb_kill_urb(devinfo->bulk_urb);
621 brcmf_usb_free_q(&devinfo->tx_postq, true);
622
623 brcmf_usb_free_q(&devinfo->rx_postq, true);
624}
625
Arend van Spriel71bb2442012-02-09 21:09:08 +0100626static void
627brcmf_usb_sync_complete(struct urb *urb)
628{
629 struct brcmf_usbdev_info *devinfo =
630 (struct brcmf_usbdev_info *)urb->context;
631
Hante Meuleman1e271c92012-10-22 10:36:18 -0700632 devinfo->ctl_completed = true;
633 brcmf_usb_ioctl_resp_wake(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100634}
635
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200636static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
637 void *buffer, int buflen)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100638{
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200639 int ret;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100640 char *tmpbuf;
641 u16 size;
642
643 if ((!devinfo) || (devinfo->ctl_urb == NULL))
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200644 return -EINVAL;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100645
646 tmpbuf = kmalloc(buflen, GFP_ATOMIC);
647 if (!tmpbuf)
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200648 return -ENOMEM;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100649
650 size = buflen;
651 devinfo->ctl_urb->transfer_buffer_length = size;
652
653 devinfo->ctl_read.wLength = cpu_to_le16p(&size);
654 devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
655 USB_RECIP_INTERFACE;
656 devinfo->ctl_read.bRequest = cmd;
657
658 usb_fill_control_urb(devinfo->ctl_urb,
659 devinfo->usbdev,
660 usb_rcvctrlpipe(devinfo->usbdev, 0),
661 (unsigned char *) &devinfo->ctl_read,
662 (void *) tmpbuf, size,
663 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
664
Hante Meuleman1e271c92012-10-22 10:36:18 -0700665 devinfo->ctl_completed = false;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100666 ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
667 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100668 brcmf_err("usb_submit_urb failed %d\n", ret);
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200669 goto finalize;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100670 }
671
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200672 if (!brcmf_usb_ioctl_resp_wait(devinfo))
673 ret = -ETIMEDOUT;
674 else
675 memcpy(buffer, tmpbuf, buflen);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100676
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200677finalize:
678 kfree(tmpbuf);
Hante Meuleman1e271c92012-10-22 10:36:18 -0700679 return ret;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100680}
681
682static bool
683brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
684{
685 struct bootrom_id_le id;
686 u32 chipid, chiprev;
687
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800688 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100689
690 if (devinfo == NULL)
691 return false;
692
693 /* Check if firmware downloaded already by querying runtime ID */
694 id.chip = cpu_to_le32(0xDEAD);
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800695 brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100696
697 chipid = le32_to_cpu(id.chip);
698 chiprev = le32_to_cpu(id.chiprev);
699
700 if ((chipid & 0x4300) == 0x4300)
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800701 brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100702 else
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800703 brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100704 if (chipid == BRCMF_POSTBOOT_ID) {
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800705 brcmf_dbg(USB, "firmware already downloaded\n");
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800706 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100707 return false;
708 } else {
Arend van Sprielac94f192012-03-02 22:55:46 +0100709 devinfo->bus_pub.devid = chipid;
710 devinfo->bus_pub.chiprev = chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100711 }
712 return true;
713}
714
715static int
716brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
717{
718 struct bootrom_id_le id;
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800719 u32 loop_cnt;
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200720 int err;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100721
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800722 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100723
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800724 loop_cnt = 0;
725 do {
726 mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
727 loop_cnt++;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100728 id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200729 err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
730 if ((err) && (err != -ETIMEDOUT))
731 return err;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100732 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
733 break;
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800734 } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100735
736 if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800737 brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
738 le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100739
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800740 brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100741 return 0;
742 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100743 brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
Hante Meuleman83bc9c312012-11-14 18:46:06 -0800744 BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100745 return -EINVAL;
746 }
747}
748
749
750static int
751brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
752{
753 int ret;
754
755 if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
756 return -EINVAL;
757
758 /* Prepare the URB */
759 usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
760 devinfo->tx_pipe, buffer, len,
761 (usb_complete_t)brcmf_usb_sync_complete, devinfo);
762
763 devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
764
Hante Meuleman1e271c92012-10-22 10:36:18 -0700765 devinfo->ctl_completed = false;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100766 ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
767 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100768 brcmf_err("usb_submit_urb failed %d\n", ret);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100769 return ret;
770 }
Hante Meuleman1e271c92012-10-22 10:36:18 -0700771 ret = brcmf_usb_ioctl_resp_wait(devinfo);
772 return (ret == 0);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100773}
774
775static int
776brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
777{
778 unsigned int sendlen, sent, dllen;
779 char *bulkchunk = NULL, *dlpos;
780 struct rdl_state_le state;
781 u32 rdlstate, rdlbytes;
782 int err = 0;
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800783
784 brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100785
786 bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
787 if (bulkchunk == NULL) {
788 err = -ENOMEM;
789 goto fail;
790 }
791
792 /* 1) Prepare USB boot loader for runtime image */
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200793 brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100794
795 rdlstate = le32_to_cpu(state.state);
796 rdlbytes = le32_to_cpu(state.bytes);
797
798 /* 2) Check we are in the Waiting state */
799 if (rdlstate != DL_WAITING) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100800 brcmf_err("Failed to DL_START\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100801 err = -EINVAL;
802 goto fail;
803 }
804 sent = 0;
805 dlpos = fw;
806 dllen = fwlen;
807
808 /* Get chip id and rev */
809 while (rdlbytes != dllen) {
810 /* Wait until the usb device reports it received all
811 * the bytes we sent */
812 if ((rdlbytes == sent) && (rdlbytes != dllen)) {
813 if ((dllen-sent) < RDL_CHUNK)
814 sendlen = dllen-sent;
815 else
816 sendlen = RDL_CHUNK;
817
818 /* simply avoid having to send a ZLP by ensuring we
819 * never have an even
820 * multiple of 64
821 */
822 if (!(sendlen % 64))
823 sendlen -= 4;
824
825 /* send data */
826 memcpy(bulkchunk, dlpos, sendlen);
827 if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
828 sendlen)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100829 brcmf_err("send_bulk failed\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100830 err = -EINVAL;
831 goto fail;
832 }
833
834 dlpos += sendlen;
835 sent += sendlen;
836 }
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200837 err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
838 sizeof(state));
839 if (err) {
840 brcmf_err("DL_GETSTATE Failed\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100841 goto fail;
842 }
843
844 rdlstate = le32_to_cpu(state.state);
845 rdlbytes = le32_to_cpu(state.bytes);
846
847 /* restart if an error is reported */
848 if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100849 brcmf_err("Bad Hdr or Bad CRC state %d\n",
Arend van Spriel71bb2442012-02-09 21:09:08 +0100850 rdlstate);
851 err = -EINVAL;
852 goto fail;
853 }
854 }
855
856fail:
857 kfree(bulkchunk);
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800858 brcmf_dbg(USB, "Exit, err=%d\n", err);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100859 return err;
860}
861
862static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
863{
864 int err;
865
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800866 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100867
868 if (devinfo == NULL)
869 return -EINVAL;
870
Arend van Sprielac94f192012-03-02 22:55:46 +0100871 if (devinfo->bus_pub.devid == 0xDEAD)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100872 return -EINVAL;
873
874 err = brcmf_usb_dl_writeimage(devinfo, fw, len);
875 if (err == 0)
Hante Meuleman7c38e692012-11-14 18:46:07 -0800876 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100877 else
Hante Meuleman7c38e692012-11-14 18:46:07 -0800878 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800879 brcmf_dbg(USB, "Exit, err=%d\n", err);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100880
881 return err;
882}
883
884static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
885{
886 struct rdl_state_le state;
887
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800888 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100889 if (!devinfo)
890 return -EINVAL;
891
Arend van Sprielac94f192012-03-02 22:55:46 +0100892 if (devinfo->bus_pub.devid == 0xDEAD)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100893 return -EINVAL;
894
895 /* Check we are runnable */
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200896 state.state = 0;
897 brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
Arend van Spriel71bb2442012-02-09 21:09:08 +0100898
899 /* Start the image */
900 if (state.state == cpu_to_le32(DL_RUNNABLE)) {
Hante Meuleman51c7f5e2014-06-21 12:11:15 +0200901 if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
Arend van Spriel71bb2442012-02-09 21:09:08 +0100902 return -ENODEV;
903 if (brcmf_usb_resetcfg(devinfo))
904 return -ENODEV;
905 /* The Dongle may go for re-enumeration. */
906 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100907 brcmf_err("Dongle not runnable\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100908 return -EINVAL;
909 }
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800910 brcmf_dbg(USB, "Exit\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100911 return 0;
912}
913
914static bool brcmf_usb_chip_support(int chipid, int chiprev)
915{
916 switch(chipid) {
Hante Meuleman5779ae62014-07-12 08:49:34 +0200917 case BRCM_CC_43143_CHIP_ID:
Hante Meuleman70f08222012-08-30 19:43:01 +0200918 return true;
Hante Meuleman5779ae62014-07-12 08:49:34 +0200919 case BRCM_CC_43235_CHIP_ID:
920 case BRCM_CC_43236_CHIP_ID:
921 case BRCM_CC_43238_CHIP_ID:
Arend van Spriel71bb2442012-02-09 21:09:08 +0100922 return (chiprev == 3);
Hante Meuleman5779ae62014-07-12 08:49:34 +0200923 case BRCM_CC_43242_CHIP_ID:
Hante Meuleman1212d372012-08-30 19:43:00 +0200924 return true;
Hante Meuleman5779ae62014-07-12 08:49:34 +0200925 case BRCM_CC_43566_CHIP_ID:
926 case BRCM_CC_43569_CHIP_ID:
Hante Meulemanb6fd7fd2014-06-21 12:11:12 +0200927 return true;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100928 default:
929 break;
930 }
931 return false;
932}
933
934static int
935brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
936{
Arend van Sprielac94f192012-03-02 22:55:46 +0100937 int devid, chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100938 int err;
939
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800940 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100941 if (devinfo == NULL)
942 return -ENODEV;
943
Arend van Sprielac94f192012-03-02 22:55:46 +0100944 devid = devinfo->bus_pub.devid;
945 chiprev = devinfo->bus_pub.chiprev;
Arend van Spriel71bb2442012-02-09 21:09:08 +0100946
Arend van Sprielac94f192012-03-02 22:55:46 +0100947 if (!brcmf_usb_chip_support(devid, chiprev)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100948 brcmf_err("unsupported chip %d rev %d\n",
Arend van Sprielac94f192012-03-02 22:55:46 +0100949 devid, chiprev);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100950 return -EINVAL;
951 }
952
953 if (!devinfo->image) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100954 brcmf_err("No firmware!\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +0100955 return -ENOENT;
956 }
957
958 err = brcmf_usb_dlstart(devinfo,
Arend van Spriel5b8045d2014-05-27 12:56:23 +0200959 (u8 *)devinfo->image, devinfo->image_len);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100960 if (err == 0)
961 err = brcmf_usb_dlrun(devinfo);
962 return err;
963}
964
965
Hante Meulemand74a0b52012-09-11 21:18:51 +0200966static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +0100967{
Hante Meulemancb8b73d2012-11-05 16:22:31 -0800968 brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100969
Arend van Spriel71bb2442012-02-09 21:09:08 +0100970 /* free the URBS */
971 brcmf_usb_free_q(&devinfo->rx_freeq, false);
972 brcmf_usb_free_q(&devinfo->tx_freeq, false);
973
Arend van Spriel71bb2442012-02-09 21:09:08 +0100974 usb_free_urb(devinfo->ctl_urb);
975 usb_free_urb(devinfo->bulk_urb);
976
977 kfree(devinfo->tx_reqs);
978 kfree(devinfo->rx_reqs);
Arend van Spriel71bb2442012-02-09 21:09:08 +0100979}
980
981#define TRX_MAGIC 0x30524448 /* "HDR0" */
982#define TRX_VERSION 1 /* Version 1 */
983#define TRX_MAX_LEN 0x3B0000 /* Max length */
984#define TRX_NO_HEADER 1 /* Do not write TRX header */
985#define TRX_MAX_OFFSET 3 /* Max number of individual files */
986#define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
987
988struct trx_header_le {
989 __le32 magic; /* "HDR0" */
990 __le32 len; /* Length of file including header */
991 __le32 crc32; /* CRC from flag_version to end of file */
992 __le32 flag_version; /* 0:15 flags, 16:31 version */
993 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
994 * header */
995};
996
997static int check_file(const u8 *headers)
998{
999 struct trx_header_le *trx;
1000 int actual_len = -1;
1001
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001002 brcmf_dbg(USB, "Enter\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001003 /* Extract trx header */
1004 trx = (struct trx_header_le *) headers;
1005 if (trx->magic != cpu_to_le32(TRX_MAGIC))
1006 return -1;
1007
1008 headers += sizeof(struct trx_header_le);
1009
1010 if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1011 actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1012 return actual_len + sizeof(struct trx_header_le);
1013 }
1014 return -1;
1015}
1016
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001017static const char *brcmf_usb_get_fwname(struct brcmf_usbdev_info *devinfo)
1018{
1019 switch (devinfo->bus_pub.devid) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02001020 case BRCM_CC_43143_CHIP_ID:
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001021 return BRCMF_USB_43143_FW_NAME;
Hante Meuleman5779ae62014-07-12 08:49:34 +02001022 case BRCM_CC_43235_CHIP_ID:
1023 case BRCM_CC_43236_CHIP_ID:
1024 case BRCM_CC_43238_CHIP_ID:
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001025 return BRCMF_USB_43236_FW_NAME;
Hante Meuleman5779ae62014-07-12 08:49:34 +02001026 case BRCM_CC_43242_CHIP_ID:
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001027 return BRCMF_USB_43242_FW_NAME;
Hante Meuleman5779ae62014-07-12 08:49:34 +02001028 case BRCM_CC_43566_CHIP_ID:
1029 case BRCM_CC_43569_CHIP_ID:
Hante Meulemanb6fd7fd2014-06-21 12:11:12 +02001030 return BRCMF_USB_43569_FW_NAME;
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001031 default:
1032 return NULL;
1033 }
1034}
Arend van Spriel71bb2442012-02-09 21:09:08 +01001035
1036
1037static
Hante Meulemand74a0b52012-09-11 21:18:51 +02001038struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1039 int nrxq, int ntxq)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001040{
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001041 brcmf_dbg(USB, "Enter\n");
1042
Arend van Spriel71bb2442012-02-09 21:09:08 +01001043 devinfo->bus_pub.nrxq = nrxq;
1044 devinfo->rx_low_watermark = nrxq / 2;
1045 devinfo->bus_pub.devinfo = devinfo;
1046 devinfo->bus_pub.ntxq = ntxq;
Hante Meuleman7c38e692012-11-14 18:46:07 -08001047 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001048
1049 /* flow control when too many tx urbs posted */
1050 devinfo->tx_low_watermark = ntxq / 4;
1051 devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001052 devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1053
1054 /* Initialize other structure content */
1055 init_waitqueue_head(&devinfo->ioctl_resp_wait);
1056
1057 /* Initialize the spinlocks */
1058 spin_lock_init(&devinfo->qlock);
Hante Meulemandf50f752013-06-06 13:17:48 +02001059 spin_lock_init(&devinfo->tx_flowblock_lock);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001060
1061 INIT_LIST_HEAD(&devinfo->rx_freeq);
1062 INIT_LIST_HEAD(&devinfo->rx_postq);
1063
1064 INIT_LIST_HEAD(&devinfo->tx_freeq);
1065 INIT_LIST_HEAD(&devinfo->tx_postq);
1066
Hante Meulemanc6ab4292012-09-11 21:18:48 +02001067 devinfo->tx_flowblock = false;
1068
Arend van Spriel71bb2442012-02-09 21:09:08 +01001069 devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1070 if (!devinfo->rx_reqs)
1071 goto error;
1072
1073 devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1074 if (!devinfo->tx_reqs)
1075 goto error;
Hante Meulemanc6ab4292012-09-11 21:18:48 +02001076 devinfo->tx_freecount = ntxq;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001077
Arend van Spriel71bb2442012-02-09 21:09:08 +01001078 devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1079 if (!devinfo->ctl_urb) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001080 brcmf_err("usb_alloc_urb (ctl) failed\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001081 goto error;
1082 }
Arend van Spriel71bb2442012-02-09 21:09:08 +01001083 devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1084 if (!devinfo->bulk_urb) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001085 brcmf_err("usb_alloc_urb (bulk) failed\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001086 goto error;
1087 }
1088
Arend van Spriel71bb2442012-02-09 21:09:08 +01001089 return &devinfo->bus_pub;
1090
1091error:
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001092 brcmf_err("failed!\n");
Hante Meulemand74a0b52012-09-11 21:18:51 +02001093 brcmf_usb_detach(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001094 return NULL;
1095}
1096
Arend van Sprield9cb2592012-12-05 15:25:54 +01001097static struct brcmf_bus_ops brcmf_usb_bus_ops = {
1098 .txdata = brcmf_usb_tx,
Arend van Sprield9cb2592012-12-05 15:25:54 +01001099 .stop = brcmf_usb_down,
1100 .txctl = brcmf_usb_tx_ctlpkt,
1101 .rxctl = brcmf_usb_rx_ctlpkt,
1102};
1103
Arend van Spriel4dd7de12014-05-27 12:56:19 +02001104static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
1105{
1106 int ret;
1107
1108 /* Attach to the common driver interface */
1109 ret = brcmf_attach(devinfo->dev);
1110 if (ret) {
1111 brcmf_err("brcmf_attach failed\n");
1112 return ret;
1113 }
1114
1115 ret = brcmf_usb_up(devinfo->dev);
1116 if (ret)
1117 goto fail;
1118
1119 ret = brcmf_bus_start(devinfo->dev);
1120 if (ret)
1121 goto fail;
1122
1123 return 0;
1124fail:
1125 brcmf_detach(devinfo->dev);
1126 return ret;
1127}
1128
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001129static void brcmf_usb_probe_phase2(struct device *dev,
1130 const struct firmware *fw,
1131 void *nvram, u32 nvlen)
1132{
1133 struct brcmf_bus *bus = dev_get_drvdata(dev);
1134 struct brcmf_usbdev_info *devinfo;
1135 int ret;
1136
1137 brcmf_dbg(USB, "Start fw downloading\n");
1138 ret = check_file(fw->data);
1139 if (ret < 0) {
1140 brcmf_err("invalid firmware\n");
1141 release_firmware(fw);
1142 goto error;
1143 }
1144
1145 devinfo = bus->bus_priv.usb->devinfo;
1146 devinfo->image = fw->data;
1147 devinfo->image_len = fw->size;
1148
1149 ret = brcmf_usb_fw_download(devinfo);
1150 release_firmware(fw);
1151 if (ret)
1152 goto error;
1153
1154 ret = brcmf_usb_bus_setup(devinfo);
1155 if (ret)
1156 goto error;
1157
1158 return;
1159error:
1160 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1161 device_release_driver(dev);
1162}
1163
Hante Meulemanb87e2c42012-11-14 18:46:23 -08001164static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001165{
1166 struct brcmf_bus *bus = NULL;
1167 struct brcmf_usbdev *bus_pub = NULL;
Hante Meulemand74a0b52012-09-11 21:18:51 +02001168 struct device *dev = devinfo->dev;
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001169 int ret;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001170
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001171 brcmf_dbg(USB, "Enter\n");
Hante Meulemand74a0b52012-09-11 21:18:51 +02001172 bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
Hante Meuleman9cd23a72012-09-13 21:12:04 +02001173 if (!bus_pub)
1174 return -ENODEV;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001175
1176 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1177 if (!bus) {
1178 ret = -ENOMEM;
1179 goto fail;
1180 }
1181
Arend van Sprield9cb2592012-12-05 15:25:54 +01001182 bus->dev = dev;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001183 bus_pub->bus = bus;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001184 bus->bus_priv.usb = bus_pub;
1185 dev_set_drvdata(dev, bus);
Arend van Sprield9cb2592012-12-05 15:25:54 +01001186 bus->ops = &brcmf_usb_bus_ops;
Hante Meuleman943258b2013-12-12 11:59:02 +01001187 bus->proto_type = BRCMF_PROTO_BCDC;
Hante Meuleman9cd18352014-05-12 10:47:30 +02001188 bus->always_use_fws_queue = true;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001189
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001190 if (!brcmf_usb_dlneeded(devinfo)) {
1191 ret = brcmf_usb_bus_setup(devinfo);
1192 if (ret)
1193 goto fail;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001194 }
Arend van Sprielc6bff542014-06-20 22:19:25 +02001195 bus->chip = bus_pub->devid;
1196 bus->chiprev = bus_pub->chiprev;
1197
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001198 /* request firmware here */
1199 brcmf_fw_get_firmwares(dev, 0, brcmf_usb_get_fwname(devinfo), NULL,
1200 brcmf_usb_probe_phase2);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001201 return 0;
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001202
Arend van Spriel71bb2442012-02-09 21:09:08 +01001203fail:
1204 /* Release resources in reverse order */
Arend van Spriel71bb2442012-02-09 21:09:08 +01001205 kfree(bus);
Hante Meulemand74a0b52012-09-11 21:18:51 +02001206 brcmf_usb_detach(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001207 return ret;
1208}
1209
1210static void
Hante Meulemand74a0b52012-09-11 21:18:51 +02001211brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001212{
Hante Meulemand74a0b52012-09-11 21:18:51 +02001213 if (!devinfo)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001214 return;
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001215 brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001216
Hante Meulemand74a0b52012-09-11 21:18:51 +02001217 brcmf_detach(devinfo->dev);
1218 kfree(devinfo->bus_pub.bus);
1219 brcmf_usb_detach(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001220}
1221
1222static int
1223brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1224{
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001225 struct usb_device *usb = interface_to_usbdev(intf);
1226 struct brcmf_usbdev_info *devinfo;
1227 struct usb_interface_descriptor *desc;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001228 struct usb_endpoint_descriptor *endpoint;
1229 int ret = 0;
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001230 u32 num_of_eps;
1231 u8 endpoint_num, ep;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001232
Hante Meulemanb6fd7fd2014-06-21 12:11:12 +02001233 brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001234
Hante Meulemand74a0b52012-09-11 21:18:51 +02001235 devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1236 if (devinfo == NULL)
1237 return -ENOMEM;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001238
Hante Meulemand74a0b52012-09-11 21:18:51 +02001239 devinfo->usbdev = usb;
1240 devinfo->dev = &usb->dev;
Hante Meulemand74a0b52012-09-11 21:18:51 +02001241 usb_set_intfdata(intf, devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001242
1243 /* Check that the device supports only one configuration */
1244 if (usb->descriptor.bNumConfigurations != 1) {
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001245 brcmf_err("Number of configurations: %d not supported\n",
1246 usb->descriptor.bNumConfigurations);
1247 ret = -ENODEV;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001248 goto fail;
1249 }
1250
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001251 if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1252 (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1253 (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1254 brcmf_err("Device class: 0x%x not supported\n",
1255 usb->descriptor.bDeviceClass);
1256 ret = -ENODEV;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001257 goto fail;
1258 }
1259
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001260 desc = &intf->altsetting[0].desc;
1261 if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1262 (desc->bInterfaceSubClass != 2) ||
1263 (desc->bInterfaceProtocol != 0xff)) {
1264 brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1265 desc->bInterfaceNumber, desc->bInterfaceClass,
1266 desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1267 ret = -ENODEV;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001268 goto fail;
1269 }
1270
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001271 num_of_eps = desc->bNumEndpoints;
1272 for (ep = 0; ep < num_of_eps; ep++) {
1273 endpoint = &intf->altsetting[0].endpoint[ep].desc;
1274 endpoint_num = usb_endpoint_num(endpoint);
1275 if (!usb_endpoint_xfer_bulk(endpoint))
1276 continue;
1277 if (usb_endpoint_dir_in(endpoint)) {
1278 if (!devinfo->rx_pipe)
Hante Meulemand74a0b52012-09-11 21:18:51 +02001279 devinfo->rx_pipe =
Arend van Spriel71bb2442012-02-09 21:09:08 +01001280 usb_rcvbulkpipe(usb, endpoint_num);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001281 } else {
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001282 if (!devinfo->tx_pipe)
1283 devinfo->tx_pipe =
1284 usb_sndbulkpipe(usb, endpoint_num);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001285 }
1286 }
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001287 if (devinfo->rx_pipe == 0) {
1288 brcmf_err("No RX (in) Bulk EP found\n");
1289 ret = -ENODEV;
1290 goto fail;
1291 }
1292 if (devinfo->tx_pipe == 0) {
1293 brcmf_err("No TX (out) Bulk EP found\n");
1294 ret = -ENODEV;
1295 goto fail;
1296 }
1297
1298 devinfo->ifnum = desc->bInterfaceNumber;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001299
Hante Meulemande389a52014-05-27 12:56:20 +02001300 if (usb->speed == USB_SPEED_SUPER)
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001301 brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
Hante Meulemande389a52014-05-27 12:56:20 +02001302 else if (usb->speed == USB_SPEED_HIGH)
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001303 brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001304 else
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001305 brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001306
Hante Meulemanb87e2c42012-11-14 18:46:23 -08001307 ret = brcmf_usb_probe_cb(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001308 if (ret)
1309 goto fail;
1310
1311 /* Success */
1312 return 0;
1313
1314fail:
Hante Meulemand74a0b52012-09-11 21:18:51 +02001315 kfree(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001316 usb_set_intfdata(intf, NULL);
1317 return ret;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001318}
1319
1320static void
1321brcmf_usb_disconnect(struct usb_interface *intf)
1322{
Hante Meulemand74a0b52012-09-11 21:18:51 +02001323 struct brcmf_usbdev_info *devinfo;
Arend van Spriel71bb2442012-02-09 21:09:08 +01001324
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001325 brcmf_dbg(USB, "Enter\n");
Hante Meulemand74a0b52012-09-11 21:18:51 +02001326 devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1327 brcmf_usb_disconnect_cb(devinfo);
1328 kfree(devinfo);
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001329 brcmf_dbg(USB, "Exit\n");
Arend van Spriel71bb2442012-02-09 21:09:08 +01001330}
1331
1332/*
Hante Meuleman7c38e692012-11-14 18:46:07 -08001333 * only need to signal the bus being down and update the state.
Arend van Spriel71bb2442012-02-09 21:09:08 +01001334 */
1335static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1336{
1337 struct usb_device *usb = interface_to_usbdev(intf);
1338 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1339
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001340 brcmf_dbg(USB, "Enter\n");
Hante Meuleman7c38e692012-11-14 18:46:07 -08001341 devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1342 brcmf_detach(&usb->dev);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001343 return 0;
1344}
1345
1346/*
Hante Meuleman7c38e692012-11-14 18:46:07 -08001347 * (re-) start the bus.
Arend van Spriel71bb2442012-02-09 21:09:08 +01001348 */
1349static int brcmf_usb_resume(struct usb_interface *intf)
1350{
1351 struct usb_device *usb = interface_to_usbdev(intf);
1352 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1353
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001354 brcmf_dbg(USB, "Enter\n");
Arend van Spriel4dd7de12014-05-27 12:56:19 +02001355 return brcmf_usb_bus_setup(devinfo);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001356}
1357
Hante Meuleman7c38e692012-11-14 18:46:07 -08001358static int brcmf_usb_reset_resume(struct usb_interface *intf)
1359{
1360 struct usb_device *usb = interface_to_usbdev(intf);
1361 struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
Hante Meuleman51c7f5e2014-06-21 12:11:15 +02001362
Hante Meuleman7c38e692012-11-14 18:46:07 -08001363 brcmf_dbg(USB, "Enter\n");
1364
Arend van Spriel5b8045d2014-05-27 12:56:23 +02001365 return brcmf_fw_get_firmwares(&usb->dev, 0,
1366 brcmf_usb_get_fwname(devinfo), NULL,
1367 brcmf_usb_probe_phase2);
Hante Meuleman7c38e692012-11-14 18:46:07 -08001368}
1369
Hante Meuleman5779ae62014-07-12 08:49:34 +02001370#define BRCMF_USB_DEVICE(dev_id) \
1371 { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
Arend van Spriel71bb2442012-02-09 21:09:08 +01001372
1373static struct usb_device_id brcmf_usb_devid_table[] = {
Hante Meuleman5779ae62014-07-12 08:49:34 +02001374 BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1375 BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1376 BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1377 BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
Arend van Spriel71bb2442012-02-09 21:09:08 +01001378 /* special entry for device with firmware loaded and running */
Hante Meuleman5779ae62014-07-12 08:49:34 +02001379 BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1380 { /* end: all zeroes */ }
Arend van Spriel71bb2442012-02-09 21:09:08 +01001381};
Arend van Spriel5cfd6e82013-03-03 12:45:23 +01001382
Arend van Spriel71bb2442012-02-09 21:09:08 +01001383MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
Hante Meuleman70f08222012-08-30 19:43:01 +02001384MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
Rafał Miłeckifda82412012-02-24 07:22:51 +01001385MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
Hante Meuleman1212d372012-08-30 19:43:00 +02001386MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
Hante Meulemanb6fd7fd2014-06-21 12:11:12 +02001387MODULE_FIRMWARE(BRCMF_USB_43569_FW_NAME);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001388
Arend van Spriel71bb2442012-02-09 21:09:08 +01001389static struct usb_driver brcmf_usbdrvr = {
1390 .name = KBUILD_MODNAME,
1391 .probe = brcmf_usb_probe,
1392 .disconnect = brcmf_usb_disconnect,
1393 .id_table = brcmf_usb_devid_table,
1394 .suspend = brcmf_usb_suspend,
1395 .resume = brcmf_usb_resume,
Hante Meuleman7c38e692012-11-14 18:46:07 -08001396 .reset_resume = brcmf_usb_reset_resume,
Sarah Sharpc51fa662012-05-21 05:34:24 -07001397 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001398 .disable_hub_initiated_lpm = 1,
Arend van Spriel71bb2442012-02-09 21:09:08 +01001399};
1400
Arend van Spriel81d5f1b2013-01-02 15:22:40 +01001401static int brcmf_usb_reset_device(struct device *dev, void *notused)
1402{
1403 /* device past is the usb interface so we
1404 * need to use parent here.
1405 */
1406 brcmf_dev_reset(dev->parent);
1407 return 0;
1408}
Hante Meuleman803599d2012-09-19 22:21:10 +02001409
Arend van Spriel71bb2442012-02-09 21:09:08 +01001410void brcmf_usb_exit(void)
1411{
Arend van Spriel81d5f1b2013-01-02 15:22:40 +01001412 struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1413 int ret;
1414
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001415 brcmf_dbg(USB, "Enter\n");
Arend van Spriel81d5f1b2013-01-02 15:22:40 +01001416 ret = driver_for_each_device(drv, NULL, NULL,
1417 brcmf_usb_reset_device);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001418 usb_deregister(&brcmf_usbdrvr);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001419}
1420
Arend van Sprieldb4efbb2013-09-25 12:11:01 +02001421void brcmf_usb_register(void)
Arend van Spriel71bb2442012-02-09 21:09:08 +01001422{
Hante Meulemancb8b73d2012-11-05 16:22:31 -08001423 brcmf_dbg(USB, "Enter\n");
Arend van Spriel549040a2012-03-02 22:55:48 +01001424 usb_register(&brcmf_usbdrvr);
Arend van Spriel71bb2442012-02-09 21:09:08 +01001425}