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