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);
301 #define PKTSUMNEEDED(skb) \
302 (((struct sk_buff *)(skb))->ip_summed == CHECKSUM_PARTIAL)
303 #define PKTSETSUMGOOD(skb, x) \
304 (((struct sk_buff *)(skb))->ip_summed = \
305 ((x) ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE))
307 /* PKTSETSUMNEEDED and PKTSUMGOOD are not possible because
308 skb->ip_summed is overloaded */
311 dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
312 void *params, int plen, void *arg, int len, bool set)
314 return BCME_UNSUPPORTED;
317 void dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
319 bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
322 void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, struct sk_buff *pktbuf)
325 struct bdc_header *h;
328 DHD_TRACE(("%s: Enter\n", __func__));
331 /* Push BDC header used to convey priority for buses that don't */
333 skb_push(pktbuf, BDC_HEADER_LEN);
335 h = (struct bdc_header *)(pktbuf->data);
337 h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
338 if (PKTSUMNEEDED(pktbuf))
339 h->flags |= BDC_FLAG_SUM_NEEDED;
341 h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
345 BDC_SET_IF_IDX(h, ifidx);
348 bool dhd_proto_fcinfo(dhd_pub_t *dhd, struct sk_buff *pktbuf, u8 * fcbits)
351 struct bdc_header *h;
353 if (pktbuf->len < BDC_HEADER_LEN) {
354 DHD_ERROR(("%s: rx data too short (%d < %d)\n",
355 __func__, pktbuf->len, BDC_HEADER_LEN));
359 h = (struct bdc_header *)(pktbuf->data);
361 *fcbits = h->priority >> BDC_PRIORITY_FC_SHIFT;
362 if ((h->flags2 & BDC_FLAG2_FC_FLAG) == BDC_FLAG2_FC_FLAG)
368 int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, struct sk_buff *pktbuf)
371 struct bdc_header *h;
374 DHD_TRACE(("%s: Enter\n", __func__));
377 /* Pop BDC header used to convey priority for buses that don't */
379 if (pktbuf->len < BDC_HEADER_LEN) {
380 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
381 pktbuf->len, BDC_HEADER_LEN));
385 h = (struct bdc_header *)(pktbuf->data);
387 *ifidx = BDC_GET_IF_IDX(h);
388 if (*ifidx >= DHD_MAX_IFS) {
389 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
394 if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
396 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
397 dhd_ifname(dhd, *ifidx), h->flags));
401 if (h->flags & BDC_FLAG_SUM_GOOD) {
402 DHD_INFO(("%s: BDC packet received with good rx-csum, "
404 dhd_ifname(dhd, *ifidx), h->flags));
405 PKTSETSUMGOOD(pktbuf, true);
408 pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
410 skb_pull(pktbuf, BDC_HEADER_LEN);
416 int dhd_prot_attach(dhd_pub_t *dhd)
420 cdc = kzalloc(sizeof(dhd_prot_t), GFP_ATOMIC);
422 DHD_ERROR(("%s: kmalloc failed\n", __func__));
426 /* ensure that the msg buf directly follows the cdc msg struct */
427 if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
428 DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
434 dhd->hdrlen += BDC_HEADER_LEN;
436 dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
445 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
446 void dhd_prot_detach(dhd_pub_t *dhd)
452 void dhd_prot_dstats(dhd_pub_t *dhd)
454 /* No stats from dongle added yet, copy bus stats */
455 dhd->dstats.tx_packets = dhd->tx_packets;
456 dhd->dstats.tx_errors = dhd->tx_errors;
457 dhd->dstats.rx_packets = dhd->rx_packets;
458 dhd->dstats.rx_errors = dhd->rx_errors;
459 dhd->dstats.rx_dropped = dhd->rx_dropped;
460 dhd->dstats.multicast = dhd->rx_multicast;
464 int dhd_prot_init(dhd_pub_t *dhd)
469 DHD_TRACE(("%s: Enter\n", __func__));
471 dhd_os_proto_block(dhd);
473 /* Get the device MAC address */
474 strcpy(buf, "cur_etheraddr");
475 ret = dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
477 dhd_os_proto_unblock(dhd);
480 memcpy(dhd->mac.octet, buf, ETH_ALEN);
482 dhd_os_proto_unblock(dhd);
484 #ifdef EMBEDDED_PLATFORM
485 ret = dhd_preinit_ioctls(dhd);
486 #endif /* EMBEDDED_PLATFORM */
488 /* Always assumes wl for now */
494 void dhd_prot_stop(dhd_pub_t *dhd)
496 /* Nothing to do for CDC */