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