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.
22 #include <bcmendian.h>
24 #include <dngl_stats.h>
26 #include <dhd_proto.h>
30 int wifi_get_mac_addr(unsigned char *buf);
33 extern int dhd_preinit_ioctls(dhd_pub_t *dhd);
35 /* Packet alignment for most efficient SDIO (can change based on platform) */
37 #define DHD_SDALIGN 32
39 #if !ISPOWEROF2(DHD_SDALIGN)
40 #error DHD_SDALIGN is not a power of 2!
43 #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
44 #define BUS_HEADER_LEN (16+DHD_SDALIGN) /* Must be atleast SDPCM_RESERVE
45 * defined in dhd_sdio.c
46 * (amount of header tha might be added)
47 * plus any space that might be needed
48 * for alignment padding.
50 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
51 * round off at the end of buffer
54 typedef struct dhd_prot {
58 u8 bus_header[BUS_HEADER_LEN];
60 unsigned char buf[WLC_IOCTL_MAXLEN + ROUND_UP_MARGIN];
63 static int dhdcdc_msg(dhd_pub_t *dhd)
65 dhd_prot_t *prot = dhd->prot;
66 int len = ltoh32(prot->msg.len) + sizeof(cdc_ioctl_t);
68 DHD_TRACE(("%s: Enter\n", __func__));
70 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
71 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
72 * is actually sent to the dongle
74 if (len > CDC_MAX_MSG_SIZE)
75 len = CDC_MAX_MSG_SIZE;
78 return dhd_bus_txctl(dhd->bus, (unsigned char *)&prot->msg, len);
81 static int dhdcdc_cmplt(dhd_pub_t *dhd, u32 id, u32 len)
84 dhd_prot_t *prot = dhd->prot;
86 DHD_TRACE(("%s: Enter\n", __func__));
90 dhd_bus_rxctl(dhd->bus, (unsigned char *)&prot->msg,
91 len + sizeof(cdc_ioctl_t));
94 } while (CDC_IOC_ID(ltoh32(prot->msg.flags)) != id);
100 dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
102 dhd_prot_t *prot = dhd->prot;
103 cdc_ioctl_t *msg = &prot->msg;
105 int ret = 0, retries = 0;
108 DHD_TRACE(("%s: Enter\n", __func__));
109 DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
111 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
112 if (cmd == WLC_GET_VAR && buf) {
113 if (!strcmp((char *)buf, "bcmerrorstr")) {
114 strncpy((char *)buf, bcmerrorstr(dhd->dongle_error),
117 } else if (!strcmp((char *)buf, "bcmerror")) {
118 *(int *)buf = dhd->dongle_error;
123 memset(msg, 0, sizeof(cdc_ioctl_t));
125 msg->cmd = htol32(cmd);
126 msg->len = htol32(len);
127 msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
128 CDC_SET_IF_IDX(msg, ifidx);
129 msg->flags = htol32(msg->flags);
132 memcpy(prot->buf, buf, len);
134 ret = dhdcdc_msg(dhd);
136 DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status "
142 /* wait for interrupt and get first fragment */
143 ret = dhdcdc_cmplt(dhd, prot->reqid, len);
147 flags = ltoh32(msg->flags);
148 id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
150 if ((id < prot->reqid) && (++retries < RETRIES))
152 if (id != prot->reqid) {
153 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
154 dhd_ifname(dhd, ifidx), __func__, id, prot->reqid));
159 /* Check info buffer */
160 info = (void *)&msg[1];
162 /* Copy info buffer */
166 memcpy(buf, info, len);
169 /* Check the ERROR flag */
170 if (flags & CDCF_IOC_ERROR) {
171 ret = ltoh32(msg->status);
172 /* Cache error from dongle */
173 dhd->dongle_error = ret;
180 int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
182 dhd_prot_t *prot = dhd->prot;
183 cdc_ioctl_t *msg = &prot->msg;
187 DHD_TRACE(("%s: Enter\n", __func__));
188 DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
190 memset(msg, 0, sizeof(cdc_ioctl_t));
192 msg->cmd = htol32(cmd);
193 msg->len = htol32(len);
194 msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT) | CDCF_IOC_SET;
195 CDC_SET_IF_IDX(msg, ifidx);
196 msg->flags = htol32(msg->flags);
199 memcpy(prot->buf, buf, len);
201 ret = dhdcdc_msg(dhd);
205 ret = dhdcdc_cmplt(dhd, prot->reqid, len);
209 flags = ltoh32(msg->flags);
210 id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
212 if (id != prot->reqid) {
213 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
214 dhd_ifname(dhd, ifidx), __func__, id, prot->reqid));
219 /* Check the ERROR flag */
220 if (flags & CDCF_IOC_ERROR) {
221 ret = ltoh32(msg->status);
222 /* Cache error from dongle */
223 dhd->dongle_error = ret;
230 extern int dhd_bus_interface(struct dhd_bus *bus, uint arg, void *arg2);
232 dhd_prot_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t *ioc, void *buf, int len)
234 dhd_prot_t *prot = dhd->prot;
237 if (dhd->busstate == DHD_BUS_DOWN) {
238 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
242 dhd_os_proto_block(dhd);
244 DHD_TRACE(("%s: Enter\n", __func__));
246 ASSERT(len <= WLC_IOCTL_MAXLEN);
248 if (len > WLC_IOCTL_MAXLEN)
251 if (prot->pending == true) {
252 DHD_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
253 "lastcmd=0x%x (%lu)\n",
254 ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
255 (unsigned long)prot->lastcmd));
256 if ((ioc->cmd == WLC_SET_VAR) || (ioc->cmd == WLC_GET_VAR)) {
257 DHD_TRACE(("iovar cmd=%s\n", (char *)buf));
262 prot->pending = true;
263 prot->lastcmd = ioc->cmd;
265 ret = dhdcdc_set_ioctl(dhd, ifidx, ioc->cmd, buf, len);
267 ret = dhdcdc_query_ioctl(dhd, ifidx, ioc->cmd, buf, len);
269 ioc->used = ret - sizeof(cdc_ioctl_t);
272 /* Too many programs assume ioctl() returns 0 on success */
276 cdc_ioctl_t *msg = &prot->msg;
277 ioc->needed = ltoh32(msg->len); /* len == needed when set/query
281 /* Intercept the wme_dp ioctl here */
282 if ((!ret) && (ioc->cmd == WLC_SET_VAR) && (!strcmp(buf, "wme_dp"))) {
285 slen = strlen("wme_dp") + 1;
286 if (len >= (int)(slen + sizeof(int)))
287 bcopy(((char *)buf + slen), &val, sizeof(int));
288 dhd->wme_dp = (u8) ltoh32(val);
291 prot->pending = false;
294 dhd_os_proto_unblock(dhd);
300 dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
301 void *params, int plen, void *arg, int len, bool set)
303 return BCME_UNSUPPORTED;
306 void dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
308 bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
311 void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *pktbuf)
314 struct bdc_header *h;
317 DHD_TRACE(("%s: Enter\n", __func__));
320 /* Push BDC header used to convey priority for buses that don't */
322 PKTPUSH(pktbuf, BDC_HEADER_LEN);
324 h = (struct bdc_header *)PKTDATA(pktbuf);
326 h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
327 if (PKTSUMNEEDED(pktbuf))
328 h->flags |= BDC_FLAG_SUM_NEEDED;
330 h->priority = (PKTPRIO(pktbuf) & BDC_PRIORITY_MASK);
334 BDC_SET_IF_IDX(h, ifidx);
337 bool dhd_proto_fcinfo(dhd_pub_t *dhd, void *pktbuf, u8 * fcbits)
340 struct bdc_header *h;
342 if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
343 DHD_ERROR(("%s: rx data too short (%d < %d)\n",
344 __func__, PKTLEN(pktbuf), BDC_HEADER_LEN));
348 h = (struct bdc_header *)PKTDATA(pktbuf);
350 *fcbits = h->priority >> BDC_PRIORITY_FC_SHIFT;
351 if ((h->flags2 & BDC_FLAG2_FC_FLAG) == BDC_FLAG2_FC_FLAG)
357 int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pktbuf)
360 struct bdc_header *h;
363 DHD_TRACE(("%s: Enter\n", __func__));
366 /* Pop BDC header used to convey priority for buses that don't */
368 if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
369 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
370 PKTLEN(pktbuf), BDC_HEADER_LEN));
374 h = (struct bdc_header *)PKTDATA(pktbuf);
376 *ifidx = BDC_GET_IF_IDX(h);
377 if (*ifidx >= DHD_MAX_IFS) {
378 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
383 if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
385 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
386 dhd_ifname(dhd, *ifidx), h->flags));
390 if (h->flags & BDC_FLAG_SUM_GOOD) {
391 DHD_INFO(("%s: BDC packet received with good rx-csum, "
393 dhd_ifname(dhd, *ifidx), h->flags));
394 PKTSETSUMGOOD(pktbuf, true);
397 PKTSETPRIO(pktbuf, (h->priority & BDC_PRIORITY_MASK));
399 PKTPULL(pktbuf, BDC_HEADER_LEN);
405 int dhd_prot_attach(dhd_pub_t *dhd)
409 #ifndef DHD_USE_STATIC_BUF
410 cdc = (dhd_prot_t *) MALLOC(dhd->osh, sizeof(dhd_prot_t));
412 DHD_ERROR(("%s: kmalloc failed\n", __func__));
416 cdc = (dhd_prot_t *) dhd_os_prealloc(DHD_PREALLOC_PROT,
419 DHD_ERROR(("%s: kmalloc failed\n", __func__));
422 #endif /* DHD_USE_STATIC_BUF */
423 memset(cdc, 0, sizeof(dhd_prot_t));
425 /* ensure that the msg buf directly follows the cdc msg struct */
426 if ((uintptr) (&cdc->msg + 1) != (uintptr) cdc->buf) {
427 DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
433 dhd->hdrlen += BDC_HEADER_LEN;
435 dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
439 #ifndef DHD_USE_STATIC_BUF
441 MFREE(dhd->osh, cdc, sizeof(dhd_prot_t));
446 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
447 void dhd_prot_detach(dhd_pub_t *dhd)
449 #ifndef DHD_USE_STATIC_BUF
450 MFREE(dhd->osh, dhd->prot, sizeof(dhd_prot_t));
455 void dhd_prot_dstats(dhd_pub_t *dhd)
457 /* No stats from dongle added yet, copy bus stats */
458 dhd->dstats.tx_packets = dhd->tx_packets;
459 dhd->dstats.tx_errors = dhd->tx_errors;
460 dhd->dstats.rx_packets = dhd->rx_packets;
461 dhd->dstats.rx_errors = dhd->rx_errors;
462 dhd->dstats.rx_dropped = dhd->rx_dropped;
463 dhd->dstats.multicast = dhd->rx_multicast;
467 int dhd_prot_init(dhd_pub_t *dhd)
472 DHD_TRACE(("%s: Enter\n", __func__));
474 dhd_os_proto_block(dhd);
476 /* Get the device MAC address */
477 strcpy(buf, "cur_etheraddr");
478 ret = dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
480 dhd_os_proto_unblock(dhd);
483 memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
485 dhd_os_proto_unblock(dhd);
487 #ifdef EMBEDDED_PLATFORM
488 ret = dhd_preinit_ioctls(dhd);
489 #endif /* EMBEDDED_PLATFORM */
491 /* Always assumes wl for now */
497 void dhd_prot_stop(dhd_pub_t *dhd)
499 /* Nothing to do for CDC */