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