staging: brcm80211: further renaming in fullmac sources
[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 brcmf_proto_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 - Broadcom specific extension of CDC.
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 brcmf_proto_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 #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
88 #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE
89 * defined in dhd_sdio.c
90 * (amount of header tha might be added)
91 * plus any space that might be needed
92 * for alignment padding.
93 */
94 #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
95 * round off at the end of buffer
96 */
97
98 struct brcmf_proto {
99 u16 reqid;
100 u8 pending;
101 u32 lastcmd;
102 u8 bus_header[BUS_HEADER_LEN];
103 struct brcmf_proto_cdc_ioctl msg;
104 unsigned char buf[BRCMF_C_IOCTL_MAXLEN + ROUND_UP_MARGIN];
105 };
106
107 static int brcmf_proto_cdc_msg(dhd_pub_t *dhd)
108 {
109 struct brcmf_proto *prot = dhd->prot;
110 int len = le32_to_cpu(prot->msg.len) +
111 sizeof(struct brcmf_proto_cdc_ioctl);
112
113 DHD_TRACE(("%s: Enter\n", __func__));
114
115 /* NOTE : cdc->msg.len holds the desired length of the buffer to be
116 * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
117 * is actually sent to the dongle
118 */
119 if (len > CDC_MAX_MSG_SIZE)
120 len = CDC_MAX_MSG_SIZE;
121
122 /* Send request */
123 return brcmf_sdbrcm_bus_txctl(dhd->bus, (unsigned char *)&prot->msg,
124 len);
125 }
126
127 static int brcmf_proto_cdc_cmplt(dhd_pub_t *dhd, u32 id, u32 len)
128 {
129 int ret;
130 struct brcmf_proto *prot = dhd->prot;
131
132 DHD_TRACE(("%s: Enter\n", __func__));
133
134 do {
135 ret = brcmf_sdbrcm_bus_rxctl(dhd->bus,
136 (unsigned char *)&prot->msg,
137 len + sizeof(struct brcmf_proto_cdc_ioctl));
138 if (ret < 0)
139 break;
140 } while (CDC_IOC_ID(le32_to_cpu(prot->msg.flags)) != id);
141
142 return ret;
143 }
144
145 int
146 brcmf_proto_cdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
147 uint len)
148 {
149 struct brcmf_proto *prot = dhd->prot;
150 struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
151 void *info;
152 int ret = 0, retries = 0;
153 u32 id, flags = 0;
154
155 DHD_TRACE(("%s: Enter\n", __func__));
156 DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
157
158 /* Respond "bcmerror" and "bcmerrorstr" with local cache */
159 if (cmd == BRCMF_C_GET_VAR && buf) {
160 if (!strcmp((char *)buf, "bcmerrorstr")) {
161 strncpy((char *)buf, "bcm_error",
162 BCME_STRLEN);
163 goto done;
164 } else if (!strcmp((char *)buf, "bcmerror")) {
165 *(int *)buf = dhd->dongle_error;
166 goto done;
167 }
168 }
169
170 memset(msg, 0, sizeof(struct brcmf_proto_cdc_ioctl));
171
172 msg->cmd = cpu_to_le32(cmd);
173 msg->len = cpu_to_le32(len);
174 msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
175 CDC_SET_IF_IDX(msg, ifidx);
176 msg->flags = cpu_to_le32(msg->flags);
177
178 if (buf)
179 memcpy(prot->buf, buf, len);
180
181 ret = brcmf_proto_cdc_msg(dhd);
182 if (ret < 0) {
183 DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status "
184 "%d\n", ret));
185 goto done;
186 }
187
188 retry:
189 /* wait for interrupt and get first fragment */
190 ret = brcmf_proto_cdc_cmplt(dhd, prot->reqid, len);
191 if (ret < 0)
192 goto done;
193
194 flags = le32_to_cpu(msg->flags);
195 id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
196
197 if ((id < prot->reqid) && (++retries < RETRIES))
198 goto retry;
199 if (id != prot->reqid) {
200 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
201 brcmf_ifname(dhd, ifidx), __func__, id,
202 prot->reqid));
203 ret = -EINVAL;
204 goto done;
205 }
206
207 /* Check info buffer */
208 info = (void *)&msg[1];
209
210 /* Copy info buffer */
211 if (buf) {
212 if (ret < (int)len)
213 len = ret;
214 memcpy(buf, info, len);
215 }
216
217 /* Check the ERROR flag */
218 if (flags & CDCF_IOC_ERROR) {
219 ret = le32_to_cpu(msg->status);
220 /* Cache error from dongle */
221 dhd->dongle_error = ret;
222 }
223
224 done:
225 return ret;
226 }
227
228 int brcmf_proto_cdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd,
229 void *buf, uint len)
230 {
231 struct brcmf_proto *prot = dhd->prot;
232 struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
233 int ret = 0;
234 u32 flags, id;
235
236 DHD_TRACE(("%s: Enter\n", __func__));
237 DHD_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
238
239 memset(msg, 0, sizeof(struct brcmf_proto_cdc_ioctl));
240
241 msg->cmd = cpu_to_le32(cmd);
242 msg->len = cpu_to_le32(len);
243 msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT) | CDCF_IOC_SET;
244 CDC_SET_IF_IDX(msg, ifidx);
245 msg->flags = cpu_to_le32(msg->flags);
246
247 if (buf)
248 memcpy(prot->buf, buf, len);
249
250 ret = brcmf_proto_cdc_msg(dhd);
251 if (ret < 0)
252 goto done;
253
254 ret = brcmf_proto_cdc_cmplt(dhd, prot->reqid, len);
255 if (ret < 0)
256 goto done;
257
258 flags = le32_to_cpu(msg->flags);
259 id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
260
261 if (id != prot->reqid) {
262 DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
263 brcmf_ifname(dhd, ifidx), __func__, id,
264 prot->reqid));
265 ret = -EINVAL;
266 goto done;
267 }
268
269 /* Check the ERROR flag */
270 if (flags & CDCF_IOC_ERROR) {
271 ret = le32_to_cpu(msg->status);
272 /* Cache error from dongle */
273 dhd->dongle_error = ret;
274 }
275
276 done:
277 return ret;
278 }
279
280 extern int dhd_bus_interface(struct dhd_bus *bus, uint arg, void *arg2);
281 int
282 brcmf_proto_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t *ioc, void *buf,
283 int len)
284 {
285 struct brcmf_proto *prot = dhd->prot;
286 int ret = -1;
287
288 if (dhd->busstate == DHD_BUS_DOWN) {
289 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
290 __func__));
291 return ret;
292 }
293 brcmf_os_proto_block(dhd);
294
295 DHD_TRACE(("%s: Enter\n", __func__));
296
297 ASSERT(len <= BRCMF_C_IOCTL_MAXLEN);
298
299 if (len > BRCMF_C_IOCTL_MAXLEN)
300 goto done;
301
302 if (prot->pending == true) {
303 DHD_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
304 "lastcmd=0x%x (%lu)\n",
305 ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
306 (unsigned long)prot->lastcmd));
307 if ((ioc->cmd == BRCMF_C_SET_VAR) ||
308 (ioc->cmd == BRCMF_C_GET_VAR))
309 DHD_TRACE(("iovar cmd=%s\n", (char *)buf));
310
311 goto done;
312 }
313
314 prot->pending = true;
315 prot->lastcmd = ioc->cmd;
316 if (ioc->set)
317 ret = brcmf_proto_cdc_set_ioctl(dhd, ifidx, ioc->cmd, buf, len);
318 else {
319 ret = brcmf_proto_cdc_query_ioctl(dhd, ifidx, ioc->cmd,
320 buf, len);
321 if (ret > 0)
322 ioc->used = ret - sizeof(struct brcmf_proto_cdc_ioctl);
323 }
324
325 /* Too many programs assume ioctl() returns 0 on success */
326 if (ret >= 0)
327 ret = 0;
328 else {
329 struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
330 /* len == needed when set/query fails from dongle */
331 ioc->needed = le32_to_cpu(msg->len);
332 }
333
334 /* Intercept the wme_dp ioctl here */
335 if (!ret && ioc->cmd == BRCMF_C_SET_VAR &&
336 !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 brcmf_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 brcmf_proto_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 brcmf_proto_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 brcmf_proto_hdrpush(dhd_pub_t *dhd, int ifidx, struct sk_buff *pktbuf)
375 {
376 struct brcmf_proto_bdc_header *h;
377
378 DHD_TRACE(("%s: Enter\n", __func__));
379
380 /* Push BDC header used to convey priority for buses that don't */
381
382 skb_push(pktbuf, BDC_HEADER_LEN);
383
384 h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
385
386 h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
387 if (PKTSUMNEEDED(pktbuf))
388 h->flags |= BDC_FLAG_SUM_NEEDED;
389
390 h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
391 h->flags2 = 0;
392 h->rssi = 0;
393 BDC_SET_IF_IDX(h, ifidx);
394 }
395
396 int brcmf_proto_hdrpull(dhd_pub_t *dhd, int *ifidx, struct sk_buff *pktbuf)
397 {
398 struct brcmf_proto_bdc_header *h;
399
400 DHD_TRACE(("%s: Enter\n", __func__));
401
402 /* Pop BDC header used to convey priority for buses that don't */
403
404 if (pktbuf->len < BDC_HEADER_LEN) {
405 DHD_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
406 pktbuf->len, BDC_HEADER_LEN));
407 return -EBADE;
408 }
409
410 h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
411
412 *ifidx = BDC_GET_IF_IDX(h);
413 if (*ifidx >= DHD_MAX_IFS) {
414 DHD_ERROR(("%s: rx data ifnum out of range (%d)\n",
415 __func__, *ifidx));
416 return -EBADE;
417 }
418
419 if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
420 BDC_PROTO_VER) {
421 DHD_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
422 brcmf_ifname(dhd, *ifidx), h->flags));
423 return -EBADE;
424 }
425
426 if (h->flags & BDC_FLAG_SUM_GOOD) {
427 DHD_INFO(("%s: BDC packet received with good rx-csum, "
428 "flags 0x%x\n",
429 brcmf_ifname(dhd, *ifidx), h->flags));
430 PKTSETSUMGOOD(pktbuf, true);
431 }
432
433 pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
434
435 skb_pull(pktbuf, BDC_HEADER_LEN);
436
437 return 0;
438 }
439
440 int brcmf_proto_attach(dhd_pub_t *dhd)
441 {
442 struct brcmf_proto *cdc;
443
444 cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
445 if (!cdc) {
446 DHD_ERROR(("%s: kmalloc failed\n", __func__));
447 goto fail;
448 }
449
450 /* ensure that the msg buf directly follows the cdc msg struct */
451 if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
452 DHD_ERROR(("struct brcmf_proto is not correctly defined\n"));
453 goto fail;
454 }
455
456 dhd->prot = cdc;
457 dhd->hdrlen += BDC_HEADER_LEN;
458 dhd->maxctl = BRCMF_C_IOCTL_MAXLEN +
459 sizeof(struct brcmf_proto_cdc_ioctl) + ROUND_UP_MARGIN;
460 return 0;
461
462 fail:
463 kfree(cdc);
464 return -ENOMEM;
465 }
466
467 /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
468 void brcmf_proto_detach(dhd_pub_t *dhd)
469 {
470 kfree(dhd->prot);
471 dhd->prot = NULL;
472 }
473
474 void brcmf_proto_dstats(dhd_pub_t *dhd)
475 {
476 /* No stats from dongle added yet, copy bus stats */
477 dhd->dstats.tx_packets = dhd->tx_packets;
478 dhd->dstats.tx_errors = dhd->tx_errors;
479 dhd->dstats.rx_packets = dhd->rx_packets;
480 dhd->dstats.rx_errors = dhd->rx_errors;
481 dhd->dstats.rx_dropped = dhd->rx_dropped;
482 dhd->dstats.multicast = dhd->rx_multicast;
483 return;
484 }
485
486 int brcmf_proto_init(dhd_pub_t *dhd)
487 {
488 int ret = 0;
489 char buf[128];
490
491 DHD_TRACE(("%s: Enter\n", __func__));
492
493 brcmf_os_proto_block(dhd);
494
495 /* Get the device MAC address */
496 strcpy(buf, "cur_etheraddr");
497 ret = brcmf_proto_cdc_query_ioctl(dhd, 0, BRCMF_C_GET_VAR,
498 buf, sizeof(buf));
499 if (ret < 0) {
500 brcmf_os_proto_unblock(dhd);
501 return ret;
502 }
503 memcpy(dhd->mac, buf, ETH_ALEN);
504
505 brcmf_os_proto_unblock(dhd);
506
507 #ifdef EMBEDDED_PLATFORM
508 ret = brcmf_c_preinit_ioctls(dhd);
509 #endif /* EMBEDDED_PLATFORM */
510
511 /* Always assumes wl for now */
512 dhd->iswl = true;
513
514 return ret;
515 }
516
517 void brcmf_proto_stop(dhd_pub_t *dhd)
518 {
519 /* Nothing to do for CDC */
520 }
This page took 0.041762 seconds and 6 git commands to generate.