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 #include <linux/types.h>
18 #include <linux/netdevice.h>
19 #include <linux/sched.h>
22 #include <brcmu_utils.h>
23 #include <brcmu_wifi.h>
26 #include "dhd_proto.h"
30 struct brcmf_proto_cdc_ioctl
{
31 u32 cmd
; /* ioctl command value */
32 u32 len
; /* lower 16: output buflen;
33 * upper 16: input buflen (excludes header) */
34 u32 flags
; /* flag defns given below */
35 u32 status
; /* status code returned from the device */
38 /* Max valid buffer size that can be sent to the dongle */
39 #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
41 /* CDC flag definitions */
42 #define CDCF_IOC_ERROR 0x01 /* 1=ioctl cmd failed */
43 #define CDCF_IOC_SET 0x02 /* 0=get, 1=set cmd */
44 #define CDCF_IOC_IF_MASK 0xF000 /* I/F index */
45 #define CDCF_IOC_IF_SHIFT 12
46 #define CDCF_IOC_ID_MASK 0xFFFF0000 /* id an ioctl pairing */
47 #define CDCF_IOC_ID_SHIFT 16 /* ID Mask shift bits */
48 #define CDC_IOC_ID(flags) \
49 (((flags) & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT)
50 #define CDC_SET_IF_IDX(hdr, idx) \
51 ((hdr)->flags = (((hdr)->flags & ~CDCF_IOC_IF_MASK) | \
52 ((idx) << CDCF_IOC_IF_SHIFT)))
55 * BDC header - Broadcom specific extension of CDC.
56 * Used on data packets to convey priority across USB.
58 #define BDC_HEADER_LEN 4
59 #define BDC_PROTO_VER 1 /* Protocol version */
60 #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
61 #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
62 #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
63 #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
64 #define BDC_PRIORITY_MASK 0x7
65 #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
66 #define BDC_FLAG2_IF_SHIFT 0
68 #define BDC_GET_IF_IDX(hdr) \
69 ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
70 #define BDC_SET_IF_IDX(hdr, idx) \
71 ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
72 ((idx) << BDC_FLAG2_IF_SHIFT)))
74 struct brcmf_proto_bdc_header
{
76 u8 priority
; /* 802.1d Priority, 4:7 flow control info for usb */
82 #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
83 #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE
84 * (amount of header tha might be added)
85 * plus any space that might be needed
86 * for alignment padding.
88 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
89 * round off at the end of buffer
96 u8 bus_header
[BUS_HEADER_LEN
];
97 struct brcmf_proto_cdc_ioctl msg
;
98 unsigned char buf
[BRCMF_C_IOCTL_MAXLEN
+ ROUND_UP_MARGIN
];
101 static int brcmf_proto_cdc_msg(struct brcmf_pub
*drvr
)
103 struct brcmf_proto
*prot
= drvr
->prot
;
104 int len
= le32_to_cpu(prot
->msg
.len
) +
105 sizeof(struct brcmf_proto_cdc_ioctl
);
107 BRCMF_TRACE(("%s: Enter\n", __func__
));
109 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
110 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
111 * is actually sent to the dongle
113 if (len
> CDC_MAX_MSG_SIZE
)
114 len
= CDC_MAX_MSG_SIZE
;
117 return brcmf_sdbrcm_bus_txctl(drvr
->bus
, (unsigned char *)&prot
->msg
,
121 static int brcmf_proto_cdc_cmplt(struct brcmf_pub
*drvr
, u32 id
, u32 len
)
124 struct brcmf_proto
*prot
= drvr
->prot
;
126 BRCMF_TRACE(("%s: Enter\n", __func__
));
129 ret
= brcmf_sdbrcm_bus_rxctl(drvr
->bus
,
130 (unsigned char *)&prot
->msg
,
131 len
+ sizeof(struct brcmf_proto_cdc_ioctl
));
134 } while (CDC_IOC_ID(le32_to_cpu(prot
->msg
.flags
)) != id
);
140 brcmf_proto_cdc_query_ioctl(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
143 struct brcmf_proto
*prot
= drvr
->prot
;
144 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
146 int ret
= 0, retries
= 0;
149 BRCMF_TRACE(("%s: Enter\n", __func__
));
150 BRCMF_CTL(("%s: cmd %d len %d\n", __func__
, cmd
, len
));
152 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
153 if (cmd
== BRCMF_C_GET_VAR
&& buf
) {
154 if (!strcmp((char *)buf
, "bcmerrorstr")) {
155 strncpy((char *)buf
, "bcm_error",
158 } else if (!strcmp((char *)buf
, "bcmerror")) {
159 *(int *)buf
= drvr
->dongle_error
;
164 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_ioctl
));
166 msg
->cmd
= cpu_to_le32(cmd
);
167 msg
->len
= cpu_to_le32(len
);
168 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
);
169 CDC_SET_IF_IDX(msg
, ifidx
);
170 msg
->flags
= cpu_to_le32(msg
->flags
);
173 memcpy(prot
->buf
, buf
, len
);
175 ret
= brcmf_proto_cdc_msg(drvr
);
177 BRCMF_ERROR(("brcmf_proto_cdc_query_ioctl: brcmf_proto_cdc_msg "
178 "failed w/status %d\n", ret
));
183 /* wait for interrupt and get first fragment */
184 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
188 flags
= le32_to_cpu(msg
->flags
);
189 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
191 if ((id
< prot
->reqid
) && (++retries
< RETRIES
))
193 if (id
!= prot
->reqid
) {
194 BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
195 brcmf_ifname(drvr
, ifidx
), __func__
, id
,
201 /* Check info buffer */
202 info
= (void *)&msg
[1];
204 /* Copy info buffer */
208 memcpy(buf
, info
, len
);
211 /* Check the ERROR flag */
212 if (flags
& CDCF_IOC_ERROR
) {
213 ret
= le32_to_cpu(msg
->status
);
214 /* Cache error from dongle */
215 drvr
->dongle_error
= ret
;
222 int brcmf_proto_cdc_set_ioctl(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
225 struct brcmf_proto
*prot
= drvr
->prot
;
226 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
230 BRCMF_TRACE(("%s: Enter\n", __func__
));
231 BRCMF_CTL(("%s: cmd %d len %d\n", __func__
, cmd
, len
));
233 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_ioctl
));
235 msg
->cmd
= cpu_to_le32(cmd
);
236 msg
->len
= cpu_to_le32(len
);
237 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
) | CDCF_IOC_SET
;
238 CDC_SET_IF_IDX(msg
, ifidx
);
239 msg
->flags
= cpu_to_le32(msg
->flags
);
242 memcpy(prot
->buf
, buf
, len
);
244 ret
= brcmf_proto_cdc_msg(drvr
);
248 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
252 flags
= le32_to_cpu(msg
->flags
);
253 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
255 if (id
!= prot
->reqid
) {
256 BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
257 brcmf_ifname(drvr
, ifidx
), __func__
, id
,
263 /* Check the ERROR flag */
264 if (flags
& CDCF_IOC_ERROR
) {
265 ret
= le32_to_cpu(msg
->status
);
266 /* Cache error from dongle */
267 drvr
->dongle_error
= ret
;
275 brcmf_proto_ioctl(struct brcmf_pub
*drvr
, int ifidx
, struct brcmf_ioctl
*ioc
,
278 struct brcmf_proto
*prot
= drvr
->prot
;
281 if (drvr
->busstate
== BRCMF_BUS_DOWN
) {
282 BRCMF_ERROR(("%s : bus is down. we have nothing to do\n",
286 brcmf_os_proto_block(drvr
);
288 BRCMF_TRACE(("%s: Enter\n", __func__
));
290 if (len
> BRCMF_C_IOCTL_MAXLEN
)
293 if (prot
->pending
== true) {
294 BRCMF_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
295 "lastcmd=0x%x (%lu)\n",
296 ioc
->cmd
, (unsigned long)ioc
->cmd
, prot
->lastcmd
,
297 (unsigned long)prot
->lastcmd
));
298 if ((ioc
->cmd
== BRCMF_C_SET_VAR
) ||
299 (ioc
->cmd
== BRCMF_C_GET_VAR
))
300 BRCMF_TRACE(("iovar cmd=%s\n", (char *)buf
));
305 prot
->pending
= true;
306 prot
->lastcmd
= ioc
->cmd
;
308 ret
= brcmf_proto_cdc_set_ioctl(drvr
, ifidx
, ioc
->cmd
,
311 ret
= brcmf_proto_cdc_query_ioctl(drvr
, ifidx
, ioc
->cmd
,
314 ioc
->used
= ret
- sizeof(struct brcmf_proto_cdc_ioctl
);
317 /* Too many programs assume ioctl() returns 0 on success */
321 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
322 /* len == needed when set/query fails from dongle */
323 ioc
->needed
= le32_to_cpu(msg
->len
);
326 /* Intercept the wme_dp ioctl here */
327 if (!ret
&& ioc
->cmd
== BRCMF_C_SET_VAR
&&
328 !strcmp(buf
, "wme_dp")) {
331 slen
= strlen("wme_dp") + 1;
332 if (len
>= (int)(slen
+ sizeof(int)))
333 memcpy(&val
, (char *)buf
+ slen
, sizeof(int));
334 drvr
->wme_dp
= (u8
) le32_to_cpu(val
);
337 prot
->pending
= false;
340 brcmf_os_proto_unblock(drvr
);
345 #define PKTSUMNEEDED(skb) \
346 (((struct sk_buff *)(skb))->ip_summed == CHECKSUM_PARTIAL)
347 #define PKTSETSUMGOOD(skb, x) \
348 (((struct sk_buff *)(skb))->ip_summed = \
349 ((x) ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE))
351 void brcmf_proto_dump(struct brcmf_pub
*drvr
, struct brcmu_strbuf
*strbuf
)
353 brcmu_bprintf(strbuf
, "Protocol CDC: reqid %d\n", drvr
->prot
->reqid
);
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_TRACE(("%s: Enter\n", __func__
));
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 (PKTSUMNEEDED(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_TRACE(("%s: Enter\n", __func__
));
386 /* Pop BDC header used to convey priority for buses that don't */
388 if (pktbuf
->len
< BDC_HEADER_LEN
) {
389 BRCMF_ERROR(("%s: rx data too short (%d < %d)\n", __func__
,
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_ERROR(("%s: rx data ifnum out of range (%d)\n",
403 if (((h
->flags
& BDC_FLAG_VER_MASK
) >> BDC_FLAG_VER_SHIFT
) !=
405 BRCMF_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
406 brcmf_ifname(drvr
, *ifidx
), h
->flags
));
410 if (h
->flags
& BDC_FLAG_SUM_GOOD
) {
411 BRCMF_INFO(("%s: BDC packet received with good rx-csum, "
413 brcmf_ifname(drvr
, *ifidx
), h
->flags
));
414 PKTSETSUMGOOD(pktbuf
, true);
417 pktbuf
->priority
= h
->priority
& BDC_PRIORITY_MASK
;
419 skb_pull(pktbuf
, BDC_HEADER_LEN
);
424 int brcmf_proto_attach(struct brcmf_pub
*drvr
)
426 struct brcmf_proto
*cdc
;
428 cdc
= kzalloc(sizeof(struct brcmf_proto
), GFP_ATOMIC
);
430 BRCMF_ERROR(("%s: kmalloc failed\n", __func__
));
434 /* ensure that the msg buf directly follows the cdc msg struct */
435 if ((unsigned long)(&cdc
->msg
+ 1) != (unsigned long)cdc
->buf
) {
436 BRCMF_ERROR(("struct brcmf_proto is not correctly defined\n"));
441 drvr
->hdrlen
+= BDC_HEADER_LEN
;
442 drvr
->maxctl
= BRCMF_C_IOCTL_MAXLEN
+
443 sizeof(struct brcmf_proto_cdc_ioctl
) + ROUND_UP_MARGIN
;
451 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
452 void brcmf_proto_detach(struct brcmf_pub
*drvr
)
458 void brcmf_proto_dstats(struct brcmf_pub
*drvr
)
460 /* No stats from dongle added yet, copy bus stats */
461 drvr
->dstats
.tx_packets
= drvr
->tx_packets
;
462 drvr
->dstats
.tx_errors
= drvr
->tx_errors
;
463 drvr
->dstats
.rx_packets
= drvr
->rx_packets
;
464 drvr
->dstats
.rx_errors
= drvr
->rx_errors
;
465 drvr
->dstats
.rx_dropped
= drvr
->rx_dropped
;
466 drvr
->dstats
.multicast
= drvr
->rx_multicast
;
470 int brcmf_proto_init(struct brcmf_pub
*drvr
)
475 BRCMF_TRACE(("%s: Enter\n", __func__
));
477 brcmf_os_proto_block(drvr
);
479 /* Get the device MAC address */
480 strcpy(buf
, "cur_etheraddr");
481 ret
= brcmf_proto_cdc_query_ioctl(drvr
, 0, BRCMF_C_GET_VAR
,
484 brcmf_os_proto_unblock(drvr
);
487 memcpy(drvr
->mac
, buf
, ETH_ALEN
);
489 brcmf_os_proto_unblock(drvr
);
491 ret
= brcmf_c_preinit_ioctls(drvr
);
493 /* Always assumes wl for now */
499 void brcmf_proto_stop(struct brcmf_pub
*drvr
)
501 /* Nothing to do for CDC */