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