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>
25 #include "dngl_stats.h"
27 #include "dhd_proto.h"
31 struct brcmf_proto_cdc_ioctl
{
32 u32 cmd
; /* ioctl command value */
33 u32 len
; /* lower 16: output buflen;
34 * upper 16: input buflen (excludes header) */
35 u32 flags
; /* flag defns given below */
36 u32 status
; /* status code returned from the device */
39 /* Max valid buffer size that can be sent to the dongle */
40 #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
42 /* CDC flag definitions */
43 #define CDCF_IOC_ERROR 0x01 /* 1=ioctl cmd failed */
44 #define CDCF_IOC_SET 0x02 /* 0=get, 1=set cmd */
45 #define CDCF_IOC_IF_MASK 0xF000 /* I/F index */
46 #define CDCF_IOC_IF_SHIFT 12
47 #define CDCF_IOC_ID_MASK 0xFFFF0000 /* id an ioctl pairing */
48 #define CDCF_IOC_ID_SHIFT 16 /* ID Mask shift bits */
49 #define CDC_IOC_ID(flags) \
50 (((flags) & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT)
51 #define CDC_SET_IF_IDX(hdr, idx) \
52 ((hdr)->flags = (((hdr)->flags & ~CDCF_IOC_IF_MASK) | \
53 ((idx) << CDCF_IOC_IF_SHIFT)))
56 * BDC header - Broadcom specific extension of CDC.
57 * Used on data packets to convey priority across USB.
59 #define BDC_HEADER_LEN 4
60 #define BDC_PROTO_VER 1 /* Protocol version */
61 #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
62 #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
63 #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
64 #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
65 #define BDC_PRIORITY_MASK 0x7
66 #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
67 #define BDC_FLAG2_IF_SHIFT 0
69 #define BDC_GET_IF_IDX(hdr) \
70 ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
71 #define BDC_SET_IF_IDX(hdr, idx) \
72 ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
73 ((idx) << BDC_FLAG2_IF_SHIFT)))
75 struct brcmf_proto_bdc_header
{
77 u8 priority
; /* 802.1d Priority, 4:7 flow control info for usb */
83 #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
84 #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE
85 * (amount of header tha might be added)
86 * plus any space that might be needed
87 * for alignment padding.
89 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
90 * round off at the end of buffer
97 u8 bus_header
[BUS_HEADER_LEN
];
98 struct brcmf_proto_cdc_ioctl msg
;
99 unsigned char buf
[BRCMF_C_IOCTL_MAXLEN
+ ROUND_UP_MARGIN
];
102 static int brcmf_proto_cdc_msg(struct brcmf_pub
*drvr
)
104 struct brcmf_proto
*prot
= drvr
->prot
;
105 int len
= le32_to_cpu(prot
->msg
.len
) +
106 sizeof(struct brcmf_proto_cdc_ioctl
);
108 BRCMF_TRACE(("%s: Enter\n", __func__
));
110 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
111 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
112 * is actually sent to the dongle
114 if (len
> CDC_MAX_MSG_SIZE
)
115 len
= CDC_MAX_MSG_SIZE
;
118 return brcmf_sdbrcm_bus_txctl(drvr
->bus
, (unsigned char *)&prot
->msg
,
122 static int brcmf_proto_cdc_cmplt(struct brcmf_pub
*drvr
, u32 id
, u32 len
)
125 struct brcmf_proto
*prot
= drvr
->prot
;
127 BRCMF_TRACE(("%s: Enter\n", __func__
));
130 ret
= brcmf_sdbrcm_bus_rxctl(drvr
->bus
,
131 (unsigned char *)&prot
->msg
,
132 len
+ sizeof(struct brcmf_proto_cdc_ioctl
));
135 } while (CDC_IOC_ID(le32_to_cpu(prot
->msg
.flags
)) != id
);
141 brcmf_proto_cdc_query_ioctl(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
144 struct brcmf_proto
*prot
= drvr
->prot
;
145 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
147 int ret
= 0, retries
= 0;
150 BRCMF_TRACE(("%s: Enter\n", __func__
));
151 BRCMF_CTL(("%s: cmd %d len %d\n", __func__
, 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_ioctl
));
167 msg
->cmd
= cpu_to_le32(cmd
);
168 msg
->len
= cpu_to_le32(len
);
169 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
);
170 CDC_SET_IF_IDX(msg
, ifidx
);
171 msg
->flags
= cpu_to_le32(msg
->flags
);
174 memcpy(prot
->buf
, buf
, len
);
176 ret
= brcmf_proto_cdc_msg(drvr
);
178 BRCMF_ERROR(("brcmf_proto_cdc_query_ioctl: brcmf_proto_cdc_msg "
179 "failed w/status %d\n", ret
));
184 /* wait for interrupt and get first fragment */
185 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
189 flags
= le32_to_cpu(msg
->flags
);
190 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
192 if ((id
< prot
->reqid
) && (++retries
< RETRIES
))
194 if (id
!= prot
->reqid
) {
195 BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
196 brcmf_ifname(drvr
, ifidx
), __func__
, id
,
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
& CDCF_IOC_ERROR
) {
214 ret
= le32_to_cpu(msg
->status
);
215 /* Cache error from dongle */
216 drvr
->dongle_error
= ret
;
223 int brcmf_proto_cdc_set_ioctl(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
226 struct brcmf_proto
*prot
= drvr
->prot
;
227 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
231 BRCMF_TRACE(("%s: Enter\n", __func__
));
232 BRCMF_CTL(("%s: cmd %d len %d\n", __func__
, cmd
, len
));
234 memset(msg
, 0, sizeof(struct brcmf_proto_cdc_ioctl
));
236 msg
->cmd
= cpu_to_le32(cmd
);
237 msg
->len
= cpu_to_le32(len
);
238 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
) | CDCF_IOC_SET
;
239 CDC_SET_IF_IDX(msg
, ifidx
);
240 msg
->flags
= cpu_to_le32(msg
->flags
);
243 memcpy(prot
->buf
, buf
, len
);
245 ret
= brcmf_proto_cdc_msg(drvr
);
249 ret
= brcmf_proto_cdc_cmplt(drvr
, prot
->reqid
, len
);
253 flags
= le32_to_cpu(msg
->flags
);
254 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
256 if (id
!= prot
->reqid
) {
257 BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
258 brcmf_ifname(drvr
, ifidx
), __func__
, id
,
264 /* Check the ERROR flag */
265 if (flags
& CDCF_IOC_ERROR
) {
266 ret
= le32_to_cpu(msg
->status
);
267 /* Cache error from dongle */
268 drvr
->dongle_error
= ret
;
276 brcmf_proto_ioctl(struct brcmf_pub
*drvr
, int ifidx
, struct brcmf_ioctl
*ioc
,
279 struct brcmf_proto
*prot
= drvr
->prot
;
282 if (drvr
->busstate
== BRCMF_BUS_DOWN
) {
283 BRCMF_ERROR(("%s : bus is down. we have nothing to do\n",
287 brcmf_os_proto_block(drvr
);
289 BRCMF_TRACE(("%s: Enter\n", __func__
));
291 if (len
> BRCMF_C_IOCTL_MAXLEN
)
294 if (prot
->pending
== true) {
295 BRCMF_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
296 "lastcmd=0x%x (%lu)\n",
297 ioc
->cmd
, (unsigned long)ioc
->cmd
, prot
->lastcmd
,
298 (unsigned long)prot
->lastcmd
));
299 if ((ioc
->cmd
== BRCMF_C_SET_VAR
) ||
300 (ioc
->cmd
== BRCMF_C_GET_VAR
))
301 BRCMF_TRACE(("iovar cmd=%s\n", (char *)buf
));
306 prot
->pending
= true;
307 prot
->lastcmd
= ioc
->cmd
;
309 ret
= brcmf_proto_cdc_set_ioctl(drvr
, ifidx
, ioc
->cmd
,
312 ret
= brcmf_proto_cdc_query_ioctl(drvr
, ifidx
, ioc
->cmd
,
315 ioc
->used
= ret
- sizeof(struct brcmf_proto_cdc_ioctl
);
318 /* Too many programs assume ioctl() returns 0 on success */
322 struct brcmf_proto_cdc_ioctl
*msg
= &prot
->msg
;
323 /* len == needed when set/query fails from dongle */
324 ioc
->needed
= le32_to_cpu(msg
->len
);
327 /* Intercept the wme_dp ioctl here */
328 if (!ret
&& ioc
->cmd
== BRCMF_C_SET_VAR
&&
329 !strcmp(buf
, "wme_dp")) {
332 slen
= strlen("wme_dp") + 1;
333 if (len
>= (int)(slen
+ sizeof(int)))
334 memcpy(&val
, (char *)buf
+ slen
, sizeof(int));
335 drvr
->wme_dp
= (u8
) le32_to_cpu(val
);
338 prot
->pending
= false;
341 brcmf_os_proto_unblock(drvr
);
346 #define PKTSUMNEEDED(skb) \
347 (((struct sk_buff *)(skb))->ip_summed == CHECKSUM_PARTIAL)
348 #define PKTSETSUMGOOD(skb, x) \
349 (((struct sk_buff *)(skb))->ip_summed = \
350 ((x) ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE))
352 void brcmf_proto_dump(struct brcmf_pub
*drvr
, struct brcmu_strbuf
*strbuf
)
354 brcmu_bprintf(strbuf
, "Protocol CDC: reqid %d\n", drvr
->prot
->reqid
);
357 void brcmf_proto_hdrpush(struct brcmf_pub
*drvr
, int ifidx
,
358 struct sk_buff
*pktbuf
)
360 struct brcmf_proto_bdc_header
*h
;
362 BRCMF_TRACE(("%s: Enter\n", __func__
));
364 /* Push BDC header used to convey priority for buses that don't */
366 skb_push(pktbuf
, BDC_HEADER_LEN
);
368 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
370 h
->flags
= (BDC_PROTO_VER
<< BDC_FLAG_VER_SHIFT
);
371 if (PKTSUMNEEDED(pktbuf
))
372 h
->flags
|= BDC_FLAG_SUM_NEEDED
;
374 h
->priority
= (pktbuf
->priority
& BDC_PRIORITY_MASK
);
377 BDC_SET_IF_IDX(h
, ifidx
);
380 int brcmf_proto_hdrpull(struct brcmf_pub
*drvr
, int *ifidx
,
381 struct sk_buff
*pktbuf
)
383 struct brcmf_proto_bdc_header
*h
;
385 BRCMF_TRACE(("%s: Enter\n", __func__
));
387 /* Pop BDC header used to convey priority for buses that don't */
389 if (pktbuf
->len
< BDC_HEADER_LEN
) {
390 BRCMF_ERROR(("%s: rx data too short (%d < %d)\n", __func__
,
391 pktbuf
->len
, BDC_HEADER_LEN
));
395 h
= (struct brcmf_proto_bdc_header
*)(pktbuf
->data
);
397 *ifidx
= BDC_GET_IF_IDX(h
);
398 if (*ifidx
>= BRCMF_MAX_IFS
) {
399 BRCMF_ERROR(("%s: rx data ifnum out of range (%d)\n",
404 if (((h
->flags
& BDC_FLAG_VER_MASK
) >> BDC_FLAG_VER_SHIFT
) !=
406 BRCMF_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
407 brcmf_ifname(drvr
, *ifidx
), h
->flags
));
411 if (h
->flags
& BDC_FLAG_SUM_GOOD
) {
412 BRCMF_INFO(("%s: BDC packet received with good rx-csum, "
414 brcmf_ifname(drvr
, *ifidx
), h
->flags
));
415 PKTSETSUMGOOD(pktbuf
, true);
418 pktbuf
->priority
= h
->priority
& BDC_PRIORITY_MASK
;
420 skb_pull(pktbuf
, BDC_HEADER_LEN
);
425 int brcmf_proto_attach(struct brcmf_pub
*drvr
)
427 struct brcmf_proto
*cdc
;
429 cdc
= kzalloc(sizeof(struct brcmf_proto
), GFP_ATOMIC
);
431 BRCMF_ERROR(("%s: kmalloc failed\n", __func__
));
435 /* ensure that the msg buf directly follows the cdc msg struct */
436 if ((unsigned long)(&cdc
->msg
+ 1) != (unsigned long)cdc
->buf
) {
437 BRCMF_ERROR(("struct brcmf_proto is not correctly defined\n"));
442 drvr
->hdrlen
+= BDC_HEADER_LEN
;
443 drvr
->maxctl
= BRCMF_C_IOCTL_MAXLEN
+
444 sizeof(struct brcmf_proto_cdc_ioctl
) + ROUND_UP_MARGIN
;
452 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
453 void brcmf_proto_detach(struct brcmf_pub
*drvr
)
459 void brcmf_proto_dstats(struct brcmf_pub
*drvr
)
461 /* No stats from dongle added yet, copy bus stats */
462 drvr
->dstats
.tx_packets
= drvr
->tx_packets
;
463 drvr
->dstats
.tx_errors
= drvr
->tx_errors
;
464 drvr
->dstats
.rx_packets
= drvr
->rx_packets
;
465 drvr
->dstats
.rx_errors
= drvr
->rx_errors
;
466 drvr
->dstats
.rx_dropped
= drvr
->rx_dropped
;
467 drvr
->dstats
.multicast
= drvr
->rx_multicast
;
471 int brcmf_proto_init(struct brcmf_pub
*drvr
)
476 BRCMF_TRACE(("%s: Enter\n", __func__
));
478 brcmf_os_proto_block(drvr
);
480 /* Get the device MAC address */
481 strcpy(buf
, "cur_etheraddr");
482 ret
= brcmf_proto_cdc_query_ioctl(drvr
, 0, BRCMF_C_GET_VAR
,
485 brcmf_os_proto_unblock(drvr
);
488 memcpy(drvr
->mac
, buf
, ETH_ALEN
);
490 brcmf_os_proto_unblock(drvr
);
492 ret
= brcmf_c_preinit_ioctls(drvr
);
494 /* Always assumes wl for now */
500 void brcmf_proto_stop(struct brcmf_pub
*drvr
)
502 /* Nothing to do for CDC */