staging: brcm80211: Purge linuxver.h and redistribute #includes as required
[GitHub/mt8127/android_kernel_alcatel_ttab.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 int
302 dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
303 void *params, int plen, void *arg, int len, bool set)
304 {
305 return BCME_UNSUPPORTED;
306 }
307
308 void dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
309 {
310 bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
311 }
312
313 void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *pktbuf)
314 {
315 #ifdef BDC
316 struct bdc_header *h;
317 #endif /* BDC */
318
319 DHD_TRACE(("%s: Enter\n", __func__));
320
321 #ifdef BDC
322 /* Push BDC header used to convey priority for buses that don't */
323
324 PKTPUSH(pktbuf, BDC_HEADER_LEN);
325
326 h = (struct bdc_header *)PKTDATA(pktbuf);
327
328 h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
329 if (PKTSUMNEEDED(pktbuf))
330 h->flags |= BDC_FLAG_SUM_NEEDED;
331
332 h->priority = (PKTPRIO(pktbuf) & BDC_PRIORITY_MASK);
333 h->flags2 = 0;
334 h->rssi = 0;
335 #endif /* BDC */
336 BDC_SET_IF_IDX(h, ifidx);
337 }
338
339 bool dhd_proto_fcinfo(dhd_pub_t *dhd, void *pktbuf, u8 * fcbits)
340 {
341 #ifdef BDC
342 struct bdc_header *h;
343
344 if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
345 DHD_ERROR(("%s: rx data too short (%d < %d)\n",
346 __func__, PKTLEN(pktbuf), BDC_HEADER_LEN));
347 return BCME_ERROR;
348 }
349
350 h = (struct bdc_header *)PKTDATA(pktbuf);
351
352 *fcbits = h->priority >> BDC_PRIORITY_FC_SHIFT;
353 if ((h->flags2 & BDC_FLAG2_FC_FLAG) == BDC_FLAG2_FC_FLAG)
354 return true;
355 #endif
356 return false;
357 }
358
359 int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pktbuf)
360 {
361 #ifdef BDC
362 struct bdc_header *h;
363 #endif
364
365 DHD_TRACE(("%s: Enter\n", __func__));
366
367 #ifdef BDC
368 /* Pop BDC header used to convey priority for buses that don't */
369
370 if (PKTLEN(pktbuf) < BDC_HEADER_LEN) {
371 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
372 PKTLEN(pktbuf), BDC_HEADER_LEN));
373 return BCME_ERROR;
374 }
375
376 h = (struct bdc_header *)PKTDATA(pktbuf);
377
378 *ifidx = BDC_GET_IF_IDX(h);
379 if (*ifidx >= DHD_MAX_IFS) {
380 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
381 __func__, *ifidx));
382 return BCME_ERROR;
383 }
384
385 if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
386 BDC_PROTO_VER) {
387 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
388 dhd_ifname(dhd, *ifidx), h->flags));
389 return BCME_ERROR;
390 }
391
392 if (h->flags & BDC_FLAG_SUM_GOOD) {
393 DHD_INFO(("%s: BDC packet received with good rx-csum, "
394 "flags 0x%x\n",
395 dhd_ifname(dhd, *ifidx), h->flags));
396 PKTSETSUMGOOD(pktbuf, true);
397 }
398
399 PKTSETPRIO(pktbuf, (h->priority & BDC_PRIORITY_MASK));
400
401 PKTPULL(pktbuf, BDC_HEADER_LEN);
402 #endif /* BDC */
403
404 return 0;
405 }
406
407 int dhd_prot_attach(dhd_pub_t *dhd)
408 {
409 dhd_prot_t *cdc;
410
411 cdc = kzalloc(sizeof(dhd_prot_t), GFP_ATOMIC);
412 if (!cdc) {
413 DHD_ERROR(("%s: kmalloc failed\n", __func__));
414 goto fail;
415 }
416
417 /* ensure that the msg buf directly follows the cdc msg struct */
418 if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
419 DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
420 goto fail;
421 }
422
423 dhd->prot = cdc;
424 #ifdef BDC
425 dhd->hdrlen += BDC_HEADER_LEN;
426 #endif
427 dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
428 return 0;
429
430 fail:
431 if (cdc != NULL)
432 kfree(cdc);
433 return BCME_NOMEM;
434 }
435
436 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
437 void dhd_prot_detach(dhd_pub_t *dhd)
438 {
439 kfree(dhd->prot);
440 dhd->prot = NULL;
441 }
442
443 void dhd_prot_dstats(dhd_pub_t *dhd)
444 {
445 /* No stats from dongle added yet, copy bus stats */
446 dhd->dstats.tx_packets = dhd->tx_packets;
447 dhd->dstats.tx_errors = dhd->tx_errors;
448 dhd->dstats.rx_packets = dhd->rx_packets;
449 dhd->dstats.rx_errors = dhd->rx_errors;
450 dhd->dstats.rx_dropped = dhd->rx_dropped;
451 dhd->dstats.multicast = dhd->rx_multicast;
452 return;
453 }
454
455 int dhd_prot_init(dhd_pub_t *dhd)
456 {
457 int ret = 0;
458 char buf[128];
459
460 DHD_TRACE(("%s: Enter\n", __func__));
461
462 dhd_os_proto_block(dhd);
463
464 /* Get the device MAC address */
465 strcpy(buf, "cur_etheraddr");
466 ret = dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
467 if (ret < 0) {
468 dhd_os_proto_unblock(dhd);
469 return ret;
470 }
471 memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
472
473 dhd_os_proto_unblock(dhd);
474
475 #ifdef EMBEDDED_PLATFORM
476 ret = dhd_preinit_ioctls(dhd);
477 #endif /* EMBEDDED_PLATFORM */
478
479 /* Always assumes wl for now */
480 dhd->iswl = true;
481
482 return ret;
483 }
484
485 void dhd_prot_stop(dhd_pub_t *dhd)
486 {
487 /* Nothing to do for CDC */
488 }