Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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 <bcmdefs.h>
19 #include <osl.h>
20
21 #include <bcmutils.h>
22 #include <bcmcdc.h>
23 #include <bcmendian.h>
24
25 #include <dngl_stats.h>
26 #include <dhd.h>
27 #include <dhd_proto.h>
28 #include <dhd_bus.h>
29 #include <dhd_dbg.h>
30 #ifdef CUSTOMER_HW2
31 int wifi_get_mac_addr(unsigned char *buf);
32 #endif
33
34 extern int dhd_preinit_ioctls(dhd_pub_t *dhd);
35
36 /* Packet alignment for most efficient SDIO (can change based on platform) */
37 #ifndef DHD_SDALIGN
38 #define DHD_SDALIGN     32
39 #endif
40 #if !ISPOWEROF2(DHD_SDALIGN)
41 #error DHD_SDALIGN is not a power of 2!
42 #endif
43
44 #define RETRIES 2       /* # of retries to retrieve matching ioctl response */
45 #define BUS_HEADER_LEN  (16+DHD_SDALIGN) /* Must be atleast SDPCM_RESERVE
46                                          * defined in dhd_sdio.c
47                                          * (amount of header tha might be added)
48                                          * plus any space that might be needed
49                                          * for alignment padding.
50                                          */
51 #define ROUND_UP_MARGIN 2048    /* Biggest SDIO block size possible for
52                                  * round off at the end of buffer
53                                  */
54
55 typedef struct dhd_prot {
56         u16 reqid;
57         u8 pending;
58         u32 lastcmd;
59         u8 bus_header[BUS_HEADER_LEN];
60         cdc_ioctl_t msg;
61         unsigned char buf[WLC_IOCTL_MAXLEN + ROUND_UP_MARGIN];
62 } dhd_prot_t;
63
64 static int dhdcdc_msg(dhd_pub_t *dhd)
65 {
66         dhd_prot_t *prot = dhd->prot;
67         int len = ltoh32(prot->msg.len) + sizeof(cdc_ioctl_t);
68
69         DHD_TRACE(("%s: Enter\n", __func__));
70
71         /* NOTE : cdc->msg.len holds the desired length of the buffer to be
72          *        returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
73          *        is actually sent to the dongle
74          */
75         if (len > CDC_MAX_MSG_SIZE)
76                 len = CDC_MAX_MSG_SIZE;
77
78         /* Send request */
79         return dhd_bus_txctl(dhd->bus, (unsigned char *)&prot->msg, len);
80 }
81
82 static int dhdcdc_cmplt(dhd_pub_t *dhd, u32 id, u32 len)
83 {
84         int ret;
85         dhd_prot_t *prot = dhd->prot;
86
87         DHD_TRACE(("%s: Enter\n", __func__));
88
89         do {
90                 ret =
91                     dhd_bus_rxctl(dhd->bus, (unsigned char *)&prot->msg,
92                                   len + sizeof(cdc_ioctl_t));
93                 if (ret < 0)
94                         break;
95         } while (CDC_IOC_ID(ltoh32(prot->msg.flags)) != id);
96
97         return ret;
98 }
99
100 int
101 dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
102 {
103         dhd_prot_t *prot = dhd->prot;
104         cdc_ioctl_t *msg = &prot->msg;
105         void *info;
106         int ret = 0, retries = 0;
107         u32 id, flags = 0;
108
109         DHD_TRACE(("%s: Enter\n", __func__));
110         DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
111
112         /* Respond "bcmerror" and "bcmerrorstr" with local cache */
113         if (cmd == WLC_GET_VAR && buf) {
114                 if (!strcmp((char *)buf, "bcmerrorstr")) {
115                         strncpy((char *)buf, bcmerrorstr(dhd->dongle_error),
116                                 BCME_STRLEN);
117                         goto done;
118                 } else if (!strcmp((char *)buf, "bcmerror")) {
119                         *(int *)buf = dhd->dongle_error;
120                         goto done;
121                 }
122         }
123
124         memset(msg, 0, sizeof(cdc_ioctl_t));
125
126         msg->cmd = htol32(cmd);
127         msg->len = htol32(len);
128         msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
129         CDC_SET_IF_IDX(msg, ifidx);
130         msg->flags = htol32(msg->flags);
131
132         if (buf)
133                 memcpy(prot->buf, buf, len);
134
135         ret = dhdcdc_msg(dhd);
136         if (ret < 0) {
137                 DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status "
138                         "%d\n", ret));
139                 goto done;
140         }
141
142 retry:
143         /* wait for interrupt and get first fragment */
144         ret = dhdcdc_cmplt(dhd, prot->reqid, len);
145         if (ret < 0)
146                 goto done;
147
148         flags = ltoh32(msg->flags);
149         id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
150
151         if ((id < prot->reqid) && (++retries < RETRIES))
152                 goto retry;
153         if (id != prot->reqid) {
154                 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
155                            dhd_ifname(dhd, ifidx), __func__, id, prot->reqid));
156                 ret = -EINVAL;
157                 goto done;
158         }
159
160         /* Check info buffer */
161         info = (void *)&msg[1];
162
163         /* Copy info buffer */
164         if (buf) {
165                 if (ret < (int)len)
166                         len = ret;
167                 memcpy(buf, info, len);
168         }
169
170         /* Check the ERROR flag */
171         if (flags & CDCF_IOC_ERROR) {
172                 ret = ltoh32(msg->status);
173                 /* Cache error from dongle */
174                 dhd->dongle_error = ret;
175         }
176
177 done:
178         return ret;
179 }
180
181 int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len)
182 {
183         dhd_prot_t *prot = dhd->prot;
184         cdc_ioctl_t *msg = &prot->msg;
185         int ret = 0;
186         u32 flags, id;
187
188         DHD_TRACE(("%s: Enter\n", __func__));
189         DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
190
191         memset(msg, 0, sizeof(cdc_ioctl_t));
192
193         msg->cmd = htol32(cmd);
194         msg->len = htol32(len);
195         msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT) | CDCF_IOC_SET;
196         CDC_SET_IF_IDX(msg, ifidx);
197         msg->flags = htol32(msg->flags);
198
199         if (buf)
200                 memcpy(prot->buf, buf, len);
201
202         ret = dhdcdc_msg(dhd);
203         if (ret < 0)
204                 goto done;
205
206         ret = dhdcdc_cmplt(dhd, prot->reqid, len);
207         if (ret < 0)
208                 goto done;
209
210         flags = ltoh32(msg->flags);
211         id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
212
213         if (id != prot->reqid) {
214                 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
215                            dhd_ifname(dhd, ifidx), __func__, id, prot->reqid));
216                 ret = -EINVAL;
217                 goto done;
218         }
219
220         /* Check the ERROR flag */
221         if (flags & CDCF_IOC_ERROR) {
222                 ret = ltoh32(msg->status);
223                 /* Cache error from dongle */
224                 dhd->dongle_error = ret;
225         }
226
227 done:
228         return ret;
229 }
230
231 extern int dhd_bus_interface(struct dhd_bus *bus, uint arg, void *arg2);
232 int
233 dhd_prot_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t *ioc, void *buf, int len)
234 {
235         dhd_prot_t *prot = dhd->prot;
236         int ret = -1;
237
238         if (dhd->busstate == DHD_BUS_DOWN) {
239                 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
240                            __func__));
241                 return ret;
242         }
243         dhd_os_proto_block(dhd);
244
245         DHD_TRACE(("%s: Enter\n", __func__));
246
247         ASSERT(len <= WLC_IOCTL_MAXLEN);
248
249         if (len > WLC_IOCTL_MAXLEN)
250                 goto done;
251
252         if (prot->pending == true) {
253                 DHD_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
254                         "lastcmd=0x%x (%lu)\n",
255                         ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
256                         (unsigned long)prot->lastcmd));
257                 if ((ioc->cmd == WLC_SET_VAR) || (ioc->cmd == WLC_GET_VAR)) {
258                         DHD_TRACE(("iovar cmd=%s\n", (char *)buf));
259                 }
260                 goto done;
261         }
262
263         prot->pending = true;
264         prot->lastcmd = ioc->cmd;
265         if (ioc->set)
266                 ret = dhdcdc_set_ioctl(dhd, ifidx, ioc->cmd, buf, len);
267         else {
268                 ret = dhdcdc_query_ioctl(dhd, ifidx, ioc->cmd, buf, len);
269                 if (ret > 0)
270                         ioc->used = ret - sizeof(cdc_ioctl_t);
271         }
272
273         /* Too many programs assume ioctl() returns 0 on success */
274         if (ret >= 0)
275                 ret = 0;
276         else {
277                 cdc_ioctl_t *msg = &prot->msg;
278                 ioc->needed = ltoh32(msg->len); /* len == needed when set/query
279                                                  fails from dongle */
280         }
281
282         /* Intercept the wme_dp ioctl here */
283         if ((!ret) && (ioc->cmd == WLC_SET_VAR) && (!strcmp(buf, "wme_dp"))) {
284                 int slen, val = 0;
285
286                 slen = strlen("wme_dp") + 1;
287                 if (len >= (int)(slen + sizeof(int)))
288                         bcopy(((char *)buf + slen), &val, sizeof(int));
289                 dhd->wme_dp = (u8) ltoh32(val);
290         }
291
292         prot->pending = false;
293
294 done:
295         dhd_os_proto_unblock(dhd);
296
297         return ret;
298 }
299
300 int
301 dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
302                   void *params, int plen, void *arg, int len, bool set)
303 {
304         return BCME_UNSUPPORTED;
305 }
306
307 void dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
308 {
309         bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
310 }
311
312 void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *pktbuf)
313 {
314 #ifdef BDC
315         struct bdc_header *h;
316 #endif                          /* BDC */
317
318         DHD_TRACE(("%s: Enter\n", __func__));
319
320 #ifdef BDC
321         /* Push BDC header used to convey priority for buses that don't */
322
323         PKTPUSH(pktbuf, BDC_HEADER_LEN);
324
325         h = (struct bdc_header *)PKTDATA(pktbuf);
326
327         h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
328         if (PKTSUMNEEDED(pktbuf))
329                 h->flags |= BDC_FLAG_SUM_NEEDED;
330
331         h->priority = (PKTPRIO(pktbuf) & BDC_PRIORITY_MASK);
332         h->flags2 = 0;
333         h->rssi = 0;
334 #endif                          /* BDC */
335         BDC_SET_IF_IDX(h, ifidx);
336 }
337
338 bool dhd_proto_fcinfo(dhd_pub_t *dhd, void *pktbuf, u8 * fcbits)
339 {
340 #ifdef BDC
341         struct bdc_header *h;
342
343         if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
344                 DHD_ERROR(("%s: rx data too short (%d < %d)\n",
345                            __func__, PKTLEN(pktbuf), BDC_HEADER_LEN));
346                 return BCME_ERROR;
347         }
348
349         h = (struct bdc_header *)PKTDATA(pktbuf);
350
351         *fcbits = h->priority >> BDC_PRIORITY_FC_SHIFT;
352         if ((h->flags2 & BDC_FLAG2_FC_FLAG) == BDC_FLAG2_FC_FLAG)
353                 return true;
354 #endif
355         return false;
356 }
357
358 int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pktbuf)
359 {
360 #ifdef BDC
361         struct bdc_header *h;
362 #endif
363
364         DHD_TRACE(("%s: Enter\n", __func__));
365
366 #ifdef BDC
367         /* Pop BDC header used to convey priority for buses that don't */
368
369         if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
370                 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
371                            PKTLEN(pktbuf), BDC_HEADER_LEN));
372                 return BCME_ERROR;
373         }
374
375         h = (struct bdc_header *)PKTDATA(pktbuf);
376
377         *ifidx = BDC_GET_IF_IDX(h);
378         if (*ifidx >= DHD_MAX_IFS) {
379                 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
380                            __func__, *ifidx));
381                 return BCME_ERROR;
382         }
383
384         if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
385             BDC_PROTO_VER) {
386                 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
387                            dhd_ifname(dhd, *ifidx), h->flags));
388                 return BCME_ERROR;
389         }
390
391         if (h->flags & BDC_FLAG_SUM_GOOD) {
392                 DHD_INFO(("%s: BDC packet received with good rx-csum, "
393                         "flags 0x%x\n",
394                         dhd_ifname(dhd, *ifidx), h->flags));
395                 PKTSETSUMGOOD(pktbuf, true);
396         }
397
398         PKTSETPRIO(pktbuf, (h->priority & BDC_PRIORITY_MASK));
399
400         PKTPULL(pktbuf, BDC_HEADER_LEN);
401 #endif                          /* BDC */
402
403         return 0;
404 }
405
406 int dhd_prot_attach(dhd_pub_t *dhd)
407 {
408         dhd_prot_t *cdc;
409
410         cdc = kzalloc(sizeof(dhd_prot_t), GFP_ATOMIC);
411         if (!cdc) {
412                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
413                 goto fail;
414         }
415
416         /* ensure that the msg buf directly follows the cdc msg struct */
417         if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
418                 DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
419                 goto fail;
420         }
421
422         dhd->prot = cdc;
423 #ifdef BDC
424         dhd->hdrlen += BDC_HEADER_LEN;
425 #endif
426         dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
427         return 0;
428
429 fail:
430         if (cdc != NULL)
431                 kfree(cdc);
432         return BCME_NOMEM;
433 }
434
435 /* ~NOTE~ What if another thread is waiting on the semaphore?  Holding it? */
436 void dhd_prot_detach(dhd_pub_t *dhd)
437 {
438         kfree(dhd->prot);
439         dhd->prot = NULL;
440 }
441
442 void dhd_prot_dstats(dhd_pub_t *dhd)
443 {
444         /* No stats from dongle added yet, copy bus stats */
445         dhd->dstats.tx_packets = dhd->tx_packets;
446         dhd->dstats.tx_errors = dhd->tx_errors;
447         dhd->dstats.rx_packets = dhd->rx_packets;
448         dhd->dstats.rx_errors = dhd->rx_errors;
449         dhd->dstats.rx_dropped = dhd->rx_dropped;
450         dhd->dstats.multicast = dhd->rx_multicast;
451         return;
452 }
453
454 int dhd_prot_init(dhd_pub_t *dhd)
455 {
456         int ret = 0;
457         char buf[128];
458
459         DHD_TRACE(("%s: Enter\n", __func__));
460
461         dhd_os_proto_block(dhd);
462
463         /* Get the device MAC address */
464         strcpy(buf, "cur_etheraddr");
465         ret = dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
466         if (ret < 0) {
467                 dhd_os_proto_unblock(dhd);
468                 return ret;
469         }
470         memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
471
472         dhd_os_proto_unblock(dhd);
473
474 #ifdef EMBEDDED_PLATFORM
475         ret = dhd_preinit_ioctls(dhd);
476 #endif                          /* EMBEDDED_PLATFORM */
477
478         /* Always assumes wl for now */
479         dhd->iswl = true;
480
481         return ret;
482 }
483
484 void dhd_prot_stop(dhd_pub_t *dhd)
485 {
486         /* Nothing to do for CDC */
487 }