]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/wireless/brcm80211/brcmfmac/dhd_cdc.c
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/types.h>
25 #include <linux/netdevice.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 2 /* 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+64) /* Must be atleast SDPCM_RESERVE
86 * (amount of header tha might be added)
87 * plus any space that might be needed
88 * for bus alignment padding.
90 #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
91 * round off at the end of buffer
99 u8 bus_header
[BUS_HEADER_LEN
];
100 struct brcmf_proto_cdc_dcmd msg
;
101 unsigned char buf
[BRCMF_DCMD_MAXLEN
+ ROUND_UP_MARGIN
];
104 static int brcmf_proto_cdc_msg(struct brcmf_pub
*drvr
)
106 struct brcmf_proto
*prot
= drvr
->prot
;
107 int len
= le32_to_cpu(prot
->msg
.len
) +
108 sizeof(struct brcmf_proto_cdc_dcmd
);
110 brcmf_dbg(TRACE
, "Enter\n");
112 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
113 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
114 * is actually sent to the dongle
116 if (len
> CDC_MAX_MSG_SIZE
)
117 len
= CDC_MAX_MSG_SIZE
;
120 return brcmf_bus_txctl(drvr
->bus_if
, (unsigned char *)&prot
->msg
, len
);
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");
129 len
+= sizeof(struct brcmf_proto_cdc_dcmd
);
131 ret
= brcmf_bus_rxctl(drvr
->bus_if
, (unsigned char *)&prot
->msg
,
135 } while (CDC_DCMD_ID(le32_to_cpu(prot
->msg
.flags
)) != id
);
141 brcmf_proto_cdc_query_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
144 struct brcmf_proto
*prot
= drvr
->prot
;
145 struct brcmf_proto_cdc_dcmd
*msg
= &prot
->msg
;
147 int ret
= 0, retries
= 0;
150 brcmf_dbg(TRACE
, "Enter\n");
151 brcmf_dbg(CTL
, "cmd %d len %d\n", cmd
, len
);
153 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
154 if (cmd
== BRCMF_C_GET_VAR
&& buf
) {
155 if (!strcmp((char *)buf
, "bcmerrorstr")) {
156 strncpy((char *)buf
, "bcm_error",
159 } else if (!strcmp((char *)buf
, "bcmerror")) {
160 *(int *)buf
= drvr
->dongle_error
;
165 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_dcmd
));
167 msg
->cmd
= cpu_to_le32(cmd
);
168 msg
->len
= cpu_to_le32(len
);
169 flags
= (++prot
->reqid
<< CDC_DCMD_ID_SHIFT
);
170 flags
= (flags
& ~CDC_DCMD_IF_MASK
) |
171 (ifidx
<< CDC_DCMD_IF_SHIFT
);
172 msg
->flags
= cpu_to_le32(flags
);
175 memcpy(prot
->buf
, buf
, len
);
177 ret
= brcmf_proto_cdc_msg(drvr
);
179 brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
185 /* wait for interrupt and get first fragment */
186 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
190 flags
= le32_to_cpu(msg
->flags
);
191 id
= (flags
& CDC_DCMD_ID_MASK
) >> CDC_DCMD_ID_SHIFT
;
193 if ((id
< prot
->reqid
) && (++retries
< RETRIES
))
195 if (id
!= prot
->reqid
) {
196 brcmf_err("%s: unexpected request id %d (expected %d)\n",
197 brcmf_ifname(drvr
, ifidx
), id
, prot
->reqid
);
202 /* Check info buffer */
203 info
= (void *)&msg
[1];
205 /* Copy info buffer */
209 memcpy(buf
, info
, len
);
212 /* Check the ERROR flag */
213 if (flags
& CDC_DCMD_ERROR
) {
214 ret
= le32_to_cpu(msg
->status
);
215 /* Cache error from dongle */
216 drvr
->dongle_error
= ret
;
223 int brcmf_proto_cdc_set_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
226 struct brcmf_proto
*prot
= drvr
->prot
;
227 struct brcmf_proto_cdc_dcmd
*msg
= &prot
->msg
;
231 brcmf_dbg(TRACE
, "Enter\n");
232 brcmf_dbg(CTL
, "cmd %d len %d\n", cmd
, len
);
234 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_dcmd
));
236 msg
->cmd
= cpu_to_le32(cmd
);
237 msg
->len
= cpu_to_le32(len
);
238 flags
= (++prot
->reqid
<< CDC_DCMD_ID_SHIFT
) | CDC_DCMD_SET
;
239 flags
= (flags
& ~CDC_DCMD_IF_MASK
) |
240 (ifidx
<< CDC_DCMD_IF_SHIFT
);
241 msg
->flags
= cpu_to_le32(flags
);
244 memcpy(prot
->buf
, buf
, len
);
246 ret
= brcmf_proto_cdc_msg(drvr
);
250 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
254 flags
= le32_to_cpu(msg
->flags
);
255 id
= (flags
& CDC_DCMD_ID_MASK
) >> CDC_DCMD_ID_SHIFT
;
257 if (id
!= prot
->reqid
) {
258 brcmf_err("%s: unexpected request id %d (expected %d)\n",
259 brcmf_ifname(drvr
, ifidx
), id
, prot
->reqid
);
264 /* Check the ERROR flag */
265 if (flags
& CDC_DCMD_ERROR
) {
266 ret
= le32_to_cpu(msg
->status
);
267 /* Cache error from dongle */
268 drvr
->dongle_error
= ret
;
275 static bool pkt_sum_needed(struct sk_buff
*skb
)
277 return skb
->ip_summed
== CHECKSUM_PARTIAL
;
280 static void pkt_set_sum_good(struct sk_buff
*skb
, bool x
)
282 skb
->ip_summed
= (x
? CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
);
285 void brcmf_proto_hdrpush(struct brcmf_pub
*drvr
, int ifidx
,
286 struct sk_buff
*pktbuf
)
288 struct brcmf_proto_bdc_header
*h
;
290 brcmf_dbg(TRACE
, "Enter\n");
292 /* Push BDC header used to convey priority for buses that don't */
294 skb_push(pktbuf
, BDC_HEADER_LEN
);
296 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
298 h
->flags
= (BDC_PROTO_VER
<< BDC_FLAG_VER_SHIFT
);
299 if (pkt_sum_needed(pktbuf
))
300 h
->flags
|= BDC_FLAG_SUM_NEEDED
;
302 h
->priority
= (pktbuf
->priority
& BDC_PRIORITY_MASK
);
305 BDC_SET_IF_IDX(h
, ifidx
);
308 int brcmf_proto_hdrpull(struct device
*dev
, int *ifidx
,
309 struct sk_buff
*pktbuf
)
311 struct brcmf_proto_bdc_header
*h
;
312 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
313 struct brcmf_pub
*drvr
= bus_if
->drvr
;
315 brcmf_dbg(TRACE
, "Enter\n");
317 /* Pop BDC header used to convey priority for buses that don't */
319 if (pktbuf
->len
< BDC_HEADER_LEN
) {
320 brcmf_err("rx data too short (%d < %d)\n",
321 pktbuf
->len
, BDC_HEADER_LEN
);
325 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
327 *ifidx
= BDC_GET_IF_IDX(h
);
328 if (*ifidx
>= BRCMF_MAX_IFS
) {
329 brcmf_err("rx data ifnum out of range (%d)\n", *ifidx
);
333 if (((h
->flags
& BDC_FLAG_VER_MASK
) >> BDC_FLAG_VER_SHIFT
) !=
335 brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
336 brcmf_ifname(drvr
, *ifidx
), h
->flags
);
340 if (h
->flags
& BDC_FLAG_SUM_GOOD
) {
341 brcmf_dbg(INFO
, "%s: BDC packet received with good rx-csum, flags 0x%x\n",
342 brcmf_ifname(drvr
, *ifidx
), h
->flags
);
343 pkt_set_sum_good(pktbuf
, true);
346 pktbuf
->priority
= h
->priority
& BDC_PRIORITY_MASK
;
348 skb_pull(pktbuf
, BDC_HEADER_LEN
);
349 skb_pull(pktbuf
, h
->data_offset
<< 2);
354 int brcmf_proto_attach(struct brcmf_pub
*drvr
)
356 struct brcmf_proto
*cdc
;
358 cdc
= kzalloc(sizeof(struct brcmf_proto
), GFP_ATOMIC
);
362 /* ensure that the msg buf directly follows the cdc msg struct */
363 if ((unsigned long)(&cdc
->msg
+ 1) != (unsigned long)cdc
->buf
) {
364 brcmf_err("struct brcmf_proto is not correctly defined\n");
369 drvr
->hdrlen
+= BDC_HEADER_LEN
;
370 drvr
->bus_if
->maxctl
= BRCMF_DCMD_MAXLEN
+
371 sizeof(struct brcmf_proto_cdc_dcmd
) + ROUND_UP_MARGIN
;
379 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
380 void brcmf_proto_detach(struct brcmf_pub
*drvr
)
386 void brcmf_proto_stop(struct brcmf_pub
*drvr
)
388 /* Nothing to do for CDC */