93f31de60eb2473eefe77ed5bae98c078b840d69
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmfmac / dhd_cdc.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16
17 #include <typedefs.h>
18 #include <osl.h>
19
20 #include <bcmutils.h>
21 #include <bcmcdc.h>
22 #include <bcmendian.h>
23
24 #include <dngl_stats.h>
25 #include <dhd.h>
26 #include <dhd_proto.h>
27 #include <dhd_bus.h>
28 #include <dhd_dbg.h>
29 #ifdef CUSTOMER_HW2
30 int wifi_get_mac_addr(unsigned char *buf);
31 #endif
32
33 extern int dhd_preinit_ioctls(dhd_pub_t *dhd);
34
35 /* Packet alignment for most efficient SDIO (can change based on platform) */
36 #ifndef DHD_SDALIGN
37 #define DHD_SDALIGN     32
38 #endif
39 #if !ISPOWEROF2(DHD_SDALIGN)
40 #error DHD_SDALIGN is not a power of 2!
41 #endif
42
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.
49                                          */
50 #define ROUND_UP_MARGIN 2048    /* Biggest SDIO block size possible for
51                                  * round off at the end of buffer
52                                  */
53
54 typedef struct dhd_prot {
55         uint16 reqid;
56         u8 pending;
57         uint32 lastcmd;
58         u8 bus_header[BUS_HEADER_LEN];
59         cdc_ioctl_t msg;
60         unsigned char buf[WLC_IOCTL_MAXLEN + ROUND_UP_MARGIN];
61 } dhd_prot_t;
62
63 static int dhdcdc_msg(dhd_pub_t *dhd)
64 {
65         dhd_prot_t *prot = dhd->prot;
66         int len = ltoh32(prot->msg.len) + sizeof(cdc_ioctl_t);
67
68         DHD_TRACE(("%s: Enter\n", __func__));
69
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
73          */
74         if (len > CDC_MAX_MSG_SIZE)
75                 len = CDC_MAX_MSG_SIZE;
76
77         /* Send request */
78         return dhd_bus_txctl(dhd->bus, (uchar *)&prot->msg, len);
79 }
80
81 static int dhdcdc_cmplt(dhd_pub_t *dhd, uint32 id, uint32 len)
82 {
83         int ret;
84         dhd_prot_t *prot = dhd->prot;
85
86         DHD_TRACE(("%s: Enter\n", __func__));
87
88         do {
89                 ret =
90                     dhd_bus_rxctl(dhd->bus, (uchar *)&prot->msg,
91                                   len + sizeof(cdc_ioctl_t));
92                 if (ret < 0)
93                         break;
94         } while (CDC_IOC_ID(ltoh32(prot->msg.flags)) != id);
95
96         return ret;
97 }
98
99 int
100 dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
101 {
102         dhd_prot_t *prot = dhd->prot;
103         cdc_ioctl_t *msg = &prot->msg;
104         void *info;
105         int ret = 0, retries = 0;
106         uint32 id, flags = 0;
107
108         DHD_TRACE(("%s: Enter\n", __func__));
109         DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
110
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),
115                                 BCME_STRLEN);
116                         goto done;
117                 } else if (!strcmp((char *)buf, "bcmerror")) {
118                         *(int *)buf = dhd->dongle_error;
119                         goto done;
120                 }
121         }
122
123         memset(msg, 0, sizeof(cdc_ioctl_t));
124
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);
130
131         if (buf)
132                 memcpy(prot->buf, buf, len);
133
134         ret = dhdcdc_msg(dhd);
135         if (ret < 0) {
136                 DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status "
137                         "%d\n", ret));
138                 goto done;
139         }
140
141 retry:
142         /* wait for interrupt and get first fragment */
143         ret = dhdcdc_cmplt(dhd, prot->reqid, len);
144         if (ret < 0)
145                 goto done;
146
147         flags = ltoh32(msg->flags);
148         id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
149
150         if ((id < prot->reqid) && (++retries < RETRIES))
151                 goto retry;
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));
155                 ret = -EINVAL;
156                 goto done;
157         }
158
159         /* Check info buffer */
160         info = (void *)&msg[1];
161
162         /* Copy info buffer */
163         if (buf) {
164                 if (ret < (int)len)
165                         len = ret;
166                 memcpy(buf, info, len);
167         }
168
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;
174         }
175
176 done:
177         return ret;
178 }
179
180 int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
181 {
182         dhd_prot_t *prot = dhd->prot;
183         cdc_ioctl_t *msg = &prot->msg;
184         int ret = 0;
185         uint32 flags, id;
186
187         DHD_TRACE(("%s: Enter\n", __func__));
188         DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
189
190         memset(msg, 0, sizeof(cdc_ioctl_t));
191
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);
197
198         if (buf)
199                 memcpy(prot->buf, buf, len);
200
201         ret = dhdcdc_msg(dhd);
202         if (ret < 0)
203                 goto done;
204
205         ret = dhdcdc_cmplt(dhd, prot->reqid, len);
206         if (ret < 0)
207                 goto done;
208
209         flags = ltoh32(msg->flags);
210         id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
211
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));
215                 ret = -EINVAL;
216                 goto done;
217         }
218
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;
224         }
225
226 done:
227         return ret;
228 }
229
230 extern int dhd_bus_interface(struct dhd_bus *bus, uint arg, void *arg2);
231 int
232 dhd_prot_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t *ioc, void *buf, int len)
233 {
234         dhd_prot_t *prot = dhd->prot;
235         int ret = -1;
236
237         if (dhd->busstate == DHD_BUS_DOWN) {
238                 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
239                            __func__));
240                 return ret;
241         }
242         dhd_os_proto_block(dhd);
243
244         DHD_TRACE(("%s: Enter\n", __func__));
245
246         ASSERT(len <= WLC_IOCTL_MAXLEN);
247
248         if (len > WLC_IOCTL_MAXLEN)
249                 goto done;
250
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));
258                 }
259                 goto done;
260         }
261
262         prot->pending = TRUE;
263         prot->lastcmd = ioc->cmd;
264         if (ioc->set)
265                 ret = dhdcdc_set_ioctl(dhd, ifidx, ioc->cmd, buf, len);
266         else {
267                 ret = dhdcdc_query_ioctl(dhd, ifidx, ioc->cmd, buf, len);
268                 if (ret > 0)
269                         ioc->used = ret - sizeof(cdc_ioctl_t);
270         }
271
272         /* Too many programs assume ioctl() returns 0 on success */
273         if (ret >= 0)
274                 ret = 0;
275         else {
276                 cdc_ioctl_t *msg = &prot->msg;
277                 ioc->needed = ltoh32(msg->len); /* len == needed when set/query
278                                                  fails from dongle */
279         }
280
281         /* Intercept the wme_dp ioctl here */
282         if ((!ret) && (ioc->cmd == WLC_SET_VAR) && (!strcmp(buf, "wme_dp"))) {
283                 int slen, val = 0;
284
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);
289         }
290
291         prot->pending = FALSE;
292
293 done:
294         dhd_os_proto_unblock(dhd);
295
296         return ret;
297 }
298
299 int
300 dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
301                   void *params, int plen, void *arg, int len, bool set)
302 {
303         return BCME_UNSUPPORTED;
304 }
305
306 void dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
307 {
308         bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
309 }
310
311 void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *pktbuf)
312 {
313 #ifdef BDC
314         struct bdc_header *h;
315 #endif                          /* BDC */
316
317         DHD_TRACE(("%s: Enter\n", __func__));
318
319 #ifdef BDC
320         /* Push BDC header used to convey priority for buses that don't */
321
322         PKTPUSH(pktbuf, BDC_HEADER_LEN);
323
324         h = (struct bdc_header *)PKTDATA(pktbuf);
325
326         h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
327         if (PKTSUMNEEDED(pktbuf))
328                 h->flags |= BDC_FLAG_SUM_NEEDED;
329
330         h->priority = (PKTPRIO(pktbuf) & BDC_PRIORITY_MASK);
331         h->flags2 = 0;
332         h->rssi = 0;
333 #endif                          /* BDC */
334         BDC_SET_IF_IDX(h, ifidx);
335 }
336
337 bool dhd_proto_fcinfo(dhd_pub_t *dhd, void *pktbuf, u8 * fcbits)
338 {
339 #ifdef BDC
340         struct bdc_header *h;
341
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));
345                 return BCME_ERROR;
346         }
347
348         h = (struct bdc_header *)PKTDATA(pktbuf);
349
350         *fcbits = h->priority >> BDC_PRIORITY_FC_SHIFT;
351         if ((h->flags2 & BDC_FLAG2_FC_FLAG) == BDC_FLAG2_FC_FLAG)
352                 return TRUE;
353 #endif
354         return FALSE;
355 }
356
357 int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pktbuf)
358 {
359 #ifdef BDC
360         struct bdc_header *h;
361 #endif
362
363         DHD_TRACE(("%s: Enter\n", __func__));
364
365 #ifdef BDC
366         /* Pop BDC header used to convey priority for buses that don't */
367
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));
371                 return BCME_ERROR;
372         }
373
374         h = (struct bdc_header *)PKTDATA(pktbuf);
375
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",
379                            __func__, *ifidx));
380                 return BCME_ERROR;
381         }
382
383         if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
384             BDC_PROTO_VER) {
385                 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
386                            dhd_ifname(dhd, *ifidx), h->flags));
387                 return BCME_ERROR;
388         }
389
390         if (h->flags & BDC_FLAG_SUM_GOOD) {
391                 DHD_INFO(("%s: BDC packet received with good rx-csum, "
392                         "flags 0x%x\n",
393                         dhd_ifname(dhd, *ifidx), h->flags));
394                 PKTSETSUMGOOD(pktbuf, TRUE);
395         }
396
397         PKTSETPRIO(pktbuf, (h->priority & BDC_PRIORITY_MASK));
398
399         PKTPULL(pktbuf, BDC_HEADER_LEN);
400 #endif                          /* BDC */
401
402         return 0;
403 }
404
405 int dhd_prot_attach(dhd_pub_t *dhd)
406 {
407         dhd_prot_t *cdc;
408
409 #ifndef DHD_USE_STATIC_BUF
410         cdc = (dhd_prot_t *) MALLOC(dhd->osh, sizeof(dhd_prot_t));
411         if (!cdc) {
412                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
413                 goto fail;
414         }
415 #else
416         cdc = (dhd_prot_t *) dhd_os_prealloc(DHD_PREALLOC_PROT,
417                                                 sizeof(dhd_prot_t));
418         if (!cdc) {
419                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
420                 goto fail;
421         }
422 #endif                          /* DHD_USE_STATIC_BUF */
423         memset(cdc, 0, sizeof(dhd_prot_t));
424
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"));
428                 goto fail;
429         }
430
431         dhd->prot = cdc;
432 #ifdef BDC
433         dhd->hdrlen += BDC_HEADER_LEN;
434 #endif
435         dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
436         return 0;
437
438 fail:
439 #ifndef DHD_USE_STATIC_BUF
440         if (cdc != NULL)
441                 MFREE(dhd->osh, cdc, sizeof(dhd_prot_t));
442 #endif
443         return BCME_NOMEM;
444 }
445
446 /* ~NOTE~ What if another thread is waiting on the semaphore?  Holding it? */
447 void dhd_prot_detach(dhd_pub_t *dhd)
448 {
449 #ifndef DHD_USE_STATIC_BUF
450         MFREE(dhd->osh, dhd->prot, sizeof(dhd_prot_t));
451 #endif
452         dhd->prot = NULL;
453 }
454
455 void dhd_prot_dstats(dhd_pub_t *dhd)
456 {
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;
464         return;
465 }
466
467 int dhd_prot_init(dhd_pub_t *dhd)
468 {
469         int ret = 0;
470         char buf[128];
471
472         DHD_TRACE(("%s: Enter\n", __func__));
473
474         dhd_os_proto_block(dhd);
475
476         /* Get the device MAC address */
477         strcpy(buf, "cur_etheraddr");
478         ret = dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
479         if (ret < 0) {
480                 dhd_os_proto_unblock(dhd);
481                 return ret;
482         }
483         memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
484
485         dhd_os_proto_unblock(dhd);
486
487 #ifdef EMBEDDED_PLATFORM
488         ret = dhd_preinit_ioctls(dhd);
489 #endif                          /* EMBEDDED_PLATFORM */
490
491         /* Always assumes wl for now */
492         dhd->iswl = TRUE;
493
494         return ret;
495 }
496
497 void dhd_prot_stop(dhd_pub_t *dhd)
498 {
499         /* Nothing to do for CDC */
500 }