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>
24 #include <bcmendian.h>
26 #include <dngl_stats.h>
28 #include <dhd_proto.h>
32 int wifi_get_mac_addr(unsigned char *buf
);
35 extern int dhd_preinit_ioctls(dhd_pub_t
*dhd
);
37 /* Packet alignment for most efficient SDIO (can change based on platform) */
39 #define DHD_SDALIGN 32
41 #if !ISPOWEROF2(DHD_SDALIGN)
42 #error DHD_SDALIGN is not a power of 2!
45 #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
46 #define BUS_HEADER_LEN (16+DHD_SDALIGN) /* Must be atleast SDPCM_RESERVE
47 * defined in dhd_sdio.c
48 * (amount of header tha might be added)
49 * plus any space that might be needed
50 * for alignment padding.
52 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
53 * round off at the end of buffer
56 typedef struct dhd_prot
{
60 u8 bus_header
[BUS_HEADER_LEN
];
62 unsigned char buf
[WLC_IOCTL_MAXLEN
+ ROUND_UP_MARGIN
];
65 static int dhdcdc_msg(dhd_pub_t
*dhd
)
67 dhd_prot_t
*prot
= dhd
->prot
;
68 int len
= ltoh32(prot
->msg
.len
) + sizeof(cdc_ioctl_t
);
70 DHD_TRACE(("%s: Enter\n", __func__
));
72 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
73 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
74 * is actually sent to the dongle
76 if (len
> CDC_MAX_MSG_SIZE
)
77 len
= CDC_MAX_MSG_SIZE
;
80 return dhd_bus_txctl(dhd
->bus
, (unsigned char *)&prot
->msg
, len
);
83 static int dhdcdc_cmplt(dhd_pub_t
*dhd
, u32 id
, u32 len
)
86 dhd_prot_t
*prot
= dhd
->prot
;
88 DHD_TRACE(("%s: Enter\n", __func__
));
92 dhd_bus_rxctl(dhd
->bus
, (unsigned char *)&prot
->msg
,
93 len
+ sizeof(cdc_ioctl_t
));
96 } while (CDC_IOC_ID(ltoh32(prot
->msg
.flags
)) != id
);
102 dhdcdc_query_ioctl(dhd_pub_t
*dhd
, int ifidx
, uint cmd
, void *buf
, uint len
)
104 dhd_prot_t
*prot
= dhd
->prot
;
105 cdc_ioctl_t
*msg
= &prot
->msg
;
107 int ret
= 0, retries
= 0;
110 DHD_TRACE(("%s: Enter\n", __func__
));
111 DHD_CTL(("%s: cmd %d len %d\n", __func__
, cmd
, len
));
113 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
114 if (cmd
== WLC_GET_VAR
&& buf
) {
115 if (!strcmp((char *)buf
, "bcmerrorstr")) {
116 strncpy((char *)buf
, bcmerrorstr(dhd
->dongle_error
),
119 } else if (!strcmp((char *)buf
, "bcmerror")) {
120 *(int *)buf
= dhd
->dongle_error
;
125 memset(msg
, 0, sizeof(cdc_ioctl_t
));
127 msg
->cmd
= htol32(cmd
);
128 msg
->len
= htol32(len
);
129 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
);
130 CDC_SET_IF_IDX(msg
, ifidx
);
131 msg
->flags
= htol32(msg
->flags
);
134 memcpy(prot
->buf
, buf
, len
);
136 ret
= dhdcdc_msg(dhd
);
138 DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status "
144 /* wait for interrupt and get first fragment */
145 ret
= dhdcdc_cmplt(dhd
, prot
->reqid
, len
);
149 flags
= ltoh32(msg
->flags
);
150 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
152 if ((id
< prot
->reqid
) && (++retries
< RETRIES
))
154 if (id
!= prot
->reqid
) {
155 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
156 dhd_ifname(dhd
, ifidx
), __func__
, id
, prot
->reqid
));
161 /* Check info buffer */
162 info
= (void *)&msg
[1];
164 /* Copy info buffer */
168 memcpy(buf
, info
, len
);
171 /* Check the ERROR flag */
172 if (flags
& CDCF_IOC_ERROR
) {
173 ret
= ltoh32(msg
->status
);
174 /* Cache error from dongle */
175 dhd
->dongle_error
= ret
;
182 int dhdcdc_set_ioctl(dhd_pub_t
*dhd
, int ifidx
, uint cmd
, void *buf
, uint len
)
184 dhd_prot_t
*prot
= dhd
->prot
;
185 cdc_ioctl_t
*msg
= &prot
->msg
;
189 DHD_TRACE(("%s: Enter\n", __func__
));
190 DHD_CTL(("%s: cmd %d len %d\n", __func__
, cmd
, len
));
192 memset(msg
, 0, sizeof(cdc_ioctl_t
));
194 msg
->cmd
= htol32(cmd
);
195 msg
->len
= htol32(len
);
196 msg
->flags
= (++prot
->reqid
<< CDCF_IOC_ID_SHIFT
) | CDCF_IOC_SET
;
197 CDC_SET_IF_IDX(msg
, ifidx
);
198 msg
->flags
= htol32(msg
->flags
);
201 memcpy(prot
->buf
, buf
, len
);
203 ret
= dhdcdc_msg(dhd
);
207 ret
= dhdcdc_cmplt(dhd
, prot
->reqid
, len
);
211 flags
= ltoh32(msg
->flags
);
212 id
= (flags
& CDCF_IOC_ID_MASK
) >> CDCF_IOC_ID_SHIFT
;
214 if (id
!= prot
->reqid
) {
215 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
216 dhd_ifname(dhd
, ifidx
), __func__
, id
, prot
->reqid
));
221 /* Check the ERROR flag */
222 if (flags
& CDCF_IOC_ERROR
) {
223 ret
= ltoh32(msg
->status
);
224 /* Cache error from dongle */
225 dhd
->dongle_error
= ret
;
232 extern int dhd_bus_interface(struct dhd_bus
*bus
, uint arg
, void *arg2
);
234 dhd_prot_ioctl(dhd_pub_t
*dhd
, int ifidx
, wl_ioctl_t
*ioc
, void *buf
, int len
)
236 dhd_prot_t
*prot
= dhd
->prot
;
239 if (dhd
->busstate
== DHD_BUS_DOWN
) {
240 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
244 dhd_os_proto_block(dhd
);
246 DHD_TRACE(("%s: Enter\n", __func__
));
248 ASSERT(len
<= WLC_IOCTL_MAXLEN
);
250 if (len
> WLC_IOCTL_MAXLEN
)
253 if (prot
->pending
== true) {
254 DHD_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
255 "lastcmd=0x%x (%lu)\n",
256 ioc
->cmd
, (unsigned long)ioc
->cmd
, prot
->lastcmd
,
257 (unsigned long)prot
->lastcmd
));
258 if ((ioc
->cmd
== WLC_SET_VAR
) || (ioc
->cmd
== WLC_GET_VAR
)) {
259 DHD_TRACE(("iovar cmd=%s\n", (char *)buf
));
264 prot
->pending
= true;
265 prot
->lastcmd
= ioc
->cmd
;
267 ret
= dhdcdc_set_ioctl(dhd
, ifidx
, ioc
->cmd
, buf
, len
);
269 ret
= dhdcdc_query_ioctl(dhd
, ifidx
, ioc
->cmd
, buf
, len
);
271 ioc
->used
= ret
- sizeof(cdc_ioctl_t
);
274 /* Too many programs assume ioctl() returns 0 on success */
278 cdc_ioctl_t
*msg
= &prot
->msg
;
279 ioc
->needed
= ltoh32(msg
->len
); /* len == needed when set/query
283 /* Intercept the wme_dp ioctl here */
284 if ((!ret
) && (ioc
->cmd
== WLC_SET_VAR
) && (!strcmp(buf
, "wme_dp"))) {
287 slen
= strlen("wme_dp") + 1;
288 if (len
>= (int)(slen
+ sizeof(int)))
289 bcopy(((char *)buf
+ slen
), &val
, sizeof(int));
290 dhd
->wme_dp
= (u8
) ltoh32(val
);
293 prot
->pending
= false;
296 dhd_os_proto_unblock(dhd
);
302 dhd_prot_iovar_op(dhd_pub_t
*dhdp
, const char *name
,
303 void *params
, int plen
, void *arg
, int len
, bool set
)
305 return BCME_UNSUPPORTED
;
308 void dhd_prot_dump(dhd_pub_t
*dhdp
, struct bcmstrbuf
*strbuf
)
310 bcm_bprintf(strbuf
, "Protocol CDC: reqid %d\n", dhdp
->prot
->reqid
);
313 void dhd_prot_hdrpush(dhd_pub_t
*dhd
, int ifidx
, void *pktbuf
)
316 struct bdc_header
*h
;
319 DHD_TRACE(("%s: Enter\n", __func__
));
322 /* Push BDC header used to convey priority for buses that don't */
324 PKTPUSH(pktbuf
, BDC_HEADER_LEN
);
326 h
= (struct bdc_header
*)PKTDATA(pktbuf
);
328 h
->flags
= (BDC_PROTO_VER
<< BDC_FLAG_VER_SHIFT
);
329 if (PKTSUMNEEDED(pktbuf
))
330 h
->flags
|= BDC_FLAG_SUM_NEEDED
;
332 h
->priority
= (PKTPRIO(pktbuf
) & BDC_PRIORITY_MASK
);
336 BDC_SET_IF_IDX(h
, ifidx
);
339 bool dhd_proto_fcinfo(dhd_pub_t
*dhd
, void *pktbuf
, u8
* fcbits
)
342 struct bdc_header
*h
;
344 if (PKTLEN(pktbuf
) < BDC_HEADER_LEN
) {
345 DHD_ERROR(("%s: rx data too short (%d < %d)\n",
346 __func__
, PKTLEN(pktbuf
), BDC_HEADER_LEN
));
350 h
= (struct bdc_header
*)PKTDATA(pktbuf
);
352 *fcbits
= h
->priority
>> BDC_PRIORITY_FC_SHIFT
;
353 if ((h
->flags2
& BDC_FLAG2_FC_FLAG
) == BDC_FLAG2_FC_FLAG
)
359 int dhd_prot_hdrpull(dhd_pub_t
*dhd
, int *ifidx
, void *pktbuf
)
362 struct bdc_header
*h
;
365 DHD_TRACE(("%s: Enter\n", __func__
));
368 /* Pop BDC header used to convey priority for buses that don't */
370 if (PKTLEN(pktbuf
) < BDC_HEADER_LEN
) {
371 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__
,
372 PKTLEN(pktbuf
), BDC_HEADER_LEN
));
376 h
= (struct bdc_header
*)PKTDATA(pktbuf
);
378 *ifidx
= BDC_GET_IF_IDX(h
);
379 if (*ifidx
>= DHD_MAX_IFS
) {
380 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
385 if (((h
->flags
& BDC_FLAG_VER_MASK
) >> BDC_FLAG_VER_SHIFT
) !=
387 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
388 dhd_ifname(dhd
, *ifidx
), h
->flags
));
392 if (h
->flags
& BDC_FLAG_SUM_GOOD
) {
393 DHD_INFO(("%s: BDC packet received with good rx-csum, "
395 dhd_ifname(dhd
, *ifidx
), h
->flags
));
396 PKTSETSUMGOOD(pktbuf
, true);
399 PKTSETPRIO(pktbuf
, (h
->priority
& BDC_PRIORITY_MASK
));
401 PKTPULL(pktbuf
, BDC_HEADER_LEN
);
407 int dhd_prot_attach(dhd_pub_t
*dhd
)
411 cdc
= kzalloc(sizeof(dhd_prot_t
), GFP_ATOMIC
);
413 DHD_ERROR(("%s: kmalloc failed\n", __func__
));
417 /* ensure that the msg buf directly follows the cdc msg struct */
418 if ((unsigned long)(&cdc
->msg
+ 1) != (unsigned long)cdc
->buf
) {
419 DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
425 dhd
->hdrlen
+= BDC_HEADER_LEN
;
427 dhd
->maxctl
= WLC_IOCTL_MAXLEN
+ sizeof(cdc_ioctl_t
) + ROUND_UP_MARGIN
;
436 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
437 void dhd_prot_detach(dhd_pub_t
*dhd
)
443 void dhd_prot_dstats(dhd_pub_t
*dhd
)
445 /* No stats from dongle added yet, copy bus stats */
446 dhd
->dstats
.tx_packets
= dhd
->tx_packets
;
447 dhd
->dstats
.tx_errors
= dhd
->tx_errors
;
448 dhd
->dstats
.rx_packets
= dhd
->rx_packets
;
449 dhd
->dstats
.rx_errors
= dhd
->rx_errors
;
450 dhd
->dstats
.rx_dropped
= dhd
->rx_dropped
;
451 dhd
->dstats
.multicast
= dhd
->rx_multicast
;
455 int dhd_prot_init(dhd_pub_t
*dhd
)
460 DHD_TRACE(("%s: Enter\n", __func__
));
462 dhd_os_proto_block(dhd
);
464 /* Get the device MAC address */
465 strcpy(buf
, "cur_etheraddr");
466 ret
= dhdcdc_query_ioctl(dhd
, 0, WLC_GET_VAR
, buf
, sizeof(buf
));
468 dhd_os_proto_unblock(dhd
);
471 memcpy(dhd
->mac
.octet
, buf
, ETHER_ADDR_LEN
);
473 dhd_os_proto_unblock(dhd
);
475 #ifdef EMBEDDED_PLATFORM
476 ret
= dhd_preinit_ioctls(dhd
);
477 #endif /* EMBEDDED_PLATFORM */
479 /* Always assumes wl for now */
485 void dhd_prot_stop(dhd_pub_t
*dhd
)
487 /* Nothing to do for CDC */