2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 /*******************************************************************************
18 * Communicates with the dongle by using dcmd codes.
19 * For certain dcmd codes, the dongle interprets string data from the host.
20 ******************************************************************************/
22 #include <linux/types.h>
23 #include <linux/netdevice.h>
24 #include <linux/sched.h>
27 #include <brcmu_utils.h>
28 #include <brcmu_wifi.h>
31 #include "dhd_proto.h"
35 struct brcmf_proto_cdc_dcmd
{
36 __le32 cmd
; /* dongle command value */
37 __le32 len
; /* lower 16: output buflen;
38 * upper 16: input buflen (excludes header) */
39 __le32 flags
; /* flag defns given below */
40 __le32 status
; /* status code returned from the device */
43 /* Max valid buffer size that can be sent to the dongle */
44 #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
46 /* CDC flag definitions */
47 #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */
48 #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
49 #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */
50 #define CDC_DCMD_IF_SHIFT 12
51 #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
52 #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
53 #define CDC_DCMD_ID(flags) \
54 (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT)
57 * BDC header - Broadcom specific extension of CDC.
58 * Used on data packets to convey priority across USB.
60 #define BDC_HEADER_LEN 4
61 #define BDC_PROTO_VER 1 /* Protocol version */
62 #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
63 #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
64 #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
65 #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
66 #define BDC_PRIORITY_MASK 0x7
67 #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
68 #define BDC_FLAG2_IF_SHIFT 0
70 #define BDC_GET_IF_IDX(hdr) \
71 ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
72 #define BDC_SET_IF_IDX(hdr, idx) \
73 ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
74 ((idx) << BDC_FLAG2_IF_SHIFT)))
76 struct brcmf_proto_bdc_header
{
78 u8 priority
; /* 802.1d Priority, 4:7 flow control info for usb */
84 #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
85 #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE
86 * (amount of header tha might be added)
87 * plus any space that might be needed
88 * for alignment padding.
90 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
91 * round off at the end of buffer
98 u8 bus_header
[BUS_HEADER_LEN
];
99 struct brcmf_proto_cdc_dcmd msg
;
100 unsigned char buf
[BRCMF_DCMD_MAXLEN
+ ROUND_UP_MARGIN
];
103 static int brcmf_proto_cdc_msg(struct brcmf_pub
*drvr
)
105 struct brcmf_proto
*prot
= drvr
->prot
;
106 int len
= le32_to_cpu(prot
->msg
.len
) +
107 sizeof(struct brcmf_proto_cdc_dcmd
);
109 brcmf_dbg(TRACE
, "Enter\n");
111 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
112 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
113 * is actually sent to the dongle
115 if (len
> CDC_MAX_MSG_SIZE
)
116 len
= CDC_MAX_MSG_SIZE
;
119 return brcmf_sdbrcm_bus_txctl(drvr
->bus
, (unsigned char *)&prot
->msg
,
123 static int brcmf_proto_cdc_cmplt(struct brcmf_pub
*drvr
, u32 id
, u32 len
)
126 struct brcmf_proto
*prot
= drvr
->prot
;
128 brcmf_dbg(TRACE
, "Enter\n");
131 ret
= brcmf_sdbrcm_bus_rxctl(drvr
->bus
,
132 (unsigned char *)&prot
->msg
,
133 len
+ sizeof(struct brcmf_proto_cdc_dcmd
));
136 } while (CDC_DCMD_ID(le32_to_cpu(prot
->msg
.flags
)) != id
);
142 brcmf_proto_cdc_query_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
145 struct brcmf_proto
*prot
= drvr
->prot
;
146 struct brcmf_proto_cdc_dcmd
*msg
= &prot
->msg
;
148 int ret
= 0, retries
= 0;
151 brcmf_dbg(TRACE
, "Enter\n");
152 brcmf_dbg(CTL
, "cmd %d len %d\n", cmd
, len
);
154 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
155 if (cmd
== BRCMF_C_GET_VAR
&& buf
) {
156 if (!strcmp((char *)buf
, "bcmerrorstr")) {
157 strncpy((char *)buf
, "bcm_error",
160 } else if (!strcmp((char *)buf
, "bcmerror")) {
161 *(int *)buf
= drvr
->dongle_error
;
166 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_dcmd
));
168 msg
->cmd
= cpu_to_le32(cmd
);
169 msg
->len
= cpu_to_le32(len
);
170 flags
= (++prot
->reqid
<< CDC_DCMD_ID_SHIFT
);
171 flags
= (flags
& ~CDC_DCMD_IF_MASK
) |
172 (ifidx
<< CDC_DCMD_IF_SHIFT
);
173 msg
->flags
= cpu_to_le32(flags
);
176 memcpy(prot
->buf
, buf
, len
);
178 ret
= brcmf_proto_cdc_msg(drvr
);
180 brcmf_dbg(ERROR
, "brcmf_proto_cdc_msg failed w/status %d\n",
186 /* wait for interrupt and get first fragment */
187 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
191 flags
= le32_to_cpu(msg
->flags
);
192 id
= (flags
& CDC_DCMD_ID_MASK
) >> CDC_DCMD_ID_SHIFT
;
194 if ((id
< prot
->reqid
) && (++retries
< RETRIES
))
196 if (id
!= prot
->reqid
) {
197 brcmf_dbg(ERROR
, "%s: unexpected request id %d (expected %d)\n",
198 brcmf_ifname(drvr
, ifidx
), id
, prot
->reqid
);
203 /* Check info buffer */
204 info
= (void *)&msg
[1];
206 /* Copy info buffer */
210 memcpy(buf
, info
, len
);
213 /* Check the ERROR flag */
214 if (flags
& CDC_DCMD_ERROR
) {
215 ret
= le32_to_cpu(msg
->status
);
216 /* Cache error from dongle */
217 drvr
->dongle_error
= ret
;
224 int brcmf_proto_cdc_set_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
227 struct brcmf_proto
*prot
= drvr
->prot
;
228 struct brcmf_proto_cdc_dcmd
*msg
= &prot
->msg
;
232 brcmf_dbg(TRACE
, "Enter\n");
233 brcmf_dbg(CTL
, "cmd %d len %d\n", cmd
, len
);
235 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_dcmd
));
237 msg
->cmd
= cpu_to_le32(cmd
);
238 msg
->len
= cpu_to_le32(len
);
239 flags
= (++prot
->reqid
<< CDC_DCMD_ID_SHIFT
) | CDC_DCMD_SET
;
240 flags
= (flags
& ~CDC_DCMD_IF_MASK
) |
241 (ifidx
<< CDC_DCMD_IF_SHIFT
);
242 msg
->flags
= cpu_to_le32(flags
);
245 memcpy(prot
->buf
, buf
, len
);
247 ret
= brcmf_proto_cdc_msg(drvr
);
251 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
255 flags
= le32_to_cpu(msg
->flags
);
256 id
= (flags
& CDC_DCMD_ID_MASK
) >> CDC_DCMD_ID_SHIFT
;
258 if (id
!= prot
->reqid
) {
259 brcmf_dbg(ERROR
, "%s: unexpected request id %d (expected %d)\n",
260 brcmf_ifname(drvr
, ifidx
), id
, prot
->reqid
);
265 /* Check the ERROR flag */
266 if (flags
& CDC_DCMD_ERROR
) {
267 ret
= le32_to_cpu(msg
->status
);
268 /* Cache error from dongle */
269 drvr
->dongle_error
= ret
;
277 brcmf_proto_dcmd(struct brcmf_pub
*drvr
, int ifidx
, struct brcmf_dcmd
*dcmd
,
280 struct brcmf_proto
*prot
= drvr
->prot
;
283 if (drvr
->busstate
== BRCMF_BUS_DOWN
) {
284 brcmf_dbg(ERROR
, "bus is down. we have nothing to do.\n");
287 brcmf_os_proto_block(drvr
);
289 brcmf_dbg(TRACE
, "Enter\n");
291 if (len
> BRCMF_DCMD_MAXLEN
)
294 if (prot
->pending
== true) {
295 brcmf_dbg(TRACE
, "CDC packet is pending!!!! cmd=0x%x (%lu) lastcmd=0x%x (%lu)\n",
296 dcmd
->cmd
, (unsigned long)dcmd
->cmd
, prot
->lastcmd
,
297 (unsigned long)prot
->lastcmd
);
298 if (dcmd
->cmd
== BRCMF_C_SET_VAR
||
299 dcmd
->cmd
== BRCMF_C_GET_VAR
)
300 brcmf_dbg(TRACE
, "iovar cmd=%s\n", (char *)dcmd
->buf
);
305 prot
->pending
= true;
306 prot
->lastcmd
= dcmd
->cmd
;
308 ret
= brcmf_proto_cdc_set_dcmd(drvr
, ifidx
, dcmd
->cmd
,
311 ret
= brcmf_proto_cdc_query_dcmd(drvr
, ifidx
, dcmd
->cmd
,
315 sizeof(struct brcmf_proto_cdc_dcmd
);
321 struct brcmf_proto_cdc_dcmd
*msg
= &prot
->msg
;
322 /* len == needed when set/query fails from dongle */
323 dcmd
->needed
= le32_to_cpu(msg
->len
);
326 /* Intercept the wme_dp dongle cmd here */
327 if (!ret
&& dcmd
->cmd
== BRCMF_C_SET_VAR
&&
328 !strcmp(dcmd
->buf
, "wme_dp")) {
332 slen
= strlen("wme_dp") + 1;
333 if (len
>= (int)(slen
+ sizeof(int)))
334 memcpy(&val
, (char *)dcmd
->buf
+ slen
, sizeof(int));
335 drvr
->wme_dp
= (u8
) le32_to_cpu(val
);
338 prot
->pending
= false;
341 brcmf_os_proto_unblock(drvr
);
346 static bool pkt_sum_needed(struct sk_buff
*skb
)
348 return skb
->ip_summed
== CHECKSUM_PARTIAL
;
351 static void pkt_set_sum_good(struct sk_buff
*skb
, bool x
)
353 skb
->ip_summed
= (x
? CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
);
356 void brcmf_proto_hdrpush(struct brcmf_pub
*drvr
, int ifidx
,
357 struct sk_buff
*pktbuf
)
359 struct brcmf_proto_bdc_header
*h
;
361 brcmf_dbg(TRACE
, "Enter\n");
363 /* Push BDC header used to convey priority for buses that don't */
365 skb_push(pktbuf
, BDC_HEADER_LEN
);
367 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
369 h
->flags
= (BDC_PROTO_VER
<< BDC_FLAG_VER_SHIFT
);
370 if (pkt_sum_needed(pktbuf
))
371 h
->flags
|= BDC_FLAG_SUM_NEEDED
;
373 h
->priority
= (pktbuf
->priority
& BDC_PRIORITY_MASK
);
376 BDC_SET_IF_IDX(h
, ifidx
);
379 int brcmf_proto_hdrpull(struct brcmf_pub
*drvr
, int *ifidx
,
380 struct sk_buff
*pktbuf
)
382 struct brcmf_proto_bdc_header
*h
;
384 brcmf_dbg(TRACE
, "Enter\n");
386 /* Pop BDC header used to convey priority for buses that don't */
388 if (pktbuf
->len
< BDC_HEADER_LEN
) {
389 brcmf_dbg(ERROR
, "rx data too short (%d < %d)\n",
390 pktbuf
->len
, BDC_HEADER_LEN
);
394 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
396 *ifidx
= BDC_GET_IF_IDX(h
);
397 if (*ifidx
>= BRCMF_MAX_IFS
) {
398 brcmf_dbg(ERROR
, "rx data ifnum out of range (%d)\n", *ifidx
);
402 if (((h
->flags
& BDC_FLAG_VER_MASK
) >> BDC_FLAG_VER_SHIFT
) !=
404 brcmf_dbg(ERROR
, "%s: non-BDC packet received, flags 0x%x\n",
405 brcmf_ifname(drvr
, *ifidx
), h
->flags
);
409 if (h
->flags
& BDC_FLAG_SUM_GOOD
) {
410 brcmf_dbg(INFO
, "%s: BDC packet received with good rx-csum, flags 0x%x\n",
411 brcmf_ifname(drvr
, *ifidx
), h
->flags
);
412 pkt_set_sum_good(pktbuf
, true);
415 pktbuf
->priority
= h
->priority
& BDC_PRIORITY_MASK
;
417 skb_pull(pktbuf
, BDC_HEADER_LEN
);
422 int brcmf_proto_attach(struct brcmf_pub
*drvr
)
424 struct brcmf_proto
*cdc
;
426 cdc
= kzalloc(sizeof(struct brcmf_proto
), GFP_ATOMIC
);
430 /* ensure that the msg buf directly follows the cdc msg struct */
431 if ((unsigned long)(&cdc
->msg
+ 1) != (unsigned long)cdc
->buf
) {
432 brcmf_dbg(ERROR
, "struct brcmf_proto is not correctly defined\n");
437 drvr
->hdrlen
+= BDC_HEADER_LEN
;
438 drvr
->maxctl
= BRCMF_DCMD_MAXLEN
+
439 sizeof(struct brcmf_proto_cdc_dcmd
) + ROUND_UP_MARGIN
;
447 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
448 void brcmf_proto_detach(struct brcmf_pub
*drvr
)
454 void brcmf_proto_dstats(struct brcmf_pub
*drvr
)
456 /* No stats from dongle added yet, copy bus stats */
457 drvr
->dstats
.tx_packets
= drvr
->tx_packets
;
458 drvr
->dstats
.tx_errors
= drvr
->tx_errors
;
459 drvr
->dstats
.rx_packets
= drvr
->rx_packets
;
460 drvr
->dstats
.rx_errors
= drvr
->rx_errors
;
461 drvr
->dstats
.rx_dropped
= drvr
->rx_dropped
;
462 drvr
->dstats
.multicast
= drvr
->rx_multicast
;
466 int brcmf_proto_init(struct brcmf_pub
*drvr
)
471 brcmf_dbg(TRACE
, "Enter\n");
473 brcmf_os_proto_block(drvr
);
475 /* Get the device MAC address */
476 strcpy(buf
, "cur_etheraddr");
477 ret
= brcmf_proto_cdc_query_dcmd(drvr
, 0, BRCMF_C_GET_VAR
,
480 brcmf_os_proto_unblock(drvr
);
483 memcpy(drvr
->mac
, buf
, ETH_ALEN
);
485 brcmf_os_proto_unblock(drvr
);
487 ret
= brcmf_c_preinit_dcmds(drvr
);
489 /* Always assumes wl for now */
495 void brcmf_proto_stop(struct brcmf_pub
*drvr
)
497 /* Nothing to do for CDC */